-
Notifications
You must be signed in to change notification settings - Fork 155
/
ssconf.py
256 lines (207 loc) · 7.96 KB
/
ssconf.py
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#!/usr/bin/env python3
#coding=utf-8
#
#
#https://www.logcg.com
import urllib3
import re
import datetime
import certifi
import codecs
import os
def get_list(list_url):
http = urllib3.PoolManager(
cert_reqs='CERT_REQUIRED', # Force certificate check.
ca_certs=certifi.where(), # Path to the Certifi bundle.
)
data = http.request('GET', list_url, timeout=10).data
return data
def white_list_check():
dnsmasq_china_list = 'https://r0uter.github.io/gfw_domain_whitelist/whitelist.pac'
try:
content = get_list(dnsmasq_china_list)
content = content.decode('utf-8')
f = codecs.open('./list/whitelist', 'w', 'utf-8')
f.write(content)
f.close()
except:
print('Get list update failed,use cache to update instead.')
whitelist = codecs.open('./list/whitelist', 'r', 'utf-8')
whitelistTxt = codecs.open('./list/whitelist.txt', 'w', 'utf-8')
whitelistTxt.write('// updated on ' + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S" + '\n'))
# Write list
for line in whitelist.readlines():
domain = re.findall(r'(?<=")[a-z0-9|\-]+\.\w+', line)
if len(domain) > 0:
whitelistTxt.write('DOMAIN-SUFFIX,%s,DIRECT\n' % (domain[0]))
whitelist.close()
whitelistTxt.close()
def get_gfw_list():
# the url of gfwlist
base_url = 'https://raw.githubusercontent.com/gfwlist/gfwlist/master/gfwlist.txt'
comment_pattern = r'^\!|\[|^@@|^\d+\.\d+\.\d+\.\d+'
domain_pattern = r'([\w\-\_]+\.[\w\.\-\_]+)[\/\*]*'
tmp_file = './list/tmp'
gfwListTxt = codecs.open('./list/gfwlist.txt', 'w+', 'utf-8')
gfwListTxt.write('// SS config file for surge with gfw list \n')
gfwListTxt.write('// updated on ' + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + '\n')
gfwListTxt.write('\n')
try:
data = get_list(base_url)
content = codecs.decode(data, 'base64_codec').decode('utf-8')
# write the decoded content to file then read line by line
tfs = codecs.open(tmp_file, 'w', 'utf-8')
tfs.write(content)
tfs.close()
print('GFW list fetched, writing...')
except:
print('GFW list fetch failed, use tmp instead...')
tfs = codecs.open(tmp_file, 'r', 'utf-8')
# Store all domains, deduplicate records
domainList = []
# Write list
for line in tfs.readlines():
if re.findall(comment_pattern, line):
continue
else:
domain = re.findall(domain_pattern, line)
if domain:
try:
found = domainList.index(domain[0])
except ValueError:
domainList.append(domain[0])
gfwListTxt.write('DOMAIN-SUFFIX,%s,Proxy,force-remote-dns\n' % (domain[0]))
else:
continue
tfs.close()
gfwListTxt.close()
def get_ad_list():
# get list to block most of the ads.
# the url of https://github.com/zhouzhouprogram/Shadowrocket-ADBlock-Rules
outfile = './list/adlist.txt'
tmpfile = './list/adtmp'
baseurl = 'https://raw.githubusercontent.com/zhouzhouprogram/Shadowrocket-ADBlock-Rules/master/sr_proxy_banad.conf'
comment_pattern = r'^\!|\[|^@@|\/|http|\#|\*|\?|\_|^\.|^\d+\.\d+\.\d+\.\d+'
domain_pattern = r'.+,.+,(REJECT|Reject)'
fs = codecs.open(outfile, 'w', 'utf-8')
fs.write('// thx https://github.com/zhouzhouprogram/Shadowrocket-ADBlock-Rules \n')
fs.write('// updated on ' + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + '\n')
fs.write('\n')
try:
data = get_list(baseurl)
content = data.decode('utf-8')
# write the decoded content to file then read line by line
tfs = codecs.open(tmpfile, 'w', 'utf-8')
tfs.write(content)
tfs.close()
print('adlist fetched, writing...')
except:
print('adlist fetch failed, use tmpfile instead...')
# Store all domains, deduplicate records
domainlist = []
# Write list
tfs = codecs.open(tmpfile, 'r', 'utf-8')
for line in tfs.readlines():
line = line.strip()
if re.findall(comment_pattern, line):
continue
if re.findall(domain_pattern, line):
try:
found = domainlist.index(line)
except ValueError:
line = line.replace('Reject', 'REJECT')
domainlist.append(line)
fs.write(line+'\n')
else:
continue
tfs.close()
fs.close()
def gen_gfw_conf():
f = codecs.open('template/ss_gfwlist_conf', 'r', 'utf-8')
gfw_list = codecs.open('list/gfwlist.txt', 'r', 'utf-8')
ad_list = codecs.open('list/adlist.txt', 'r', 'utf-8')
proxy = codecs.open('ServerConfig.txt', 'r', 'utf-8-sig')
file_content = f.read()
ad_list_buffer = ad_list.read()
gfw_list_buffer = gfw_list.read()
proxy_buffer = proxy.read()
gfw_list.close()
ad_list.close()
f.close()
proxy.close()
file_content = file_content.replace('__ADBLOCK__', ad_list_buffer)
file_content = file_content.replace('__GFWLIST__', gfw_list_buffer)
file_content = file_content.replace('__Proxy__', proxy_buffer)
confs = codecs.open('configFileHere/gfwlist.conf', 'w', 'utf-8')
confs.write(file_content)
confs.close()
def gen_white_conf():
cfs = codecs.open('template/ss_whitelist_conf', 'r', 'utf-8')
gfw_list = codecs.open('list/whitelist.txt', 'r', 'utf-8')
ad_list = codecs.open('list/adlist.txt', 'r', 'utf-8')
proxy = codecs.open('ServerConfig.txt', 'r', 'utf8')
file_content = cfs.read()
ad_list_buffer = ad_list.read()
gfw_list_buffer = gfw_list.read()
proxy_buffer = proxy.read()
gfw_list.close()
ad_list.close()
cfs.close()
proxy.close()
file_content = file_content.replace('__ADBLOCK__', ad_list_buffer)
file_content = file_content.replace('__GFWWHITELIST__', gfw_list_buffer)
file_content = file_content.replace('__Proxy__', proxy_buffer)
confs = codecs.open('configFileHere/whitelist.conf', 'w', 'utf-8')
confs.write(file_content)
confs.close()
def gen_geo_ip_white_conf():
cfs = codecs.open('template/ss_geoip_white_conf', 'r', 'utf-8')
ad_list = codecs.open('list/adlist.txt', 'r', 'utf-8')
proxy = codecs.open('ServerConfig.txt', 'r', 'utf8')
file_content = cfs.read()
ad_list_buffer = ad_list.read()
proxy_buffer = proxy.read()
ad_list.close()
cfs.close()
proxy.close()
file_content = file_content.replace('__ADBLOCK__', ad_list_buffer)
file_content = file_content.replace('__Proxy__', proxy_buffer)
confs = codecs.open('configFileHere/geoip_whitelist.conf', 'w', 'utf-8')
confs.write(file_content)
confs.close()
def gen_cn_conf():
cfs = codecs.open('template/ss_cn_conf', 'r', 'utf-8')
ad_list = codecs.open('list/adlist.txt', 'r', 'utf-8')
proxy = codecs.open('ServerConfig.txt', 'r', 'utf8')
file_content = cfs.read()
ad_list_buffer = ad_list.read()
proxy_buffer = proxy.read()
ad_list.close()
cfs.close()
proxy.close()
file_content = file_content.replace('__ADBLOCK__', ad_list_buffer)
file_content = file_content.replace('__Proxy__', proxy_buffer)
confs = codecs.open('configFileHere/cn.conf', 'w', 'utf-8')
confs.write(file_content)
confs.close()
def main():
os.makedirs(os.path.dirname('./list/'), exist_ok=True)
os.makedirs(os.path.dirname('./configFileHere/'), exist_ok=True)
print('Getting GFW list...')
get_gfw_list()
print('Getting AD list...')
get_ad_list()
print('Getting white list...')
white_list_check()
print('Generate config file: gfwlist.conf')
gen_gfw_conf()
print('Generate config file: whitelist.conf')
gen_white_conf()
print('Generate config file: geoip_whitelist.conf')
gen_geo_ip_white_conf()
print('Generate back to china config file: cn.conf')
gen_cn_conf()
print('All done!')
print('Now you need edit config file to add your server information.')
if __name__ == '__main__':
main()