-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpac.go
168 lines (149 loc) · 3.15 KB
/
pac.go
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
package main
import (
"bytes"
"fmt"
"html/template"
"strings"
)
func GenPac() []byte {
var pacRawTmpl = `
var direct = 'DIRECT';
var proxyCN = "{{.ProxyDefault}}";
var proxyUS = "{{.ProxyBlock}}";
var directList = [
"",
"{{.DirectList}}"
];
var directAcc = {};
for (var i = 0; i < directList.length; i += 1) {
directAcc[directList[i]] = true;
}
var blockList = [
"{{.BlockList}}"
];
var blockAcc = {};
for (var i = 0; i < blockList.length; i += 1) {
blockAcc[blockList[i]] = true;
}
var topLevel = {
"com": true,
"edu": true,
"gov": true,
"net": true,
"org": true,
"ac": true,
"co": 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;
//use the top domain
return host.substring(dot2ndLast+1);
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) {
if (url.substring(0,4) == "ftp:")
return direct;
if (host.indexOf(".local", host.length - 6) !== -1) {
return direct;
}
var domain = host2Domain(host);
if (host.length == domain.length) {
if (directAcc[host]) {
return direct;
}
if (blockAcc[host]) {
return proxyUS;
}
} else {
if (directAcc[host] || directAcc[domain]) {
return direct;
}
if (blockAcc[host] || blockAcc[domain]) {
return proxyUS;
}
}
return proxyCN;
}
`
pacTmpl, err := template.New("pac").Parse(pacRawTmpl)
if err != nil {
panic(err)
}
//获取gfwlist
gfwlist := getGFWlist()
//获取blocklist并合并
blockList := getBlockList()
blockList = append(gfwlist, blockList...)
//获取directlist
directList := getDirectList()
buf := new(bytes.Buffer)
data := struct {
ProxyBlock string
ProxyDefault string
DirectList string
BlockList string
}{
config.ProxyBlock,
config.ProxyDefault,
strings.Join(directList, "\",\n\""),
strings.Join(blockList, "\",\n\""),
}
if err := pacTmpl.Execute(buf, data); err != nil {
fmt.Println(err)
panic("Error generating pac file")
}
return buf.Bytes()
}