-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgfwlist_parser.py
149 lines (131 loc) · 4.75 KB
/
gfwlist_parser.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
#
# gfwlist2shadowrocket
# gfwlist_parser
#
# Created by wuwenhan on 18/10/2017.
# 03:26
# Copyright (c) 2017 wuwenhan. All rights reserved.
#
import base64
from os import path
from urllib.parse import urlparse
from urllib.request import urlopen
def get_hostname(something):
try:
# quite enough for GFW
if not something.startswith('http:'):
something = 'http://' + something
r = urlparse(something)
return r.hostname
except Exception as e:
print(e)
return None
def add_domain_to_set(s, something):
hostname = get_hostname(something)
if hostname is not None:
if hostname.startswith('.'):
hostname = hostname.lstrip('.')
if hostname.endswith('/'):
hostname = hostname.rstrip('/')
if hostname:
s.add(hostname)
class GFWListParser(object):
def __init__(self, output, gfwlist_path=None):
super().__init__()
self.output = output
self.gfwlist_path = gfwlist_path
self.base_conf = './base.conf'
self.user_rule = './user_rule.txt'
self.gfwlist_url = 'https://raw.githubusercontent.com/gfwlist' \
'/gfwlist/master/gfwlist.txt '
def generate_conf(self):
gfwlist = self.parse_gfwlist()
user_rule = self.parse_user_rule()
gfwlist = ['DOMAIN-SUFFIX,' + d + ',Proxy' for d in gfwlist]
user_rule = ['DOMAIN-SUFFIX,' + d + ',Proxy' for d in user_rule]
base_conf = self.load_base_config()
conf = base_conf.format(gfwlist='\n'.join(gfwlist),
user_rule='\n'.join(user_rule))
self.save_conf(conf)
def load_user_rule(self):
if path.exists(self.user_rule) and path.isfile(self.user_rule):
with open(self.user_rule) as f:
user_rule = f.read()
user_rule = user_rule.splitlines()
return user_rule
else:
raise Exception('File `user_rule.txt` does not exist.')
def load_gfwlist(self):
if self.gfwlist_path:
if path.exists(self.gfwlist_path) \
and path.isfile(self.gfwlist_path):
with open(self.gfwlist_path, 'rb') as f:
gfwlist = f.read()
gfwlist = base64.decodebytes(gfwlist)
else:
raise Exception(
'File {} does not exist.'.format(self.gfwlist_path))
else:
res = urlopen(self.gfwlist_url)
if res.code == 200:
gfwlist = base64.b64decode(res.read())
else:
raise Exception(
'Get GFWList from GitHub failed, check you network.')
gfwlist = str(gfwlist, encoding='utf-8')
gfwlist = gfwlist.splitlines()
return gfwlist
def load_base_config(self):
if path.exists(self.base_conf) and path.isfile(self.base_conf):
with open(self.base_conf) as f:
base_config = f.read()
return base_config
else:
raise Exception('File `base.conf` does not exist.')
def save_conf(self, conf):
if path.exists(path.abspath(self.output)):
print('File {} already exists, do you want to replace it?'.format(
path.basename(self.output)))
print('-' * 80)
print('(Y)es/(N)o: ', end='')
input_ = input()
if input_ not in ['Y', 'y']:
return
with open(self.output, 'w') as f:
f.write(conf)
print('{} saved.'.format(path.basename(self.output)))
def parse_gfwlist(self):
gfwlist = self.load_gfwlist()
domains = set()
for rule in gfwlist:
if rule.find('.*') >= 0:
continue
elif rule.find('*') >= 0:
rule = rule.replace('*', '/')
if rule.startswith('!'):
continue
elif rule.startswith('['):
continue
elif rule.startswith('@'):
# ignore white list
continue
elif rule.startswith('||'):
add_domain_to_set(domains, rule.lstrip('||'))
elif rule.startswith('|'):
add_domain_to_set(domains, rule.lstrip('|'))
elif rule.startswith('.'):
add_domain_to_set(domains, rule.lstrip('.'))
else:
add_domain_to_set(domains, rule)
return domains
def parse_user_rule(self):
user_rule = self.load_user_rule()
domains = set()
for rule in user_rule:
if rule.startswith('!'):
continue
elif rule == '':
continue
else:
domains.add(rule)
return domains