-
Notifications
You must be signed in to change notification settings - Fork 8
/
get-china-ip.py
42 lines (39 loc) · 1.63 KB
/
get-china-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
import requests
import codecs
from IPy import IPSet, IP
from contextlib import closing
import math
ipset = IPSet()
str_update = ""
print ("read ip list from apnic...", end="")
with closing(requests.get('http://ftp.apnic.net/stats/apnic/delegated-apnic-latest', stream=True)) as r:
r.encoding='utf-8'
content_size = int(r.headers['content-length'])
response = r.iter_lines()
total=0
for gen_lines in response:
total += len(gen_lines)
# print("\rread ip list from apnic...%0.1f%%" % (float(total/content_size) * 100) , end='')
try:
l=str(gen_lines).split("|")
if (l[1]=="CN" and l[2][0:2]=="ip"):
ipset.add(IP( l[3]+"/"+str(int(32-math.log(int(l[4]))/math.log(2))), make_net = True ))
elif (l[1]=="apnic"):
str_update=l[2]
except:
pass
print ("\b\b\b\b\b\b...done")
print ("translate ip to rsc...", end="")
with codecs.open('china-ip-ranges-ipv4.txt', 'w' ,"utf-8") as output_ipv4, codecs.open('china-ip-ranges-ipv6.txt', 'w' ,"utf-8") as output_ipv6:
output_ipv4.write('/ip fir add remove [/ip fir add find comment~"^China20[0-9]*"]'+"\r\n")
output_ipv6.write('/ipv6 fir add remove [/ipv6 fir add find comment~"^China20[0-9]*"]'+"\r\n")
for ip_range in ipset:
try:
if ip_range.version() == 4:
# print(ip_range.strNormal())
output_ipv4.write(':do { /ip fir add add add='+ip_range.strNormal()+' comment="China'+str_update+'" list=dst-use-no-vpn'+" } on-error={}\r\n")
else:
output_ipv6.write(':do { /ipv6 fir add add add='+ip_range.strNormal()+' comment="China'+str_update+'" list=dst-use-no-vpn'+" } on-error={}\r\n")
except:
pass
print ("done")