1 | var url = require('url')
|
2 | var http = require('http');
|
3 | var httpProxy = require('http-proxy');
|
4 |
|
5 | function startForward(proxyPort,proxyHost){
|
6 |
|
7 | var proxy = httpProxy.createServer(function (req, res, proxy) {
|
8 |
|
9 | var _url = url.parse(req.url)
|
10 |
|
11 | var dest = _url.hostname
|
12 | var port = _url.port || 80
|
13 | var host = '127.0.0.1';
|
14 |
|
15 | var target
|
16 | if(proxyHost=='<ANY>' || proxyHost == dest){
|
17 |
|
18 | target = {
|
19 | host: host,
|
20 | port: port
|
21 | }
|
22 |
|
23 | var urlmatch = req.url.match(/http:\/\/[^/]*:?[0-9]*(\/.*)$/);
|
24 |
|
25 | if(urlmatch){
|
26 | req.url = urlmatch[1];
|
27 | }else{
|
28 | req.url = '/';
|
29 | }
|
30 |
|
31 | }else{
|
32 | target = {
|
33 | host: dest,
|
34 | port: port
|
35 | }
|
36 | }
|
37 | proxy.proxyRequest(req,res,target);
|
38 |
|
39 | });
|
40 |
|
41 | proxy.listen(proxyPort,function(){
|
42 | if(process.getuid()==0) process.setuid( process.env.SUDO_USER );
|
43 | });
|
44 |
|
45 | };
|
46 |
|
47 | startForward(process.env.PROXY_PORT,process.env.PROXY_HOST);
|