-
Notifications
You must be signed in to change notification settings - Fork 87
/
pxy_ws.js
486 lines (419 loc) · 18.1 KB
/
pxy_ws.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/**
* Copyright (c) 2018, KETI
* All rights reserved.
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* @file
* @copyright KETI Korea 2018, KETI
* @author Il Yeup Ahn [[email protected]]
*/
var fs = require('fs');
var http = require('http');
var https = require('https');
var express = require('express');
var bodyParser = require('body-parser');
var util = require('util');
var xml2js = require('xml2js');
var js2xmlparser = require('js2xmlparser');
var url = require('url');
var xmlbuilder = require('xmlbuilder');
var moment = require('moment');
var ip = require("ip");
var events = require('events');
var cbor = require('cbor');
var WebSocketServer = require('websocket').server;
var _server = null;
var _this = this;
var ws_state = 'init';
// ������ �����մϴ�.
var ws_app = express();
var usewscbhost = 'localhost'; // pxyws to mobius
var pxy_ws_server = null;
function originIsAllowed(origin) {
// put logic here to detect whether the specified origin is allowed.
return true;
}
exports.ws_watchdog = function() {
if(ws_state === 'init') {
if(use_secure === 'disable') {
http.globalAgent.maxSockets = 1000000;
_server = http.createServer(ws_app);
_server.listen({port: usepxywsport, agent: false}, function () {
console.log('pxyws server (' + ip.address() + ') running at ' + usepxywsport + ' port');
ws_state = 'connect';
});
}
else {
var options = {
key: fs.readFileSync('server-key.pem'),
cert: fs.readFileSync('server-crt.pem'),
ca: fs.readFileSync('ca-crt.pem')
};
https.globalAgent.maxSockets = 1000000;
_server = https.createServer(options, ws_app);
_server.listen({port: usepxywsport, agent: false}, function () {
console.log('pxyws server (' + ip.address() + ') running at ' + usepxywsport + ' port');
ws_state = 'connect';
});
}
}
else if(ws_state === 'connect') {
http_retrieve_CSEBase(function(status, res_body) {
if (status == '2000') {
var jsonObj = JSON.parse(res_body);
usecseid = jsonObj['m2m:cb'].csi;
ws_state = 'connecting';
}
else {
console.log('Target CSE(' + usewscbhost + ') is not ready');
}
});
}
else if(ws_state === 'connecting') {
if(pxy_ws_server == null) {
pxy_ws_server = 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
});
ws_state = 'ready';
pxy_ws_server.on('request', function (request) {
if (!originIsAllowed(request.origin)) {
// Make sure we only accept requests from an allowed origin
request.reject();
console.log((new Date()) + ' Connection from origin ' + request.origin + ' rejected.');
return;
}
if(request.requestedProtocols.length) {
for (var index in request.requestedProtocols) {
if (request.requestedProtocols.hasOwnProperty(index)) {
if (request.requestedProtocols[index] === 'onem2m.r2.0.xml' || request.requestedProtocols[index] === 'onem2m.xml') {
let connection = request.accept('onem2m.r2.0.xml', request.origin);
console.log((new Date()) + ' Connection accepted. (xml)');
connection.on('message', ws_message_handler);
connection.on('close', function (reasonCode, description) {
console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.');
});
break;
}
else if (request.requestedProtocols[index] === 'onem2m.r2.0.cbor' || request.requestedProtocols[index] === 'onem2m.cbor') {
let connection = request.accept('onem2m.r2.0.cbor', request.origin);
console.log((new Date()) + ' Connection accepted. (cbor)');
connection.on('message', ws_message_handler);
connection.on('close', function (reasonCode, description) {
console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.');
});
break;
}
else if (request.requestedProtocols[index] === 'onem2m.r2.0.json' || request.requestedProtocols[index] === 'onem2m.json') {
let connection = request.accept('onem2m.r2.0.json', request.origin);
console.log((new Date()) + ' Connection accepted. (json)');
connection.on('message', ws_message_handler);
connection.on('close', function (reasonCode, description) {
console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.');
});
break;
}
else {
request.reject();
console.log((new Date()) + ' requestedProtocols is not supported.');
}
}
}
}
else {
request.reject();
console.log((new Date()) + ' requestedProtocols is empty.');
}
});
}
}
};
var ws_tid = require('shortid').generate();
wdt.set_wdt(ws_tid, 2, _this.ws_watchdog);
function ws_message_handler(message) {
var _this = this;
if(message.type === 'utf8') {
console.log(message.utf8Data.toString());
var protocol_arr = this.protocol.split('.');
var bodytype = protocol_arr[protocol_arr.length-1];
make_json_obj(bodytype, message.utf8Data.toString(), function(rsc, result) {
if(rsc == '1') {
ws_message_action(_this, bodytype, result);
}
else {
ws_response(_this, 4000, '', '', '', 'to parsing error', bodytype);
}
});
}
else if(message.type === 'binary') {
// Buffer.from('80', 'hex').toString('utf8');
// Buffer.from(message).toString('hex');
console.log(message.binaryData.toString('hex'));
//var data = Buffer.from(message);
//Array.prototype.map.call(new Uint8Array(data), x => ('00' + x.toString(16)).slice(-2)).join('').match(/[a-fA-F0-9]{2}/g).reverse().join('');
var protocol_arr = this.protocol.split('.');
var bodytype = protocol_arr[protocol_arr.length-1];
var str = message.binaryData.toString('hex');
make_json_obj(bodytype, str, function(rsc, result) {
if(rsc == '1') {
ws_message_action(_this, bodytype, result);
}
else {
ws_response(_this, 4000, '', '', '', 'to parsing error', bodytype);
}
});
// var protocol_arr = this.protocol.split('.');
// var bodytype = protocol_arr[protocol_arr.length-1];
//
// make_json_obj(bodytype, message.utf8Data.toString(), function(rsc, result) {
// if(rsc == '1') {
// ws_message_action(_this, bodytype, result);
// }
// else {
// ws_response(_this, 4000, '', '', '', 'to parsing error', bodytype);
// }
// });
}
}
function ws_message_action(ws_conn, bodytype, jsonObj) {
if (jsonObj['m2m:rqp'] != null) {
console.log('m2m:rqp tag of ws message is removed');
var res_body = {};
res_body['m2m:dbg'] = 'm2m:rqp tag of ws message is removed';
ws_response(ws_conn, 4000, "", usecseid, "", JSON.parse(res_body), bodytype);
}
else {
var op = (jsonObj.op == null) ? '' : jsonObj.op;
var to = (jsonObj.to == null) ? '' : jsonObj.to;
to = to.replace(usespid + usecseid + '/', '/');
to = to.replace(usecseid + '/', '/');
if(to.charAt(0) != '/') {
to = '/' + to;
}
var fr = (jsonObj.fr == null) ? '' : jsonObj.fr;
var rqi = (jsonObj.rqi == null) ? '' : jsonObj.rqi;
var ty = (jsonObj.ty == null) ? '' : jsonObj.ty.toString();
var pc = (jsonObj.pc == null) ? '' : jsonObj.pc;
if(jsonObj.fc) {
var query_count = 0;
for(var fc_idx in jsonObj.fc) {
if(jsonObj.fc.hasOwnProperty(fc_idx)) {
if(query_count == 0) {
to += '?';
query_count++;
}
else {
to += '&';
query_count++;
}
to += fc_idx;
to += '=';
to += jsonObj.fc[fc_idx].toString();
}
}
}
try {
//if (to.split('/')[1].split('?')[0] == usecsebase) {
ws_binding(op, to, fr, rqi, ty, pc, bodytype, function (res, res_body) {
if (res_body == '') {
res_body = '{}';
}
ws_response(ws_conn, res.headers['x-m2m-rsc'], to, usecseid, rqi, JSON.parse(res_body), bodytype);
});
// }
// else {
// ws_response(ws_conn, 4004, fr, usecseid, rqi, 'this is not MN-CSE, csebase do not exist', bodytype);
// }
}
catch (e) {
console.error(e);
ws_response(ws_conn, 5000, fr, usecseid, rqi, 'to parsing error', bodytype);
}
}
}
function ws_binding(op, to, fr, rqi, ty, pc, bodytype, callback) {
var content_type = 'application/vnd.onem2m-res+json';
switch (op.toString()) {
case '1':
op = 'post';
content_type += ('; ty=' + ty);
break;
case '2':
op = 'get';
break;
case '3':
op = 'put';
break;
case '4':
op = 'delete';
break;
}
var reqBodyString = '';
if( op == 'post' || op == 'put') {
reqBodyString = JSON.stringify(pc);
}
var bodyStr = '';
var options = {
hostname: usewscbhost,
port: usecsebaseport,
path: to,
method: op,
headers: {
'X-M2M-RI': rqi,
'Accept': 'application/json',
'X-M2M-Origin': fr,
'Content-Type': content_type,
'binding': 'W',
'X-M2M-RVI': uservi
},
rejectUnauthorized: false
};
if(use_secure == 'disable') {
var req = http.request(options, function (res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
bodyStr += chunk;
});
res.on('end', function () {
callback(res, bodyStr);
});
});
}
else {
options.ca = fs.readFileSync('ca-crt.pem');
req = https.request(options, function (res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
bodyStr += chunk;
});
res.on('end', function () {
callback(res, bodyStr);
});
});
}
req.on('error', function (e) {
console.log('[pxyws_binding] problem with request: ' + e.message);
});
// write data to request body
//console.log(options);
//console.log(reqBodyString);
req.write(reqBodyString);
req.end();
}
function ws_response(ws_conn, rsc, to, fr, rqi, inpc, bodytype) {
var rsp_message = {};
rsp_message['m2m:rsp'] = {};
//rsp_message['m2m:rsp'].rsc = rsc;
rsp_message['m2m:rsp'].rsc = parseInt(rsc); // convert to int
//rsp_message['m2m:rsp'].to = to;
//rsp_message['m2m:rsp'].fr = fr;
rsp_message['m2m:rsp'].rqi = rqi;
rsp_message['m2m:rsp'].rvi = uservi;
rsp_message['m2m:rsp'].pc = inpc;
if (bodytype === 'xml') {
rsp_message['m2m:rsp']['@'] = {
"xmlns:m2m": "http://www.onem2m.org/xml/protocols",
"xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance"
};
for(var prop in rsp_message['m2m:rsp'].pc) {
if (rsp_message['m2m:rsp'].pc.hasOwnProperty(prop)) {
for(var prop2 in rsp_message['m2m:rsp'].pc[prop]) {
if (rsp_message['m2m:rsp'].pc[prop].hasOwnProperty(prop2)) {
if(prop2 == 'rn') {
rsp_message['m2m:rsp'].pc[prop]['@'] = {rn : rsp_message['m2m:rsp'].pc[prop][prop2]};
delete rsp_message['m2m:rsp'].pc[prop][prop2];
}
for(var prop3 in rsp_message['m2m:rsp'].pc[prop][prop2]) {
if (rsp_message['m2m:rsp'].pc[prop][prop2].hasOwnProperty(prop3)) {
if(prop3 == 'rn') {
rsp_message['m2m:rsp'].pc[prop][prop2]['@'] = {rn : rsp_message['m2m:rsp'].pc[prop][prop2][prop3]};
delete rsp_message['m2m:rsp'].pc[prop][prop2][prop3];
}
}
}
}
}
}
}
var bodyString = js2xmlparser.parse("m2m:rsp", rsp_message['m2m:rsp']);
ws_conn.sendUTF(bodyString.toString());
}
else if (bodytype === 'cbor') { // 'cbor'
bodyString = cbor.encode(rsp_message['m2m:rsp']).toString('hex');
var bytearray = Buffer.from(bodyString, 'hex');
ws_conn.send(bytearray);
}
else { // 'json'
ws_conn.sendUTF(JSON.stringify(rsp_message['m2m:rsp']));
}
}
function http_retrieve_CSEBase(callback) {
var rqi = require('shortid').generate();
var resourceid = '/' + usecsebase;
var responseBody = '';
if(use_secure == 'disable') {
var options = {
hostname: usewscbhost,
port: usecsebaseport,
path: resourceid,
method: 'get',
headers: {
'X-M2M-RI': rqi,
'Accept': 'application/json',
'X-M2M-Origin': usecseid,
'X-M2M-RVI': uservi
}
};
var req = http.request(options, function (res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
responseBody += chunk;
});
res.on('end', function () {
callback(res.headers['x-m2m-rsc'], responseBody);
});
});
}
else {
options = {
hostname: usewscbhost,
port: usecsebaseport,
path: resourceid,
method: 'get',
headers: {
'X-M2M-RI': rqi,
'Accept': 'application/json',
'X-M2M-Origin': usecseid,
'X-M2M-RVI': uservi
},
ca: fs.readFileSync('ca-crt.pem')
};
req = https.request(options, function (res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
responseBody += chunk;
});
res.on('end', function () {
callback(res.headers['x-m2m-rsc'], responseBody);
});
});
}
req.on('error', function (e) {
if(e.message != 'read ECONNRESET') {
//console.log('[pxyws - http_retrieve_CSEBase] problem with request: ' + e.message);
}
});
// write data to request body
req.write('');
req.end();
}