forked from cyfdecyf/cow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pac.js
99 lines (89 loc) · 2.33 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
var direct = 'DIRECT';
var httpProxy = 'PROXY';
var directList = [
"", // corresponds to simple host name
"taobao.com",
"www.baidu.com",
];
var directAcc = {};
for (var i = 0; i < directList.length; i += 1) {
directAcc[directList[i]] = true;
}
var topLevel = {
"net": true,
"org": true,
"edu": true,
"com": true,
"ac": true,
"co": true
};
// only handles IPv4 address now
function hostIsIP(host) {
var parts = host.split('.');
if (parts.length != 4) {
return false;
}
for (var i = 3; i >= 0; i--) {
if (parts[i].length == 0 || parts[i].length > 3) {
return false
}
var n = Number(parts[i])
if (isNaN(n) || n < 0 || n > 255) {
return false;
}
}
return true;
}
function host2domain(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;
var part = host.substring(dot2ndLast+1, lastDot)
if (topLevel[part]) {
var dot3rdLast = host.lastIndexOf(".", dot2ndLast-1)
if (dot3rdLast === -1) {
return host;
}
return host.substring(dot3rdLast+1);
}
return host.substring(dot2ndLast+1);
};
function FindProxyForURL(url, host) {
return (hostIsIP(host) || directAcc[host] || directAcc[host2domain(host)]) ? direct : httpProxy;
};
// Tests
if (FindProxyForURL("", "192.168.1.1") != direct) {
console.log("ip should return direct");
}
if (FindProxyForURL("", "localhost") != direct) {
console.log("localhost should return direct");
}
if (FindProxyForURL("", "simple") != direct) {
console.log("simple host name should return direct");
}
if (FindProxyForURL("", "taobao.com") != direct) {
console.log("taobao.com should return direct");
}
if (FindProxyForURL("", "www.taobao.com") != direct) {
console.log("www.taobao.com should return direct");
}
if (FindProxyForURL("", "www.baidu.com") != direct) {
console.log("www.baidu.com should return direct");
}
if (FindProxyForURL("", "baidu.com") != httpProxy) {
console.log("baidu.com should return proxy");
}
if (FindProxyForURL("", "google.com") != httpProxy) {
console.log("google.com should return proxy");
}
if (hostIsIP("192.168.1.1") !== true) {
console.log("192.168.1.1 is ip");
}
if (hostIsIP("google.com") === true) {
console.log("google.com is not ip");
}