/
template.pac
148 lines (125 loc) · 3.28 KB
/
template.pac
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
var PAC_DIRECT = 'DIRECT;';
var PAC_PROXY = 'SOCKS5 {{&proxy}}; SOCKS {{&proxy}}; PROXY {{&proxy}}; DIRECT;';
var hashFuncCount = {{&hashFuncCount}};
var white = {{&white}};
function FindProxyForURL(url, host) {
if (isPlainHostName(host) || host === '127.0.0.1') {
return PAC_DIRECT;
}
if (isIP(host)){
if( isInNet(host, "10.0.0.0", "255.0.0.0") ||
isInNet(host, "172.16.0.0", "255.240.0.0") ||
isInNet(host, "192.168.0.0", "255.255.0.0") ||
isInNet(host, "127.0.0.0", "255.255.255.0")){
return PAC_DIRECT;
}
}
var pos = host.lastIndexOf('.');
var suffix = host.substring(pos + 1);
if (suffix === 'cn' || suffix === 'local') {
return PAC_DIRECT;
}
if(white.length > 0){
if (isDomainSafe(host)) {
return PAC_DIRECT;
}
}
if (isDomainInBloomFilter(host)){
return PAC_PROXY;
}
return PAC_DIRECT;
}
function isDomainSafe(target) {
var pos = target.lastIndexOf('.');
pos = target.lastIndexOf('.', pos - 1);
while (pos > 0) {
suffix = target.substring(pos + 1);
if (white.indexOf(suffix) >= 0) {
return true;
}
pos = target.lastIndexOf('.', pos - 1);
}
return white.indexOf(target) >= 0;
}
function isIP(host){
var isIpV4Addr = /^(\d+.){3}\d+$/;
return isIpV4Addr.test (host);
}
function isDomainInBloomFilter(target){
var pos = target.lastIndexOf('.');
pos = target.lastIndexOf('.', pos - 1);
while (pos > 0) {
suffix = target.substring(pos + 1);
if (bloomFilterTest(suffix)) {
return true;
}
pos = target.lastIndexOf('.', pos - 1);
}
return bloomFilterTest(target);
}
function bloomFilterTest(host) {
var l = bloomFilterLocations(host),
i = -1,
b;
while (++i < hashFuncCount) {
b = l[i];
if (bloomfilter.charAt(b) === '0') {
return false;
}
}
return true;
}
function bloomFilterLocations(v){
var m = bloomfilter.length,
r = [],
a = fnv_1a(v),
b = fnv_1a_b(a),
i = -1,
x = a % m;
while (++i < hashFuncCount) {
r[i] = x < 0 ? (x + m) : x;
x = (x + b) % m;
}
return r;
}
function fnv_1a(v) {
var n = v.length,
a = 2166136261,
c,
d,
i = -1;
while (++i < n) {
c = v.charCodeAt(i);
if (d = c & 0xff000000) {
a ^= d >> 24;
a += (a << 1) + (a << 4) + (a << 7) + (a << 8) + (a << 24);
}
if (d = c & 0xff0000) {
a ^= d >> 16;
a += (a << 1) + (a << 4) + (a << 7) + (a << 8) + (a << 24);
}
if (d = c & 0xff00) {
a ^= d >> 8;
a += (a << 1) + (a << 4) + (a << 7) + (a << 8) + (a << 24);
}
a ^= c & 0xff;
a += (a << 1) + (a << 4) + (a << 7) + (a << 8) + (a << 24);
}
// From http://home.comcast.net/~bretm/hash/6.html
a += a << 13;
a ^= a >> 7;
a += a << 3;
a ^= a >> 17;
a += a << 5;
return a & 0xffffffff;
}
function fnv_1a_b(a) {
a += (a << 1) + (a << 4) + (a << 7) + (a << 8) + (a << 24);
a += a << 13;
a ^= a >> 7;
a += a << 3;
a ^= a >> 17;
a += a << 5;
return a & 0xffffffff;
}
var bloomfilter = '{{&bloomfilter}}';