Merge branch 'master' into commercial

This commit is contained in:
AJ ONeal 2018-06-07 07:47:57 +00:00
commit d8e54c179d
1 changed files with 65 additions and 39 deletions

View File

@ -186,6 +186,9 @@ module.exports.create = function (state) {
function addToken(jwtoken) { function addToken(jwtoken) {
function onAuth(token) { function onAuth(token) {
if ('string' !== typeof jwtoken) {
jwtoken = JSON.stringify(jwtoken);
}
var err; var err;
if (!token) { if (!token) {
err = new Error("invalid access token"); err = new Error("invalid access token");
@ -193,6 +196,20 @@ module.exports.create = function (state) {
return state.Promise.reject(err); return state.Promise.reject(err);
} }
if (token.jwt && jwtoken !== token.jwt) {
// Access Token
sendTunnelMsg(
null
, [ 3
, 'access_token'
, { jwt: token.jwt }
]
, 'control'
);
// these aren't needed internally once they're sent
token.jwt = null;
}
if (!Array.isArray(token.domains)) { if (!Array.isArray(token.domains)) {
if ('string' === typeof token.name) { if ('string' === typeof token.name) {
token.domains = [ token.name ]; token.domains = [ token.name ];
@ -241,49 +258,58 @@ module.exports.create = function (state) {
Devices.add(state.deviceLists, domainname, token); Devices.add(state.deviceLists, domainname, token);
}); });
function onDynTcpReady() {
var serviceport = this.address().port;
console.info('[DynTcpConn] Port', serviceport, 'now open for', token.deviceId);
//token.dynamicPorts.push(serviceport);
Devices.add(state.deviceLists, serviceport, token);
//var hri = require('human-readable-ids').hri;
//var hrname = hri.random() + '.telebit.cloud';
//token.dynamicNames.push(hrname);
// TODO restrict to authenticated device
// TODO pull servername from config
// TODO remove hrname on disconnect
//Devices.add(state.deviceLists, hrname, token);
sendTunnelMsg(
null
, [ 2
, 'grant'
, [ ['ssh+https', token.domains[0], 443 ]
, ['ssh', 'ssh.telebit.cloud', serviceport ]
, ['tcp', 'tcp.telebit.cloud', serviceport]
, ['https', token.domains[0] ]
]
]
, 'control'
);
}
console.log('[DEBUG] got to firstToken check'); console.log('[DEBUG] got to firstToken check');
if (!token.ports) {
token.ports = [];
}
if (!firstToken || firstToken === jwtoken) { if (!firstToken || firstToken === jwtoken) {
firstToken = jwtoken; if (!token.ports.length) {
token.dynamicPorts = []; token.ports.push( 0 );
token.dynamicNames = [];
function onDynTcpReady() {
var serviceport = this.address().port;
console.info('[DynTcpConn] Port', serviceport, 'now open for', token.deviceId);
token.dynamicPorts.push(serviceport);
Devices.add(state.deviceLists, serviceport, token);
var hri = require('human-readable-ids').hri;
var hrname = hri.random() + '.telebit.cloud';
token.dynamicNames.push(hrname);
// TODO restrict to authenticated device
// TODO pull servername from config
// TODO remove hrname on disconnect
Devices.add(state.deviceLists, hrname, token);
sendTunnelMsg(
null
, [ 2
, 'grant'
, [ ['ssh+https', hrname, 443 ]
, ['ssh', 'ssh.telebit.cloud', serviceport ]
, ['tcp', 'tcp.telebit.cloud', serviceport]
, ['https', hrname ]
]
]
, 'control'
);
} }
firstToken = token.jwt || jwtoken;
}
try { //token.dynamicPorts = [];
token.server = require('net').createServer(onDynTcpConn).listen(0, onDynTcpReady); //token.dynamicNames = [];
token.server.on('error', function (e) {
console.error("Server Error assigning a dynamic port to a new connection:", e); var onePortForNow = parseInt(token.ports[0], 10) || 0;
}); // TODO try again with random port
} catch(e) { try {
// what a wonderful problem it will be the day that this bug needs to be fixed token.server = require('net').createServer(onDynTcpConn).listen(onePortForNow, onDynTcpReady);
// (i.e. there are enough users to run out of ports) token.server.on('error', function (e) {
console.error("Error assigning a dynamic port to a new connection:", e); console.error("Server Error assigning a dynamic port to a new connection:", e);
} });
} catch(e) {
// what a wonderful problem it will be the day that this bug needs to be fixed
// (i.e. there are enough users to run out of ports)
console.error("Error assigning a dynamic port to a new connection:", e);
} }
remotes[jwtoken] = token; remotes[jwtoken] = token;
@ -310,7 +336,7 @@ module.exports.create = function (state) {
remote.domains.forEach(function (domainname) { remote.domains.forEach(function (domainname) {
Devices.remove(state.deviceLists, domainname, remote); Devices.remove(state.deviceLists, domainname, remote);
}); });
remote.dynamicPorts.forEach(function (portnumber) { remote.ports.forEach(function (portnumber) {
Devices.remove(state.deviceLists, portnumber, remote); Devices.remove(state.deviceLists, portnumber, remote);
}); });
remote.ws = null; remote.ws = null;