-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
156 lines (152 loc) · 4.15 KB
/
main.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
package main
import (
"encoding/base64"
"io/ioutil"
"net/http"
"regexp"
"strings"
)
func makeData(reg *regexp.Regexp) []byte {
rsp, err := http.Get("https://raw.githubusercontent.com/gfwlist/gfwlist/master/gfwlist.txt")
if err != nil {
panic(err)
}
rspData, err := ioutil.ReadAll(rsp.Body)
if err != nil {
panic(err)
}
strData, err := base64.StdEncoding.DecodeString(string(rspData))
if err != nil {
panic(err)
}
lines := strings.Split(string(strData), "\n")
directMap := make(map[string]bool, len(lines))
//value true means suffix,false means all match
proxyMap := make(map[string]bool, len(lines))
unknownMap := make(map[string]bool, len(lines))
for _, line := range lines {
if len(line) < 2 {
unknownMap[line] = true
continue
}
switch line[0] {
case '!', '[':
break
case '|':
if line[1] == '|' {
//||: Stands for matching specific URI, in such a case, no need to write down scheme, e.g. ||example.com will match (replace www with any subdomain):
//http://example.com
//http://www.example.com
//https://example.com
//https://www.example.com
//
//It will NOT match:
//
//http://anotherexample.com
//https://anotherexample.com
//http://example.com.co
//https://example.com.co
if name := reg.FindString(line); name != "" {
proxyMap[name] = true
} else {
unknownMap[line] = true
}
} else {
//|: Stands for matching from beginning (In URI, it's scheme): e.g.|http://example.com will match:
//http://example.com
//http://example.com/page
//http://example.com.co
//
//It will NOT match (replace www with any subdomain):
//
//http://www.example.com
//https://example.com/page
//https://example.com
//https://www.example.com
//https://example.com.co
if name := reg.FindString(line); name != "" {
proxyMap[name] = false
} else {
unknownMap[line] = false
}
}
break
case '.':
//.example.com
//It means example.com is suffering a block while http://sub.example.com is not brought in.
if name := reg.FindString(line); name != "" {
proxyMap[name] = true
} else {
unknownMap[line] = true
}
break
case '@':
//@@: Stands for whitelist rules.
//Although GFWList was designed to conform to the GFW mechanisms,
//it still has consideration of whitelist since sometimes there are some exceptions under special circumstances.
// @@|http://blog.ontrac.com
if len(line) > 11 && strings.Contains(line[11:], "/") {
unknownMap[line] = true
break
}
if name := reg.FindString(line); name != "" {
directMap[name] = true
} else {
unknownMap[line] = false
}
break
default:
if name := reg.FindString(line); name != "" {
proxyMap[name] = false
} else {
unknownMap[line] = false
}
break
}
}
sb := strings.Builder{}
sb.Write(initData)
sb.WriteString("[Rule]\n")
for line, _ := range directMap {
sb.WriteString("DOMAIN,")
sb.WriteString(line)
sb.WriteString(",DIRECT\n")
}
for line, b := range proxyMap {
if b {
sb.WriteString("DOMAIN-SUFFIX,")
} else {
sb.WriteString("DOMAIN,")
}
sb.WriteString(line)
sb.WriteString(",PROXY\n")
}
sb.WriteString("[Host]\nlocalhost = 127.0.0.1\n")
return []byte(sb.String())
}
var initData = []byte(`# Shadowrocket: 2022-03-25 16:07:46
[General]
bypass-system = true
skip-proxy = 192.168.0.0/16, 10.0.0.0/8, 172.16.0.0/12, localhost, *.local, captive.apple.com
tun-excluded-routes = 10.0.0.0/8, 100.64.0.0/10, 127.0.0.0/8, 169.254.0.0/16, 172.16.0.0/12, 192.0.0.0/24, 192.0.2.0/24, 192.88.99.0/24, 192.168.0.0/16, 198.51.100.0/24, 203.0.113.0/24, 224.0.0.0/4, 255.255.255.255/32
dns-server = system
ipv6 = true
prefer-ipv6 = false
dns-fallback-system = false
dns-direct-system = false
icmp-auto-reply = true
always-reject-url-rewrite = false
private-ip-answer = true
dns-direct-fallback-proxy = true
`)
func main() {
reg := regexp.MustCompile(`[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+`)
if reg == nil {
panic("reg is nil")
}
http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
rs := makeData(reg)
writer.Write(rs)
})
http.ListenAndServe(":8082", nil)
}