Skip to content

Instantly share code, notes, and snippets.

@mrajagopal
Last active August 29, 2015 14:08
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 mrajagopal/c88ac98b2abe1081c84e to your computer and use it in GitHub Desktop.
Save mrajagopal/c88ac98b2abe1081c84e to your computer and use it in GitHub Desktop.
LineRate snippet: Forward Proxy HTTP redirect
/*************************************************************************************************
* Description:
* HTTP redirection in a forward-proxy context. A forward-proxy is useful for filter
* connections particularly outbound to the internet for clients sitting behind a
* firewall for example. In this example below, any user HTTP request from clients
* results in a redirection to the specified site (in this case a bland example.com site).
* More sophistication can be added like a login page for authenticating outbound/internet
* access and other enterprise policy enforcement.
*/
"use strict";
var fpm = require('lrs/forwardProxyModule');
var redirectHost = 'www.example.com';
// Handler for client requests - this pulls out the host field value
function onClientReq(servReq, servResp, next) {
// extract the full URL request from the header
var requestedHost = servReq.headers.host || '';
/* Test if the URL request matches the redirection.
* Forward the request along on such a match thereby preventing
* a redirect loop.
*/
if(requestedHost == redirectHost) {
next();
}
else {
servResp.setHeader('Content-Type', 'text/html');
servResp.writeHead(301, {'Location' : 'http://' + redirectHost, 'Server' : 'LineRate'});
servResp.end();
}
}
var onExists = function(fp) {
fp.on('request', onClientReq);
};
fpm.on('exist', 'fp1', onExists);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment