-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
116 lines (106 loc) · 3.29 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
const express = require('express');
const fs = require('fs');
const path = require('path');
const jwt = require('jsonwebtoken');
const axios = require("axios");
const config = require('./settings.json');
const app = express();
const conn = config["AzureSignalRConnectionString"];
if (!conn){
console.error("Please specify `AzureSignalRConnectionString` with your connection string in settings.json file!");
return;
}
const parsed = parse(conn);
if (!parsed) {
console.error(`Set valid connectiong string in settins.json: ${conn}`);
return;
}
const hub = 'simplechat';
// serve default to return index.html
app.get('/', function (req, res) {
const name = req.query.name;
if (!name) {
res.status(404).send("User name is not found in query 'name'");
return;
}
fs.readFile(path.join(__dirname, 'index.html'), { encoding: 'utf8' }, function (err, data) {
if (err) {
res.sendStatus(404);
} else {
var rendered = data.replace(/%%%____URL____%%%/g, `${parsed.wshost}ws/client/hubs/${hub}/?user=${name}`)
.replace(/%%%___user___%%%/g, name);
res.send(rendered);
}
});
});
// serve requests for "simplechat"
app.post(`/${hub}/connect`, function (req, res) {
var user = new URLSearchParams(req.header("x-asrs-client-query")).get('user');
console.log(`connect ${req.header("x-asrs-connection-id")} user ${user} connected.`);
// set back the user id for the connection
res.header("x-asrs-user-id", user);
res.send(`User ${user}(${req.header("x-asrs-connection-id")}) connected.`);
});
app.post(`/${hub}/message`, function (req, res) {
console.log(`User ${res.header("x-asrs-user-id")}(${req.header("x-asrs-connection-id")}) sending message.`);
// broadcast through Azure SignalR
req.setEncoding('utf8');
var body = '';
req.on('data', function(chunk) {
body += chunk;
});
req.on('end', async function(){
if (body){
console.log("broadcasting " + body);
await broadcast(body);
}
});
});
app.post(`/${hub}/disconnect`, function (req, res) {
console.log(`User ${res.header("x-asrs-user-id")}(${req.header("x-asrs-connection-id")}) disconnected.`);
});
var server = app.listen(8088, function () {
var host = server.address().address;
var port = server.address().port;
console.log("Serving http://%s:%s", host, port);
});
function broadcast(content) {
var path = "ws/api/v1/hubs/" + hub;
var url = parsed.host + path;
var token = getToken(path);
return axios.post(url, content, {
headers: {
"Content-Type": "text/plain",
"Authorization": token
}
});
}
function getToken(path) {
return "Bearer " + jwt.sign({}, parsed.key, {
audience: parsed.audience + path,
expiresIn: "1h",
algorithm: "HS256"
});
}
function parse(conn) {
const em = /Endpoint=(.*?);/g.exec(conn);
if (!em) return null;
const endpoint = em[1];
const km = /AccessKey=(.*?);/g.exec(conn);
if (!km) return null;
const key = km[1];
if (!endpoint || !key) return null;
const pm = /Port=(.*?);/g.exec(conn);
const port = pm == null ? '' : pm[1];
var url = new URL(endpoint);
url.port = port;
const host = url.toString();
url.port = '';
const audience = url.toString();
return {
host: host,
audience: audience,
key: key,
wshost: host.replace('https://', 'wss://').replace('http://', 'ws://')
};
}