forked from jackyz/pobi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gfw.js
181 lines (162 loc) · 4.16 KB
/
gfw.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
var fs = require('fs')
, path = require('path')
, vm = require('vm')
, url = require('url')
, debug = require('./debug')('GFW');
/*
color:
white : direct works | i don't know
black : direct fail, retry upstream works
fail : direct fail, retry upstream fail
identifyDomain(dn) : color
identifyDomain(dn, color) : void
identifyIp(dn, ip) : color
identifyIp(dn, ip, color) : void
identifyUrl(dn, url) : color
identifyUrl(dn, url, color) : void
*/
// ----
var hosts = '/list/hosts.whitelist';
var whiteList = '/list/pac.whitelist';
var blackList = '/list/pac.blacklist';
// ---- implements
var init = false;
var black = null;
var white = null;
var cache = {};
function start(config){
black = vm.createContext({});
vm.runInContext(fs.readFileSync(path.dirname(__filename)+blackList, 'utf8'), black, 'blacklist');
white = vm.createContext({});
vm.runInContext(fs.readFileSync(path.dirname(__filename)+blackList, 'utf8'), white, 'whitelist');
cache = {};
init = true;
debug("started");
}
exports.start = start;
function stop(){
init = false;
cache = {};
black = null;
white = null;
debug("stoped");
}
exports.stop = stop;
function identifyDomain(d, v){
if (!init) throw new Error("NOT_INIT_YET");
if (v) { // set
var v0 = (cache[d] || {}).dns;
if (!cache[d]) cache[d] = {};
cache[d].dns = v;
if (v0 != v) debug('identifyDomain(%s,%s)', d, v);
return;
} else if (d) { // get domain
var v = (cache[d] || {}).dns;
if (!v) {
v = checkDomain(d);
if (!cache[d]) cache[d] = {};
cache[d].dns = v;
}
// debug('identifyDomain(%s):%s', d, v);
return v;
} else { // list all
return listAll(cache);
}
}
exports.identifyDomain = identifyDomain;
function identifyIp(d, i, v){
if (!init) throw new Error("NOT_INIT_YET");
if (v) { // set
var v0 = ((cache[d] || {}).ips || {})[i];
if (!cache[d]) cache[d] = {};
if (!cache[d].ips) cache[d].ips = {};
cache[d].ips[i] = v;
if (v0 != v) debug('identifyIp(%s,%s,%s)', d, i, v);
return;
} else if (d && i) { // get
var v = ((cache[d] || {}).ips || {})[i];
if (!v) {
v = checkIp(i);
if (!cache[d]) cache[d] = {};
if (!cache[d].ips) cache[d].ips = {};
cache[d].ips[i] = v;
}
// debug('identifyIp(%d,%s):%s', d, i, v);
return v;
} else if (d) { // list ips by domain
return list(((cache[d] || {}).ips || {}));
} else { // list all
return listAll(cache);
}
}
exports.identifyIp = identifyIp;
function identifyUrl(d, u, v){
if (!init) throw new Error("NOT_INIT_YET");
if (v) { // set
var v0 = ((cache[d] || {}).url || {})[u];
if (!cache[d]) cache[d] = {};
if (!cache[d].url) cache[d].url = {};
cache[d].url[u] = v;
if (v0 = v) debug('identifyUrl(%s,%s,%s)', d, u, v);
return;
} else if (d && u) { // get
var v = ((cache[d] || {}).url || {})[u];
if (!v) {
v = checkUrl(u);
if (!cache[d]) cache[d] = {};
if (!cache[d].url) cache[d].url = {};
cache[d].url[u] = v;
}
// debug('identifyUrl(%s,%s):%s', d, u, v);
return v;
} else if (d) { // list url by domain
return list(((cache[d] || {}).url || {}));
} else { // list all
return listAll(cache);
}
}
exports.identifyUrl = identifyUrl;
function checkDomain(d){
/*
var r = null;
var u = 'http://'+d;
var v1 = white.FindProxyForURL(u, d);
r = (v1 == 'DIRECT') ? 'white' : null;
if (r) return r;
var v2 = black.FindProxyForURL(u, d);
r = (v2 != 'DIRECT') ? 'black' : null;
if (r) return r;
*/
return 'gray';
}
function checkIp(i){
return 'gray';
}
function checkUrl(u){
/*
var r = null;
var d = url.parse(u).hostname;
var v2 = black.FindProxyForURL(u, d);
r = (v2 != 'DIRECT') ? 'black' : null;
if (r) return r;
*/
return 'gray';
}
function list(list){
var s = '';
for (var i in list){
s += list[i] + '\t' + i + '\n';
}
return s;
}
function listAll(cache){
var s = '';
for (var i in cache){
s += '\n' + ((cache[i] || {}).dns || 'gray') + '\t' + i + '\n'; // dns
// s += '# ip:\n';
s += list(((cache[i] || {}).ips || {})); // ips color
// s += '# url:\n'
s += list(((cache[i] || {}).url || {})); // url color
}
return s;
}