fix bug tunnel bug with --sites

This commit is contained in:
AJ ONeal 2017-02-01 20:00:00 -07:00
parent afdf0920ca
commit 929f555d73
4 changed files with 49 additions and 48 deletions

View File

@ -95,7 +95,6 @@ function createServer(port, pubdir, content, opts) {
var app = require('../lib/app.js'); var app = require('../lib/app.js');
var directive = { public: pubdir, content: content, livereload: opts.livereload var directive = { public: pubdir, content: content, livereload: opts.livereload
, servername: opts.servername
, sites: opts.sites , sites: opts.sites
, expressApp: opts.expressApp }; , expressApp: opts.expressApp };
var insecureServer; var insecureServer;
@ -141,21 +140,21 @@ function createServer(port, pubdir, content, opts) {
var secureContexts = { var secureContexts = {
'localhost.daplie.me': null 'localhost.daplie.me': null
}; };
opts.httpsOptions.SNICallback = function (servername, cb ) { opts.httpsOptions.SNICallback = function (sni, cb ) {
console.log('[https] servername', servername); console.log('[https] sni', sni);
// Static Certs // Static Certs
if ('localhost.daplie.me' === servername) { if ('localhost.daplie.me' === sni) {
// TODO implement // TODO implement
if (!secureContexts[servername]) { if (!secureContexts[sni]) {
secureContexts[servername] = tls.createSecureContext(require('localhost.daplie.me-certificates').merge({})); secureContexts[sni] = tls.createSecureContext(require('localhost.daplie.me-certificates').merge({}));
} }
cb(null, secureContexts[servername]); cb(null, secureContexts[sni]);
return; return;
} }
// Dynamic Certs // Dynamic Certs
lex.httpsOptions.SNICallback(servername, cb); lex.httpsOptions.SNICallback(sni, cb);
}; };
var server = https.createServer(opts.httpsOptions); var server = https.createServer(opts.httpsOptions);
@ -277,7 +276,7 @@ function run() {
var p; var p;
opts.PromiseA = PromiseA; opts.PromiseA = PromiseA;
opts.httpsOptions.SNICallback = function (servername, cb) { opts.httpsOptions.SNICallback = function (sni, cb) {
if (!secureContext) { if (!secureContext) {
secureContext = tls.createSecureContext(opts.httpsOptions); secureContext = tls.createSecureContext(opts.httpsOptions);
} }
@ -286,11 +285,11 @@ function run() {
}; };
if (letsencryptHost) { if (letsencryptHost) {
// TODO remove in v3.x (aka goldilocks)
argv.key = argv.key || '/etc/letsencrypt/live/' + letsencryptHost + '/privkey.pem'; argv.key = argv.key || '/etc/letsencrypt/live/' + letsencryptHost + '/privkey.pem';
argv.cert = argv.cert || '/etc/letsencrypt/live/' + letsencryptHost + '/fullchain.pem'; argv.cert = argv.cert || '/etc/letsencrypt/live/' + letsencryptHost + '/fullchain.pem';
argv.root = argv.root || argv.chain || ''; argv.root = argv.root || argv.chain || '';
argv.servername = argv.servername || letsencryptHost; argv.sites = argv.sites || (argv.servername || letsencryptHost);
argv.sites = argv.sites || [ { name: argv.servername || letsencryptHost , path: '.' } ];
argv['serve-root'] = argv['serve-root'] || argv['serve-chain']; argv['serve-root'] = argv['serve-root'] || argv['serve-chain'];
// argv[express-app] // argv[express-app]
} }
@ -341,18 +340,17 @@ function run() {
} }
opts.servername = defaultServername;
opts.sites = [ { name: defaultServername , path: '.' } ]; opts.sites = [ { name: defaultServername , path: '.' } ];
if (argv.servername) { if (argv.servername) {
opts.servername = argv.servername; // TODO remove in v3.x (aka goldilocks)
if (!argv.sites) { if (argv.sites) {
opts.sites = [ { name: argv.servername, path: '.' } ]; throw new Error('specify only --sites, not --servername');
} }
opts.sites = [ { name: argv.servername, path: '.' } ];
} }
if (argv.sites) { if (argv.sites) {
opts.sites = argv.sites.split(',').map(function (servername) { opts.sites = argv.sites.split(',').map(function (name) {
var serverparts = servername.split('|'); var serverparts = name.split('|');
// TODO allow reverse proxy // TODO allow reverse proxy
return { return {
name: serverparts.shift() name: serverparts.shift()
@ -360,6 +358,8 @@ function run() {
}; };
}); });
} }
// TODO use arrays in all things
opts._old_server_name = opts.sites[0].name;
if (argv.p || argv.port || argv._[0]) { if (argv.p || argv.port || argv._[0]) {
opts.manualPort = true; opts.manualPort = true;
@ -380,7 +380,7 @@ function run() {
opts.expressApp = require(path.resolve(process.cwd(), argv['express-app'])); opts.expressApp = require(path.resolve(process.cwd(), argv['express-app']));
} }
if (opts.email || opts.servername) { if (opts.email || argv.sites || argv.servername) {
if (!opts.agreeTos) { if (!opts.agreeTos) {
console.warn("You may need to specify --agree-tos to agree to both the Let's Encrypt and Daplie DNS terms of service."); console.warn("You may need to specify --agree-tos to agree to both the Let's Encrypt and Daplie DNS terms of service.");
} }
@ -423,7 +423,7 @@ function run() {
console.info(''); console.info('');
// Port // Port
httpsUrl = 'https://' + opts.servername; httpsUrl = 'https://' + opts._old_server_name;
p = opts.port; p = opts.port;
if (httpsPort !== p) { if (httpsPort !== p) {
httpsUrl += ':' + p; httpsUrl += ':' + p;
@ -431,7 +431,7 @@ function run() {
console.info('\t' + httpsUrl); console.info('\t' + httpsUrl);
// Insecure Port // Insecure Port
httpUrl = 'http://' + opts.servername; httpUrl = 'http://' + opts._old_server_name;
p = opts.insecurePort; p = opts.insecurePort;
if (httpPort !== p) { if (httpPort !== p) {
httpUrl += ':' + p; httpUrl += ':' + p;
@ -439,16 +439,16 @@ function run() {
console.info('\t' + httpUrl + ' (redirecting to https)'); console.info('\t' + httpUrl + ' (redirecting to https)');
console.info(''); console.info('');
if (!(argv.servername && defaultServername !== argv.servername && !(argv.key && argv.cert))) { if (!((argv.sites || argv.servername) && defaultServername !== (argv.sites || argv.servername) && !(argv.key && argv.cert))) {
// ifaces // ifaces
opts.ifaces = require('../lib/local-ip.js').find(); opts.ifaces = require('../lib/local-ip.js').find();
promise = PromiseA.resolve(); promise = PromiseA.resolve();
} else { } else {
console.info("Attempting to resolve external connection for '" + argv.servername + "'"); console.info("Attempting to resolve external connection for '" + opts._old_server_name + "'");
try { try {
promise = require('../lib/match-ips.js').match(argv.servername, opts); promise = require('../lib/match-ips.js').match(opts._old_server_name, opts);
} catch(e) { } catch(e) {
console.warn("Upgrade to version 2.x to use automatic certificate issuance for '" + argv.servername + "'"); console.warn("Upgrade to version 2.x to use automatic certificate issuance for '" + opts._old_server_name + "'");
promise = PromiseA.resolve(); promise = PromiseA.resolve();
} }
} }
@ -456,7 +456,7 @@ function run() {
return promise.then(function (matchingIps) { return promise.then(function (matchingIps) {
if (matchingIps) { if (matchingIps) {
if (!matchingIps.length) { if (!matchingIps.length) {
console.info("Neither the attached nor external interfaces match '" + argv.servername + "'"); console.info("Neither the attached nor external interfaces match '" + opts._old_server_name + "'");
} }
} }
opts.matchingIps = matchingIps || []; opts.matchingIps = matchingIps || [];

View File

@ -11,7 +11,6 @@ module.exports = function (opts) {
var server; var server;
function addServer(hostname) { function addServer(hostname) {
console.log('add server:', hostname);
if (hostsMap[hostname]) { if (hostsMap[hostname]) {
return hostsMap[hostname]; return hostsMap[hostname];
@ -24,8 +23,6 @@ module.exports = function (opts) {
return; return;
} }
console.log('add server for reals', tmp);
site.path = site.path || site.paths[0] || '.'; site.path = site.path || site.paths[0] || '.';
if (!pathsMap[site.path]) { if (!pathsMap[site.path]) {
@ -67,8 +64,6 @@ module.exports = function (opts) {
} }
opts.servername = opts.servername || opts.sites[0].name;
addServer(opts.sites[0].name); addServer(opts.sites[0].name);
return function (req, res) { return function (req, res) {
@ -79,7 +74,7 @@ module.exports = function (opts) {
} }
var done = finalhandler(req, res); var done = finalhandler(req, res);
var host = req.headers.host; var host = req.headers.host;
var hostname = (host||'').split(':')[0] || opts.servername; var hostname = (host||'').split(':')[0] || opts.sites[0].name;
function serveStatic(server) { function serveStatic(server) {
if (server.expressApp) { if (server.expressApp) {
@ -95,7 +90,7 @@ module.exports = function (opts) {
if (opts.livereload) { if (opts.livereload) {
res.__my_livereload = '<script src="//' res.__my_livereload = '<script src="//'
+ (host || opts.servername).split(':')[0] + (host || opts.sites[0].name).split(':')[0]
+ ':35729/livereload.js?snipver=1"></script>'; + ':35729/livereload.js?snipver=1"></script>';
res.__my_addLen = res.__my_livereload.length; res.__my_addLen = res.__my_livereload.length;

View File

@ -5,10 +5,10 @@ module.exports.create = function (opts/*, servers*/) {
var dns = PromiseA.promisifyAll(require('dns')); var dns = PromiseA.promisifyAll(require('dns'));
return PromiseA.all([ return PromiseA.all([
dns.resolve4Async(opts.servername).then(function (results) { dns.resolve4Async(opts._old_server_name).then(function (results) {
return results; return results;
}, function () {}) }, function () {})
, dns.resolve6Async(opts.servername).then(function (results) { , dns.resolve6Async(opts._old_server_name).then(function (results) {
return results; return results;
}, function () {}) }, function () {})
]).then(function (results) { ]).then(function (results) {
@ -73,14 +73,14 @@ module.exports.create = function (opts/*, servers*/) {
if (require.main === module) { if (require.main === module) {
var opts = { var opts = {
servername: 'aj.daplie.me' _old_server_name: 'aj.daplie.me'
, PromiseA: require('bluebird') , PromiseA: require('bluebird')
}; };
// ifaces // ifaces
opts.ifaces = require('./local-ip.js').find(); opts.ifaces = require('./local-ip.js').find();
console.log('opts.ifaces'); console.log('opts.ifaces');
console.log(opts.ifaces); console.log(opts.ifaces);
require('./match-ips.js').match(opts.servername, opts).then(function (ips) { require('./match-ips.js').match(opts._old_server_name, opts).then(function (ips) {
opts.matchingIps = ips.matchingIps || []; opts.matchingIps = ips.matchingIps || [];
opts.externalIps = ips.externalIps; opts.externalIps = ips.externalIps;
module.exports.create(opts); module.exports.create(opts);

View File

@ -63,25 +63,31 @@ module.exports.create = function (opts, servers) {
return Tunnel.token({ return Tunnel.token({
refreshToken: opts.refreshToken refreshToken: opts.refreshToken
, email: opts.email , email: opts.email
, domains: [ opts.servername ] , domains: opts.sites.map(function (site) {
return site.name;
})
, device: { hostname: opts.devicename || opts.device } , device: { hostname: opts.devicename || opts.device }
}).then(function (result) { }).then(function (result) {
// { jwt, tunnelUrl } // { jwt, tunnelUrl }
var locals = [];
opts.sites.map(function (site) {
locals.push({
protocol: 'https'
, hostname: site.name
, port: opts.port
});
locals.push({
protocol: 'http'
, hostname: site.name
, port: opts.insecurePort || opts.port
});
});
return stunnel.connect({ return stunnel.connect({
token: result.jwt token: result.jwt
, stunneld: result.tunnelUrl , stunneld: result.tunnelUrl
// XXX TODO BUG // this is just for testing // XXX TODO BUG // this is just for testing
, insecure: /*opts.insecure*/ true , insecure: /*opts.insecure*/ true
, locals: [ , locals: locals
{ protocol: 'https'
, hostname: opts.servername
, port: opts.port
}
, { protocol: 'http'
, hostname: opts.servername
, port: opts.insecurePort || opts.port
}
]
// a simple passthru is proving to not be so simple // a simple passthru is proving to not be so simple
, net: require('net') /* , net: require('net') /*
{ {