From 17913da65f1f6cb465b90941c68f220fb52787ff Mon Sep 17 00:00:00 2001 From: Ylian Saint-Hilaire Date: Thu, 18 Nov 2021 12:00:45 -0800 Subject: [PATCH] Made it easier to debug log WebRTC/WebSocket --- agents/meshcore.js | 2 +- public/scripts/agent-redir-rtc-0.1.0.js | 4 ++-- public/scripts/agent-redir-ws-0.1.1.js | 19 +++++++++++++++++-- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/agents/meshcore.js b/agents/meshcore.js index 2ceeb1cf..0dfdf7cc 100644 --- a/agents/meshcore.js +++ b/agents/meshcore.js @@ -2968,7 +2968,7 @@ function onTunnelControlData(data, ws) { ws.webrtc.on('disconnected', function () { /*sendConsoleText('Tunnel #' + this.websocket.tunnel.index + ' WebRTC disconnected');*/ }); ws.webrtc.on('dataChannel', function (rtcchannel) { //sendConsoleText('WebRTC Datachannel open, protocol: ' + this.websocket.httprequest.protocol); - rtcchannel.maxFragmentSize = 32768; + //rtcchannel.maxFragmentSize = 32768; rtcchannel.xrtc = this; rtcchannel.websocket = this.websocket; this.rtcchannel = rtcchannel; diff --git a/public/scripts/agent-redir-rtc-0.1.0.js b/public/scripts/agent-redir-rtc-0.1.0.js index d274869b..3092bb41 100644 --- a/public/scripts/agent-redir-rtc-0.1.0.js +++ b/public/scripts/agent-redir-rtc-0.1.0.js @@ -55,7 +55,7 @@ var CreateKvmDataChannel = function (webchannel, module, keepalive) { fileReader.readAsArrayBuffer(e.data); } else { // IE10, readAsBinaryString does not exist, use an alternative. - var binary = "", bytes = new Uint8Array(e.data), length = bytes.byteLength; + var binary = '', bytes = new Uint8Array(e.data), length = bytes.byteLength; for (var i = 0; i < length; i++) { binary += String.fromCharCode(bytes[i]); } obj.xxOnSocketData(binary); } @@ -99,7 +99,7 @@ var CreateKvmDataChannel = function (webchannel, module, keepalive) { if (!data) return; if (typeof data === 'object') { // This is an ArrayBuffer, convert it to a string array (used in IE) - var binary = "", bytes = new Uint8Array(data), length = bytes.byteLength; + var binary = '', bytes = new Uint8Array(data), length = bytes.byteLength; for (var i = 0; i < length; i++) { binary += String.fromCharCode(bytes[i]); } data = binary; } diff --git a/public/scripts/agent-redir-ws-0.1.1.js b/public/scripts/agent-redir-ws-0.1.1.js index 387d110b..04d3bdd6 100644 --- a/public/scripts/agent-redir-ws-0.1.1.js +++ b/public/scripts/agent-redir-ws-0.1.1.js @@ -43,6 +43,18 @@ var CreateAgentRedirect = function (meshserver, module, serverPublicNamePort, au // Private method //obj.debug = function (msg) { console.log(msg); } + // Display websocket or webrtc data to the console + function logData(e, name) { + if (typeof e.data == 'object') { + var view = new Uint8Array(e.data), cmd = (view[0] << 8) + view[1], cmdsize = (view[2] << 8) + view[3]; + console.log(name + ' binary data', cmd, cmdsize, e.data.byteLength, buf2hex(e.data).substring(0, 24)); + } else if (typeof e.data == 'string') { + console.log(name + ' string data', e.data.length, e.data); + } else { + console.log(name + ' unknown data', e.data); + } + } + obj.Start = function (nodeid) { 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; //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 obj.socket.binaryType = 'arraybuffer'; obj.socket.onopen = obj.xxOnSocketConnected; obj.socket.onmessage = obj.xxOnMessage; - //obj.socket.onmessage = function (e) { console.log('Websocket data', e.data); obj.xxOnMessage(e); } + //obj.socket.onmessage = function (e) { logData(e, 'WebSocket'); obj.xxOnMessage(e); } obj.socket.onerror = function (e) { /* console.error(e); */ } obj.socket.onclose = obj.xxOnSocketClosed; obj.xxStateChange(1); @@ -140,7 +152,7 @@ var CreateAgentRedirect = function (meshserver, module, serverPublicNamePort, au obj.webchannel = obj.webrtc.createDataChannel('DataChannel', {}); // { ordered: false, maxRetransmits: 2 } obj.webchannel.binaryType = 'arraybuffer'; obj.webchannel.onmessage = obj.xxOnMessage; - //obj.webchannel.onmessage = function (e) { console.log('WebRTC data', e.data); obj.xxOnMessage(e); } + //obj.webchannel.onmessage = function (e) { logData(e, 'WebRTC'); obj.xxOnMessage(e); } obj.webchannel.onopen = function () { obj.webRtcActive = true; performWebRtcSwitch(); }; obj.webchannel.onclose = function (event) { if (obj.webRtcActive) { obj.Stop(); } } obj.webrtc.onicecandidate = function (e) { @@ -288,5 +300,8 @@ var CreateAgentRedirect = function (meshserver, module, serverPublicNamePort, au obj.xxStateChange(0); } + // Buffer is an ArrayBuffer + function buf2hex(buffer) { return [...new Uint8Array(buffer)].map(x => x.toString(16).padStart(2, '0')).join(''); } + return obj; }