Skip to content

Instantly share code, notes, and snippets.

@Swivelgames
Last active January 16, 2023 05:04
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Swivelgames/1cdf37c3317a4f82db89 to your computer and use it in GitHub Desktop.
Save Swivelgames/1cdf37c3317a4f82db89 to your computer and use it in GitHub Desktop.
Node.js: Simple "Virtual Hosts" with Bouncy
module.exports = [
[ 'sterm.bluelogicteam.com', 8881 ],
[ /^(www\.)?bluelogicteam.com$/, 8882 ],
[ /^(www\.)?toggleswitch.tv$/, 8883 ]
];
var HostsFile = './hosts.js';
var bouncy = require('bouncy');
var path = require('path');
var _invalidateRequireCacheForFile = function(filePath){
delete require.cache[path.resolve(filePath)];
return void 0;
};
var server = bouncy(function (req, res, bounce) {
var curHost = req.headers.host;
try {
var hosts = _invalidateRequireCacheForFile(HostsFile) || require(HostsFile);
for(var i=0;i<hosts.length;i++) {
if( (curHost.match(hosts[i][0])||[]).length > 0) {
bounce( hosts[i][1] );
return;
}
}
} catch(e) {
console.log(e);
res.statusCode = 500;
res.end('Error routing to host. Please report to [support at bluelogicteam dot com]');
}
res.statusCode = 404;
res.end('Unable to route to host');
}).listen(80);
#!/bin/sh
forever start "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/listener.js"
#!/bin/sh
if [[ $EUID -ne 0 ]]; then
echo "WARNING: You may be required to run this script with sudo to avoid permissions issues."
fi
SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
npm install -g forever
npm install bouncy
read -p "Start the service? (y/n) " -n 1 -r
echo # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]]
then
exec "$SCRIPTDIR/run.sh"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment