-
Notifications
You must be signed in to change notification settings - Fork 0
/
NodeHTTPProxy.js
executable file
·91 lines (75 loc) · 1.95 KB
/
NodeHTTPProxy.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
var httpProxy = require('http-proxy'),
util = require('util');
if ( process.argv.length > 2 && process.argv[2] == '--debug' ){
var debug = true;
}
var https = require('https');
var fs = require('fs');
var options = {
https: {
key: fs.readFileSync('../server.key', 'utf8'),
cert: fs.readFileSync('../server.crt', 'utf8')
}
};
var routefile = fs.readFileSync('routes.json', 'utf8');
var routes = JSON.parse(routefile);
var matchers = [];
routes.forEach(function(route){
var r = new RegExp(route.path.replace(/\//, '\^\\/'));
var matcher = {
'path': route.path,
'host': route.host,
'port': route.port,
'stripBase': route.stripBase,
'r': r
};
matchers.push(matcher);
});
function matches (url) {
for ( var i in matchers ){
if (matchers.hasOwnProperty(i)){
var m = url.match(matchers[i].r);
if (m){
return { 'path': matchers[i].path,
'host': matchers[i].host,
'port': matchers[i].port,
'stripBase': matchers[i].stripBase }
}
}
}
return null;
}
var visualizer = require('./Request-Visualizer.js');
httpProxy.createServer(visualizer.log, function(req,res, proxy){
var m = matches(req.url);
if (m){
if (m.stripBase == true){
req.url = req.url.substr(m.path.length - 1);
}
if ( debug == true ){
console.log("Proxying to: " + m.host + ":" + m.port + req.url);
}
proxy.proxyRequest(req, res, {
host: m.host,
port: m.port
});
}
else{
if ( debug == true ){
console.log("Proxying to: " + req.url);
}
proxy.proxyRequest(req, res, {
host: 'localhost',
port: 80
});
}
}).listen(8000);
var proxyServer = new httpProxy.HttpProxy({
target: {
host: 'localhost',
port: 8000
}
});
https.createServer(options.https, function(req,res){
proxyServer.proxyRequest(req,res);
}).listen(8080);