Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
45 additions
and
45 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |