forked from strongloop/node-foreman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforward.js
47 lines (34 loc) · 885 Bytes
/
forward.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
var url = require('url')
var http = require('http');
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);