forked from jackyz/pobi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wpad.js
108 lines (95 loc) · 2.55 KB
/
wpad.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
var fs = require('fs')
, path = require('path')
, url = require('url')
, http = require('http')
, gfw = require('./gfw')
, server = require('./server')
, debug = require('./debug')('WPAD');
// ----
var pacTmpl = '/tmpl/pac.tmpl'; // all-by-proxy pac template
var wpad_path = '/wpad.da';
// ----
function serveWpad(req, res, cb){
var self = this;
if (req.method == 'GET' && wpad_path == req.url.substr(0, wpad_path.length)){
res.writeHead(200, {
'Content-Type': 'application/x-ns-proxy-autoconfig; charset=UTF-8',
'Cache-Control': 'no-cache'
});
res.write(self.pac);
res.end();
debug('%s : %s %s ok', req.ip, req.method, req.url);
cb();
} else if (req.method == 'GET' && req.url.substr(0,4) == '/gfw'){
var o = url.parse(req.url.substr(4), true);
var code = 200;
var data = '';
switch (o.pathname) {
case '/identifyDomain' :
var d = o.query.domain || undefined;
var v = o.query.val || undefined;
data = gfw.identifyDomain(d,v);
break;
case '/identifyIp' :
var d = o.query.domain || undefined;
var i = o.query.ip || undefined;
var v = o.query.val || undefined;
data = gfw.identifyIp(d,i,v);
break;
case '/identifyUrl' :
var d = o.query.domain || undefined;
var u = o.query.url || undefined;
var v = o.query.val || undefined;
data = gfw.identifyUrl(d,u,v);
break;
default :
code = 404;
}
res.writeHead(code);
res.end(data);
cb();
} else {
res.writeHead(404);
res.end();
cb(404);
}
}
// ----
var wpad = null;
function start(config){
var onListening = function(){
debug("listening on %s:%s",
this.address().address, this.address().port);
};
var onRequest = function(req, res){
req.ip = req.connection.remoteAddress;
serveWpad.call(this, req, res, function(e){
if (e) debug('%s : %s %s fail %j', req.ip, req.method, req.url, e);
});
};
var onClose = function(){
debug("closed %j", this.address());
};
var onError = function(err){
debug("error %j", err);
};
// init
var proxy = config.proxy || "DIRECT";
var pac = server.tmpl(fs.readFileSync(path.dirname(__filename)+pacTmpl, 'utf8'), {proxy:proxy});
wpad = http.createServer();
wpad.on('listening', onListening);
wpad.on('request', onRequest);
wpad.on('close', onClose);
wpad.on('error', onError);
wpad.pac = pac;
var o = url.parse(config.listen);
var host = o.hostname || '0.0.0.0';
var port = o.port || 80; // wpad must on 80
//
wpad.listen(port, host);
}
exports.start = start;
function stop(){
wpad.close();
}
exports.stop = stop;