forked from cyfdecyf/cow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pac.js
160 lines (142 loc) · 4.06 KB
/
pac.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
var direct = 'DIRECT';
var httpProxy = 'PROXY';
var directList = [
"baidu.com",
"www.taobao.com"
"", // corresponds to simple host name and ip address
];
var directAcc = {};
for (var i = 0; i < directList.length; i += 1) {
directAcc[directList[i]] = true;
}
// hostIsIP determines whether a host address is an IP address and whether
// it is private. Currenly only handles IPv4 addresses.
function hostIsIP(host) {
var part = host.split('.');
if (part.length != 4) {
return [false, false];
}
var n;
for (var i = 3; i >= 0; i--) {
if (part[i].length === 0 || part[i].length > 3) {
return [false, false];
}
n = Number(part[i]);
if (isNaN(n) || n < 0 || n > 255) {
return [false, false];
}
}
if (part[0] == '127' || part[0] == '10' || (part[0] == '192' && part[1] == '168')) {
return [true, true];
}
if (part[0] == '172') {
n = Number(part[1]);
if (16 <= n && n <= 31) {
return [true, true];
}
}
return [true, false];
}
function host2Domain(host) {
var arr, isIP, isPrivate;
arr = hostIsIP(host);
isIP = arr[0];
isPrivate = arr[1];
if (isPrivate) {
return "";
}
if (isIP) {
return host;
}
var lastDot = host.lastIndexOf('.');
if (lastDot === -1) {
return ""; // simple host name has no domain
}
// Find the second last dot
dot2ndLast = host.lastIndexOf(".", lastDot-1);
if (dot2ndLast === -1)
return host;
return host.substring(dot2ndLast+1);
}
function FindProxyForURL(url, host) {
if (url.substring(0,4) == "ftp:")
return direct;
if (host.indexOf(".local", host.length - 6) !== -1) {
return direct;
}
return (directAcc[host] || directAcc[host2Domain(host)]) ? direct : httpProxy;
}
// Tests
var testData, td, i;
testData = [
{ ip: '127.0.0.1', isIP: true, isPrivate: true },
{ ip: '127.2.1.1', isIP: true, isPrivate: true },
{ ip: '192.168.1.1', isIP: true, isPrivate: true },
{ ip: '172.16.1.1', isIP: true, isPrivate: true },
{ ip: '172.20.1.1', isIP: true, isPrivate: true },
{ ip: '172.31.1.1', isIP: true, isPrivate: true },
{ ip: '172.15.1.1', isIP: true, isPrivate: false },
{ ip: '172.32.1.1', isIP: true, isPrivate: false },
{ ip: '10.16.1.1', isIP: true, isPrivate: true },
{ ip: '12.3.4.5', isIP: true, isPrivate: false },
{ ip: '1.2.3.4.5', isIP: false, isPrivate: false },
{ ip: 'google.com', isIP: false, isPrivate: false },
{ ip: 'www.google.com.hk', isIP: false, isPrivate: false }
];
for (i = 0; i < testData.length; i += 1) {
td = testData[i];
arr = hostIsIP(td.ip);
if (arr[0] !== td.isIP) {
if (td.isIP) {
console.log(td.ip + " is ip");
} else {
console.log(td.ip + " is NOT ip");
}
}
if (arr[0] !== td.isIP) {
if (td.isIP) {
console.log(td.ip + " is private ip");
} else {
console.log(td.ip + " is NOT private ip");
}
}
}
testData = [
// private ip should return direct
{ host: '192.168.1.1', mode: direct},
{ host: '10.1.1.1', mode: direct},
{ host: '172.16.2.1', mode: direct},
{ host: '172.20.255.255', mode: direct},
{ host: '172.31.255.255', mode: direct},
{ host: '192.168.2.255', mode: direct},
// simple host should return direct
{ host: 'localhost', mode: direct},
{ host: 'simple', mode: direct},
// non private ip should return proxy
{ host: '172.32.2.255', mode: httpProxy},
{ host: '172.15.0.255', mode: httpProxy},
{ host: '12.20.2.1', mode: httpProxy},
// host in direct domain/host should return direct
{ host: 'baidu.com', mode: direct},
{ host: 'www.baidu.com', mode: direct},
{ host: 'www.taobao.com', mode: direct},
// host not in direct domain should return proxy
{ host: 'taobao.com', mode: httpProxy},
{ host: 'foo.taobao.com', mode: httpProxy},
{ host: 'google.com', mode: httpProxy},
{ host: 'www.google.com', mode: httpProxy},
{ host: 'www.google.com.hk', mode: httpProxy},
// host in local domain should return direct
{ host: 'test.local', mode: direct},
{ host: '.local', mode: direct},
];
for (i = 0; i < testData.length; i += 1) {
td = testData[i];
if (FindProxyForURL("", td.host) !== td.mode) {
if (td.mode === direct) {
console.log(td.host + " should return direct");
} else {
console.log(td.host + " should return proxy");
}
}
}