Made it easier to debug log WebRTC/WebSocket

Ylian Saint-Hilaire committed Nov 18, 2021 at 12:00 UTC 17913da65f1f6cb465b90941c68f220fb52787ff
3 files changed +20 -5
agents/meshcore.js
+1 -1
@@ -2968,7 +2968,7 @@ function onTunnelControlData(data, ws) {
2968 ws.webrtc.on('disconnected', function () { /*sendConsoleText('Tunnel #' + this.websocket.tunnel.index + ' WebRTC disconnected');*/ });
2969 ws.webrtc.on('dataChannel', function (rtcchannel) {
2970 //sendConsoleText('WebRTC Datachannel open, protocol: ' + this.websocket.httprequest.protocol);
2971 - rtcchannel.maxFragmentSize = 32768;
2971 + //rtcchannel.maxFragmentSize = 32768;
2972 rtcchannel.xrtc = this;
2973 rtcchannel.websocket = this.websocket;
2974 this.rtcchannel = rtcchannel;
public/scripts/agent-redir-rtc-0.1.0.js
+2 -2
@@ -55,7 +55,7 @@ var CreateKvmDataChannel = function (webchannel, module, keepalive) {
55 fileReader.readAsArrayBuffer(e.data);
56 } else {
57 // IE10, readAsBinaryString does not exist, use an alternative.
58 - var binary = "", bytes = new Uint8Array(e.data), length = bytes.byteLength;
58 + var binary = '', bytes = new Uint8Array(e.data), length = bytes.byteLength;
59 for (var i = 0; i < length; i++) { binary += String.fromCharCode(bytes[i]); }
60 obj.xxOnSocketData(binary);
61 }
@@ -99,7 +99,7 @@ var CreateKvmDataChannel = function (webchannel, module, keepalive) {
99 if (!data) return;
100 if (typeof data === 'object') {
101 // This is an ArrayBuffer, convert it to a string array (used in IE)
102 - var binary = "", bytes = new Uint8Array(data), length = bytes.byteLength;
102 + var binary = '', bytes = new Uint8Array(data), length = bytes.byteLength;
103 for (var i = 0; i < length; i++) { binary += String.fromCharCode(bytes[i]); }
104 data = binary;
105 }
public/scripts/agent-redir-ws-0.1.1.js
+17 -2
@@ -43,6 +43,18 @@ var CreateAgentRedirect = function (meshserver, module, serverPublicNamePort, au
43 // Private method
44 //obj.debug = function (msg) { console.log(msg); }
45
46 + // Display websocket or webrtc data to the console
47 + function logData(e, name) {
48 + if (typeof e.data == 'object') {
49 + var view = new Uint8Array(e.data), cmd = (view[0] << 8) + view[1], cmdsize = (view[2] << 8) + view[3];
50 + console.log(name + ' binary data', cmd, cmdsize, e.data.byteLength, buf2hex(e.data).substring(0, 24));
51 + } else if (typeof e.data == 'string') {
52 + console.log(name + ' string data', e.data.length, e.data);
53 + } else {
54 + console.log(name + ' unknown data', e.data);
55 + }
56 + }
57 +
58 obj.Start = function (nodeid) {
59 var url2, url = window.location.protocol.replace('http', 'ws') + '//' + window.location.host + window.location.pathname.substring(0, window.location.pathname.lastIndexOf('/')) + '/' + obj.urlname + '?browser=1&p=' + obj.protocol + (nodeid?('&nodeid=' + nodeid):'') + '&id=' + obj.tunnelid;
60 //if (serverPublicNamePort) { url2 = window.location.protocol.replace('http', 'ws') + '//' + serverPublicNamePort + '/meshrelay.ashx?id=' + obj.tunnelid; } else { url2 = url; }
@@ -54,7 +66,7 @@ var CreateAgentRedirect = function (meshserver, module, serverPublicNamePort, au
66 obj.socket.binaryType = 'arraybuffer';
67 obj.socket.onopen = obj.xxOnSocketConnected;
68 obj.socket.onmessage = obj.xxOnMessage;
57 - //obj.socket.onmessage = function (e) { console.log('Websocket data', e.data); obj.xxOnMessage(e); }
69 + //obj.socket.onmessage = function (e) { logData(e, 'WebSocket'); obj.xxOnMessage(e); }
70 obj.socket.onerror = function (e) { /* console.error(e); */ }
71 obj.socket.onclose = obj.xxOnSocketClosed;
72 obj.xxStateChange(1);
@@ -140,7 +152,7 @@ var CreateAgentRedirect = function (meshserver, module, serverPublicNamePort, au
152 obj.webchannel = obj.webrtc.createDataChannel('DataChannel', {}); // { ordered: false, maxRetransmits: 2 }
153 obj.webchannel.binaryType = 'arraybuffer';
154 obj.webchannel.onmessage = obj.xxOnMessage;
143 - //obj.webchannel.onmessage = function (e) { console.log('WebRTC data', e.data); obj.xxOnMessage(e); }
155 + //obj.webchannel.onmessage = function (e) { logData(e, 'WebRTC'); obj.xxOnMessage(e); }
156 obj.webchannel.onopen = function () { obj.webRtcActive = true; performWebRtcSwitch(); };
157 obj.webchannel.onclose = function (event) { if (obj.webRtcActive) { obj.Stop(); } }
158 obj.webrtc.onicecandidate = function (e) {
@@ -288,5 +300,8 @@ var CreateAgentRedirect = function (meshserver, module, serverPublicNamePort, au
300 obj.xxStateChange(0);
301 }
302
303 + // Buffer is an ArrayBuffer
304 + function buf2hex(buffer) { return [...new Uint8Array(buffer)].map(x => x.toString(16).padStart(2, '0')).join(''); }
305 +
306 return obj;
307 }