-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverse-proxy.js
More file actions
113 lines (102 loc) · 3.08 KB
/
reverse-proxy.js
File metadata and controls
113 lines (102 loc) · 3.08 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
var sys = require('sys'),
fs = require('fs'),
util = require('util'),
http = require('http'),
httpProxy = require('http-proxy');
var incomingPort=80;
var defaultHandlerPort=8000;
var routeFile = __dirname + "/proxy-hosts.json";
var hosts = null;
loadHosts(routeFile, hosts);
fs.watchFile(routeFile, function (curr, prev) {
try{
console.log("curr:\n" + util.inspect(curr));
loadHosts(routeFile, hosts);
}
catch(err){
console.log("filewatcher: error parsing " + routeFile + ":\n" + util.inspect(err));
}
});
httpProxy.createServer(function (req, res, proxy) {
console.log("forwarding " + req.headers['host'] + req.url);
for(var i = 0; i < hosts.length; i++){
var host = hosts[i];
if (req.headers['host'].match(host.nameRE)){
console.log(" hostname match: " + host.name);
for(var j = 0; j < host.destinations.length; j++){
var dest = host.destinations[j];
if ( destinationMatch(req, res, proxy, dest)) {
forward(req, res, proxy, dest);
return;
}
}
}
}
// if we got here, there was no match. Forward to default handler
console.log(" no match.");
proxy.proxyRequest(req, res, {
host: 'localhost',
port: 82
});
}).listen(80);
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.write('you have hit the reverse proxy at drewshafer.com<br/><br/>');
res.write('probably you meant to go to one of these sites:<br/>');
for (var i = 0; i < hosts.length; i++){
var host = hosts[i];
for (var j = 0; j < host.destinations.length; j++){
var dest = host.destinations[j];
res.write(" ");
var destHost = 'http://' + host.name;
if(!(typeof dest.url === "undefined")){
destHost += dest.url;
}
res.write('<a href="' + destHost + '">' + destHost + '</a><br/>');
}
}
res.end();
}).listen(82);
function destinationMatch(req, res, proxy, dest) {
if(!(typeof dest.urlRE === "undefined")){
// check against the url
if (req.url.match(dest.urlRE)){
return true;
}
}
else { // no url route defined - just forward to destination
return true;
}
return false;
}
function forward(req, res, proxy, dest) {
var destHost = dest.destinationHost;
if(typeof destHost === "undefined") {destHost = 'localhost';}
proxy.proxyRequest(req, res, {
host: destHost,
port: dest.port
});
}
function loadHosts(fname, ret) {
console.log("loadHosts: " + fname);
fs.readFile(fname, function(err, data) {
if (err) throw err;
try
{
var raw = JSON.parse(data);
for(var i = 0; i < raw.hosts.length; i++){
raw.hosts[i].nameRE = new RegExp(raw.hosts[i].name);
for (var j = 0; j < raw.hosts[i].destinations.length; j++){
var dest = raw.hosts[i].destinations[j];
if(!(typeof dest.url === "undefined")){
dest.urlRE = new RegExp('^' + dest.url);
}
}
}
hosts = raw.hosts;
}
catch(e){
console.log("loadHosts: error parsing " + routeFile + ":\n" + e.message);
}
});
}