一个用来把gfwList里网址转换成IP的脚本
ZZJ于8月17日提出用gfwList的网址通过可信的DNS转换成IP,写入hosts文件进而实现翻墙的方法。鄙人通过Python脚本进行了实现。代码丑陋望大牛指教。
01
import
sys,DNS,base64
02
03
def
splitList(txt):
04
arr
=
txt.split(
"\n"
)
05
06
l
=
[]
07
for
line
in
arr:
08
if
(
not
len
(line)):
#empty line
09
continue
10
if
(line[
0
]
=
=
"!"
):
#Comment line
11
continue
12
elif
(line[
0
:
2
]
=
=
"@@"
):
#Forbidding line
13
continue
14
elif
(line.find(
"/"
)!
=
-
1
or
line.find(
"*"
)!
=
-
1
):
#URL is ignored, only domains left
15
continue
16
17
#In this case, domain name is irrelevant to protocol(http or https)
18
elif
(line[
0
:
2
]
=
=
"||"
):
19
l.append(line[
2
:])
20
elif
(line[
0
]
=
=
"."
):
21
l.append(line[
1
:])
22
else
:
23
l.append(line)
24
25
return
l
26
27
28
29
#Decode and decorate the input string
30
f
=
open
(
"gfwlist.txt"
,
"r"
)
31
txt
=
f.read().replace(
"\n"
,"")
32
txt
=
base64.decodestring(txt)
33
34
domains
=
splitList(txt)
35
36
#Set default DNS server
37
DNSServer
=
[
'8.8.8.8'
]
38
DNS.defaults[
'server'
]
=
DNSServer
39
40
DNS.DiscoverNameServers()
41
42
#These two varible are used to track the percentage of parsing process.
43
l
=
0
44
a
=
len
(domains)
45
46
for
line
in
domains:
47
request
=
DNS.Request()
48
try
:
49
result
=
request.req(name
=
line,qtype
=
"A"
)
50
except
DNS.Base.DNSError:
51
msg
=
line
+
"\tTime Out\n"
52
else
:
53
if
not
len
(result.answers):
54
msg
=
line
+
"\tNo record\n"
55
else
:
56
msg
=
line
+
"\tBingo\n"
57
print
line
+
" "
+
result.answers[
0
][
"data"
]
58
print
>> sys.stderr, line
+
msg, l
*
1.0
/
a,
"%"
代码文件:makeIP.py
文中使用了模块pyDNS,这一非标准模块需要到http://pydns.sourceforge.net/下载。
解压后安装,python setup.py install。
然后运行程序,只需要将标准输出重定向到指定文件即可。python makeIP.py > hosts
接一句Fvck GFW
PS:21:51更新,增加了过滤CNAME记录的功能,解决有跳转网站的解析问题,请下载最新的代码文件
发表评论