Last active
January 16, 2023 05:04
-
-
Save Swivelgames/1cdf37c3317a4f82db89 to your computer and use it in GitHub Desktop.
Node.js: Simple "Virtual Hosts" with Bouncy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = [ | |
[ 'sterm.bluelogicteam.com', 8881 ], | |
[ /^(www\.)?bluelogicteam.com$/, 8882 ], | |
[ /^(www\.)?toggleswitch.tv$/, 8883 ] | |
]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
forever start "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/listener.js" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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