forked from nodester/nodester
/
proxy.js
executable file
·159 lines (141 loc) · 4.69 KB
/
proxy.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env node
var Logger = require('bunyan')
, log = process.log = new Logger({name: "proxy::nodester"})
, lib = require('../lib/lib')
, fs = require('fs')
, path = require('path')
, config = require('../config')
, bouncy = require('bouncy')
, https = false
;
// try {
// var ssl = {
// key: fs.readFileSync(config.opt.ssl_key_file, 'utf8'),
// cert: fs.readFileSync(config.opt.ssl_cert_file, 'utf8')
// };
// } catch (excp) {
// https = false;
// }
log.info('Starting proxy initialization');
// Set some defaults in case that read process fail.
var proxymap = {
"nodester.com":4001,
"api.nodester.com":4001
};
// Avoid DDOS
var bannedIPs = [];
// Ghetto hack for error page
var getErrorPage = function (title, code, error) {
var errorPage = '<html><head><title id="title">{title}</title></head><body><br/><br/><br/><br/><br/><center><img src="http://nodester.com/images/rocket-down.png" alt="logo" /><br/><h1 style ="color:#000;font-family:Arial,Helvetica,sans-serif;font-size:38px;font-weight:bold;letter-spacing:-2px;padding:0 0 5px;margin:0;">{code}</h1><h3 style ="color:#000;font-family:Arial,Helvetica,sans-serif;font-size:24px;font-weight:bold;padding:0 0 5px;margin:0;">{error}</h3></center></body></html>';
return errorPage.replace(/\{title\}/gi, title)
.replace(/\{code\}/gi, code)
.replace(/\{error\}/gi, error)
};
//Update proxymap any time the routing file is updated
fs.watchFile(config.opt.proxy_table_file, function (oldts, newts) {
fs.readFile(config.opt.proxy_table_file, function (err, data) {
if (err) {
log.info('Proxy map failed to update! (read)')
throw err;
} else {
var old = JSON.parse(JSON.stringify(proxymap));
try {
proxymap = JSON.parse(data);
log.info('Proxy map updated')
} catch(e){
log.warn(e)
proxymap = old;
}
}
});
});
function readIP (){
return fs.readFile(__dirname + '/ips.json', function(err, data){
if (err) {
log.info('Can\'t load the ips (read)');
} else {
try {
bannedIPs = JSON.parse(data);
log.info('ips updated');
} catch (exc) {
log.warn(exc);
}
}
});
}
fs.watchFile(__dirname + '/ips.json', function(oldts, newts){
return readIP();
});
readIP();
//Don't crash br0
process.on('uncaughtException', function (err) {
log.fatal(err.stack);
setTimeout(function(){
process.kill(0)
},150)
});
lib.update_proxytable_map(function (err) {
if (err) {
log.fatal('err writing initial proxy file: ' + JSON.stringify(err));
throw err;
} else {
log.info('Initial Proxy file written successfully!');
}
});
function proxy (req, bounce) {
try {
if (!req.headers.host) {
var res = bounce.respond();
res.statusCode = 400;
return res.end(getErrorPage('400 - Invalid request', '400', 'Invalid request'));
}
var host = req.headers.host.replace(/:\d+$/, '');
var route = proxymap[host] || proxymap[''];
// log only urls that are not media files like public folders, css,js
if (!path.extname(req.url)) {
var ip = req.headers['x-real-ip'] || req.headers['x-forwarded-for'] || req.client.remoteAddress
if (bannedIPs.indexOf(ip) > -1) {
var res = bounce.respond()
res.statusCode = 509
return res.end('Bandwith Limit Exceed')
}
log.info(host + ':' + route, ip);
}
req.on('error', function (err) {
var res = bounce.respond();
res.statusCode = 500;
return res.end(getErrorPage('500 - Application error', '503', 'Application error'));
});
if (route) {
// pass headers to the app
delete req.headers.host; // avoiding infiny loops
if (req.headers.connection) req.headers.connection = 'close';
req.headers.Connection = 'close';
var stream = bounce(route, req.headers);
stream.on('error', function (err) {
var res = bounce.respond();
res.statusCode = 503;
return res.end(getErrorPage('503 - Application offline', '503', 'Application offline'));
});
} else {
var res = bounce.respond();
res.statusCode = 404;
return res.end(getErrorPage('404 - Application not found', '404', 'Application not found'));
}
} catch(e){
log.warn(e);
var res = bounce.respond();
res.statusCode = 500;
return res.end(getErrorPage('500 - Application error', '503', 'Application error'));
}
}
bouncy(proxy).on('error', function(){
log.fatal('Bouncy goes nuts');
}).listen(80);
// if (https) {
// log.info('Setting up https server');
// bouncy(ssl, proxy).on('error', function(){
// log.fatal('Bouncy goes nuts');
// }).listen(433);
// }
log.info('Proxy initialization completed');