-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwsclearsvr.js
267 lines (219 loc) · 7.9 KB
/
wsclearsvr.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
var WebSocketServer = require('websocket').server;
var WebSocketClient = require("websocket").client;
var http = require('http');
var https = require('https');
const fs = require('fs');
var options = {
hostname: 'sandbox.api.ndi.gov.sg',
port: 443,
path: '/asp/api/v1/asp/di-auth',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': 200
}
};
var clients = [];
var sync_server = http.createServer(function(req, res) {
console.log("SYNC_SERVER: IN received request");
if (req.method == 'POST') {
console.log("POST");
var body = '';
var sync_resp = '';
var sync_status = 0;
req.on('data', function (data) {
body += data;
});
req.on('end', function () {
console.log("SYNC_SERVER: Body: " + body);
options.headers["Content-Length"] = body.length;
var NDI_req = https.request(options,(NDI_res) => {
console.log('SYNC_SERVER: OUT NDI post status code:', NDI_res.statusCode);
console.log('SYNC_SERVER: OUT NDI post headers: ',NDI_res.headers);
NDI_res.on('data',(NDI_data) =>{
process.stdout.write(NDI_data);
//console.log('NDI post response [',NDI_data,']');
{
// here we do the websocket wait
var NDI_auth_req_id = '';
var expires = 300;
NDI_auth_req_id = (JSON.parse(NDI_data))["auth_req_id"];
expires = (JSON.parse(NDI_data))["expires_in"];
if (NDI_auth_req_id.length < 1)
{
sync_status = 404;
sync_resp = "Error - Unable to find auth_req_id from NDI";
res.statusCode = sync_status;
res.end(sync_resp);
}
else
{
// I haven't handled timeouts or other websocket errors
// assume that you send to websocket and got response.
var webclient = new WebSocketClient();
webclient.on('connectFailed',function(error) {
console.log('SYNC_SERVER: socketclient connectFailed: ' + error.toString());
});
webclient.on('connect',function(connection) {
console.log('SYNC_SERVER: socketclient Websocket client connected');
connection.on('error',function(error) {
console.log('SYNC_SERVER: socketclient connect, connection error: '+error.toString());
});
connection.on('close',function(){
console.log('SYNC_SERVER: socketclient connection closed');
});
connection.on('message',function(message){
console.log('SYNC_SERVER: socketclient message: ['+ message.utf8Data + ']');
sync_status = 200;
sync_resp = message.utf8Data ;
res.statusCode = sync_status;
res.end(sync_resp);
connection.close();
});
});
webclient.connect("ws://localhost:3000/","echo-protocol",NDI_auth_req_id,null,null);
}
}
});
NDI_res.on('error',(e) => {
console.error(e);
});
});
NDI_req.write(body);
});
}
else
{
res.statusCode = 404;
res.end();
}
});
sync_server.listen(80, function() {
console.log((new Date()) + ' SyncServer is listening on port 80');
});
var ext_server = https.createServer({
pfx: fs.readFileSync('ws.pfx')
}, function(req, res) {
console.log("EXT_SERVER: received request");
if (req.method == 'POST') {
console.log("EXT_SERVER: received POST from NDI");
var body = '';
req.on('data', function (data) {
body += data;
});
req.on('end', function () {
console.log("EXT_SERVER: received Body: " + body);
});
{
// I haven't handled timeouts or other websocket errors
// assume that you send to websocket and got response.
var webclient = new WebSocketClient();
webclient.on('connectFailed',function(error) {
console.log('EXT_SERVER: socketclient connectFailed: ' + error.toString());
});
webclient.on('connect',function(connection) {
console.log('EXT_SERVER: socketclient Websocket client connected');
connection.on('error',function(error) {
console.log('EXT_SERVER: socketclient connect, connection error: '+error.toString());
});
connection.on('close',function(){
console.log('EXT_SERVER: socketclient connection closed');
});
connection.on('message',function(message){
console.log('EXT_SERVER: socketclient message: ['+ message + ']');
});
connection.send(body);
connection.close();
});
webclient.connect("ws://localhost:3000/","echo-protocol","external",null,null);
}
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('post received');
}
res.end();
});
ext_server.listen(443, function() {
console.log((new Date()) + ' ExtServer is listening on port 443');
});
var server = http.createServer(function(request, response) {
console.log((new Date()) + 'SOCK_SERVER Received request for ' + request.url);
response.writeHead(404);
response.end();
});
server.listen(3000, function() {
console.log((new Date()) + ' Websocket Server is listening on port 3000');
});
wsServer = new WebSocketServer({
httpServer: server,
// You should not use autoAcceptConnections for production
// applications, as it defeats all standard cross-origin protection
// facilities built into the protocol and the browser. You should
// *always* verify the connection's origin and decide whether or not
// to accept it.
autoAcceptConnections: false
});
function originIsExternal(origin) {
// put logic here to detect whether the specified origin is allowed.
if (origin === "external")
return true
return false;
}
var history = [];
function htmlEntities(str) {
return String(str)
.replace(/&/g, '&').replace(/</g, '<')
.replace(/>/g, '>').replace(/"/g, '"');
}
wsServer.on('request', function(request) {
var connection = request.accept("echo-protocol", request.origin);
var index;
if (!originIsExternal(request.origin)) {
var obj = {
time : (new Date()).getTime(),
authID : request.origin,
connID : connection
};
index = clients.push(obj) -1;
}
console.log((new Date()) + 'SOCK_SERVER: Connection from ' + request.origin + ' accepted.');
connection.on('message', function(message) {
if (message.type === 'utf8') {
console.log('SOCK_SERVER : Received Message: ' + message.utf8Data);
if (originIsExternal(request.origin)) {
console.log("SOCK_SERVER: received message from NDI [",message,"]");
var extAuthID = (JSON.parse(message.utf8Data))["auth_req_id"];
/*
var nowtime = (new Date()).getTime();
while (clients.length > 0)
{
if (nowtime < (clients[0].time + 3000000)) {
clients.shift();
console.log("SOCK_SERVER: clearing client ", clients[0]["authID"] , " due to timeout");
}
else
break;
}
*/
// we assume that the client has already connected to the server. Else the message is lost
console.log("SOCK_SERVER number of clients ", clients.length);
for (i=0;i < clients.length; i++) {
console.log("SOCK_SERVER client authid [",clients[i]["authID"],"] ext authid [",extAuthID,"]");
if (clients[i]["authID"] === extAuthID) {
clients[i]["connID"].send(message.utf8Data);
console.log("SOCK_SERVER sending response message ",message.utf8Data," from NDI to client ", clients[i]["authID"]);
}
}
connection.close();
}
}
else if (message.type === 'binary') {
console.log('SOCK_SERVER Received Binary Message of ' + message.binaryData.length + ' bytes');
// these are ignored
}
});
connection.on('close', function(reasonCode, description) {
if (!originIsExternal(request.origin))
clients.splice(index,1);
console.log('SOCK_SERVER' + (new Date()) + ' origin [' +request.origin+'] '+ connection.remoteAddress + ' disconnected.');
});
});