moved most things related to TCP connections to a tcp directory

This commit is contained in:
tigerbot 2017-10-30 15:57:18 -06:00
parent 9a63f30bf2
commit 16589e65f6
5 changed files with 25 additions and 29 deletions

View File

@ -1,6 +1,6 @@
'use strict'; 'use strict';
module.exports.create = function (deps, conf, greenlockMiddleware) { module.exports.create = function (deps, conf, tcpMods) {
var PromiseA = require('bluebird'); var PromiseA = require('bluebird');
var statAsync = PromiseA.promisify(require('fs').stat); var statAsync = PromiseA.promisify(require('fs').stat);
var domainMatches = require('../domain-utils').match; var domainMatches = require('../domain-utils').match;
@ -172,7 +172,7 @@ module.exports.create = function (deps, conf, greenlockMiddleware) {
} }
if (!acmeServer) { if (!acmeServer) {
acmeServer = require('http').createServer(greenlockMiddleware); acmeServer = require('http').createServer(tcpMods.tls.middleware);
} }
return emitConnection(acmeServer, conn, opts); return emitConnection(acmeServer, conn, opts);
} }
@ -241,7 +241,7 @@ module.exports.create = function (deps, conf, greenlockMiddleware) {
res.statusCode = 502; res.statusCode = 502;
res.setHeader('Connection', 'close'); res.setHeader('Connection', 'close');
res.setHeader('Content-Type', 'text/html'); res.setHeader('Content-Type', 'text/html');
res.end(require('../proxy-conn').getRespBody(err, conf.debug)); res.end(tcpMods.proxy.getRespBody(err, conf.debug));
}); });
proxyServer = http.createServer(function (req, res) { proxyServer = http.createServer(function (req, res) {
@ -292,7 +292,7 @@ module.exports.create = function (deps, conf, greenlockMiddleware) {
newConnOpts.remoteAddress = opts.address || conn.remoteAddress; newConnOpts.remoteAddress = opts.address || conn.remoteAddress;
newConnOpts.remotePort = opts.port || conn.remotePort; newConnOpts.remotePort = opts.port || conn.remotePort;
deps.proxy(conn, newConnOpts, opts.firstChunk); tcpMods.proxy(conn, newConnOpts, opts.firstChunk);
} }
function checkProxy(mod, conn, opts, headers) { function checkProxy(mod, conn, opts, headers) {

View File

@ -3,10 +3,8 @@
module.exports.create = function (deps, config) { module.exports.create = function (deps, config) {
console.log('config', config); console.log('config', config);
//var PromiseA = global.Promise; var listeners = require('../servers').listeners.tcp;
var PromiseA = require('bluebird'); var domainUtils = require('../domain-utils');
var listeners = require('./servers').listeners.tcp;
var domainUtils = require('./domain-utils');
var modules; var modules;
var addrProperties = [ var addrProperties = [
@ -24,13 +22,6 @@ module.exports.create = function (deps, config) {
}); });
} }
function loadModules() {
modules = {};
modules.tls = require('./modules/tls').create(deps, config, tcpHandler);
modules.http = require('./modules/http').create(deps, config, modules.tls.middleware);
}
function proxy(mod, conn, opts) { function proxy(mod, conn, opts) {
// First thing we need to add to the connection options is where to proxy the connection to // First thing we need to add to the connection options is where to proxy the connection to
var newConnOpts = domainUtils.separatePort(mod.address || ''); var newConnOpts = domainUtils.separatePort(mod.address || '');
@ -44,7 +35,7 @@ module.exports.create = function (deps, config) {
newConnOpts['_' + name] = opts[name] || opts['_'+name] || conn[name] || conn['_'+name]; newConnOpts['_' + name] = opts[name] || opts['_'+name] || conn[name] || conn['_'+name];
}); });
deps.proxy(conn, newConnOpts); modules.proxy(conn, newConnOpts);
return true; return true;
} }
@ -91,10 +82,6 @@ module.exports.create = function (deps, config) {
// opts = { servername, encrypted, peek, data, remoteAddress, remotePort } // opts = { servername, encrypted, peek, data, remoteAddress, remotePort }
function peek(conn, firstChunk, opts) { function peek(conn, firstChunk, opts) {
if (!modules) {
loadModules();
}
opts.firstChunk = firstChunk; opts.firstChunk = firstChunk;
conn.__opts = opts; conn.__opts = opts;
// TODO port/service-based routing can do here // TODO port/service-based routing can do here
@ -172,6 +159,12 @@ module.exports.create = function (deps, config) {
}); });
} }
modules = {};
modules.tcpHandler = tcpHandler;
modules.proxy = require('./proxy-conn').create(deps, config);
modules.tls = require('./tls').create(deps, config, modules);
modules.http = require('./http').create(deps, config, modules);
deps.tunnel = deps.tunnel || {}; deps.tunnel = deps.tunnel || {};
deps.tunnel.net = { deps.tunnel.net = {
createConnection: function (opts, cb) { createConnection: function (opts, cb) {
@ -235,8 +228,8 @@ module.exports.create = function (deps, config) {
return writer; return writer;
} }
}; };
deps.tunnelClients = require('./tunnel-client-manager').create(deps, config); deps.tunnelClients = require('../tunnel-client-manager').create(deps, config);
deps.tunnelServer = require('./tunnel-server-manager').create(deps, config); deps.tunnelServer = require('../tunnel-server-manager').create(deps, config);
function updateListeners() { function updateListeners() {
var current = listeners.list(); var current = listeners.list();

View File

@ -32,7 +32,7 @@ module.exports.getRespBody = getRespBody;
module.exports.sendBadGateway = sendBadGateway; module.exports.sendBadGateway = sendBadGateway;
module.exports.create = function (deps, config) { module.exports.create = function (deps, config) {
return function proxy(conn, newConnOpts, firstChunk, decrypt) { function proxy(conn, newConnOpts, firstChunk, decrypt) {
var connected = false; var connected = false;
newConnOpts.allowHalfOpen = true; newConnOpts.allowHalfOpen = true;
var newConn = deps.net.createConnection(newConnOpts, function () { var newConn = deps.net.createConnection(newConnOpts, function () {
@ -73,5 +73,9 @@ module.exports.create = function (deps, config) {
newConn.on('close', function () { newConn.on('close', function () {
conn.destroy(); conn.destroy();
}); });
}; }
proxy.getRespBody = getRespBody;
proxy.sendBadGateway = sendBadGateway;
return proxy;
}; };

View File

@ -1,6 +1,6 @@
'use strict'; 'use strict';
module.exports.create = function (deps, config, netHandler) { module.exports.create = function (deps, config, tcpMods) {
var path = require('path'); var path = require('path');
var tls = require('tls'); var tls = require('tls');
var parseSni = require('sni'); var parseSni = require('sni');
@ -208,7 +208,7 @@ module.exports.create = function (deps, config, netHandler) {
var terminateServer = tls.createServer(terminatorOpts, function (socket) { var terminateServer = tls.createServer(terminatorOpts, function (socket) {
console.log('(post-terminated) tls connection, addr:', extractSocketProp(socket, 'remoteAddress')); console.log('(post-terminated) tls connection, addr:', extractSocketProp(socket, 'remoteAddress'));
netHandler(socket, { tcpMods.tcpHandler(socket, {
servername: socket.servername servername: socket.servername
, encrypted: true , encrypted: true
// remoteAddress... ugh... https://github.com/nodejs/node/issues/8854 // remoteAddress... ugh... https://github.com/nodejs/node/issues/8854
@ -232,7 +232,7 @@ module.exports.create = function (deps, config, netHandler) {
newConnOpts.remoteAddress = opts.address || extractSocketProp(socket, 'remoteAddress'); newConnOpts.remoteAddress = opts.address || extractSocketProp(socket, 'remoteAddress');
newConnOpts.remotePort = opts.port || extractSocketProp(socket, 'remotePort'); newConnOpts.remotePort = opts.port || extractSocketProp(socket, 'remotePort');
deps.proxy(socket, newConnOpts, opts.firstChunk, function () { tcpMods.proxy(socket, newConnOpts, opts.firstChunk, function () {
// This function is called in the event of a connection error and should decrypt // This function is called in the event of a connection error and should decrypt
// the socket so the proxy module can send a 502 HTTP response. // the socket so the proxy module can send a 502 HTTP response.
var tlsOpts = localhostCerts.mergeTlsOptions('localhost.daplie.me', {isServer: true}); var tlsOpts = localhostCerts.mergeTlsOptions('localhost.daplie.me', {isServer: true});

View File

@ -48,12 +48,11 @@ function create(conf) {
modules = { modules = {
storage: require('./storage').create(deps, conf) storage: require('./storage').create(deps, conf)
, proxy: require('./proxy-conn').create(deps, conf)
, socks5: require('./socks5-server').create(deps, conf) , socks5: require('./socks5-server').create(deps, conf)
, ddns: require('./ddns').create(deps, conf) , ddns: require('./ddns').create(deps, conf)
, mdns: require('./mdns').create(deps, conf) , mdns: require('./mdns').create(deps, conf)
, udp: require('./udp').create(deps, conf) , udp: require('./udp').create(deps, conf)
, tcp: require('./goldilocks').create(deps, conf) , tcp: require('./tcp').create(deps, conf)
}; };
Object.assign(deps, modules); Object.assign(deps, modules);