forked from coolaj86/goldilocks.js
remoteAddress priority... and whitespace... ooops
This commit is contained in:
parent
4b470ffe51
commit
350d87c38d
|
@ -7,76 +7,76 @@ module.exports.create = function (deps, config) {
|
||||||
var PromiseA = require('bluebird');
|
var PromiseA = require('bluebird');
|
||||||
var greenlock = require('greenlock');
|
var greenlock = require('greenlock');
|
||||||
var listeners = require('./servers').listeners;
|
var listeners = require('./servers').listeners;
|
||||||
var parseSni = require('sni');
|
var parseSni = require('sni');
|
||||||
var modules = { };
|
var modules = { };
|
||||||
var program = {
|
var program = {
|
||||||
tlsOptions: require('localhost.daplie.me-certificates').merge({})
|
tlsOptions: require('localhost.daplie.me-certificates').merge({})
|
||||||
, acmeDirectoryUrl: 'https://acme-v01.api.letsencrypt.org/directory'
|
, acmeDirectoryUrl: 'https://acme-v01.api.letsencrypt.org/directory'
|
||||||
, challengeType: 'tls-sni-01'
|
, challengeType: 'tls-sni-01'
|
||||||
};
|
};
|
||||||
var secureContexts = {};
|
var secureContexts = {};
|
||||||
var tunnelAdminTlsOpts = {};
|
var tunnelAdminTlsOpts = {};
|
||||||
var tls = require('tls');
|
var tls = require('tls');
|
||||||
|
|
||||||
var tcpRouter = {
|
var tcpRouter = {
|
||||||
_map: { }
|
_map: { }
|
||||||
, _create: function (address, port) {
|
, _create: function (address, port) {
|
||||||
// port provides hinting for http, smtp, etc
|
// port provides hinting for http, smtp, etc
|
||||||
return function (conn, firstChunk, opts) {
|
return function (conn, firstChunk, opts) {
|
||||||
console.log('[tcpRouter] ' + address + ':' + port + ' ' + (opts.servername || ''));
|
console.log('[tcpRouter] ' + address + ':' + port + ' ' + (opts.servername || ''));
|
||||||
|
|
||||||
var m;
|
var m;
|
||||||
var str;
|
var str;
|
||||||
var hostname;
|
var hostname;
|
||||||
var newHeads;
|
var newHeads;
|
||||||
|
|
||||||
// TODO test per-module
|
// TODO test per-module
|
||||||
// Maybe HTTP
|
// Maybe HTTP
|
||||||
if (firstChunk[0] > 32 && firstChunk[0] < 127) {
|
if (firstChunk[0] > 32 && firstChunk[0] < 127) {
|
||||||
str = firstChunk.toString();
|
str = firstChunk.toString();
|
||||||
m = str.match(/(?:^|[\r\n])Host: ([^\r\n]+)[\r\n]*/im);
|
m = str.match(/(?:^|[\r\n])Host: ([^\r\n]+)[\r\n]*/im);
|
||||||
hostname = (m && m[1].toLowerCase() || '').split(':')[0];
|
hostname = (m && m[1].toLowerCase() || '').split(':')[0];
|
||||||
console.log('[tcpRouter] hostname', hostname);
|
console.log('[tcpRouter] hostname', hostname);
|
||||||
if (/HTTP\//i.test(str)) {
|
if (/HTTP\//i.test(str)) {
|
||||||
//conn.__service = 'http';
|
//conn.__service = 'http';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!hostname) {
|
if (!hostname) {
|
||||||
// TODO allow tcp tunneling
|
// TODO allow tcp tunneling
|
||||||
// TODO we need some way of tagging tcp as either terminated tls or insecure
|
// TODO we need some way of tagging tcp as either terminated tls or insecure
|
||||||
conn.write(
|
conn.write(
|
||||||
"HTTP/1.1 404 Not Found\r\n"
|
"HTTP/1.1 404 Not Found\r\n"
|
||||||
+ "Date: Fri, 31 Dec 1999 23:59:59 GMT\r\n"
|
+ "Date: Fri, 31 Dec 1999 23:59:59 GMT\r\n"
|
||||||
+ "Content-Type: text/html\r\n"
|
+ "Content-Type: text/html\r\n"
|
||||||
+ "Content-Length: " + 9 + "\r\n"
|
+ "Content-Length: " + 9 + "\r\n"
|
||||||
+ "\r\n"
|
+ "\r\n"
|
||||||
+ "Not Found"
|
+ "Not Found"
|
||||||
);
|
);
|
||||||
conn.end();
|
conn.end();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Poor-man's http proxy
|
// Poor-man's http proxy
|
||||||
// XXX SECURITY XXX: should strip existing X-Forwarded headers
|
// XXX SECURITY XXX: should strip existing X-Forwarded headers
|
||||||
newHeads =
|
newHeads =
|
||||||
[ "X-Forwarded-Proto: " + (opts.encrypted ? 'https' : 'http')
|
[ "X-Forwarded-Proto: " + (opts.encrypted ? 'https' : 'http')
|
||||||
, "X-Forwarded-For: " + (conn.remoteAddress || opts.remoteAddress)
|
, "X-Forwarded-For: " + (opts.remoteAddress || conn.remoteAddress)
|
||||||
, "X-Forwarded-Host: " + hostname
|
, "X-Forwarded-Host: " + hostname
|
||||||
];
|
];
|
||||||
|
|
||||||
if (!opts.encrypted) {
|
if (!opts.encrypted) {
|
||||||
// a exists-only header that a bad client could not remove
|
// a exists-only header that a bad client could not remove
|
||||||
newHeads.push("X-Not-Encrypted: yes");
|
newHeads.push("X-Not-Encrypted: yes");
|
||||||
}
|
}
|
||||||
if (opts.servername) {
|
if (opts.servername) {
|
||||||
newHeads.push("X-Forwarded-Sni: " + opts.servername);
|
newHeads.push("X-Forwarded-Sni: " + opts.servername);
|
||||||
if (opts.servername !== hostname) {
|
if (opts.servername !== hostname) {
|
||||||
// an exists-only header that a bad client could not remove
|
// an exists-only header that a bad client could not remove
|
||||||
newHeads.push("X-Two-Servernames: yes");
|
newHeads.push("X-Two-Servernames: yes");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
firstChunk = firstChunk.toString('utf8');
|
firstChunk = firstChunk.toString('utf8');
|
||||||
// JSON.stringify("Host: example.com\r\nNext: Header".replace(/(Host: [^\r\n]*)/i, "$1" + "\r\n" + "X: XYZ"))
|
// JSON.stringify("Host: example.com\r\nNext: Header".replace(/(Host: [^\r\n]*)/i, "$1" + "\r\n" + "X: XYZ"))
|
||||||
|
@ -87,16 +87,16 @@ module.exports.create = function (deps, config) {
|
||||||
|
|
||||||
//
|
//
|
||||||
// hard-coded routes for the admin interface
|
// hard-coded routes for the admin interface
|
||||||
if (
|
if (
|
||||||
/\blocalhost\.admin\./.test(hostname) || /\badmin\.localhost\./.test(hostname)
|
/\blocalhost\.admin\./.test(hostname) || /\badmin\.localhost\./.test(hostname)
|
||||||
|| /\blocalhost\.alpha\./.test(hostname) || /\balpha\.localhost\./.test(hostname)
|
|| /\blocalhost\.alpha\./.test(hostname) || /\balpha\.localhost\./.test(hostname)
|
||||||
) {
|
) {
|
||||||
if (!modules.admin) {
|
if (!modules.admin) {
|
||||||
modules.admin = require('./modules/admin.js').create(deps, config);
|
modules.admin = require('./modules/admin.js').create(deps, config);
|
||||||
}
|
}
|
||||||
modules.admin.emit('connection', conn);
|
modules.admin.emit('connection', conn);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO static file handiling and such or whatever
|
// TODO static file handiling and such or whatever
|
||||||
if (!modules.http) {
|
if (!modules.http) {
|
||||||
|
@ -105,35 +105,37 @@ module.exports.create = function (deps, config) {
|
||||||
opts.hostname = hostname;
|
opts.hostname = hostname;
|
||||||
conn.__opts = opts;
|
conn.__opts = opts;
|
||||||
modules.http.emit('connection', conn);
|
modules.http.emit('connection', conn);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
, get: function getTcpRouter(address, port) {
|
, get: function getTcpRouter(address, port) {
|
||||||
address = address || '0.0.0.0';
|
address = address || '0.0.0.0';
|
||||||
|
|
||||||
var id = address + ':' + port;
|
var id = address + ':' + port;
|
||||||
if (!tcpRouter._map[id]) {
|
if (!tcpRouter._map[id]) {
|
||||||
tcpRouter._map[id] = tcpRouter._create(address, port);
|
tcpRouter._map[id] = tcpRouter._create(address, port);
|
||||||
}
|
}
|
||||||
|
|
||||||
return tcpRouter._map[id];
|
return tcpRouter._map[id];
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
var tlsRouter = {
|
var tlsRouter = {
|
||||||
_map: { }
|
_map: { }
|
||||||
, _create: function (address, port/*, nextServer*/) {
|
, _create: function (address, port/*, nextServer*/) {
|
||||||
// port provides hinting for https, smtps, etc
|
// port provides hinting for https, smtps, etc
|
||||||
return function (socket, firstChunk, opts) {
|
return function (socket, firstChunk, opts) {
|
||||||
var servername = opts.servername;
|
var servername = opts.servername;
|
||||||
var packerStream = require('tunnel-packer').Stream;
|
var packerStream = require('tunnel-packer').Stream;
|
||||||
var myDuplex = packerStream.create(socket);
|
var myDuplex = packerStream.create(socket);
|
||||||
|
|
||||||
console.log('[tlsRouter] ' + address + ':' + port + ' servername', servername, myDuplex.remoteAddress);
|
myDuplex.remoteAddress = opts.remoteAddress || myDuplex.remoteAddress;
|
||||||
|
myDuplex.remotePort = opts.remotePort || myDuplex.remotePort;
|
||||||
|
console.log('[tlsRouter] ' + address + ':' + port + ' servername', servername, myDuplex.remoteAddress);
|
||||||
|
|
||||||
// needs to wind up in one of 3 states:
|
// needs to wind up in one of 3 states:
|
||||||
// 1. SNI-based Proxy / Tunnel (we don't even need to put it through the tlsSocket)
|
// 1. SNI-based Proxy / Tunnel (we don't even need to put it through the tlsSocket)
|
||||||
// 2. Admin Interface (skips the proxying)
|
// 2. Admin Interface (skips the proxying)
|
||||||
// 3. Terminated (goes on to a particular module or route)
|
// 3. Terminated (goes on to a particular module or route)
|
||||||
//myDuplex.__tlsTerminated = true;
|
//myDuplex.__tlsTerminated = true;
|
||||||
|
|
||||||
process.nextTick(function () {
|
process.nextTick(function () {
|
||||||
// this must happen after the socket is emitted to the next in the chain,
|
// this must happen after the socket is emitted to the next in the chain,
|
||||||
|
@ -142,36 +144,36 @@ module.exports.create = function (deps, config) {
|
||||||
});
|
});
|
||||||
|
|
||||||
// nextServer.emit could be used here
|
// nextServer.emit could be used here
|
||||||
program.tlsTunnelServer.emit('connection', myDuplex);
|
program.tlsTunnelServer.emit('connection', myDuplex);
|
||||||
|
|
||||||
// Why all this wacky-do with the myDuplex?
|
// Why all this wacky-do with the myDuplex?
|
||||||
// because https://github.com/nodejs/node/issues/8854, that's why
|
// because https://github.com/nodejs/node/issues/8854, that's why
|
||||||
// (because node's internal networking layer == 💩 sometimes)
|
// (because node's internal networking layer == 💩 sometimes)
|
||||||
socket.on('data', function (chunk) {
|
socket.on('data', function (chunk) {
|
||||||
console.log('[' + Date.now() + '] tls socket data', chunk.byteLength);
|
console.log('[' + Date.now() + '] tls socket data', chunk.byteLength);
|
||||||
myDuplex.push(chunk);
|
myDuplex.push(chunk);
|
||||||
});
|
});
|
||||||
socket.on('error', function (err) {
|
socket.on('error', function (err) {
|
||||||
console.error('[error] httpsTunnel (Admin) TODO close');
|
console.error('[error] httpsTunnel (Admin) TODO close');
|
||||||
console.error(err);
|
console.error(err);
|
||||||
myDuplex.emit('error', err);
|
myDuplex.emit('error', err);
|
||||||
});
|
});
|
||||||
socket.on('close', function () {
|
socket.on('close', function () {
|
||||||
myDuplex.end();
|
myDuplex.end();
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
, get: function getTcpRouter(address, port) {
|
, get: function getTcpRouter(address, port) {
|
||||||
address = address || '0.0.0.0';
|
address = address || '0.0.0.0';
|
||||||
|
|
||||||
var id = address + ':' + port;
|
var id = address + ':' + port;
|
||||||
if (!tlsRouter._map[id]) {
|
if (!tlsRouter._map[id]) {
|
||||||
tlsRouter._map[id] = tlsRouter._create(address, port);
|
tlsRouter._map[id] = tlsRouter._create(address, port);
|
||||||
}
|
}
|
||||||
|
|
||||||
return tlsRouter._map[id];
|
return tlsRouter._map[id];
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// opts = { servername, encrypted, remoteAddress, remotePort }
|
// opts = { servername, encrypted, remoteAddress, remotePort }
|
||||||
|
@ -184,77 +186,77 @@ module.exports.create = function (deps, config) {
|
||||||
|
|
||||||
// TODO port-based routing can do here
|
// TODO port-based routing can do here
|
||||||
|
|
||||||
// TLS
|
// TLS
|
||||||
if (22 === firstChunk[0]) {
|
if (22 === firstChunk[0]) {
|
||||||
servername = (parseSni(firstChunk)||'').toLowerCase() || 'localhost.invalid';
|
servername = (parseSni(firstChunk)||'').toLowerCase() || 'localhost.invalid';
|
||||||
console.log('tryTls');
|
console.log('tryTls');
|
||||||
tlsRouter.get(conn.localAddress, conn.localPort)(conn, firstChunk, opts);
|
tlsRouter.get(conn.localAddress, conn.localPort)(conn, firstChunk, opts);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
console.log('tryTcp');
|
console.log('tryTcp');
|
||||||
tcpRouter.get(conn.localAddress, conn.localPort)(conn, firstChunk, opts);
|
tcpRouter.get(conn.localAddress, conn.localPort)(conn, firstChunk, opts);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function approveDomains(opts, certs, cb) {
|
function approveDomains(opts, certs, cb) {
|
||||||
// This is where you check your database and associated
|
// This is where you check your database and associated
|
||||||
// email addresses with domains and agreements and such
|
// email addresses with domains and agreements and such
|
||||||
|
|
||||||
// The domains being approved for the first time are listed in opts.domains
|
// The domains being approved for the first time are listed in opts.domains
|
||||||
// Certs being renewed are listed in certs.altnames
|
// Certs being renewed are listed in certs.altnames
|
||||||
|
|
||||||
function complete(err, stuff) {
|
function complete(err, stuff) {
|
||||||
opts.email = stuff.email;
|
opts.email = stuff.email;
|
||||||
opts.agreeTos = stuff.agreeTos;
|
opts.agreeTos = stuff.agreeTos;
|
||||||
opts.server = stuff.server;
|
opts.server = stuff.server;
|
||||||
opts.challengeType = stuff.challengeType;
|
opts.challengeType = stuff.challengeType;
|
||||||
|
|
||||||
cb(null, { options: opts, certs: certs });
|
cb(null, { options: opts, certs: certs });
|
||||||
}
|
}
|
||||||
|
|
||||||
if (certs) {
|
if (certs) {
|
||||||
// TODO make sure the same options are used for renewal as for registration?
|
// TODO make sure the same options are used for renewal as for registration?
|
||||||
opts.domains = certs.altnames;
|
opts.domains = certs.altnames;
|
||||||
|
|
||||||
cb(null, { options: opts, certs: certs });
|
cb(null, { options: opts, certs: certs });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// check config for domain name
|
// check config for domain name
|
||||||
if (-1 !== config.tls.servernames.indexOf(opts.domain)) {
|
if (-1 !== config.tls.servernames.indexOf(opts.domain)) {
|
||||||
// TODO how to handle SANs?
|
// TODO how to handle SANs?
|
||||||
// TODO fetch domain-specific email
|
// TODO fetch domain-specific email
|
||||||
// TODO fetch domain-specific acmeDirectory
|
// TODO fetch domain-specific acmeDirectory
|
||||||
// NOTE: you can also change other options such as `challengeType` and `challenge`
|
// NOTE: you can also change other options such as `challengeType` and `challenge`
|
||||||
// opts.challengeType = 'http-01';
|
// opts.challengeType = 'http-01';
|
||||||
// opts.challenge = require('le-challenge-fs').create({}); // TODO this doesn't actually work yet
|
// opts.challenge = require('le-challenge-fs').create({}); // TODO this doesn't actually work yet
|
||||||
complete(null, {
|
complete(null, {
|
||||||
email: config.tls.email, agreeTos: true, server: program.acmeDirectoryUrl, challengeType: program.challengeType });
|
email: config.tls.email, agreeTos: true, server: program.acmeDirectoryUrl, challengeType: program.challengeType });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// TODO ask http module about the default path (/srv/www/:hostname)
|
// TODO ask http module about the default path (/srv/www/:hostname)
|
||||||
// (if it exists, we can allow and add to config)
|
// (if it exists, we can allow and add to config)
|
||||||
if (!modules.http) {
|
if (!modules.http) {
|
||||||
modules.http = require('./modules/http.js').create(config);
|
modules.http = require('./modules/http.js').create(config);
|
||||||
}
|
}
|
||||||
modules.http.checkServername(opts.domain).then(function (stuff) {
|
modules.http.checkServername(opts.domain).then(function (stuff) {
|
||||||
if (!stuff.domains) {
|
if (!stuff.domains) {
|
||||||
// TODO once precheck is implemented we can just let it pass if it passes, yknow?
|
// TODO once precheck is implemented we can just let it pass if it passes, yknow?
|
||||||
cb(new Error('domain is not allowed'));
|
cb(new Error('domain is not allowed'));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
complete(null, {
|
complete(null, {
|
||||||
domain: stuff.domain || stuff.domains[0]
|
domain: stuff.domain || stuff.domains[0]
|
||||||
, domains: stuff.domains
|
, domains: stuff.domains
|
||||||
, email: program.email
|
, email: program.email
|
||||||
, server: program.acmeDirectoryUrl
|
, server: program.acmeDirectoryUrl
|
||||||
, challengeType: program.challengeType
|
, challengeType: program.challengeType
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}, cb);
|
}, cb);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getAcme() {
|
function getAcme() {
|
||||||
return greenlock.create({
|
return greenlock.create({
|
||||||
|
@ -319,7 +321,7 @@ module.exports.create = function (deps, config) {
|
||||||
// console.log('terminated data:', chunk.toString());
|
// console.log('terminated data:', chunk.toString());
|
||||||
//});
|
//});
|
||||||
//(program.httpTunnelServer || program.httpServer).emit('connection', tlsSocket);
|
//(program.httpTunnelServer || program.httpServer).emit('connection', tlsSocket);
|
||||||
//tcpRouter.get(conn.localAddress, conn.localPort)(conn, firstChunk, { encrypted: false });
|
//tcpRouter.get(conn.localAddress, conn.localPort)(conn, firstChunk, { encrypted: false });
|
||||||
handler(tlsSocket, {
|
handler(tlsSocket, {
|
||||||
servername: tlsSocket.servername
|
servername: tlsSocket.servername
|
||||||
, encrypted: true
|
, encrypted: true
|
||||||
|
|
Loading…
Reference in New Issue