forked from msl4437/MiWiFi-R1D-ShadowSocks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gfwlist-dnsmasq.py
155 lines (130 loc) · 4.31 KB
/
gfwlist-dnsmasq.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
#!/usr/bin/env python
# coding=utf-8
# copy and paste from
# https://github.com/cokebar/gfwlist2dnsmasq
# https://github.com/JinnLynn/genpac
#
# how to:
# gfwlist-dnsmasq.py -i gfwlist -d 127.0.0.1 -p 5353 -o gfw_ipset.conf
#
#
# just skip -l in normal case, direct fetch from github
# -l="/path/to/gfwlist.txt" if your want use local raw gfwlist
#
# -ipset "your_ipset_name"
# -ipset - skip generate ipset rule
#
# -d "your_dns_ip"
#
# -p "your_dns_port"
#
# -o "output_file"
# -o - just print on stdout
#
#
from __future__ import print_function
import urllib2
import re
import os
import datetime
import base64
import argparse
import codecs
import StringIO
import sys
GFWLIST_URL = \
'https://raw.githubusercontent.com/gfwlist/gfwlist/master/gfwlist.txt'
# Extra Domain;
EX_DOMAIN = [
]
def fetch_gfwlist(local_path):
content = None
if local_path:
try:
with codecs.open(abspath(local_path), 'r', 'utf-8') as fp:
raw = base64.decodestring(fp.read())
content = StringIO.StringIO(raw)
except:
error('base64 decode fail.', exit=True)
else:
try:
resp = urllib2.urlopen(GFWLIST_URL, timeout=10)
content = StringIO.StringIO(resp.read().decode('base64'))
except:
error('fetch gfwlist fail.', exit=True)
return content
def generate(gfwlist, ipset_name, dns_ip, dns_port):
# the url of gfwlist
# match comments/title/whitelist/ip address
comment_pattern = '^\!|\[|^@@|^\d+\.\d+\.\d+\.\d+'
domain_pattern = '([\w\-\_]+\.[\w\.\-\_]+)[\/\*]*'
ip_pattern = re.compile(r'\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b')
domainlist = []
output = []
output.append('# gfwlist rules for dnsmasq')
output.append('# you can get it online')
output.append('# go https://github.com/kiss4437')
output.append('# ' + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
for line in gfwlist.readlines():
if re.findall(comment_pattern, line):
continue
domain = re.findall(domain_pattern, line)
if domain:
try:
_ = domainlist.index(domain[0])
except ValueError:
if ip_pattern.match(domain[0]):
continue
domainlist.append(domain[0])
append_domain(output, domain[0], dns_ip, dns_port)
check_append_ipset(output, ipset_name, domain[0])
for each in EX_DOMAIN:
append_domain(output, each, dns_ip, dns_port)
check_append_ipset(output, ipset_name, each)
output.append('# end\n')
return output
def append_domain(output_list, domain, dns_ip, dns_port):
output_list.append('server=/.%s/%s#%s' % (domain, dns_ip, dns_port))
def check_append_ipset(output_list, ipset_name, domain):
if ipset_name != '-':
output_list.append('ipset=/.%s/%s' % (domain, ipset_name))
def abspath(path):
if not path:
return path
if path.startswith('~'):
path = os.path.expanduser(path)
return os.path.abspath(path)
def error(*args, **kwargs):
print(*args, file=sys.stderr)
if kwargs.get('exit', False):
sys.exit(1)
def get_args():
parser = argparse.ArgumentParser(
description='Generate a list of dnsmasq rules for gfwlist')
parser.add_argument('-l', '--local', type=str)
parser.add_argument('-i', '--ipset', type=str, required=True)
parser.add_argument('-d', '--dnsip', type=str, required=True)
parser.add_argument('-p', '--dnsport', type=int, required=True)
parser.add_argument('-o', '--output', type=str, required=True)
args = parser.parse_args()
local_path = args.local
ipset_name = args.ipset
dns_ip = args.dnsip
dns_port = args.dnsport
output_path = args.output
return local_path, ipset_name, dns_ip, dns_port, output_path
def main():
local_path, ipset_name, dns_ip, dns_port, output_path = get_args()
gfwlist = fetch_gfwlist(local_path)
output = generate(gfwlist, ipset_name, dns_ip, dns_port)
file = '\n'.join(output)
if output_path == '-':
return sys.stdout.write(file)
else:
try:
with codecs.open(abspath(output_path), 'w', 'utf-8') as fp:
fp.write(file)
except Exception:
error('write output file fail.', exit=True)
if __name__ == '__main__':
main()