-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpxy.js
119 lines (108 loc) · 3.23 KB
/
pxy.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// url rewriter
// Unclephil 2011 tc.unclephil.net
// ================================
// Derived from well knowed http-proxy.js
// Is able to manage international domain name
// In this case the configuration must be in idn fom, NOT ASCII
// ============================================================
var http = require('http');
var sys = require('sys');
var fs = require('fs');
var idn = require('punycode');
// CONFIGURATION
// this section contains message
var msgErrorHeader = "My beautiful Rewriter\nIt seems that we have a problem\nPlease contact our Helpdesk\n";
var msgIpNotAllowed = "is not allowed to use this proxy";
var msgErrorReadConfig = "Error during config file access";
var msgErrorReadBlacklist = "";
var msgErrorHostNotDefined = "is not in the configuration file";
//this section contains config
var cfgresturl = './rwconfx';
var cfgport = 8080;
//END CONFIGURATION
var configlist = [];
var iplist = [];
//watch config files , with this rewriter never stopp working
// if config files are not present, server is crashing
//
fs.watchFile('./configlist', function(c,p) { update_configlist(); });
fs.watchFile('./iplist', function(c,p) { update_iplist(); });
function update_configlist() {
configlist = fs.readFileSync('./configlist',encoding='UTF8').split('\n');
}
function update_iplist() {
iplist = fs.readFileSync('./iplist',encoding='UTF8').split('\n')
.filter(function(rx) { return rx.length });
}
//Blacklist
function ip_rejected(ip) {
for (i in iplist) {
if (iplist[i] == ip) {
return true;
}
}
return false;
}
//Match requested url and return Destination
function rewrite_host(host) {
for (i in configlist) {
if (configlist[i].split(',')[0]== host) {
return configlist[i].split(',')[1];
}
}
return "";
}
//send error screen
function deny(response, msg, code) {
response.writeHead(code);
response.write(msgErrorHeader);
response.write(msg+"\n");
response.write((new Date).toGMTString()+"\n");
response.end();
sys.log(msg);
}
update_configlist();
update_iplist();
sys.log("Rewriter starting");
http.createServer(function(request, response) {
var ip = request.headers['x-real-ip'];
var url = '.'+request.url;
if (ip_rejected(ip)) {
msg = "IP " + ip + " " +msgIpNotAllowed;
deny(response, msg, "401");
sys.log(msg);
return;
}
sys.log(url);
// reply to config url
if (url == cfgresturl){
fs.readFile('./configlist', function(error, content) {
if (error) {
msg = msgErrorReadConfig;
deny(response, msg, "500");
return;
}
else {
response.writeHead(200, { 'Content-Type': 'text/plain' });
response.end(content, 'utf-8');
return;
}
});
}
else {
oldhost = request.headers['host'];
oldloc = idn.toUnicode(oldhost);
newloc = rewrite_host(oldloc);
if (newloc=="") {
msg = oldloc + " "+msgErrorHostNotDefined;
deny(response, msg,"404");
sys.log(ip+": "+msg);
return;
}
else {
sys.log(ip + ": " + request.method + " " + oldloc +" to "+newloc);
response.writeHead(301,{'Location':idn.toASCII(newloc), 'Expires': (new Date).toGMTString()});
response.end();
}
}
}).listen(cfgport);