A simple proxy service that takes requests and send them to specific ports based on their hostname.
Nothing special at all & extremely helpful once upon a time ago.
Uses http-proxy and forever.
| #!/bin/bash | |
| # A simple script to control running this NodeJS server! | |
| # Because we're hijacking port 80 we need to be a superhero. | |
| sudo true | |
| TIMESTAMP=$(date +"%s") | |
| BASE=`readlink -f "$0"` | |
| BASE="`dirname "$BASE"`/" | |
| pushd "$BASE" | |
| sudo forever stop sherlock.js | |
| echo "Sherlock stopped" | |
| mkdir ~/.forever/logs -p | |
| mv ~/.forever/forever-sherlock.log ~/.forever/logs/sherlock.$TIMESTAMP.log | |
| mkdir forever/$TIMESTAMP -p | |
| mv forever/*.log forever/$TIMESTAMP/ | |
| sudo forever start -l forever-sherlock.log -o forever/out.log -e forever/error.log sherlock.js | |
| echo "Sherlock started" | |
| popd |
| { | |
| "a-hostname-here.example.com" : { "host" : "127.0.0.1", "port" : 82 }, | |
| "another-hostname-here.example.com" : { "host" : "10.9.10.31", "port" : 80 } | |
| } |
| var http = require('http'), | |
| httpProxy = require('http-proxy'), | |
| router = require('./hosts.json'); | |
| httpProxy.createServer( | |
| function(req, res, proxy) { | |
| var destination = { "host" : "127.0.0.1", "port" : 82 }; | |
| for(var route in router) | |
| if (route == req.headers.host) | |
| destination = router[route]; | |
| proxy.proxyRequest(req, res, destination); | |
| } | |
| ).listen(80); | |
| http.createServer(function (req, res) { | |
| res.writeHead(200, { 'Content-Type': 'application/json' }); | |
| res.write(JSON.stringify(req.headers, true, 2)); | |
| res.end(); | |
| }).listen(82); |
A simple proxy service that takes requests and send them to specific ports based on their hostname.
Nothing special at all & extremely helpful once upon a time ago.
Uses http-proxy and forever.