-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgenerate_rule_set.py
196 lines (176 loc) · 6.31 KB
/
generate_rule_set.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
import math
import re
import maxminddb
import requests
import json
import os
from aggregate6 import aggregate
dnsmasq_china_list = [
"https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/accelerated-domains.china.conf",
"https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/apple.china.conf",
"https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/google.china.conf"
]
chnroutes2 = [
"https://raw.githubusercontent.com/misakaio/chnroutes2/master/chnroutes.txt"
]
apnic = [
"https://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest"
]
maxmind = [
"https://raw.githubusercontent.com/Dreamacro/maxmind-geoip/release/Country.mmdb"
]
adguard = [
"https://adguardteam.github.io/AdGuardSDNSFilter/Filters/filter.txt"
]
output_dir = "./rule-set"
def convert_dnsmasq(url: str) -> str:
r = requests.get(url)
domain_suffix_list = []
if r.status_code == 200:
lines = r.text.splitlines()
for line in lines:
if not line.startswith("#"):
domain = re.match(r"server=\/(.*)\/(.*)", line)
if domain:
domain_suffix_list.append(domain.group(1))
result = {
"version": 1,
"rules": [
{
"domain_suffix": []
}
]
}
result["rules"][0]["domain_suffix"] = domain_suffix_list
filepath = os.path.join(output_dir, url.split("/")[-1] + ".json")
with open(filepath, "w") as f:
f.write(json.dumps(result, indent=4))
return filepath
def convert_chnroutes2(url: str) -> str:
r = requests.get(url)
ip_cidr_list = []
if r.status_code == 200:
lines = r.text.splitlines()
for line in lines:
if not line.startswith("#"):
ip_cidr_list.append(line)
result = {
"version": 1,
"rules": [
{
"ip_cidr": []
}
]
}
result["rules"][0]["ip_cidr"] = ip_cidr_list
filepath = os.path.join(output_dir, url.split("/")[-1] + ".json")
with open(filepath, "w") as f:
f.write(json.dumps(result, indent=4))
return filepath
def convert_apnic(url: str, country_code: str, ip_version: str) -> str:
r = requests.get(url)
ip_cidr_list = []
if r.status_code == 200:
lines = r.text.splitlines()
for line in lines:
if not line.startswith("#"):
if line.split("|")[1] == country_code and line.split("|")[2] == ip_version:
if ip_version == "ipv4":
ip_cidr_list.append(line.split(
"|")[3] + "/" + str(32 - int(math.log2(int(line.split("|")[4])))))
else:
ip_cidr_list.append(line.split(
"|")[3] + "/" + line.split("|")[4])
result = {
"version": 1,
"rules": [
{
"ip_cidr": []
}
]
}
result["rules"][0]["ip_cidr"] = aggregate(ip_cidr_list)
filepath = os.path.join(output_dir, "apnic-" +
country_code.lower() + "-" + ip_version + ".json")
with open(filepath, "w") as f:
f.write(json.dumps(result, indent=4))
return filepath
def convert_maxmind(url: str, country_code: str, ip_version: str) -> str:
r = requests.get(url)
with open("Country.mmdb", "wb") as f:
f.write(r.content)
f.close()
reader = maxminddb.open_database("Country.mmdb")
ip_cidr_list = []
for cidr, info in reader.__iter__():
if info.get("country") is not None:
if info["country"]["iso_code"] == country_code:
if ip_version == "ipv4" and cidr.version == 4:
ip_cidr_list.append(str(cidr))
elif ip_version == "ipv6" and cidr.version == 6:
ip_cidr_list.append(str(cidr))
elif info.get("registered_country") is not None:
if info["registered_country"]["iso_code"] == country_code:
if ip_version == "ipv4" and cidr.version == 4:
ip_cidr_list.append(str(cidr))
elif ip_version == "ipv6" and cidr.version == 6:
ip_cidr_list.append(str(cidr))
reader.close()
result = {
"version": 1,
"rules": [
{
"ip_cidr": []
}
]
}
result["rules"][0]["ip_cidr"] = aggregate(ip_cidr_list)
filepath = os.path.join(output_dir, "maxmind-" +
country_code.lower() + "-" + ip_version + ".json")
with open(filepath, "w") as f:
f.write(json.dumps(result, indent=4))
return filepath
def get_adguard(url: str) -> str:
r = requests.get(url)
filepath = os.path.join(output_dir, url.split("/")[-1])
with open(filepath, "wb") as f:
f.write(r.content)
return filepath
def main():
files = []
files_adguard = []
os.mkdir(output_dir)
for url in dnsmasq_china_list:
filepath = convert_dnsmasq(url)
files.append(filepath)
for url in chnroutes2:
filepath = convert_chnroutes2(url)
files.append(filepath)
for url in apnic:
filepath = convert_apnic(url, "CN", "ipv4")
files.append(filepath)
filepath = convert_apnic(url, "CN", "ipv6")
files.append(filepath)
for url in maxmind:
filepath = convert_maxmind(url, "CN", "ipv4")
files.append(filepath)
filepath = convert_maxmind(url, "CN", "ipv6")
files.append(filepath)
for url in adguard:
filepath = get_adguard(url)
files_adguard.append(filepath)
print("rule-set source:")
for filepath in files:
print(filepath)
for filepath in files_adguard:
print(filepath)
for filepath in files:
srs_path = filepath.replace(".json", ".srs")
os.system("sing-box rule-set compile --output " +
srs_path + " " + filepath)
for filepath in files_adguard:
srs_path = filepath + ".srs"
os.system("sing-box rule-set convert --type adguard --output " +
srs_path + " " + filepath)
if __name__ == "__main__":
main()