From bd8d32d8ec8670523065dba93f42fbc4162a0dc8 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Wed, 8 Aug 2018 01:11:29 -0600 Subject: [PATCH] WIP v0.20.x: add onconnection handler --- lib/remote.js | 49 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 15 deletions(-) diff --git a/lib/remote.js b/lib/remote.js index b4fdaa3..940a047 100644 --- a/lib/remote.js +++ b/lib/remote.js @@ -243,6 +243,23 @@ function _connect(state) { var connCallback; + function hyperPeek(tun) { + var m; + var str; + if (tun.data) { + if ('http' === tun.service) { + str = tun.data.toString(); + m = str.match(/(?:^|[\r\n])Host: ([^\r\n]+)[\r\n]*/im); + tun._name = tun._hostname = (m && m[1].toLowerCase() || '').split(':')[0]; + } + else if ('https' === tun.service || 'tls' === tun.service) { + tun._name = tun._servername = sni(tun.data); + } else { + tun._name = ''; + } + } + } + var packerHandlers = { oncontrol: function (opts) { var cmd, err; @@ -305,24 +322,14 @@ function _connect(state) { wsHandlers.sendMessage(Packer.packHeader(null, [-cmd[0], err], 'control', packBody)); } - , onmessage: function (tun) { + , onconnection: function (tun) { var cid = tun._id = Packer.addrToId(tun); - var str; - var m; - if ('http' === tun.service) { - str = tun.data.toString(); - m = str.match(/(?:^|[\r\n])Host: ([^\r\n]+)[\r\n]*/im); - tun._name = tun._hostname = (m && m[1].toLowerCase() || '').split(':')[0]; - } - else if ('https' === tun.service || 'tls' === tun.service) { - tun._name = tun._servername = sni(tun.data); - } else { - tun._name = ''; - } - - if (clientHandlers.write(cid, tun)) { return; } + // this data should have been gathered already as part of the proxy protocol + // but if it's available again here we can double check + hyperPeek(tun); + // TODO use readable streams instead wstunneler.pause(); require(state.sortingHat).assign(state, tun, function (err, conn) { if (err) { @@ -336,6 +343,18 @@ function _connect(state) { }); } + , onmessage: function (tun) { + var cid = tun._id = Packer.addrToId(tun); + var handled; + + hyperPeek(tun); + + handled = clientHandlers.write(cid, tun); + + // quasi backwards compat + if (!handled) { clientHandlers.onconnection(tun); } + } + , onpause: function (opts) { var cid = Packer.addrToId(opts); if (localclients[cid]) {