added way to specify proxy destination

This commit is contained in:
tigerbot 2017-05-24 13:05:37 -06:00
parent be67f04afa
commit 21a77ad10a
5 changed files with 31 additions and 12 deletions

View File

@ -71,7 +71,7 @@ function readConfigAndRun(args) {
config = recase.camelCopy(config); config = recase.camelCopy(config);
if (!config.dns) { 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. // 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. // 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 = {}; config.tcp = {};
} }
if (!config.http) { if (!config.http) {
config.http = { modules: { name: 'proxy', port: 3000 } }; config.http = { modules: [{ name: 'proxy', domains: ['*'], port: 3000 }] };
} }
if (!config.tls) { if (!config.tls) {
console.log("TODO: tls: { modules: { name: 'acme', email: 'foo@bar.com', domains: [ '*' ] } }"); console.log("TODO: tls: { modules: { name: 'acme', email: 'foo@bar.com', domains: [ '*' ] } }");

View File

@ -35,7 +35,7 @@ http:
from: /nowhere/in/particular from: /nowhere/in/particular
to: /just/an/example to: /just/an/example
- name: proxy - name: proxy
address: '127.0.0.1:3001' port: 3001
modules: modules:
- name: redirect - name: redirect
@ -47,7 +47,8 @@ http:
- name: proxy - name: proxy
domains: domains:
- localhost.daplie.me - localhost.daplie.me
address: '127.0.0.1:4000' host: locahost
port: 4000
- name: static - name: static
domains: domains:
- '*.localhost.daplie.me' - '*.localhost.daplie.me'

View File

@ -74,22 +74,36 @@ module.exports.create = function (deps, config) {
} }
function dnsListener(msg) { function dnsListener(msg) {
var dgram = require('dgram'); if (!Array.isArray(config.dns.modules)) {
var socket = dgram.createSocket('udp4'); return;
socket.send(msg, config.dns.proxy.port, config.dns.proxy.address || '127.0.0.1'); }
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) { function createTcpForwarder(mod) {
return function (conn) { var dest = require('./domain-utils').separatePort(mod.address || '');
var newConnOpts = 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) { ['remote', 'local'].forEach(function (end) {
['Family', 'Address', 'Port'].forEach(function (name) { ['Family', 'Address', 'Port'].forEach(function (name) {
newConnOpts[end+name] = conn[end+name]; newConnOpts[end+name] = conn[end+name];
}); });
}); });
deps.proxy(conn, newConnOpts); deps.proxy(conn, Object.assign({}, dest, newConnOpts));
}; };
} }

View File

@ -224,7 +224,9 @@ module.exports.create = function (deps, conf, greenlockMiddleware) {
opts.firstChunk = Buffer.concat([head, body]); 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.servername = separatePort(headers.host).host;
newConnOpts.data = opts.firstChunk; newConnOpts.data = opts.firstChunk;

View File

@ -186,7 +186,9 @@ module.exports.create = function (deps, config, netHandler) {
}); });
function proxy(socket, opts, mod) { 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.servername = opts.servername;
newConnOpts.data = opts.firstChunk; newConnOpts.data = opts.firstChunk;