forked from pkrumins/nodejs-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.js
More file actions
executable file
·112 lines (96 loc) · 2.92 KB
/
Copy pathproxy.js
File metadata and controls
executable file
·112 lines (96 loc) · 2.92 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
/*
** Peteris Krumins (peter@catonmat.net)
** http://www.catonmat.net -- good coders code, great reuse
**
** A simple proxy server written in node.js.
**
*/
var http = require('http');
var sys = require('sys');
var fs = require('fs');
var config = require('./config').config;
var blacklist = [];
var iplist = [];
fs.watchFile(config.black_list, function(c,p) { update_blacklist(); });
fs.watchFile(config.allow_ip_list, function(c,p) { update_iplist(); });
function update_list(msg, file, mapf, collectorf) {
fs.stat(file, function(err, stats) {
if (!err) {
sys.log(msg);
fs.readFile(file, function(err, data) {
collectorf(data.toString().split("\n")
.filter(function(rx) { return rx.length })
.map(mapf));
});
}
else {
sys.log("File '" + file + "' was not found.");
collectorf([]);
}
});
}
function update_blacklist() {
update_list(
"Updating host black list.",
config.black_list,
function(rx) { return RegExp(rx) },
function(list) { blacklist = list }
);
}
function update_iplist() {
update_list(
"Updating allowed ip list.",
config.allow_ip_list,
function(ip){return ip},
function(list) { iplist = list }
);
}
function ip_allowed(ip) {
return iplist.some(function(ip_) { return ip==ip_; });
}
function host_allowed(host) {
return !blacklist.some(function(host_) { return host_.test(host); });
}
function deny(response, msg) {
response.writeHead(401);
response.write(msg);
response.end();
}
function server_cb(request, response) {
var ip = request.connection.remoteAddress;
if (!ip_allowed(ip)) {
msg = "IP " + ip + " is not allowed to use this proxy";
deny(response, msg);
sys.log(msg);
return;
}
if (!host_allowed(request.url)) {
msg = "Host " + request.url + " has been denied by proxy configuration";
deny(response, msg);
sys.log(msg);
return;
}
sys.log(ip + ": " + request.method + " " + request.url);
var host = request.headers['host'].split(':');
var proxy = http.createClient(host[1] || 80, host[0])
var proxy_request = proxy.request(request.method, request.url, request.headers);
proxy_request.addListener('response', function(proxy_response) {
proxy_response.addListener('data', function(chunk) {
response.write(chunk, 'binary');
});
proxy_response.addListener('end', function() {
response.end();
});
response.writeHead(proxy_response.statusCode, proxy_response.headers);
});
request.addListener('data', function(chunk) {
proxy_request.write(chunk, 'binary');
});
request.addListener('end', function() {
proxy_request.end();
});
}
update_blacklist();
update_iplist();
sys.log("Starting the proxy server on port '" + config.proxy_port);
http.createServer(server_cb).listen(config.proxy_port);