Skip to content

Instantly share code, notes, and snippets.

@mbrevoort
Created February 25, 2012 00:19
Show Gist options
  • Save mbrevoort/1904823 to your computer and use it in GitHub Desktop.
Save mbrevoort/1904823 to your computer and use it in GitHub Desktop.
Simple proxy/service registration
var DNode = require('dnode'),
argv = require('optimist')
.usage('Usage: $0 --p [num] --pp [num] --name [str]')
.demand(['pp', 'name'])
.argv,
sprintf = require('sprintf').sprintf,
http = require('http'),
util = require('./util'),
DNode = require('dnode'),
logf = require('./util').logf,
proxyPort = argv.pp,
port = argv.p || 8000,
clientName = argv.name;
var server = http.createServer(function(req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write('request successfully proxied! ' + clientName + ' on port ' + port + '\n' + JSON.stringify(req.headers, true, 2));
res.end();
});
server.once('listening', function() {
logf("client listening on %s", port);
});
server.on('error', handleBindError);
server.listen(port);
function handleBindError(error) {
if (error.code === 'EADDRINUSE' || error.code === 'EACCES') {
logf('Failed to bind to %s (%s) trying %s', port++, error.code, port);
server.listen(port);
}
}
DNode.connect ({
port: proxyPort,
reconnect: 500
}, function (remote) {
remote.register(clientName, port, function (result) {
logf("Registration result successful? %s", result);
});
});

An idea I'm tinkering with, services that would register themselves with a reverse proxy

Start the proxy

$ node proxy.js --p 9000 --rp 9001
proxy listening on 9000
proxy registry listening on 9001

Start a client

$ node client --pp 9001 --name "foo.com" --p 8001
client listening on 8001
Registration result successful? true

Curl the proxy:

$ curl http://foo.com:9000
request successfully proxied! foo.com on port 8001
{
  "user-agent": "curl/7.21.4 (universal-apple-darwin11.0) libcurl/7.21.4 OpenSSL/0.9.8r zlib/1.2.5",
  "host": "foo.com:9000",
  "accept": "*/*",
  "x-forwarded-for": "127.0.0.1",
  "x-forwarded-port": "55828",
  "x-forwarded-proto": "http",
  "connection": "keep-alive"
}% 

Start another client but steal the route:

$ node client --pp 9001 --name "foo.com" --p 8002
client listening on 8002
Registration result successful? true

Curl the proxy again:

$ curl http://foo.com:9000
request successfully proxied! foo.com on port 8002
{
  "user-agent": "curl/7.21.4 (universal-apple-darwin11.0) libcurl/7.21.4 OpenSSL/0.9.8r zlib/1.2.5",
  "host": "foo.com:9000",
  "accept": "*/*",
  "x-forwarded-for": "127.0.0.1",
  "x-forwarded-port": "55828",
  "x-forwarded-proto": "http",
  "connection": "keep-alive"
}% 
var http = require('http'),
httpProxy = require('http-proxy'),
argv = require('optimist')
.usage('Usage: $0 --p [port] --rp [registryPort]')
.demand(['p', 'rp'])
.argv,
sprintf = require('sprintf').sprintf,
DNode = require('dnode'),
logf = require('./util').logf,
proxyPort = argv.p || 80,
dnodePort = argv.rp || 81;
var options = {
hostnameOnly: true,
router: {
'foo.com': ['127.0.0.1:8001'],
'bar.com': ['127.0.0.1:8002']
}
}
//
// the front facing proxy server
//
var proxyServer = httpProxy.createServer(options);
proxyServer.listen(proxyPort);
logf("proxy listening on %s", proxyPort);
//
// the proxy register
//
var dnodeServer = DNode({
register : function (name, port, host /*optional*/, callback) {
if( typeof host === 'function' ) {
callback = host;
host = "localhost";
}
logf('registering member of %s to %s:%s', name, host, port);
options.router[name] = sprintf('%s:%s', host, port);
proxyServer.proxy.proxyTable.setRoutes(options.router);
//console.log(proxyServer.proxy.proxyTable);
callback(true);
},
});
dnodeServer.listen(dnodePort);
logf("proxy registry listening on %s", dnodePort);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment