This commit is contained in:
AJ ONeal 2018-07-27 22:30:58 -06:00
parent f2b16a5bcb
commit 568bcdcab2
4 changed files with 45 additions and 17 deletions

View File

@ -57,8 +57,8 @@ function help() {
console.info('\ttelebit http /module/path # load a node module to handle all https traffic');
console.info('');
console.info('\ttelebit http none example.com # remove https handler from example.com');
console.info('\ttelebit http 3001 example.com # forward https traffic for example.com to port 3001');
console.info('\ttelebit http /module/path example.com # forward https traffic for example.com to port 3001');
console.info('\ttelebit http 3001 sub.example.com # forward https traffic for sub.example.com to port 3001');
console.info('\ttelebit http /module/path sub # forward https traffic for sub.example.com to port 3001');
console.info('');
console.info('\ttelebit tcp none # remove all tcp handlers');
console.info('\ttelebit tcp 5050 # forward all tcp to port 5050');
@ -644,6 +644,7 @@ function parseCli(/*state*/) {
} else if (/\/|\\/.test(argv[1])) {
// looks like a path
argv[1] = path.resolve(argv[1]);
// TODO make a default assignment here
} else if (-1 === special.indexOf(argv[1])) {
console.error("Not sure what you meant by '" + argv[1] + "'.");
console.error("Remember: paths should begin with ." + path.sep + ", like '." + path.sep + argv[1] + "'");
@ -669,6 +670,8 @@ function handleConfig(err, config) {
//console.log('CONFIG');
//console.log(config);
state.config = config;
var verstr = [ pkg.name + ' daemon v' + state.config.version ];
console.info(verstr.join(' '));
if (err) { console.error(err); process.exit(101); return; }

View File

@ -132,7 +132,22 @@ controllers.http = function (req, res, opts) {
var appname = getAppname(portOrPath);
var subdomain = opts.body[1];
var remoteHost;
if (subdomain) {
// Assign an FQDN to brief subdomains
// ex: foo => foo.rando.telebit.cloud
if (subdomain && !/\./.test(subdomain)) {
Object.keys(state.servernames).some(function (key) {
if (state.servernames[key].wildcard) {
subdomain += '.' + key;
}
});
}
if ('none' === portOrPath) {
delete state.servernames[subdomain];
remoteHost = 'none';
} else if (subdomain) {
// use a subdomain with this handler
var handlerName = getServername(state.servernames, subdomain);
if (!handlerName) {
active = false;
@ -143,6 +158,7 @@ controllers.http = function (req, res, opts) {
state.servernames[subdomain].handler = portOrPath;
remoteHost = subdomain;
} else {
// just replace the default domain
if (!Object.keys(state.servernames).sort(function (a, b) {
return b.length - a.length;
}).some(function (key) {
@ -152,17 +168,21 @@ controllers.http = function (req, res, opts) {
return true;
}
if (state.servernames[key].wildcard) {
if (!state.servernames[appname + '.' + key]) {
state.servernames[appname + '.' + key] = {};
//var prefix = appname + '.' + key;
var prefix = key;
if (!state.servernames[prefix]) {
state.servernames[prefix] = {};
}
state.servernames[appname + '.' + key].handler = portOrPath;
remoteHost = appname + '.' + key;
state.servernames[prefix].handler = portOrPath;
remoteHost = prefix;
return true;
}
})) {
Object.keys(state.servernames).some(function (key) {
//var prefix = appname + '.' + key;
var prefix = key;
state.servernames[key].handler = portOrPath;
remoteHost = appname + '.' + key;
remoteHost = prefix;
return true;
});
}
@ -306,8 +326,10 @@ function serveControlsHelper() {
}
if (/\b(config)\b/.test(opts.pathname) && /get/i.test(req.method)) {
res.setHeader('Content-Type', 'appliCation/json');
res.end(JSON.stringify(state.config));
var resp = JSON.parse(JSON.stringify(state.config));
resp.version = pkg.version;
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(resp));
return;
}
@ -552,6 +574,10 @@ function serveControlsHelper() {
res.end(JSON.stringify(
{ status: (state.config.disable ? 'disabled' : 'enabled')
, ready: ((state.config.relay && (state.config.token || state.config.agreeTos)) ? true : false)
, active: !!tun
, connected: 'maybe (todo)'
, version: pkg.version
, servernames: state.servernames
}
));
return;

View File

@ -157,15 +157,15 @@ module.exports.assign = function (state, tun, cb) {
//if (tun.data) { conn.write(tun.data); }
return conn;
}
function fileDirTcp(conf, cb) {
function fileDirTcp(opts, cb) {
var socketPair = require('socket-pair');
var conn = socketPair.create(function (err, other) {
if (err) { cb(err); return; }
if (conf._stat.isFile()) {
fs.createReadStream(conf.handler).pipe(other);
if (opts.stat.isFile()) {
fs.createReadStream(opts.config.handler).pipe(other);
} else {
fs.readdir(conf.handler, function (err, nodes) {
fs.readdir(opts.config.handler, function (err, nodes) {
other.write('\n' + nodes.join('\n') + '\n\n');
other.end();
});
@ -282,8 +282,7 @@ module.exports.assign = function (state, tun, cb) {
errorTcp(conf, cb);
return;
}
conf._stat = stat;
fileDirTcp(conf, cb);
fileDirTcp({ config: conf, stat: stat }, cb);
});
});
}

View File

@ -1,6 +1,6 @@
{
"name": "telebit",
"version": "0.19.26",
"version": "0.19.27",
"description": "Break out of localhost. Connect to any device from anywhere over any tcp port or securely in a browser. A secure tunnel. A poor man's reverse VPN.",
"main": "lib/remote.js",
"files": [