-
Notifications
You must be signed in to change notification settings - Fork 544
Expand file tree
/
Copy pathduk_debug_proxy.js
More file actions
1044 lines (950 loc) · 35.7 KB
/
duk_debug_proxy.js
File metadata and controls
1044 lines (950 loc) · 35.7 KB
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* JSON debug proxy written in DukLuv
*
* This single file JSON debug proxy implementation is an alternative to the
* Node.js-based proxy in duk_debug.js. DukLuv is a much smaller dependency
* than Node.js so embedding DukLuv in a debug client is easier.
*/
'use strict';
// XXX: Code assumes uv.write() will write fully. This is not necessarily
// true; should add support for partial writes (or at least failing when
// a partial write occurs).
var log = new Duktape.Logger('Proxy'); // default logger
//log.l = 0; // enable debug and trace logging
/*
* Config
*/
var serverHost = '0.0.0.0';
var serverPort = 9093;
var targetHost = '127.0.0.1';
var targetPort = 9091;
var singleConnection = false;
var readableNumberValue = false;
var lenientJsonParse = false;
var jxParse = false;
var metadataFile = null;
var metadata = {};
var TORTURE = false; // for manual testing of binary/json parsing robustness
/*
* Duktape 1.x and 2.x buffer harmonization
*/
var allocPlain = (typeof Uint8Array.allocPlain === 'function' ?
Uint8Array.allocPlain : Duktape.Buffer);
var plainOf = (typeof Uint8Array.plainOf === 'function' ?
Uint8Array.plainOf : Duktape.Buffer);
var bufferToString = (typeof String.fromBuffer === 'function' ?
String.fromBuffer : String);
/*
* Detect missing 'var' declarations
*/
// Prevent new bindings on global object. This detects missing 'var'
// declarations, e.g. "x = 123;" in a function without declaring it.
var global = new Function('return this;')();
log.debug('Preventing extensions on global object');
log.debug('Global is extensible:', Object.isExtensible(global));
Object.preventExtensions(global);
log.debug('Global is extensible:', Object.isExtensible(global));
/*
* Misc helpers
*/
function jxEncode(v) {
return Duktape.enc('jx', v);
}
function plainBufferCopy(typedarray) {
// This is still pretty awkward in Duktape 1.4.x.
// Argument may be a "slice" and we want a copy of the slice
// (not the full underlying buffer).
var u8 = new Uint8Array(typedarray.length);
u8.set(typedarray); // make a copy, ensuring there's no slice offset
return plainOf(u8); // get underlying plain buffer
}
function isObject(x) {
// Note that typeof null === 'object'.
return (typeof x === 'object' && x !== null);
}
function readFully(filename, cb) {
uv.fs_open(metadataFile, 'r', 0, function (handle, err) {
var fileOff = 0;
var data = new Uint8Array(256);
var dataOff = 0;
if (err) {
return cb(null, err);
}
function readCb(buf, err) {
var res;
var newData;
log.debug('Read callback:', buf.length, err);
if (err) {
uv.fs_close(handle);
return cb(null, err);
}
if (buf.length == 0) {
uv.fs_close(handle);
res = new Uint8Array(dataOff);
res.set(data.subarray(0, dataOff));
res = plainOf(res); // plain buffer
log.debug('Read', res.length, 'bytes from', filename);
return cb(res, null);
}
while (data.length - dataOff < buf.length) {
log.debug('Resize file read buffer:', data.length, '->', data.length * 2);
newData = new Uint8Array(data.length * 2);
newData.set(data);
data = newData;
}
data.set(new Uint8Array(buf), dataOff);
dataOff += buf.length;
fileOff += buf.length;
uv.fs_read(handle, 4096, fileOff, readCb);
}
uv.fs_read(handle, 4096, fileOff, readCb);
});
}
/*
* JSON proxy server
*
* Accepts an incoming JSON proxy client and connects to a debug target,
* tying the two connections together. Supports both a single connection
* and a persistent mode.
*/
function JsonProxyServer(host, port) {
this.name = 'JsonProxyServer';
this.handle = uv.new_tcp();
uv.tcp_bind(this.handle, host, port);
uv.listen(this.handle, 128, this.onConnection.bind(this));
}
JsonProxyServer.prototype.onConnection = function onConnection(err) {
if (err) {
log.error('JSON proxy onConnection error:', err);
return;
}
log.info('JSON proxy client connected'); // XXX: it'd be nice to log remote peer host:port
var jsonSock = new JsonConnHandler(this);
var targSock = new TargetConnHandler(this);
jsonSock.targetHandler = targSock;
targSock.jsonHandler = jsonSock;
uv.accept(this.handle, jsonSock.handle);
log.info('Connecting to debug target at', targetHost + ':' + targetPort);
jsonSock.writeJson({ notify: '_TargetConnecting', args: [ targetHost, targetPort ] });
uv.tcp_connect(targSock.handle, targetHost, targetPort, targSock.onConnect.bind(targSock));
if (singleConnection) {
log.info('Single connection mode, stop listening for more connections');
uv.shutdown(this.handle);
uv.read_stop(this.handle); // unnecessary but just in case
uv.close(this.handle);
this.handle = null;
}
};
JsonProxyServer.prototype.onProxyClientDisconnected = function onProxyClientDisconnected() {
// When this is invoked the proxy connection and the target connection
// have both been closed.
if (singleConnection) {
log.info('Proxy connection finished (single connection mode: we should be exiting now)');
} else {
log.info('Proxy connection finished (persistent mode: wait for more connections)');
}
};
/*
* JSON connection handler
*/
function JsonConnHandler(server) {
var i, n;
this.name = 'JsonConnHandler';
this.server = server;
this.handle = uv.new_tcp();
this.incoming = new Uint8Array(4096);
this.incomingOffset = 0;
this.targetHandler = null;
this.commandNumberLookup = {};
if (metadata && metadata.target_commands) {
for (i = 0, n = metadata.target_commands.length; i < n; i++) {
this.commandNumberLookup[metadata.target_commands[i]] = i;
}
}
}
JsonConnHandler.prototype.finish = function finish(msg) {
var args;
if (!this.handle) {
log.info('JsonConnHandler already disconnected, ignore finish()');
return;
}
log.info('JsonConnHandler finished:', msg);
try {
args = msg ? [ msg ] : void 0;
this.writeJson({ notify: '_Disconnecting', args: args });
} catch (e) {
log.info('Failed to write _Disconnecting notify, ignoring:', e);
}
uv.shutdown(this.handle);
uv.read_stop(this.handle);
uv.close(this.handle);
this.handle = null;
this.targetHandler.finish(msg); // disconnect target too (if not already disconnected)
this.server.onProxyClientDisconnected();
};
JsonConnHandler.prototype.onRead = function onRead(err, data) {
var newIncoming;
var msg;
var errmsg;
var tmpBuf;
log.trace('Received data from JSON socket, err:', err, 'data length:', data ? data.length : 'null');
if (err) {
errmsg = 'Error reading data from JSON debug client: ' + err;
this.finish(errmsg);
return;
}
if (data) {
// Feed the data one byte at a time when torture testing.
if (TORTURE && data.length > 1) {
for (var i = 0; i < data.length; i++) {
tmpBuf = allocPlain(1);
tmpBuf[0] = data[i];
this.onRead(null, tmpBuf);
}
return;
}
// Receive data into 'incoming', resizing as necessary.
while (data.length > this.incoming.length - this.incomingOffset) {
newIncoming = new Uint8Array(this.incoming.length * 1.3 + 16);
newIncoming.set(this.incoming);
this.incoming = newIncoming;
log.debug('Resize incoming JSON buffer to ' + this.incoming.length);
}
this.incoming.set(new Uint8Array(data), this.incomingOffset);
this.incomingOffset += data.length;
// Trial parse JSON message(s).
while (true) {
msg = this.trialParseJsonMessage();
if (!msg) {
break;
}
try {
this.dispatchJsonMessage(msg);
} catch (e) {
errmsg = 'JSON message dispatch failed: ' + e;
this.writeJson({ notify: '_Error', args: [ errmsg ] });
if (lenientJsonParse) {
log.warn('JSON message dispatch failed (lenient mode, ignoring):', e);
} else {
log.warn('JSON message dispatch failed (dropping connection):', e);
this.finish(errmsg);
}
}
}
} else {
this.finish('JSON proxy client disconnected');
}
};
JsonConnHandler.prototype.writeJson = function writeJson(msg) {
log.info('PROXY --> CLIENT:', JSON.stringify(msg));
if (this.handle) {
uv.write(this.handle, JSON.stringify(msg) + '\n');
}
};
JsonConnHandler.prototype.handleDebugMessage = function handleDebugMessage(dvalues) {
var msg = {};
var idx = 0;
var cmd;
if (dvalues.length <= 0) {
throw new Error('invalid dvalues list: length <= 0');
}
var x = dvalues[idx++];
if (!isObject(x)) {
throw new Error('invalid initial dvalue: ' + Duktape.enc('jx', dvalues));
}
if (x.type === 'req') {
cmd = dvalues[idx++];
if (typeof cmd !== 'number') {
throw new Error('invalid command: ' + Duktape.enc('jx', cmd));
}
msg.request = this.determineCommandName(cmd) || true;
msg.command = cmd;
} else if (x.type === 'rep') {
msg.reply = true;
} else if (x.type === 'err') {
msg.error = true;
} else if (x.type === 'nfy') {
cmd = dvalues[idx++];
if (typeof cmd !== 'number') {
throw new Error('invalid command: ' + Duktape.enc('jx', cmd));
}
msg.notify = this.determineCommandName(cmd) || true;
msg.command = cmd;
} else {
throw new Error('invalid initial dvalue: ' + Duktape.enc('jx', dvalues));
}
for (; idx < dvalues.length - 1; idx++) {
if (!msg.args) {
msg.args = [];
}
msg.args.push(dvalues[idx]);
}
if (!isObject(dvalues[idx]) || dvalues[idx].type !== 'eom') {
throw new Error('invalid final dvalue: ' + Duktape.enc('jx', dvalues));
}
this.writeJson(msg);
};
JsonConnHandler.prototype.determineCommandName = function determineCommandName(cmd) {
if (!(metadata && metadata.client_commands)) {
return;
}
return metadata.client_commands[cmd];
};
JsonConnHandler.prototype.trialParseJsonMessage = function trialParseJsonMessage() {
var buf = this.incoming;
var avail = this.incomingOffset;
var i;
var msg, str, errmsg;
for (i = 0; i < avail; i++) {
if (buf[i] == 0x0a) {
str = bufferToString(plainBufferCopy(buf.subarray(0, i)));
try {
if (jxParse) {
msg = Duktape.dec('jx', str);
} else {
msg = JSON.parse(str);
}
} catch (e) {
// In lenient mode if JSON parse fails just send back an _Error
// and ignore the line (useful for initial development).
//
// In non-lenient mode drop the connection here; if the failed line
// was a request the client is expecting a reply/error message back
// (otherwise it may go out of sync) but we can't send a synthetic
// one (as we can't parse the request).
errmsg = 'JSON parse failed for: ' + jxEncode(str) + ': ' + e;
this.writeJson({ notify: '_Error', args: [ errmsg ] });
if (lenientJsonParse) {
log.warn('JSON parse failed (lenient mode, ignoring):', e);
} else {
log.warn('JSON parse failed (dropping connection):', e);
this.finish(errmsg);
}
}
this.incoming.set(this.incoming.subarray(i + 1));
this.incomingOffset -= i + 1;
return msg;
}
}
};
JsonConnHandler.prototype.dispatchJsonMessage = function dispatchJsonMessage(msg) {
var cmd;
var dvalues = [];
var i, n;
log.info('PROXY <-- CLIENT:', JSON.stringify(msg));
// Parse message type, determine initial marker for binary message.
if (msg.request) {
cmd = this.determineCommandNumber(msg.request, msg.command);
dvalues.push(new Uint8Array([ 0x01 ]));
dvalues.push(this.encodeJsonDvalue(cmd));
} else if (msg.reply) {
dvalues.push(new Uint8Array([ 0x02 ]));
} else if (msg.notify) {
cmd = this.determineCommandNumber(msg.notify, msg.command);
dvalues.push(new Uint8Array([ 0x04 ]));
dvalues.push(this.encodeJsonDvalue(cmd));
} else if (msg.error) {
dvalues.push(new Uint8Array([ 0x03 ]));
} else {
throw new Error('invalid input JSON message: ' + jxEncode(msg));
}
// Encode arguments into dvalues.
for (i = 0, n = (msg.args ? msg.args.length : 0); i < n; i++) {
dvalues.push(this.encodeJsonDvalue(msg.args[i]));
}
// Add an EOM, and write out the dvalues to the debug target.
dvalues.push(new Uint8Array([ 0x00 ]));
for (i = 0, n = dvalues.length; i < n; i++) {
this.targetHandler.writeBinary(dvalues[i]);
}
};
JsonConnHandler.prototype.determineCommandNumber = function determineCommandNumber(name, val) {
var res;
if (typeof name === 'string') {
res = this.commandNumberLookup[name];
if (!res) {
log.info('Unknown command name: ' + name + ', command number: ' + val);
}
} else if (typeof name === 'number') {
res = name;
} else if (name !== true) {
throw new Error('invalid command name (must be string, number, or "true"): ' + name);
}
if (typeof res === 'undefined' && typeof val === 'undefined') {
throw new Error('cannot determine command number from name: ' + name);
}
if (typeof val !== 'number' && typeof val !== 'undefined') {
throw new Error('invalid command number: ' + val);
}
res = res || val;
return res;
};
JsonConnHandler.prototype.writeDebugStringToBuffer = function writeDebugStringToBuffer(v, buf, off) {
var i, n;
for (i = 0, n = v.length; i < n; i++) {
buf[off + i] = v.charCodeAt(i) & 0xff; // truncate higher bits
}
};
JsonConnHandler.prototype.encodeJsonDvalue = function encodeJsonDvalue(v) {
var buf, dec, len, dv;
if (isObject(v)) {
if (v.type === 'eom') {
return new Uint8Array([ 0x00 ]);
} else if (v.type === 'req') {
return new Uint8Array([ 0x01 ]);
} else if (v.type === 'rep') {
return new Uint8Array([ 0x02 ]);
} else if (v.type === 'err') {
return new Uint8Array([ 0x03 ]);
} else if (v.type === 'nfy') {
return new Uint8Array([ 0x04 ]);
} else if (v.type === 'unused') {
return new Uint8Array([ 0x15 ]);
} else if (v.type === 'undefined') {
return new Uint8Array([ 0x16 ]);
} else if (v.type === 'number') {
dec = Duktape.dec('hex', v.data);
len = dec.length;
if (len !== 8) {
throw new TypeError('value cannot be converted to dvalue: ' + jxEncode(v));
}
buf = new Uint8Array(1 + len);
buf[0] = 0x1a;
buf.set(new Uint8Array(dec), 1);
return buf;
} else if (v.type === 'buffer') {
dec = Duktape.dec('hex', v.data);
len = dec.length;
if (len <= 0xffff) {
buf = new Uint8Array(3 + len);
buf[0] = 0x14;
buf[1] = (len >> 8) & 0xff;
buf[2] = (len >> 0) & 0xff;
buf.set(new Uint8Arrau(dec), 3);
return buf;
} else {
buf = new Uint8Array(5 + len);
buf[0] = 0x13;
buf[1] = (len >> 24) & 0xff;
buf[2] = (len >> 16) & 0xff;
buf[3] = (len >> 8) & 0xff;
buf[4] = (len >> 0) & 0xff;
buf.set(new Uint8Array(dec), 5);
return buf;
}
} else if (v.type === 'object') {
dec = Duktape.dec('hex', v.pointer);
len = dec.length;
buf = new Uint8Array(3 + len);
buf[0] = 0x1b;
buf[1] = v.class;
buf[2] = len;
buf.set(new Uint8Array(dec), 3);
return buf;
} else if (v.type === 'pointer') {
dec = Duktape.dec('hex', v.pointer);
len = dec.length;
buf = new Uint8Array(2 + len);
buf[0] = 0x1c;
buf[1] = len;
buf.set(new Uint8Array(dec), 2);
return buf;
} else if (v.type === 'lightfunc') {
dec = Duktape.dec('hex', v.pointer);
len = dec.length;
buf = new Uint8Array(4 + len);
buf[0] = 0x1d;
buf[1] = (v.flags >> 8) & 0xff;
buf[2] = v.flags & 0xff;
buf[3] = len;
buf.set(new Uint8Array(dec), 4);
return buf;
} else if (v.type === 'heapptr') {
dec = Duktape.dec('hex', v.pointer);
len = dec.length;
buf = new Uint8Array(2 + len);
buf[0] = 0x1e;
buf[1] = len;
buf.set(new Uint8Array(dec), 2);
return buf;
}
} else if (v === null) {
return new Uint8Array([ 0x17 ]);
} else if (typeof v === 'boolean') {
return new Uint8Array([ v ? 0x18 : 0x19 ]);
} else if (typeof v === 'number') {
if (Math.floor(v) === v && /* whole */
(v !== 0 || 1 / v > 0) && /* not negative zero */
v >= -0x80000000 && v <= 0x7fffffff) {
// Represented signed 32-bit integers as plain integers.
// Debugger code expects this for all fields that are not
// duk_tval representations (e.g. command numbers and such).
if (v >= 0x00 && v <= 0x3f) {
return new Uint8Array([ 0x80 + v ]);
} else if (v >= 0x0000 && v <= 0x3fff) {
return new Uint8Array([ 0xc0 + (v >> 8), v & 0xff ]);
} else if (v >= -0x80000000 && v <= 0x7fffffff) {
return new Uint8Array([ 0x10,
(v >> 24) & 0xff,
(v >> 16) & 0xff,
(v >> 8) & 0xff,
(v >> 0) & 0xff ]);
} else {
throw new Error('internal error when encoding integer to dvalue: ' + v);
}
} else {
// Represent non-integers as IEEE double dvalues.
buf = new Uint8Array(1 + 8);
buf[0] = 0x1a;
new DataView(buf).setFloat64(1, v, false);
return buf;
}
} else if (typeof v === 'string') {
if (v.length < 0 || v.length > 0xffffffff) {
// Not possible in practice.
throw new TypeError('cannot convert to dvalue, invalid string length: ' + v.length);
}
if (v.length <= 0x1f) {
buf = new Uint8Array(1 + v.length);
buf[0] = 0x60 + v.length;
this.writeDebugStringToBuffer(v, buf, 1);
return buf;
} else if (v.length <= 0xffff) {
buf = new Uint8Array(3 + v.length);
buf[0] = 0x12;
buf[1] = (v.length >> 8) & 0xff;
buf[2] = (v.length >> 0) & 0xff;
this.writeDebugStringToBuffer(v, buf, 3);
return buf;
} else {
buf = new Uint8Array(5 + v.length);
buf[0] = 0x11;
buf[1] = (v.length >> 24) & 0xff;
buf[2] = (v.length >> 16) & 0xff;
buf[3] = (v.length >> 8) & 0xff;
buf[4] = (v.length >> 0) & 0xff;
this.writeDebugStringToBuffer(v, buf, 5);
return buf;
}
}
throw new TypeError('value cannot be converted to dvalue: ' + jxEncode(v));
};
/*
* Target binary connection handler
*/
function TargetConnHandler(server) {
this.name = 'TargetConnHandler';
this.server = server;
this.handle = uv.new_tcp();
this.jsonHandler = null;
this.incoming = new Uint8Array(4096);
this.incomingOffset = 0;
this.dvalues = [];
}
TargetConnHandler.prototype.finish = function finish(msg) {
if (!this.handle) {
log.info('TargetConnHandler already disconnected, ignore finish()');
return;
}
log.info('TargetConnHandler finished:', msg);
this.jsonHandler.writeJson({ notify: '_TargetDisconnected' });
// XXX: write a notify to target?
uv.shutdown(this.handle);
uv.read_stop(this.handle);
uv.close(this.handle);
this.handle = null;
this.jsonHandler.finish(msg); // disconnect JSON client too (if not already disconnected)
};
TargetConnHandler.prototype.onConnect = function onConnect(err) {
var errmsg;
if (err) {
errmsg = 'Failed to connect to target: ' + err;
log.warn(errmsg);
this.jsonHandler.writeJson({ notify: '_Error', args: [ String(err) ] });
this.finish(errmsg);
return;
}
// Once we're connected to the target, start read both binary and JSON
// input. We don't want to read JSON input before this so that we can
// always translate incoming messages to dvalues and write them out
// without queueing. Any pending JSON messages will be queued by the
// OS instead.
log.info('Connected to debug target at', targetHost + ':' + targetPort);
uv.read_start(this.jsonHandler.handle, this.jsonHandler.onRead.bind(this.jsonHandler));
uv.read_start(this.handle, this.onRead.bind(this));
};
TargetConnHandler.prototype.writeBinary = function writeBinary(buf) {
var plain = plainBufferCopy(buf);
log.info('PROXY --> TARGET:', Duktape.enc('jx', plain));
if (this.handle) {
uv.write(this.handle, plain);
}
};
TargetConnHandler.prototype.onRead = function onRead(err, data) {
var res;
var errmsg;
var tmpBuf;
var newIncoming;
log.trace('Received data from target socket, err:', err, 'data length:', data ? data.length : 'null');
if (err) {
errmsg = 'Error reading data from debug target: ' + err;
this.finish(errmsg);
return;
}
if (data) {
// Feed the data one byte at a time when torture testing.
if (TORTURE && data.length > 1) {
for (var i = 0; i < data.length; i++) {
tmpBuf = allocPlain(1);
tmpBuf[0] = data[i];
this.onRead(null, tmpBuf);
}
return;
}
// Receive data into 'incoming', resizing as necessary.
while (data.length > this.incoming.length - this.incomingOffset) {
newIncoming = new Uint8Array(this.incoming.length * 1.3 + 16);
newIncoming.set(this.incoming);
this.incoming = newIncoming;
log.debug('Resize incoming binary buffer to ' + this.incoming.length);
}
this.incoming.set(new Uint8Array(data), this.incomingOffset);
this.incomingOffset += data.length;
// Trial parse handshake unless done.
if (!this.handshake) {
this.trialParseHandshake();
}
// Trial parse dvalue(s) and debug messages.
if (this.handshake) {
for (;;) {
res = this.trialParseDvalue();
if (!res) {
break;
}
log.trace('Got dvalue:', Duktape.enc('jx', res.dvalue));
this.dvalues.push(res.dvalue);
if (isObject(res.dvalue) && res.dvalue.type === 'eom') {
try {
this.jsonHandler.handleDebugMessage(this.dvalues);
this.dvalues = [];
} catch (e) {
errmsg = 'JSON message handling failed: ' + e;
this.jsonHandler.writeJson({ notify: '_Error', args: [ errmsg ] });
if (lenientJsonParse) {
log.warn('JSON message handling failed (lenient mode, ignoring):', e);
} else {
log.warn('JSON message handling failed (dropping connection):', e);
this.finish(errmsg);
}
}
}
}
}
} else {
log.info('Target disconnected');
this.finish('Target disconnected');
}
};
TargetConnHandler.prototype.trialParseHandshake = function trialParseHandshake() {
var buf = this.incoming;
var avail = this.incomingOffset;
var i;
var msg;
var m;
var protocolVersion;
for (i = 0; i < avail; i++) {
if (buf[i] == 0x0a) {
msg = bufferToString(plainBufferCopy(buf.subarray(0, i)));
this.incoming.set(this.incoming.subarray(i + 1));
this.incomingOffset -= i + 1;
// Generic handshake format: only relies on initial version field.
m = /^(\d+) (.*)$/.exec(msg) || {};
protocolVersion = +m[1];
this.handshake = {
line: msg,
protocolVersion: protocolVersion,
text: m[2]
};
// More detailed v1 handshake line.
if (protocolVersion === 1) {
m = /^(\d+) (\d+) (.*?) (.*?) (.*)$/.exec(msg) || {};
this.handshake.dukVersion = m[1];
this.handshake.dukGitDescribe = m[2];
this.handshake.targetString = m[3];
}
this.jsonHandler.writeJson({ notify: '_TargetConnected', args: [ msg ] });
log.info('Target handshake: ' + JSON.stringify(this.handshake));
return;
}
}
};
TargetConnHandler.prototype.bufferToDebugString = function bufferToDebugString(buf) {
return String.fromCharCode.apply(null, buf);
};
TargetConnHandler.prototype.trialParseDvalue = function trialParseDvalue() {
var _this = this;
var buf = this.incoming;
var avail = this.incomingOffset;
var v;
var gotValue = false; // explicit flag for e.g. v === undefined
var dv = new DataView(buf);
var tmp;
var x;
var len;
function consume(n) {
log.info('PROXY <-- TARGET:', Duktape.enc('jx', _this.incoming.subarray(0, n)));
_this.incoming.set(_this.incoming.subarray(n));
_this.incomingOffset -= n;
}
x = buf[0];
if (avail <= 0) {
;
} else if (x >= 0xc0) {
// 0xc0...0xff: integers 0-16383
if (avail >= 2) {
v = ((x - 0xc0) << 8) + buf[1];
consume(2);
}
} else if (x >= 0x80) {
// 0x80...0xbf: integers 0-63
v = x - 0x80;
consume(1);
} else if (x >= 0x60) {
// 0x60...0x7f: strings with length 0-31
len = x - 0x60;
if (avail >= 1 + len) {
v = new Uint8Array(len);
v.set(buf.subarray(1, 1 + len));
v = this.bufferToDebugString(v);
consume(1 + len);
}
} else {
switch (x) {
case 0x00: consume(1); v = { type: 'eom' }; break;
case 0x01: consume(1); v = { type: 'req' }; break;
case 0x02: consume(1); v = { type: 'rep' }; break;
case 0x03: consume(1); v = { type: 'err' }; break;
case 0x04: consume(1); v = { type: 'nfy' }; break;
case 0x10: // 4-byte signed integer
if (avail >= 5) {
v = dv.getInt32(1, false);
consume(5);
}
break;
case 0x11: // 4-byte string
if (avail >= 5) {
len = dv.getUint32(1, false);
if (avail >= 5 + len) {
v = new Uint8Array(len);
v.set(buf.subarray(5, 5 + len));
v = this.bufferToDebugString(v);
consume(5 + len);
}
}
break;
case 0x12: // 2-byte string
if (avail >= 3) {
len = dv.getUint16(1, false);
if (avail >= 3 + len) {
v = new Uint8Array(len);
v.set(buf.subarray(3, 3 + len));
v = this.bufferToDebugString(v);
consume(3 + len);
}
}
break;
case 0x13: // 4-byte buffer
if (avail >= 5) {
len = dv.getUint32(1, false);
if (avail >= 5 + len) {
v = new Uint8Array(len);
v.set(buf.subarray(5, 5 + len));
v = { type: 'buffer', data: Duktape.enc('hex', plainOf(v)) };
consume(5 + len);
}
}
break;
case 0x14: // 2-byte buffer
if (avail >= 3) {
len = dv.getUint16(1, false);
if (avail >= 3 + len) {
v = new Uint8Array(len);
v.set(buf.subarray(3, 3 + len));
v = { type: 'buffer', data: Duktape.enc('hex', plainOf(v)) };
consume(3 + len);
}
}
break;
case 0x15: // unused/none
v = { type: 'unused' };
consume(1);
break;
case 0x16: // undefined
v = { type: 'undefined' };
gotValue = true; // indicate 'v' is actually set
consume(1);
break;
case 0x17: // null
v = null;
gotValue = true; // indicate 'v' is actually set
consume(1);
break;
case 0x18: // true
v = true;
consume(1);
break;
case 0x19: // false
v = false;
consume(1);
break;
case 0x1a: // number (IEEE double), big endian
if (avail >= 9) {
tmp = new Uint8Array(8);
tmp.set(buf.subarray(1, 9));
v = { type: 'number', data: Duktape.enc('hex', plainOf(tmp)) };
if (readableNumberValue) {
// The value key should not be used programmatically,
// it is just there to make the dumps more readable.
v.value = new DataView(tmp.buffer).getFloat64(0, false);
}
consume(9);
}
break;
case 0x1b: // object
if (avail >= 3) {
len = buf[2];
if (avail >= 3 + len) {
v = new Uint8Array(len);
v.set(buf.subarray(3, 3 + len));
v = { type: 'object', 'class': buf[1], pointer: Duktape.enc('hex', plainOf(v)) };
consume(3 + len);
}
}
break;
case 0x1c: // pointer
if (avail >= 2) {
len = buf[1];
if (avail >= 2 + len) {
v = new Uint8Array(len);
v.set(buf.subarray(2, 2 + len));
v = { type: 'pointer', pointer: Duktape.enc('hex', plainOf(v)) };
consume(2 + len);
}
}
break;
case 0x1d: // lightfunc
if (avail >= 4) {
len = buf[3];
if (avail >= 4 + len) {
v = new Uint8Array(len);
v.set(buf.subarray(4, 4 + len));
v = { type: 'lightfunc', flags: dv.getUint16(1, false), pointer: Duktape.enc('hex', plainOf(v)) };
consume(4 + len);
}
}
break;
case 0x1e: // heapptr
if (avail >= 2) {
len = buf[1];
if (avail >= 2 + len) {
v = new Uint8Array(len);
v.set(buf.subarray(2, 2 + len));
v = { type: 'heapptr', pointer: Duktape.enc('hex', plainOf(v)) };
consume(2 + len);
}
}
break;
default:
throw new Error('failed parse initial byte: ' + buf[0]);
}
}
if (typeof v !== 'undefined' || gotValue) {
return { dvalue: v };
}
};
/*
* Main
*/
function main() {
var argv = typeof uv.argv === 'function' ? uv.argv() : [];
var i;
for (i = 2; i < argv.length; i++) { // skip dukluv and script name
if (argv[i] == '--help') {
print('Usage: dukluv ' + argv[1] + ' [option]+');
print('');
print(' --server-host HOST JSON proxy server listen address');
print(' --server-port PORT JSON proxy server listen port');
print(' --target-host HOST Debug target address');
print(' --target-port PORT Debug target port');
print(' --metadata FILE Proxy metadata file (usually named duk_debug_meta.json)');
print(' --log-level LEVEL Set log level, default is 2; 0=trace, 1=debug, 2=info, 3=warn, etc');
print(' --single Run a single proxy connection and exit (default: persist for multiple connections)');
print(' --readable-numbers Add a non-programmatic "value" key for IEEE doubles help readability');
print(' --lenient Ignore (with warning) invalid JSON without dropping connection');
print(' --jx-parse Parse JSON proxy input with JX, useful when testing manually');
print('');
return; // don't register any sockets/timers etc to exit
} else if (argv[i] == '--single') {
singleConnection = true;
continue;
} else if (argv[i] == '--readable-numbers') {
readableNumberValue = true;
continue;
} else if (argv[i] == '--lenient') {
lenientJsonParse = true;
continue;
} else if (argv[i] == '--jx-parse') {
jxParse = true;
continue;
}
if (i >= argv.length - 1) {
throw new Error('missing option value for ' + argv[i]);
}
if (argv[i] == '--server-host') {
serverHost = argv[i + 1];
i++;
} else if (argv[i] == '--server-port') {
serverPort = Math.floor(+argv[i + 1]);
i++;
} else if (argv[i] == '--target-host') {