From 21a77ad10a52b81bdf1637d21274f7bc11de66f0 Mon Sep 17 00:00:00 2001 From: tigerbot Date: Wed, 24 May 2017 13:05:37 -0600 Subject: [PATCH] added way to specify proxy destination --- bin/goldilocks.js | 4 ++-- goldilocks.example.yml | 5 +++-- lib/goldilocks.js | 26 ++++++++++++++++++++------ lib/modules/http.js | 4 +++- lib/modules/tls.js | 4 +++- 5 files changed, 31 insertions(+), 12 deletions(-) diff --git a/bin/goldilocks.js b/bin/goldilocks.js index e853336..28d33c0 100755 --- a/bin/goldilocks.js +++ b/bin/goldilocks.js @@ -71,7 +71,7 @@ function readConfigAndRun(args) { config = recase.camelCopy(config); if (!config.dns) { - config.dns = { modules: { name: 'proxy', port: 3053 } }; + config.dns = { modules: [{ name: 'proxy', port: 3053 }] }; } // Use Object.assign to add any properties needed but not defined in the mdns config. // It will first copy the defaults into an empty object, then copy any real config over that. @@ -82,7 +82,7 @@ function readConfigAndRun(args) { config.tcp = {}; } if (!config.http) { - config.http = { modules: { name: 'proxy', port: 3000 } }; + config.http = { modules: [{ name: 'proxy', domains: ['*'], port: 3000 }] }; } if (!config.tls) { console.log("TODO: tls: { modules: { name: 'acme', email: 'foo@bar.com', domains: [ '*' ] } }"); diff --git a/goldilocks.example.yml b/goldilocks.example.yml index 7836148..31d4f72 100644 --- a/goldilocks.example.yml +++ b/goldilocks.example.yml @@ -35,7 +35,7 @@ http: from: /nowhere/in/particular to: /just/an/example - name: proxy - address: '127.0.0.1:3001' + port: 3001 modules: - name: redirect @@ -47,7 +47,8 @@ http: - name: proxy domains: - localhost.daplie.me - address: '127.0.0.1:4000' + host: locahost + port: 4000 - name: static domains: - '*.localhost.daplie.me' diff --git a/lib/goldilocks.js b/lib/goldilocks.js index be18c92..ae6c552 100644 --- a/lib/goldilocks.js +++ b/lib/goldilocks.js @@ -74,22 +74,36 @@ module.exports.create = function (deps, config) { } function dnsListener(msg) { - var dgram = require('dgram'); - var socket = dgram.createSocket('udp4'); - socket.send(msg, config.dns.proxy.port, config.dns.proxy.address || '127.0.0.1'); + if (!Array.isArray(config.dns.modules)) { + return; + } + var socket = require('dgram').createSocket('udp4'); + config.dns.modules.forEach(function (mod) { + if (mod.name !== 'proxy') { + console.warn('found bad DNS module', mod); + return; + } + var dest = require('./domain-utils').separatePort(mod.address || ''); + dest.port = dest.port || mod.port; + dest.host = dest.host || mod.host || 'localhost'; + socket.send(msg, dest.port, dest.host); + }); } function createTcpForwarder(mod) { - return function (conn) { - var newConnOpts = require('./domain-utils').separatePort(mod.address); + var dest = require('./domain-utils').separatePort(mod.address || ''); + dest.port = dest.port || mod.port; + dest.host = dest.host || mod.host || 'localhost'; + return function (conn) { + var newConnOpts = {}; ['remote', 'local'].forEach(function (end) { ['Family', 'Address', 'Port'].forEach(function (name) { newConnOpts[end+name] = conn[end+name]; }); }); - deps.proxy(conn, newConnOpts); + deps.proxy(conn, Object.assign({}, dest, newConnOpts)); }; } diff --git a/lib/modules/http.js b/lib/modules/http.js index c6f2b1b..6d78a58 100644 --- a/lib/modules/http.js +++ b/lib/modules/http.js @@ -224,7 +224,9 @@ module.exports.create = function (deps, conf, greenlockMiddleware) { opts.firstChunk = Buffer.concat([head, body]); - var newConnOpts = separatePort(mod.address); + var newConnOpts = separatePort(mod.address || ''); + newConnOpts.port = newConnOpts.port || mod.port; + newConnOpts.host = newConnOpts.host || mod.host || 'localhost'; newConnOpts.servername = separatePort(headers.host).host; newConnOpts.data = opts.firstChunk; diff --git a/lib/modules/tls.js b/lib/modules/tls.js index a93c1da..34ad6fe 100644 --- a/lib/modules/tls.js +++ b/lib/modules/tls.js @@ -186,7 +186,9 @@ module.exports.create = function (deps, config, netHandler) { }); function proxy(socket, opts, mod) { - var newConnOpts = require('../domain-utils').separatePort(mod.address); + var newConnOpts = require('../domain-utils').separatePort(mod.address || ''); + newConnOpts.port = newConnOpts.port || mod.port; + newConnOpts.host = newConnOpts.host || mod.host || 'localhost'; newConnOpts.servername = opts.servername; newConnOpts.data = opts.firstChunk;