Skip to content

Commit

Permalink
refactor forward.js
Browse files Browse the repository at this point in the history
  • Loading branch information
e-jigsaw committed Apr 1, 2015
1 parent 1978d06 commit 15d9c42
Showing 1 changed file with 45 additions and 45 deletions.
90 changes: 45 additions & 45 deletions forward.js
@@ -1,47 +1,47 @@
var url = require('url')
var http = require('http');
var url = require('url');
var httpProxy = require('http-proxy');

function startForward(proxyPort,proxyHost){

var proxy = httpProxy.createServer(function (req, res, proxy) {

var _url = url.parse(req.url)

var dest = _url.hostname
var port = _url.port || 80
var host = '127.0.0.1';

var target
if(proxyHost=='<ANY>' || proxyHost == dest){

target = {
host: host,
port: port
}

var urlmatch = req.url.match(/http:\/\/[^/]*:?[0-9]*(\/.*)$/);

if(urlmatch){
req.url = urlmatch[1];
}else{
req.url = '/';
}

}else{
target = {
host: dest,
port: port
}
}
proxy.proxyRequest(req,res,target);

});

proxy.listen(proxyPort,function(){
if(process.getuid()==0) process.setuid( process.env.SUDO_USER );
});

};

startForward(process.env.PROXY_PORT,process.env.PROXY_HOST);
function startForward(proxy_port, proxy_host) {

var proxy = httpProxy.createServer(function (req, res, proxy) {

var _url = url.parse(req.url);

var dest = _url.hostname;
var port = _url.port || 80;
var host = '127.0.0.1';

var target;
if(proxy_host === '<ANY>' || proxy_host === dest) {

target = {
host: host,
port: port
};

var urlmatch = req.url.match(/http:\/\/[^/]*:?[0-9]*(\/.*)$/);

if(urlmatch) {
req.url = urlmatch[1];
} else {
req.url = '/';
}

} else {
target = {
host: dest,
port: port
};
}
proxy.proxyRequest(req, res, target);

});

proxy.listen(proxy_port, function(){
if(process.getuid() === 0) {
process.setuid(process.env.SUDO_USER);
}
});
}

startForward(process.env.PROXY_PORT, process.env.PROXY_HOST);

0 comments on commit 15d9c42

Please sign in to comment.