changed how browser connections are handled
This commit is contained in:
parent
02d195798f
commit
65df12ecb3
|
@ -46,6 +46,7 @@
|
||||||
},
|
},
|
||||||
"homepage": "https://github.com/Daplie/node-tunnel-server#readme",
|
"homepage": "https://github.com/Daplie/node-tunnel-server#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"bluebird": "^3.5.0",
|
||||||
"cluster-store": "^2.0.4",
|
"cluster-store": "^2.0.4",
|
||||||
"commander": "^2.9.0",
|
"commander": "^2.9.0",
|
||||||
"greenlock": "^2.1.12",
|
"greenlock": "^2.1.12",
|
||||||
|
|
165
wstunneld.js
165
wstunneld.js
|
@ -2,9 +2,16 @@
|
||||||
|
|
||||||
var sni = require('sni');
|
var sni = require('sni');
|
||||||
var url = require('url');
|
var url = require('url');
|
||||||
|
var PromiseA = require('bluebird');
|
||||||
var jwt = require('jsonwebtoken');
|
var jwt = require('jsonwebtoken');
|
||||||
var packer = require('tunnel-packer');
|
var packer = require('tunnel-packer');
|
||||||
|
|
||||||
|
function timeoutPromise(duration) {
|
||||||
|
return new PromiseA(function (resolve) {
|
||||||
|
setTimeout(resolve, duration);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
var Devices = {};
|
var Devices = {};
|
||||||
Devices.add = function (store, servername, newDevice) {
|
Devices.add = function (store, servername, newDevice) {
|
||||||
var devices = Devices.list(store, servername);
|
var devices = Devices.list(store, servername);
|
||||||
|
@ -115,44 +122,59 @@ module.exports.create = function (copts) {
|
||||||
// and we haven't implemented tls in the browser yet
|
// and we haven't implemented tls in the browser yet
|
||||||
// remote.decrypt = token.decrypt;
|
// remote.decrypt = token.decrypt;
|
||||||
|
|
||||||
|
function closeBrowserConn(cid) {
|
||||||
|
if (!remote.clients[cid]) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
PromiseA.resolve()
|
||||||
|
.then(function () {
|
||||||
|
remote.clients[cid].end();
|
||||||
|
return timeoutPromise(500);
|
||||||
|
})
|
||||||
|
.then(function () {
|
||||||
|
if (remote.clients[cid]) {
|
||||||
|
console.warn(cid, 'browser connection still present after calling `end`');
|
||||||
|
remote.clients[cid].destroy();
|
||||||
|
return timeoutPromise(500);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.then(function () {
|
||||||
|
if (remote.clients[cid]) {
|
||||||
|
console.error(cid, 'browser connection still present after calling `destroy`');
|
||||||
|
delete remote.clients[cid];
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(function (err) {
|
||||||
|
console.warn('failed to close browser connection', cid, err);
|
||||||
|
})
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
var handlers = {
|
var handlers = {
|
||||||
onmessage: function (opts) {
|
onmessage: function (opts) {
|
||||||
// opts.data
|
// opts.data
|
||||||
var cid = packer.addrToId(opts);
|
var cid = packer.addrToId(opts);
|
||||||
var cstream = remote.clients[cid];
|
var browserConn = remote.clients[cid];
|
||||||
|
|
||||||
console.log("remote '" + remote.servername + " : " + remote.id + "' has data for '" + cid + "'", opts.data.byteLength);
|
console.log("remote '" + remote.servername + " : " + remote.id + "' has data for '" + cid + "'", opts.data.byteLength);
|
||||||
|
|
||||||
if (!cstream) {
|
if (!browserConn) {
|
||||||
remote.ws.send(packer.pack(opts, null, 'error'));
|
remote.ws.send(packer.pack(opts, null, 'error'));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
cstream.browser.write(opts.data);
|
browserConn.write(opts.data);
|
||||||
}
|
}
|
||||||
, onend: function (opts) {
|
, onend: function (opts) {
|
||||||
var cid = packer.addrToId(opts);
|
var cid = packer.addrToId(opts);
|
||||||
console.log('[TunnelEnd]', cid);
|
console.log('[TunnelEnd]', cid);
|
||||||
handlers._onend(cid);
|
closeBrowserConn(cid);
|
||||||
}
|
}
|
||||||
, onerror: function (opts) {
|
, onerror: function (opts) {
|
||||||
var cid = packer.addrToId(opts);
|
var cid = packer.addrToId(opts);
|
||||||
console.log('[TunnelError]', cid);
|
console.log('[TunnelError]', cid);
|
||||||
handlers._onend(cid);
|
closeBrowserConn(cid);
|
||||||
}
|
|
||||||
, _onend: function (cid) {
|
|
||||||
var c = remote.clients[cid];
|
|
||||||
delete remote.clients[cid];
|
|
||||||
try {
|
|
||||||
c.browser.end();
|
|
||||||
} catch(e) {
|
|
||||||
// ignore
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
c.wrapped.end();
|
|
||||||
} catch(e) {
|
|
||||||
// ignore
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
remote.unpacker = packer.create(handlers);
|
remote.unpacker = packer.create(handlers);
|
||||||
|
@ -214,89 +236,52 @@ module.exports.create = function (copts) {
|
||||||
function hangup() {
|
function hangup() {
|
||||||
clearTimeout(timeoutId);
|
clearTimeout(timeoutId);
|
||||||
console.log('home cloud', remote.deviceId || remote.servername, 'connection closing');
|
console.log('home cloud', remote.deviceId || remote.servername, 'connection closing');
|
||||||
// the remote will handle closing its local connections
|
// Prevent any more browser connections being sent to this remote, and any existing
|
||||||
Object.keys(remote.clients).forEach(function (cid) {
|
// connections from trying to send more data across the connection.
|
||||||
try {
|
|
||||||
remote.clients[cid].browser.end();
|
|
||||||
} catch(e) {
|
|
||||||
// ignore
|
|
||||||
}
|
|
||||||
});
|
|
||||||
token.domains.forEach(function (domainname) {
|
token.domains.forEach(function (domainname) {
|
||||||
Devices.remove(deviceLists, domainname, remote);
|
Devices.remove(deviceLists, domainname, remote);
|
||||||
});
|
});
|
||||||
|
remote.ws = null;
|
||||||
|
|
||||||
|
// Close all of the existing browser connections associated with this websocket connection.
|
||||||
|
Object.keys(remote.clients).forEach(function (cid) {
|
||||||
|
closeBrowserConn(cid);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
ws.on('close', hangup);
|
ws.on('close', hangup);
|
||||||
ws.on('error', hangup);
|
ws.on('error', hangup);
|
||||||
}
|
}
|
||||||
|
|
||||||
function pipeWs(servername, service, browser, remote) {
|
function pipeWs(servername, service, browserConn, remote) {
|
||||||
console.log('pipeWs');
|
console.log('[pipeWs] servername:', servername, 'service:', service);
|
||||||
|
|
||||||
//var remote = deviceLists[servername];
|
var browserAddr = packer.socketToAddr(browserConn);
|
||||||
var ws = remote.ws;
|
browserAddr.service = service;
|
||||||
//var address = packer.socketToAddr(ws.upgradeReq.socket);
|
var cid = packer.addrToId(browserAddr);
|
||||||
var baddress = packer.socketToAddr(browser);
|
console.log('[pipeWs] browser is', cid, 'home-cloud is', packer.socketToId(remote.ws.upgradeReq.socket));
|
||||||
var cid = packer.addrToId(baddress);
|
|
||||||
console.log('servername:', servername);
|
function sendWs(data, serviceOverride) {
|
||||||
console.log('service:', service);
|
if (remote.ws) {
|
||||||
baddress.service = service;
|
try {
|
||||||
var wrapForRemote = packer.Transform.create({
|
remote.ws.send(packer.pack(browserAddr, data, serviceOverride), { binary: true });
|
||||||
id: cid
|
} catch (err) {
|
||||||
//, remoteId: remote.id
|
console.warn('[pipeWs] error sending websocket message', err);
|
||||||
, address: baddress
|
}
|
||||||
, servername: servername
|
}
|
||||||
, service: service
|
}
|
||||||
|
|
||||||
|
remote.clients[cid] = browserConn;
|
||||||
|
browserConn.on('data', function (chunk) {
|
||||||
|
console.log('[pipeWs] data from browser to tunneler', chunk.byteLength);
|
||||||
|
sendWs(chunk);
|
||||||
});
|
});
|
||||||
console.log('home-cloud is', packer.socketToId(remote.ws.upgradeReq.socket));
|
browserConn.on('error', function (err) {
|
||||||
console.log('browser is', cid);
|
console.warn('[pipeWs] browser connection error', err);
|
||||||
var bstream = remote.clients[cid] = {
|
|
||||||
wrapped: browser.pipe(wrapForRemote)
|
|
||||||
, browser: browser
|
|
||||||
, address: baddress
|
|
||||||
};
|
|
||||||
//var bstream = remote.clients[cid] = wrapForRemote.pipe(browser);
|
|
||||||
bstream.wrapped.on('data', function (pchunk) {
|
|
||||||
// var chunk = socket.read();
|
|
||||||
console.log('[bstream] data from browser to tunneler', pchunk.byteLength);
|
|
||||||
//console.log(JSON.stringify(pchunk.toString()));
|
|
||||||
try {
|
|
||||||
ws.send(pchunk, { binary: true });
|
|
||||||
} catch(e) {
|
|
||||||
try {
|
|
||||||
bstream.browser.end();
|
|
||||||
} catch(e) {
|
|
||||||
// ignore
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
bstream.wrapped.on('error', function (err) {
|
browserConn.on('close', function (hadErr) {
|
||||||
console.error('[error] bstream.wrapped.error');
|
console.log('[pipeWs] browser connection closing');
|
||||||
console.error(err);
|
sendWs(null, hadErr ? 'error': 'end');
|
||||||
try {
|
|
||||||
ws.send(packer.pack(baddress, null, 'error'), { binary: true });
|
|
||||||
} catch(e) {
|
|
||||||
// ignore
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
bstream.browser.end();
|
|
||||||
} catch(e) {
|
|
||||||
// ignore
|
|
||||||
}
|
|
||||||
delete remote.clients[cid];
|
|
||||||
});
|
|
||||||
bstream.wrapped.on('end', function () {
|
|
||||||
try {
|
|
||||||
ws.send(packer.pack(baddress, null, 'end'), { binary: true });
|
|
||||||
} catch(e) {
|
|
||||||
// ignore
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
bstream.browser.end();
|
|
||||||
} catch(e) {
|
|
||||||
// ignore
|
|
||||||
}
|
|
||||||
delete remote.clients[cid];
|
delete remote.clients[cid];
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue