rename a few things

This commit is contained in:
AJ ONeal 2018-05-31 05:24:43 +00:00
parent e1ee55da02
commit 9c57bac510
3 changed files with 43 additions and 42 deletions

View File

@ -1,21 +1,21 @@
'use strict'; 'use strict';
var packer = require('tunnel-packer'); var Packer = require('proxy-packer');
var sni = require('sni'); var sni = require('sni');
function pipeWs(servername, service, conn, remote) { function pipeWs(servername, service, conn, remote) {
console.log('[pipeWs] servername:', servername, 'service:', service); console.log('[pipeWs] servername:', servername, 'service:', service);
var browserAddr = packer.socketToAddr(conn); var browserAddr = Packer.socketToAddr(conn);
browserAddr.service = service; browserAddr.service = service;
var cid = packer.addrToId(browserAddr); var cid = Packer.addrToId(browserAddr);
conn.tunnelCid = cid; conn.tunnelCid = cid;
console.log('[pipeWs] browser is', cid, 'home-cloud is', packer.socketToId(remote.upgradeReq.socket)); console.log('[pipeWs] browser is', cid, 'home-cloud is', Packer.socketToId(remote.upgradeReq.socket));
function sendWs(data, serviceOverride) { function sendWs(data, serviceOverride) {
if (remote.ws && (!conn.tunnelClosing || serviceOverride)) { if (remote.ws && (!conn.tunnelClosing || serviceOverride)) {
try { try {
remote.ws.send(packer.pack(browserAddr, data, serviceOverride), { binary: true }); remote.ws.send(Packer.pack(browserAddr, data, serviceOverride), { binary: true });
// If we can't send data over the websocket as fast as this connection can send it to us // If we can't send data over the websocket as fast as this connection can send it to us
// (or there are a lot of connections trying to send over the same websocket) then we // (or there are a lot of connections trying to send over the same websocket) then we
// need to pause the connection for a little. We pause all connections if any are paused // need to pause the connection for a little. We pause all connections if any are paused

View File

@ -43,11 +43,11 @@
"greenlock": "^2.2.4", "greenlock": "^2.2.4",
"js-yaml": "^3.11.0", "js-yaml": "^3.11.0",
"jsonwebtoken": "^8.2.1", "jsonwebtoken": "^8.2.1",
"proxy-packer": "^1.4.3",
"recase": "^1.0.4", "recase": "^1.0.4",
"redirect-https": "^1.1.5", "redirect-https": "^1.1.5",
"serve-static": "^1.13.2", "serve-static": "^1.13.2",
"sni": "^1.0.0", "sni": "^1.0.0",
"tunnel-packer": "^1.4.0",
"ws": "^5.1.1" "ws": "^5.1.1"
} }
} }

View File

@ -3,7 +3,7 @@
var url = require('url'); var url = require('url');
var PromiseA = require('bluebird'); var PromiseA = require('bluebird');
var jwt = require('jsonwebtoken'); var jwt = require('jsonwebtoken');
var packer = require('tunnel-packer'); var Packer = require('proxy-packer');
function timeoutPromise(duration) { function timeoutPromise(duration) {
return new PromiseA(function (resolve) { return new PromiseA(function (resolve) {
@ -14,17 +14,17 @@ function timeoutPromise(duration) {
var Devices = require('./lib/device-tracker'); var Devices = require('./lib/device-tracker');
module.exports.store = { Devices: Devices }; module.exports.store = { Devices: Devices };
module.exports.create = function (copts) { module.exports.create = function (state) {
copts.deviceLists = {}; state.deviceLists = {};
//var deviceLists = {}; //var deviceLists = {};
var activityTimeout = copts.activityTimeout || 2*60*1000; var activityTimeout = state.activityTimeout || 2*60*1000;
var pongTimeout = copts.pongTimeout || 10*1000; var pongTimeout = state.pongTimeout || 10*1000;
copts.Devices = Devices; state.Devices = Devices;
var onTcpConnection = require('./lib/unwrap-tls').createTcpConnectionHandler(copts); var onTcpConnection = require('./lib/unwrap-tls').createTcpConnectionHandler(state);
function onWsConnection(ws, upgradeReq) { function onWsConnection(ws, upgradeReq) {
console.log(ws); console.log(ws);
var socketId = packer.socketToId(upgradeReq.socket); var socketId = Packer.socketToId(upgradeReq.socket);
var remotes = {}; var remotes = {};
function logName() { function logName() {
@ -35,7 +35,7 @@ module.exports.create = function (copts) {
return result || socketId; return result || socketId;
} }
function sendTunnelMsg(addr, data, service) { function sendTunnelMsg(addr, data, service) {
ws.send(packer.pack(addr, data, service), {binary: true}); ws.send(Packer.pack(addr, data, service), {binary: true});
} }
function getBrowserConn(cid) { function getBrowserConn(cid) {
@ -103,7 +103,7 @@ module.exports.create = function (copts) {
var token; var token;
try { try {
token = jwt.verify(jwtoken, copts.secret); token = jwt.verify(jwtoken, state.secret);
} catch (e) { } catch (e) {
token = null; token = null;
} }
@ -154,7 +154,7 @@ module.exports.create = function (copts) {
token.domains.forEach(function (domainname) { token.domains.forEach(function (domainname) {
console.log('domainname', domainname); console.log('domainname', domainname);
Devices.add(copts.deviceLists, domainname, token); Devices.add(state.deviceLists, domainname, token);
}); });
remotes[jwtoken] = token; remotes[jwtoken] = token;
console.log("added token '" + token.deviceId + "' to websocket", socketId); console.log("added token '" + token.deviceId + "' to websocket", socketId);
@ -170,7 +170,7 @@ module.exports.create = function (copts) {
// Prevent any more browser connections being sent to this remote, and any existing // Prevent any more browser connections being sent to this remote, and any existing
// connections from trying to send more data across the connection. // connections from trying to send more data across the connection.
remote.domains.forEach(function (domainname) { remote.domains.forEach(function (domainname) {
Devices.remove(copts.deviceLists, domainname, remote); Devices.remove(state.deviceLists, domainname, remote);
}); });
remote.ws = null; remote.ws = null;
remote.upgradeReq = null; remote.upgradeReq = null;
@ -220,13 +220,13 @@ module.exports.create = function (copts) {
}; };
var packerHandlers = { var packerHandlers = {
oncontrol: function (opts) { oncontrol: function (tun) {
var cmd, err; var cmd, err;
try { try {
cmd = JSON.parse(opts.data.toString()); cmd = JSON.parse(tun.data.toString());
} catch (err) {} } catch (err) {}
if (!Array.isArray(cmd) || typeof cmd[0] !== 'number') { if (!Array.isArray(cmd) || typeof cmd[0] !== 'number') {
var msg = 'received bad command "' + opts.data.toString() + '"'; var msg = 'received bad command "' + tun.data.toString() + '"';
console.warn(msg, 'from websocket', socketId); console.warn(msg, 'from websocket', socketId);
sendTunnelMsg(null, [0, {message: msg, code: 'E_BAD_COMMAND'}], 'control'); sendTunnelMsg(null, [0, {message: msg, code: 'E_BAD_COMMAND'}], 'control');
return; return;
@ -260,69 +260,70 @@ module.exports.create = function (copts) {
sendTunnelMsg(null, [-cmd[0], err], 'control'); sendTunnelMsg(null, [-cmd[0], err], 'control');
} }
, onmessage: function (opts) { , onmessage: function (tun) {
var cid = packer.addrToId(opts); var cid = packer.addrToId(tun);
console.log("remote '" + logName() + "' has data for '" + cid + "'", opts.data.byteLength); console.log("remote '" + logName() + "' has data for '" + cid + "'", tun.data.byteLength);
var browserConn = getBrowserConn(cid); var browserConn = getBrowserConn(cid);
if (!browserConn) { if (!browserConn) {
sendTunnelMsg(opts, {message: 'no matching connection', code: 'E_NO_CONN'}, 'error'); sendTunnelMsg(tun, {message: 'no matching connection', code: 'E_NO_CONN'}, 'error');
return; return;
} }
browserConn.write(opts.data); browserConn.write(tun.data);
// tunnelRead is how many bytes we've read from the tunnel, and written to the browser. // tunnelRead is how many bytes we've read from the tunnel, and written to the browser.
browserConn.tunnelRead = (browserConn.tunnelRead || 0) + opts.data.byteLength; browserConn.tunnelRead = (browserConn.tunnelRead || 0) + tun.data.byteLength;
// If we have more than 1MB buffered data we need to tell the other side to slow down. // If we have more than 1MB buffered data we need to tell the other side to slow down.
// Once we've finished sending what we have we can tell the other side to keep going. // Once we've finished sending what we have we can tell the other side to keep going.
// If we've already sent the 'pause' message though don't send it again, because we're // If we've already sent the 'pause' message though don't send it again, because we're
// probably just dealing with data queued before our message got to them. // probably just dealing with data queued before our message got to them.
if (!browserConn.remotePaused && browserConn.bufferSize > 1024*1024) { if (!browserConn.remotePaused && browserConn.bufferSize > 1024*1024) {
sendTunnelMsg(opts, browserConn.tunnelRead, 'pause'); sendTunnelMsg(tun, browserConn.tunnelRead, 'pause');
browserConn.remotePaused = true; browserConn.remotePaused = true;
browserConn.once('drain', function () { browserConn.once('drain', function () {
sendTunnelMsg(opts, browserConn.tunnelRead, 'resume'); sendTunnelMsg(tun, browserConn.tunnelRead, 'resume');
browserConn.remotePaused = false; browserConn.remotePaused = false;
}); });
} }
} }
, onpause: function (opts) { , onpause: function (tun) {
var cid = packer.addrToId(opts); var cid = Packer.addrToId(tun);
console.log('[TunnelPause]', cid); console.log('[TunnelPause]', cid);
var browserConn = getBrowserConn(cid); var browserConn = getBrowserConn(cid);
if (browserConn) { if (browserConn) {
browserConn.manualPause = true; browserConn.manualPause = true;
browserConn.pause(); browserConn.pause();
} else { } else {
sendTunnelMsg(opts, {message: 'no matching connection', code: 'E_NO_CONN'}, 'error'); sendTunnelMsg(tun, {message: 'no matching connection', code: 'E_NO_CONN'}, 'error');
} }
} }
, onresume: function (opts) {
var cid = packer.addrToId(opts); , onresume: function (tun) {
var cid = Packer.addrToId(tun);
console.log('[TunnelResume]', cid); console.log('[TunnelResume]', cid);
var browserConn = getBrowserConn(cid); var browserConn = getBrowserConn(cid);
if (browserConn) { if (browserConn) {
browserConn.manualPause = false; browserConn.manualPause = false;
browserConn.resume(); browserConn.resume();
} else { } else {
sendTunnelMsg(opts, {message: 'no matching connection', code: 'E_NO_CONN'}, 'error'); sendTunnelMsg(tun, {message: 'no matching connection', code: 'E_NO_CONN'}, 'error');
} }
} }
, onend: function (opts) { , onend: function (tun) {
var cid = packer.addrToId(opts); var cid = Packer.addrToId(tun);
console.log('[TunnelEnd]', cid); console.log('[TunnelEnd]', cid);
closeBrowserConn(cid); closeBrowserConn(cid);
} }
, onerror: function (opts) { , onerror: function (tun) {
var cid = packer.addrToId(opts); var cid = Packer.addrToId(tun);
console.log('[TunnelError]', cid, opts.message); console.log('[TunnelError]', cid, tun.message);
closeBrowserConn(cid); closeBrowserConn(cid);
} }
}; };
var unpacker = packer.create(packerHandlers); var unpacker = Packer.create(packerHandlers);
var lastActivity = Date.now(); var lastActivity = Date.now();
var timeoutId; var timeoutId;
@ -390,6 +391,6 @@ module.exports.create = function (copts) {
return { return {
tcp: onTcpConnection tcp: onTcpConnection
, ws: onWsConnection , ws: onWsConnection
, isClientDomain: Devices.exist.bind(null, copts.deviceLists) , isClientDomain: Devices.exist.bind(null, state.deviceLists)
}; };
}; };