forked from Gnnng/gfw_whitelist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist_ip.py
104 lines (97 loc) · 2.18 KB
/
list_ip.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
fakeIpList = [
'74.125.127.102',
'74.125.155.102',
'74.125.39.102',
'74.125.39.113',
'209.85.229.138',
'128.121.126.139',
'159.106.121.75',
'169.132.13.103',
'192.67.198.6',
'202.106.1.2',
'202.181.7.85',
'203.161.230.171',
'203.98.7.65',
'207.12.88.98',
'208.56.31.43',
'209.145.54.50',
'209.220.30.174',
'209.36.73.33',
'211.94.66.147',
'213.169.251.35',
'216.221.188.182',
'216.234.179.13',
'243.185.187.39',
'37.61.54.158',
'4.36.66.178',
'46.82.174.68',
'59.24.3.173',
'64.33.88.161',
'64.33.99.47',
'64.66.163.251',
'65.104.202.252',
'65.160.219.113',
'66.45.252.237',
'72.14.205.104',
'72.14.205.99',
'78.16.49.15',
'8.7.198.45',
'93.46.8.89'
]
def ip2int(ipstr):
intlist = ipstr.split('.')
ret = 0
for i in range(4):
ret = ret * 256 + int(intlist[i])
return ret
def final_list():
fileobj = open("data/cn_ip_range.txt", "r")
content = ''
if fileobj:
list_result = []
lines_list = [line.rstrip('\n').split(' ') for line in fileobj]
#list_result = [ "0x%x:%s," % (int(line[0]),int(line[1])) for line in lines_list ]
#'''
list_result.append('{')
start_num = 0
comma = ''
for line in lines_list:
while (int(line[0]) >> 24) > start_num:
start_num += 1
list_result.append('},{')
comma = ''
list_result.append("%s0x%x:%s" % ( comma, int(line[0])/256, int(line[1])/256 ))
comma = ','
list_result.append('}')
#'''
content = ''.join(list_result)
content = '[\n' + content + "\n]"
return content
def center_list():
fileobj = open("data/cn_ip_range.txt", "r")
master_net = set()
content = ''
if fileobj:
list_result = []
lines_list = [line.rstrip('\n').split(' ') for line in fileobj]
for line in lines_list:
if int(line[1]) < (1 << 14):
master_net.add( int(int(line[0]) >> 14) )
master_net = list(master_net)
master_net.sort()
list_result = ['0x%x:1,' % s for s in master_net]
content = ''.join(list_result)
content = '{\n' + content[:-1] + "\n}"
return content
def fake_list():
content = ''
list_result = [ "0x%x," % int(ip2int(ip)) for ip in fakeIpList ]
content = ''.join(list_result)
content = '[\n' + content[:-1] + "\n]"
return content
def main():
print center_list()
if __name__ == '__main__':
main()