-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGFWList2Pac.go
89 lines (81 loc) · 2.05 KB
/
GFWList2Pac.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
package gfwlistpac
import "strings"
func GFWList2Pac(list GFWList) (res string) {
data := list.ListData
dict := generateJsDict(data)
res = generatePac(list.DefaultProxy, dict)
return
}
/**
* convert bool type to string
*/
func bool2String(val bool) ( string) {
if val {
return "true"
}
return "false"
}
func generateJsDict(data GFWListData) (dic string) {
// generate host
dic = "var HOSTS={"
for key, value := range data.AllowedHosts {
dic += "\""+key+"\":"+value.ToJavaScript()+",\n"
}
dic += "};"
// generate keywords
dic += "var KEYWORDS=["
for _, keyword := range data.AllowedKeywords {
dic += "\""+strings.Replace(keyword, "\"", "\\\"", -1)+"\",\n"
}
dic += "];"
// generate exclude keywords
dic += "var X_KEYWORDS=["
for _, keyword := range data.ExcludedKeywords {
dic += "\""+strings.Replace(string(keyword), "\"", "\\\"", -1)+"\",\n"
}
dic += "];"
// delete additional ","
dic = strings.Replace(dic, ",}", "}", -1)
dic = strings.Replace(dic, ",]", "]", -1)
return
}
func generatePac(proxy Proxy, dict string) (pac string) {
pac = "var FindProxyForURL = (function () {\n"
pac += "var PROTOCOL={\"http\":"+PROTOCOL_HTTP+",\"https\":"+PROTOCOL_HTTPS+"};"
pac += dict+"\n"
pac += "var PROXY = '"+proxy.ToString()+"';"
pac += `
var DIRECT = 'DIRECT';
function lookupDomain(host, protocol) {
if (!host) {
return false;
} else if (HOSTS[host]) {
var prot = PROTOCOL[protocol];
if (!prot && prot != 0)
return false;
return HOSTS[host][prot];
} else {
return host.indexOf('.')>0 && lookupDomain(
host.slice(host.indexOf('.') +1),
protocol
);
}
}
function lookupKeyword(url) {
for (var i=0;i<X_KEYWORDS.length;i++) {
if (!X_KEYWORDS[i] instanceof RegExp) {
X_KEYWORDS[i] = new RegExp(X_KEYWORDS[i]);
}
return url.match(X_KEYWORDS[i]);
}
return false;
}
return function (url, host) {
var doProxy = false;
var protocol = url.substring(0, url.indexOf(':'));
doProxy = lookupDomain(host, protocol) || lookupKeyword(url);
return PROXY && doProxy ? PROXY : DIRECT;
}
})();`
return
}