Browse Source

perf: optimize cidr merge speed

dev
Dnomd343 2 years ago
parent
commit
40b9061276
  1. 22
      assets/china-ip.py

22
assets/china-ip.py

@ -2,7 +2,9 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import os import os
from IPy import IP, IPSet from netaddr import IPSet
from netaddr import IPAddress
from netaddr import IPNetwork
operators = ['china', 'cmcc', 'chinanet', 'unicom', 'tietong', 'cernet', 'cstnet', 'drpeng', 'googlecn'] operators = ['china', 'cmcc', 'chinanet', 'unicom', 'tietong', 'cernet', 'cstnet', 'drpeng', 'googlecn']
operators += ['%s6' % x for x in operators] # add `...6` suffix operators += ['%s6' % x for x in operators] # add `...6` suffix
@ -12,20 +14,20 @@ source = [
'curl -sL https://github.com/17mon/china_ip_list/raw/master/china_ip_list.txt', 'curl -sL https://github.com/17mon/china_ip_list/raw/master/china_ip_list.txt',
] + ['curl -sL https://gaoyifan.github.io/china-operator-ip/%s.txt' % x for x in operators] ] + ['curl -sL https://gaoyifan.github.io/china-operator-ip/%s.txt' % x for x in operators]
ipv4 = IPSet()
ipv6 = IPSet()
ipAddrs = set() ipAddrs = set()
for script in source: # traverse fetch commands for script in source: # traverse fetch commands
raw = os.popen(script).read().split('\n') raw = os.popen(script).read().split('\n')
ipAddrs.update(filter(None, raw)) ipAddrs.update(filter(None, raw))
for ipAddr in ipAddrs:
ipv4 = IPSet()
ipv6 = IPSet()
for ipAddr in ipAddrs: # load all IP data
try: try:
ip = IP(ipAddr) # ip format check ip = IPNetwork(ipAddr) if '/' in ipAddr else IPAddress(ipAddr)
(ipv4 if ip.version() == 4 else ipv6).add(ip) ipv4.add(ip) if ip.version == 4 else ipv6.add(ip)
except: except:
pass pass
release = [('%s' if '/' in str(ip) else '%s/32') % str(ip) for ip in ipv4] # format into CIDR with open('china-ip.txt', 'w') as fileObj: # save to file
release += [('%s' if '/' in str(ip) else '%s/128') % str(ip) for ip in ipv6] fileObj.write('\n'.join([str(ip) for ip in ipv4.iter_cidrs()]) + '\n') # format into CIDR
with open('china-ip.txt', 'w') as fileObj: fileObj.write('\n'.join([str(ip) for ip in ipv6.iter_cidrs()]) + '\n')
fileObj.write('\n'.join(release) + '\n')

Loading…
Cancel
Save