This repository has been archived by the owner on Jun 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
69 lines (56 loc) · 1.84 KB
/
app.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
var express = require('express');
var app = express();
var request = require('request');
var validator = require('validator');
// might be a bug in the request.js library
// proxying over HTTPS always gets error
// so we have to disable TLS checking
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
var TEST_URL = 'http://ipservice.163.com/isFromMainland';
var TEST_STR = 'true';
app.get('/:prot/:addr/:port', function(req, res){
var prot = req.param('prot');
var addr = req.param('addr');
var port = req.param('port');
if ((prot !== 'http' && prot !== 'https') ||
(!validator.isIP(addr) && !validator.isFQDN(addr)) ||
!validator.isInt(port, {min: 1, max: 65535})
) {
res.send(403);
return;
}
var request_options = {
url: TEST_URL,
method: 'GET',
proxy: prot + '://' + addr + ':' + port,
headers: {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Accept-Encoding": "gzip, deflate, sdch",
"Accept-Language": "en-US,en;q=0.8,zh;q=0.6",
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.125 Safari/537.36"
}
};
request(request_options, function(err, resp, body) {
if (err) {
res.send(503);
return;
}
if (resp.statusCode === 200 && body.indexOf(TEST_STR) !== -1) {
res.send(200);
return;
}
res.send(503);
});
});
// default handler
app.use(function(req, res){
res.send(403);
});
var port = Number(process.env.PORT || 5000);
var server = app.listen(port, function() {
console.log("Listening on " + port);
});
process.on('SIGTERM', function () {
console.log("Closing on SIGTERM");
server.close();
});