Skip to content

Instantly share code, notes, and snippets.

@jdrydn
Last active March 13, 2016 13:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdrydn/5b148ec582bc9d6dba6e to your computer and use it in GitHub Desktop.
Save jdrydn/5b148ec582bc9d6dba6e to your computer and use it in GitHub Desktop.
Archived the old Sherlock project
#!/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);

Sherlock

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment