WIP v0.20.x: add onconnection handler

This commit is contained in:
AJ ONeal 2018-08-08 01:11:29 -06:00
parent 78407f2a3e
commit bd8d32d8ec
1 changed files with 34 additions and 15 deletions

View File

@ -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]) {