-
Notifications
You must be signed in to change notification settings - Fork 14
/
gfwlist2acl.py
executable file
·167 lines (118 loc) · 3.84 KB
/
gfwlist2acl.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
#!/usr/bin/env python3
"""Convert gfwlist format to ssr compatible acl file"""
import fileinput
import re
from datetime import datetime, timedelta, tzinfo
from itertools import chain
ACL_TEMPLATE = """\
#
# Home: https://github.com/NateScarlet/gfwlist.acl
# Date: {date}
# URL: https://raw.githubusercontent.com/NateScarlet/gfwlist.acl/master/{filename}
#
[{default_action}]
[proxy_list]
{blacklist}
[bypass_list]
{whitelist}
"""
class ChinaTimezone(tzinfo):
"""Timezone of china."""
def tzname(self, dt):
return "UTC+8"
def utcoffset(self, dt):
return timedelta(hours=8)
def dst(self, dt):
return timedelta()
def get_regexp(line):
"""Get regular expression from a line.
Returns:
str
"""
# Escape, not use `re.escape` since it behavior changes in diffrent python version
ret = re.sub(r"[.*+?^${}()|[\]\\]", lambda x: "\\{}".format(x.group(0)), line)
# https://adblockplus.org/filters#basic
ret = ret.replace(r"\*", ".+")
# https://adblockplus.org/filters#separators
ret = ret.replace(r"\^", r"([^a-zA-Z0-9_-.%]|$)")
# https://adblockplus.org/filters#anchors
ret = re.sub(r"^\\\|\\\|(https?\??://)?", r"(^|\.)", ret)
ret = re.sub(r"^\\\|(https?\??://)?", "^", ret)
ret = re.sub(r"\\\|$", "$", ret)
return ret
def _split_long_regexp(regexp):
match = len(regexp) > 80 and re.match(r"(.*)\((.*)\)(.*)", regexp)
if not match:
return [regexp]
ret = []
prefix = match.group(1)
items = match.group(2).split("|")
suffix = match.group(3)
size = 10
for i in range(0, len(items), size):
chunk = items[i : i + size]
ret.append("{}({}){}".format(prefix, "|".join(chunk), suffix))
return ret
def get_rules(regexp):
"""Get acl rules from regular expression.
Returns:
List[str]
"""
regexp = re.sub(r"\^?https?\??://", "^", regexp)
regexp = re.sub(r"(\.\*)+$", "", regexp)
regexp = re.sub(r"/$", "$", regexp)
# Exclude pathname rule, since ssr only accept domain match
if "/" in re.sub(
r"(\[\^.*)/(.*\])", lambda match: match.group(1) + match.group(2), regexp
):
return []
ret = _split_long_regexp(regexp)
# SSR can not deal with too long rule in one line
ret = [i for i in ret if len(i) < 500]
return ret
def convert_line(line):
"""Convert a input line to acl rules
Returns:
List[str]
"""
if not line:
return []
line = line.replace(r"\/", "/")
# IP
match = re.match(
r"^\|*(?:https?://)?(\d{,3}\.\d{,3}\.\d{,3}\.\d{,3}(?::\d{1,5})?)/*$", line
)
if match:
return [match.group(1)]
# https://adblockplus.org/filters#regexps
if line.startswith("/") and line.endswith("/"):
return get_rules(line[1:-1])
return get_rules(get_regexp(line))
def get_acl_rules(_content):
"""Get acl rules from gfwlist
Args:
_content (Iterable[str]): gfwlist data
Returns:
(List[str], List[str]): (blacklist, whitelist)
"""
content = _content
content = (i.strip() for i in content)
# https://adblockplus.org/filters#comments
content = [i for i in content if not i.startswith(("!", "[AutoProxy"))]
# https://adblockplus.org/filters#whitelist
blacklist = chain(*(convert_line(i) for i in content if not i.startswith("@@")))
whitelist = chain(*(convert_line(i[2:]) for i in content if i.startswith("@@")))
return list(blacklist), list(whitelist)
def main():
blacklist, whitelist = get_acl_rules(fileinput.input())
print(
ACL_TEMPLATE.format(
date=datetime.now(ChinaTimezone()).isoformat(),
filename="gfwlist.acl",
default_action="bypass_all",
blacklist="\n".join(blacklist),
whitelist="\n".join(whitelist),
)
)
if __name__ == "__main__":
main()