From 78407f2a3e3b761aa7762161e6045dacf6b14e3c Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Wed, 8 Aug 2018 00:51:16 -0600 Subject: [PATCH] WIP v0.20.x: switch to proxy-packer v2.x and comment readable --- lib/remote.js | 34 ++++++++++++++++++++++------------ lib/updater.js | 1 + package.json | 4 ++-- 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/lib/remote.js b/lib/remote.js index bb6d582..b4fdaa3 100644 --- a/lib/remote.js +++ b/lib/remote.js @@ -56,6 +56,7 @@ function _connect(state) { conn.tunnelWritten = 0; conn.on('data', function onLocalData(chunk) { + //var chunk = conn.read(); if (conn.tunnelClosing) { console.warn("[onLocalData] received data for '"+cid+"' over socket after connection was ended"); return; @@ -67,8 +68,10 @@ function _connect(state) { // down the data we are getting to send over. We also want to pause all active connections // if any connections are paused to make things more fair so one connection doesn't get // stuff waiting for all other connections to finish because it tried writing near the border. - var bufSize = wsHandlers.sendMessage(Packer.pack(tun, chunk)); - if (pausedClients.length || bufSize > 1024*1024) { + var bufSize = wsHandlers.sendMessage(Packer.packHeader(tun, chunk)); + // Sending 2 messages instead of copying the buffer + var bufSize2 = wsHandlers.sendMessage(chunk); + if (pausedClients.length || (bufSize + bufSize2) > 1024*1024) { // console.log('[onLocalData] paused connection', cid, 'to allow websocket to catch up'); conn.pause(); pausedClients.push(conn); @@ -80,14 +83,15 @@ function _connect(state) { console.info("[onLocalEnd] connection '" + cid + "' ended, will probably close soon"); conn.tunnelClosing = true; if (!sentEnd) { - wsHandlers.sendMessage(Packer.pack(tun, null, 'end')); + wsHandlers.sendMessage(Packer.packHeader(tun, null, 'end')); sentEnd = true; } }); conn.on('error', function onLocalError(err) { console.info("[onLocalError] connection '" + cid + "' errored:", err); if (!sentEnd) { - wsHandlers.sendMessage(Packer.pack(tun, {message: err.message, code: err.code}, 'error')); + var packBody = true; + wsHandlers.sendMessage(Packer.packHeader(tun, {message: err.message, code: err.code}, 'error', packBody)); sentEnd = true; } }); @@ -95,7 +99,7 @@ function _connect(state) { delete localclients[cid]; console.log('[onLocalClose] closed "' + cid + '" read:'+conn.tunnelRead+', wrote:'+conn.tunnelWritten+' (' + clientHandlers.count() + ' clients)'); if (!sentEnd) { - wsHandlers.sendMessage(Packer.pack(tun, null, hadErr && 'error' || 'end')); + wsHandlers.sendMessage(Packer.packHeader(tun, null, hadErr && 'error' || 'end')); sentEnd = true; } }); @@ -119,11 +123,13 @@ function _connect(state) { conn.tunnelRead += opts.data.byteLength; if (!conn.remotePaused && conn.bufferSize > 1024*1024) { - wsHandlers.sendMessage(Packer.pack(opts, conn.tunnelRead, 'pause')); + var packBody = true; + wsHandlers.sendMessage(Packer.packHeader(opts, conn.tunnelRead, 'pause', packBody)); conn.remotePaused = true; conn.once('drain', function () { - wsHandlers.sendMessage(Packer.pack(opts, conn.tunnelRead, 'resume')); + var packBody = true; + wsHandlers.sendMessage(Packer.packHeader(opts, conn.tunnelRead, 'resume', packBody)); conn.remotePaused = false; }); } @@ -188,7 +194,8 @@ function _connect(state) { var cmd = [id, name].concat(Array.prototype.slice.call(arguments, 1)); if (state.debug) { console.log('[DEBUG] command sending', cmd); } - wsHandlers.sendMessage(Packer.pack(null, cmd, 'control')); + var packBody = true; + wsHandlers.sendMessage(Packer.packHeader(null, cmd, 'control', packBody)); setTimeout(function () { if (pendingCommands[id]) { console.warn('command', name, id, 'timed out'); @@ -294,7 +301,8 @@ function _connect(state) { err = { message: 'unknown command "'+cmd[1]+'"', code: 'E_UNKNOWN_COMMAND' }; } - wsHandlers.sendMessage(Packer.pack(null, [-cmd[0], err], 'control')); + var packBody = true; + wsHandlers.sendMessage(Packer.packHeader(null, [-cmd[0], err], 'control', packBody)); } , onmessage: function (tun) { @@ -338,7 +346,8 @@ function _connect(state) { console.log('[TunnelPause] remote tried pausing finished connection', cid); // Often we have enough latency that we've finished sending before we're told to pause, so // don't worry about sending back errors, since we won't be sending data over anyway. - // wsHandlers.sendMessage(Packer.pack(opts, {message: 'no matching connection', code: 'E_NO_CONN'}, 'error')); + // var packBody = true; + // wsHandlers.sendMessage(Packer.packHeader(opts, {message: 'no matching connection', code: 'E_NO_CONN'}, 'error', packBody)); } } , onresume: function (opts) { @@ -349,7 +358,8 @@ function _connect(state) { localclients[cid].resume(); } else { console.log('[TunnelResume] remote tried resuming finished connection', cid); - // wsHandlers.sendMessage(Packer.pack(opts, {message: 'no matching connection', code: 'E_NO_CONN'}, 'error')); + // var packBody = true; + // wsHandlers.sendMessage(Packer.packHeader(opts, {message: 'no matching connection', code: 'E_NO_CONN'}, 'error', packBody)); } } @@ -366,7 +376,7 @@ function _connect(state) { , _onConnectError: function (cid, opts, err) { console.info("[_onConnectError] opening '" + cid + "' failed because " + err.message); - wsHandlers.sendMessage(Packer.pack(opts, null, 'error')); + wsHandlers.sendMessage(Packer.packHeader(opts, null, 'error')); } }; diff --git a/lib/updater.js b/lib/updater.js index c43f025..0d44b8f 100644 --- a/lib/updater.js +++ b/lib/updater.js @@ -8,6 +8,7 @@ module.exports = function (pkg) { https.get(url, function (resp) { var str = ''; resp.on('data', function (chunk) { + //var chunk = conn.read(); str += chunk.toString('utf8'); }); resp.on('end', function () { diff --git a/package.json b/package.json index ef55e71..2a93cbf 100644 --- a/package.json +++ b/package.json @@ -59,7 +59,7 @@ "js-yaml": "^3.11.0", "jsonwebtoken": "^7.1.9", "mkdirp": "^0.5.1", - "proxy-packer": "^1.4.3", + "proxy-packer": "^2.0.0", "ps-list": "^5.0.0", "recase": "^1.0.4", "redirect-https": "^1.1.5", @@ -67,7 +67,7 @@ "serve-static": "^1.13.2", "sni": "^1.0.0", "socket-pair": "^1.0.3", - "ws": "^2.2.3" + "ws": "^2.3.1" }, "trulyOptionalDependencies": { "bluebird": "^3.5.1"