Add apf client for duktape and minor debug removal.
jsastriawan committed
Sep 16, 2019 at 07:58 UTC
a78d3ca9b6e9f00d9510065c7d05d0a02cd1acb3
2 files changed
+508
-9
agents/modules_meshcore/apfclient.js
new
+496
@@ -0,0 +1,496 @@
1
+/**
2
+* @description APF/CIRA Client for duktape
3
+* @author Joko Sastriawan
4
+* @copyright Intel Corporation 2019
5
+* @license Apache-2.0
6
+* @version v0.0.1
7
+*/
8
+
9
+function CreateAPFClient(parent, args) {
10
+ var obj = {};
11
+ obj.parent = parent;
12
+ obj.args = args;
13
+ obj.http = require('http');
14
+ //obj.common = require('common');
15
+ obj.net = require('net');
16
+ obj.forwardClient = null;
17
+ obj.downlinks = {};
18
+ obj.pfwd_idx = 0;
19
+ // keep alive timer
20
+ obj.timer = null;
21
+
22
+ // some function copied from common.js
23
+ function ReadInt(v, p) {
24
+ return (v.charCodeAt(p) * 0x1000000) + (v.charCodeAt(p + 1) << 16) + (v.charCodeAt(p + 2) << 8) + v.charCodeAt(p + 3);
25
+ }; // We use "*0x1000000" instead of "<<24" because the shift converts the number to signed int32.
26
+
27
+ function IntToStr(v) {
28
+ return String.fromCharCode((v >> 24) & 0xFF, (v >> 16) & 0xFF, (v >> 8) & 0xFF, v & 0xFF);
29
+ };
30
+
31
+ function hex2rstr(d) {
32
+ var r = '', m = ('' + d).match(/../g), t;
33
+ while (t = m.shift()) { r += String.fromCharCode('0x' + t); }
34
+ return r;
35
+ };
36
+
37
+ // Convert decimal to hex
38
+ function char2hex(i) { return (i + 0x100).toString(16).substr(-2).toUpperCase(); };
39
+
40
+ // Convert a raw string to a hex string
41
+ function rstr2hex(input) {
42
+ var r = '', i;
43
+ for (i = 0; i < input.length; i++) { r += char2hex(input.charCodeAt(i)); }
44
+ return r;
45
+ };
46
+
47
+ function d2h(d) {
48
+ return (d / 256 + 1 / 512).toString(16).substring(2, 4);
49
+ }
50
+
51
+ function buf2hex(input) {
52
+ var r = '', i;
53
+ for (i = 0; i < input.length; i++) { r += d2h(input[i]); }
54
+ return r;
55
+ };
56
+
57
+
58
+ function Debug(str) {
59
+ if (obj.parent.debug) {
60
+ console.log(str);
61
+ }
62
+ }
63
+ // CIRA state
64
+ var CIRASTATE = {
65
+ INITIAL: 0,
66
+ PROTOCOL_VERSION_SENT: 1,
67
+ AUTH_SERVICE_REQUEST_SENT: 2,
68
+ AUTH_REQUEST_SENT: 3,
69
+ PFWD_SERVICE_REQUEST_SENT: 4,
70
+ GLOBAL_REQUEST_SENT: 5,
71
+ FAILED: -1
72
+ }
73
+ obj.cirastate = CIRASTATE.INITIAL;
74
+
75
+ // REDIR state
76
+ var REDIR_TYPE = {
77
+ REDIR_UNKNOWN: 0,
78
+ REDIR_SOL: 1,
79
+ REDIR_KVM: 2,
80
+ REDIR_IDER: 3
81
+ }
82
+
83
+ // redirection start command
84
+ obj.RedirectStartSol = String.fromCharCode(0x10, 0x00, 0x00, 0x00, 0x53, 0x4F, 0x4C, 0x20);
85
+ obj.RedirectStartKvm = String.fromCharCode(0x10, 0x01, 0x00, 0x00, 0x4b, 0x56, 0x4d, 0x52);
86
+ obj.RedirectStartIder = String.fromCharCode(0x10, 0x00, 0x00, 0x00, 0x49, 0x44, 0x45, 0x52);
87
+
88
+
89
+ // AMT forwarded port list for non-TLS mode
90
+ var pfwd_ports = [16992, 623, 16994, 5900];
91
+ // protocol definitions
92
+ var APFProtocol = {
93
+ UNKNOWN: 0,
94
+ DISCONNECT: 1,
95
+ SERVICE_REQUEST: 5,
96
+ SERVICE_ACCEPT: 6,
97
+ USERAUTH_REQUEST: 50,
98
+ USERAUTH_FAILURE: 51,
99
+ USERAUTH_SUCCESS: 52,
100
+ GLOBAL_REQUEST: 80,
101
+ REQUEST_SUCCESS: 81,
102
+ REQUEST_FAILURE: 82,
103
+ CHANNEL_OPEN: 90,
104
+ CHANNEL_OPEN_CONFIRMATION: 91,
105
+ CHANNEL_OPEN_FAILURE: 92,
106
+ CHANNEL_WINDOW_ADJUST: 93,
107
+ CHANNEL_DATA: 94,
108
+ CHANNEL_CLOSE: 97,
109
+ PROTOCOLVERSION: 192,
110
+ KEEPALIVE_REQUEST: 208,
111
+ KEEPALIVE_REPLY: 209,
112
+ KEEPALIVE_OPTIONS_REQUEST: 210,
113
+ KEEPALIVE_OPTIONS_REPLY: 211
114
+ }
115
+
116
+ var APFDisconnectCode = {
117
+ HOST_NOT_ALLOWED_TO_CONNECT: 1,
118
+ PROTOCOL_ERROR: 2,
119
+ KEY_EXCHANGE_FAILED: 3,
120
+ RESERVED: 4,
121
+ MAC_ERROR: 5,
122
+ COMPRESSION_ERROR: 6,
123
+ SERVICE_NOT_AVAILABLE: 7,
124
+ PROTOCOL_VERSION_NOT_SUPPORTED: 8,
125
+ HOST_KEY_NOT_VERIFIABLE: 9,
126
+ CONNECTION_LOST: 10,
127
+ BY_APPLICATION: 11,
128
+ TOO_MANY_CONNECTIONS: 12,
129
+ AUTH_CANCELLED_BY_USER: 13,
130
+ NO_MORE_AUTH_METHODS_AVAILABLE: 14,
131
+ INVALID_CREDENTIALS: 15,
132
+ CONNECTION_TIMED_OUT: 16,
133
+ BY_POLICY: 17,
134
+ TEMPORARILY_UNAVAILABLE: 18
135
+ }
136
+
137
+ var APFChannelOpenFailCodes = {
138
+ ADMINISTRATIVELY_PROHIBITED: 1,
139
+ CONNECT_FAILED: 2,
140
+ UNKNOWN_CHANNEL_TYPE: 3,
141
+ RESOURCE_SHORTAGE: 4,
142
+ }
143
+
144
+ var APFChannelOpenFailureReasonCode = {
145
+ AdministrativelyProhibited: 1,
146
+ ConnectFailed: 2,
147
+ UnknownChannelType: 3,
148
+ ResourceShortage: 4,
149
+ }
150
+
151
+ obj.onSecureConnect = function onSecureConnect(resp, ws, head) {
152
+ Debug("APF Secure WebSocket connected.");
153
+ //console.log(JSON.stringify(resp));
154
+ obj.forwardClient.tag = { accumulator: [] };
155
+ obj.forwardClient.ws = ws;
156
+ obj.forwardClient.ws.on('end', function () {
157
+ Debug("APF: Connection is closing.");
158
+ if (obj.timer != null) {
159
+ clearInterval(obj.timer);
160
+ obj.timer = null;
161
+ }
162
+ });
163
+
164
+ obj.forwardClient.ws.on('data', function (data) {
165
+ obj.forwardClient.tag.accumulator += hex2rstr(buf2hex(data));
166
+ try {
167
+ var len = 0;
168
+ do {
169
+ len = ProcessData(obj.forwardClient);
170
+ if (len > 0) {
171
+ obj.forwardClient.tag.accumulator = obj.forwardClient.tag.accumulator.slice(len);
172
+ }
173
+ if (obj.cirastate == CIRASTATE.FAILED) {
174
+ Debug("APF: in a failed state, destroying socket.")
175
+ obj.forwardClient.ws.end();
176
+ }
177
+ } while (len > 0);
178
+ } catch (e) {
179
+ Debug(e);
180
+ }
181
+ });
182
+
183
+ obj.forwardClient.ws.on('error', function (e) {
184
+ Debug("APF: Connection error, ending connecting.");
185
+ if (obj.timer != null) {
186
+ clearInterval(obj.timer);
187
+ obj.timer = null;
188
+ }
189
+ });
190
+
191
+ obj.state = CIRASTATE.INITIAL;
192
+ SendProtocolVersion(obj.forwardClient.ws, obj.args.clientuuid);
193
+ SendServiceRequest(obj.forwardClient.ws, 'auth@amt.intel.com');
194
+ }
195
+
196
+ function guidToStr(g) { return g.substring(6, 8) + g.substring(4, 6) + g.substring(2, 4) + g.substring(0, 2) + "-" + g.substring(10, 12) + g.substring(8, 10) + "-" + g.substring(14, 16) + g.substring(12, 14) + "-" + g.substring(16, 20) + "-" + g.substring(20); }
197
+ function strToGuid(s) {
198
+ s = s.replace(/-/g, '');
199
+ var ret = s.substring(6, 8) + s.substring(4, 6) + s.substring(2, 4) + s.substring(0, 2);
200
+ ret += s.substring(10, 12) + s.substring(8, 10) + s.substring(14, 16) + s.substring(12, 14) + s.substring(16, 20) + s.substring(20);
201
+ return ret;
202
+ }
203
+
204
+ function binzerostring(len) {
205
+ var res='';
206
+ for (var l=0; l< len ; l++) {
207
+ res+=String.fromCharCode(0 & 0xFF);
208
+ }
209
+ return res;
210
+ }
211
+
212
+
213
+
214
+ function SendProtocolVersion(socket, uuid) {
215
+ var buuid = strToGuid(uuid);
216
+ var data = String.fromCharCode(APFProtocol.PROTOCOLVERSION) + '' + IntToStr(1) + IntToStr(0) + IntToStr(0) + hex2rstr(buuid) + binzerostring(64);
217
+ socket.write(data);
218
+ Debug("APF: Send protocol version 1 0 " + uuid);
219
+ obj.cirastate = CIRASTATE.PROTOCOL_VERSION_SENT;
220
+ }
221
+
222
+ function SendServiceRequest(socket, service) {
223
+ var data = String.fromCharCode(APFProtocol.SERVICE_REQUEST) + IntToStr(service.length) + service;
224
+ socket.write(data);
225
+ Debug("APF: Send service request " + service);
226
+ if (service == 'auth@amt.intel.com') {
227
+ obj.cirastate = CIRASTATE.AUTH_SERVICE_REQUEST_SENT;
228
+ } else if (service == 'pfwd@amt.intel.com') {
229
+ obj.cirastate = CIRASTATE.PFWD_SERVICE_REQUEST_SENT;
230
+ }
231
+ }
232
+
233
+ function SendUserAuthRequest(socket, user, pass) {
234
+ var service = "pfwd@amt.intel.com";
235
+ var data = String.fromCharCode(APFProtocol.USERAUTH_REQUEST) + IntToStr(user.length) + user + IntToStr(service.length) + service;
236
+ //password auth
237
+ data += IntToStr(8) + 'password';
238
+ data += binzerostring(1) + IntToStr(pass.length) + pass;
239
+ socket.write(data);
240
+ Debug("APF: Send username password authentication to MPS");
241
+ obj.cirastate = CIRASTATE.AUTH_REQUEST_SENT;
242
+ }
243
+
244
+ function SendGlobalRequestPfwd(socket, amthostname, amtport) {
245
+ var tcpipfwd = 'tcpip-forward';
246
+ var data = String.fromCharCode(APFProtocol.GLOBAL_REQUEST) + IntToStr(tcpipfwd.length) + tcpipfwd + binzerostring(1, 1);
247
+ data += IntToStr(amthostname.length) + amthostname + IntToStr(amtport);
248
+ socket.write(data);
249
+ Debug("APF: Send tcpip-forward " + amthostname + ":" + amtport);
250
+ obj.cirastate = CIRASTATE.GLOBAL_REQUEST_SENT;
251
+ }
252
+
253
+ function SendKeepAliveRequest(socket) {
254
+ var data = String.fromCharCode(APFProtocol.KEEPALIVE_REQUEST) + IntToStr(255);
255
+ socket.write(data);
256
+ Debug("APF: Send keepalive request");
257
+ }
258
+
259
+ function SendKeepAliveReply(socket, cookie) {
260
+ var data = String.fromCharCode(APFProtocol.KEEPALIVE_REPLY) + IntToStr(cookie);
261
+ socket.write(data);
262
+ Debug("APF: Send keepalive reply");
263
+ }
264
+
265
+ function ProcessData(socket) {
266
+ var cmd = socket.tag.accumulator.charCodeAt(0);
267
+ var len = socket.tag.accumulator.length;
268
+ var data = socket.tag.accumulator;
269
+ if (len == 0) { return 0; }
270
+ // respond to MPS according to obj.cirastate
271
+ switch (cmd) {
272
+ case APFProtocol.SERVICE_ACCEPT: {
273
+ var slen = ReadInt(data, 1);
274
+ var service = data.substring(5, 6 + slen);
275
+ Debug("APF: Service request to " + service + " accepted.");
276
+ if (service == 'auth@amt.intel.com') {
277
+ if (obj.cirastate >= CIRASTATE.AUTH_SERVICE_REQUEST_SENT) {
278
+ SendUserAuthRequest(socket.ws, obj.args.mpsuser, obj.args.mpspass);
279
+ }
280
+ } else if (service == 'pfwd@amt.intel.com') {
281
+ if (obj.cirastate >= CIRASTATE.PFWD_SERVICE_REQUEST_SENT) {
282
+ SendGlobalRequestPfwd(socket.ws, obj.args.clientname, pfwd_ports[obj.pfwd_idx++]);
283
+ }
284
+ }
285
+ return 5 + slen;
286
+ }
287
+ case APFProtocol.REQUEST_SUCCESS: {
288
+ if (len >= 5) {
289
+ var port = ReadInt(data, 1);
290
+ Debug("APF: Request to port forward " + port + " successful.");
291
+ // iterate to pending port forward request
292
+ if (obj.pfwd_idx < pfwd_ports.length) {
293
+ SendGlobalRequestPfwd(socket.ws, obj.args.clientname, pfwd_ports[obj.pfwd_idx++]);
294
+ } else {
295
+ // no more port forward, now setup timer to send keep alive
296
+ Debug("APF: Start keep alive for every " + obj.args.mpskeepalive + " ms.");
297
+ obj.timer = setInterval(function () {
298
+ SendKeepAliveRequest(obj.forwardClient.ws);
299
+ }, obj.args.mpskeepalive);//
300
+ }
301
+ return 5;
302
+ }
303
+ Debug("APF: Request successful.");
304
+ return 1;
305
+ }
306
+ case APFProtocol.USERAUTH_SUCCESS: {
307
+ Debug("APF: User Authentication successful");
308
+ // Send Pfwd service request
309
+ SendServiceRequest(socket.ws, 'pfwd@amt.intel.com');
310
+ return 1;
311
+ }
312
+ case APFProtocol.USERAUTH_FAILURE: {
313
+ Debug("APF: User Authentication failed");
314
+ obj.cirastate = CIRASTATE.FAILED;
315
+ return 14;
316
+ }
317
+ case APFProtocol.KEEPALIVE_REQUEST: {
318
+ Debug("APF: Keep Alive Request with cookie: " + ReadInt(data, 1));
319
+ SendKeepAliveReply(socket.ws, ReadInt(data, 1));
320
+ return 5;
321
+ }
322
+ case APFProtocol.KEEPALIVE_REPLY: {
323
+ Debug("APF: Keep Alive Reply with cookie: " + ReadInt(data, 1));
324
+ return 5;
325
+ }
326
+ // Channel management
327
+ case APFProtocol.CHANNEL_OPEN: {
328
+ //parse CHANNEL OPEN request
329
+ var p_res = parseChannelOpen(data);
330
+ Debug("APF: CHANNEL_OPEN request: " + JSON.stringify(p_res));
331
+ // Check if target port is in pfwd_ports
332
+ if (pfwd_ports.indexOf(p_res.target_port) >= 0) {
333
+ // connect socket to that port
334
+ obj.downlinks[p_res.sender_chan] = obj.net.createConnection({ host: obj.args.clientaddress, port: p_res.target_port }, function () {
335
+ obj.downlinks[p_res.sender_chan].setEncoding('binary');//assume everything is binary, not interpreting
336
+ SendChannelOpenConfirm(socket.ws, p_res);
337
+ });
338
+
339
+ obj.downlinks[p_res.sender_chan].on('data', function (ddata) {
340
+ //Relay data to fordwardclient
341
+ SendChannelData(socket.ws, p_res.sender_chan, ddata.length, ddata);
342
+ });
343
+
344
+ obj.downlinks[p_res.sender_chan].on('error', function (e) {
345
+ Debug("Downlink connection error: " + e);
346
+ });
347
+
348
+ obj.downlinks[p_res.sender_chan].on('end', function () {
349
+ if (obj.downlinks[p_res.sender_chan]) {
350
+ try {
351
+ SendChannelClose(socket.ws, p_res.sender_chan);
352
+ delete obj.downlinks[p_res.sender_chan];
353
+ } catch (e) {
354
+ Debug("Downlink connection exception: " + e);
355
+ }
356
+ }
357
+ });
358
+ } else {
359
+ SendChannelOpenFailure(socket.ws, p_res);
360
+ }
361
+ return p_res.len;
362
+ }
363
+ case APFProtocol.CHANNEL_OPEN_CONFIRMATION: {
364
+ Debug("APF: CHANNEL_OPEN_CONFIRMATION");
365
+ return 17;
366
+ }
367
+ case APFProtocol.CHANNEL_CLOSE: {
368
+ var rcpt_chan = ReadInt(data, 1);
369
+ Debug("APF: CHANNEL_CLOSE: " + rcpt_chan);
370
+ SendChannelClose(socket.ws, rcpt_chan);
371
+ try {
372
+ obj.downlinks[rcpt_chan].end();
373
+ delete obj.downlinks[rcpt_chan];
374
+ } catch (e) { }
375
+ return 5;
376
+ }
377
+ case APFProtocol.CHANNEL_DATA: {
378
+ Debug("APF: CHANNEL_DATA: " + JSON.stringify(rstr2hex(data)));
379
+ var rcpt_chan = ReadInt(data, 1);
380
+ var chan_data_len = ReadInt(data, 5);
381
+ var chan_data = data.substring(9, 9 + chan_data_len);
382
+ if (obj.downlinks[rcpt_chan]) {
383
+ try {
384
+ obj.downlinks[rcpt_chan].write(chan_data, 'binary', function () {
385
+ Debug("Write completed.");
386
+ SendChannelWindowAdjust(socket.ws, rcpt_chan, chan_data_len);//I have full window capacity now
387
+ });
388
+ } catch (e) {
389
+ Debug("Cannot forward data to downlink socket.");
390
+ }
391
+ }
392
+ return 9 + chan_data_len;
393
+ }
394
+ case APFProtocol.CHANNEL_WINDOW_ADJUST: {
395
+ Debug("APF: CHANNEL_WINDOW_ADJUST ");
396
+ return 9;
397
+ }
398
+ default: {
399
+ Debug("CMD: " + cmd + " is not implemented.");
400
+ obj.cirastate = CIRASTATE.FAILED;
401
+ return 0;
402
+ }
403
+ }
404
+ }
405
+
406
+ function parseChannelOpen(data) {
407
+ var result = {
408
+ len: 0, //to be filled later
409
+ cmd: APFProtocol.CHANNEL_OPEN,
410
+ chan_type: "", //to be filled later
411
+ sender_chan: 0, //to be filled later
412
+ window_size: 0, //to be filled later
413
+ target_address: "", //to be filled later
414
+ target_port: 0, //to be filled later
415
+ origin_address: "", //to be filled later
416
+ origin_port: 0, //to be filled later
417
+ };
418
+ var chan_type_slen = ReadInt(data, 1);
419
+ result.chan_type = data.substring(5, 5 + chan_type_slen);
420
+ result.sender_chan = ReadInt(data, 5 + chan_type_slen);
421
+ result.window_size = ReadInt(data, 9 + chan_type_slen);
422
+ var c_len = ReadInt(data, 17 + chan_type_slen);
423
+ result.target_address = data.substring(21 + chan_type_slen, 21 + chan_type_slen + c_len);
424
+ result.target_port = ReadInt(data, 21 + chan_type_slen + c_len);
425
+ var o_len = ReadInt(data, 25 + chan_type_slen + c_len);
426
+ result.origin_address = data.substring(29 + chan_type_slen + c_len, 29 + chan_type_slen + c_len + o_len);
427
+ result.origin_port = ReadInt(data, 29 + chan_type_slen + c_len + o_len);
428
+ result.len = 33 + chan_type_slen + c_len + o_len;
429
+ return result;
430
+ }
431
+ function SendChannelOpenFailure(socket, chan_data) {
432
+ var data = String.fromCharCode(APFProtocol.CHANNEL_OPEN_FAILURE) + IntToStr(chan_data.sender_chan)
433
+ + IntToStr(2) + IntToStr(0) + IntToStr(0);
434
+ socket.write(data);
435
+ Debug("APF: Send ChannelOpenFailure");
436
+ }
437
+ function SendChannelOpenConfirm(socket, chan_data) {
438
+ var data = String.fromCharCode(APFProtocol.CHANNEL_OPEN_CONFIRMATION) + IntToStr(chan_data.sender_chan)
439
+ + IntToStr(chan_data.sender_chan) + IntToStr(chan_data.window_size) + IntToStr(0xFFFFFFFF);
440
+ socket.write(data);
441
+ Debug("APF: Send ChannelOpenConfirmation");
442
+ }
443
+
444
+ function SendChannelWindowAdjust(socket, chan, size) {
445
+ var data = String.fromCharCode(APFProtocol.CHANNEL_WINDOW_ADJUST) + IntToStr(chan) + IntToStr(size);
446
+ socket.write(data);
447
+ Debug("APF: Send ChannelWindowAdjust: " + rstr2hex(data));
448
+ }
449
+
450
+ function SendChannelData(socket, chan, len, data) {
451
+ var buf = String.fromCharCode(APFProtocol.CHANNEL_DATA) + IntToStr(chan) + IntToStr(len) + data;
452
+ socket.write(Buffer.from(buf, 'binary'));
453
+ Debug("APF: Send ChannelData: " + rstr2hex(buf));
454
+ }
455
+
456
+ function SendChannelClose(socket, chan) {
457
+ var buf = String.fromCharCode(APFProtocol.CHANNEL_CLOSE) + IntToStr(chan);
458
+ socket.write(Buffer.from(buf, 'binary'));
459
+ Debug("APF: Send ChannelClose: " + rstr2hex(buf));
460
+ }
461
+
462
+ obj.connect = function () {
463
+ if (obj.forwardClient != null) {
464
+ try {
465
+ obj.forwardClient.ws.end();
466
+ } catch (e) {
467
+ Debug(e);
468
+ }
469
+ //obj.forwardClient = null;
470
+ }
471
+ obj.cirastate = CIRASTATE.INITIAL;
472
+ obj.pfwd_idx = 0;
473
+
474
+ //obj.forwardClient = new obj.ws(obj.args.mpsurl, obj.tlsoptions);
475
+ //obj.forwardClient.on("open", obj.onSecureConnect);
476
+
477
+
478
+ var wsoptions = obj.http.parseUri(obj.args.mpsurl);
479
+ wsoptions.rejectUnauthorized = 0;
480
+ obj.forwardClient = obj.http.request(wsoptions);
481
+ obj.forwardClient.upgrade = obj.onSecureConnect;
482
+ obj.forwardClient.end(); // end request, trigger completion of HTTP request
483
+ }
484
+
485
+ obj.disconnect = function () {
486
+ try {
487
+ obj.forwardClient.ws.end();
488
+ } catch (e) {
489
+ Debug(e);
490
+ }
491
+ }
492
+
493
+ return obj;
494
+}
495
+
496
+module.exports = CreateAPFClient;
\ No newline at end of file
apfserver.js
+12
-9
@@ -132,17 +132,20 @@ module.exports.CreateApfServer = function (parent, db, args) {
132
};
133
}
134
135
- obj.onConnection = function(socket) {
136
- console.log("Here");
135
+ obj.onConnection = function(socket) {
136
connectionCount++;
137
// treat APS over WS like tlsoffload APF
138
socket.tag = { first: true, clientCert: null, accumulator: "", activetunnels: 0, boundPorts: [], socket: socket, host: null, nextchannelid: 4, channels: {}, nextsourceport: 0 };
139
parent.debug('apf', "New APF connection");
140
+ parent.debug('apf',"WS Extensions:"+socket.extensions);
141
+ parent.debug('apf',"WS Binary type:"+socket.binaryType);
142
+
143
+ socket._socket.on('data', function(chunk) { console.log(chunk.toString('hex'))});
144
145
// Setup the APF keep alive timer
146
// Websocket does not have timout
147
// socket.setTimeout(MAX_IDLE);
145
- //socket.on("timeout", () => { ciraTimeoutCount++; parent.debug('mps', "APF timeout, disconnecting."); try { socket.terminate(); } catch (e) { } });
148
+ //socket.on("timeout", () => { ciraTimeoutCount++; parent.debug('apf', "APF timeout, disconnecting."); try { socket.terminate(); } catch (e) { } });
149
//use on message instead because of websocket
150
socket.on("message", function (data) {
151
// use the same debug flag like APF
@@ -206,13 +209,13 @@ module.exports.CreateApfServer = function (parent, db, args) {
209
parent.debug('apfcmd', 'USERAUTH_REQUEST user=' + username + ', service=' + serviceName + ', method=' + methodName + ', password=' + password);
210
211
// Check the APF password
209
- if ((args.mpspass != null) && (password != args.mpspass)) { incorrectPasswordCount++; parent.debug('mps', 'Incorrect password', username, password); SendUserAuthFail(socket); return -1; }
212
+ if ((args.mpspass != null) && (password != args.mpspass)) { incorrectPasswordCount++; parent.debug('apf', 'Incorrect password', username, password); SendUserAuthFail(socket); return -1; }
213
214
// Check the APF username, which should be the start of the MeshID.
212
- if (usernameLen != 16) { badUserNameLengthCount++; parent.debug('mps', 'Username length not 16', username, password); SendUserAuthFail(socket); return -1; }
215
+ if (usernameLen != 16) { badUserNameLengthCount++; parent.debug('apf', 'Username length not 16', username, password); SendUserAuthFail(socket); return -1; }
216
var meshIdStart = '/' + username, mesh = null;
217
if (obj.parent.webserver.meshes) { for (var i in obj.parent.webserver.meshes) { if (obj.parent.webserver.meshes[i]._id.replace(/\@/g, 'X').replace(/\$/g, 'X').indexOf(meshIdStart) > 0) { mesh = obj.parent.webserver.meshes[i]; break; } } }
215
- if (mesh == null) { meshNotFoundCount++; parent.debug('mps', 'Mesh not found', username, password); SendUserAuthFail(socket); return -1; }
218
+ if (mesh == null) { meshNotFoundCount++; parent.debug('apf', 'Mesh not found', username, password); SendUserAuthFail(socket); return -1; }
219
220
// If this is a agent-less mesh, use the device guid 3 times as ID.
221
if (mesh.mtype == 1) {
@@ -547,14 +550,14 @@ module.exports.CreateApfServer = function (parent, db, args) {
550
551
socket.addListener("close", function () {
552
socketClosedCount++;
550
- parent.debug('mps', 'APF connection closed');
553
+ parent.debug('apf', 'APF connection closed');
554
try { delete obj.apfConnections[socket.tag.nodeid]; } catch (e) { }
555
obj.parent.ClearConnectivityState(socket.tag.meshid, socket.tag.nodeid, 8);
556
});
557
555
- socket.addListener("error", function () {
558
+ socket.addListener("error", function (e) {
559
socketErrorCount++;
557
- //console.log("APF Error: " + socket.remoteAddress);
560
+ console.log("APF Error: " + e);
561
});
562
563
}