Skip to content

Commit

Permalink
fix(ssacl): geocn
Browse files Browse the repository at this point in the history
  • Loading branch information
JinnLynn committed Jun 6, 2024
1 parent d320f06 commit 49a4ece
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 52 deletions.
5 changes: 4 additions & 1 deletion example/config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,12 @@ output = ${DEST}/v2ray.yaml

; ssacl: shadowsocks的访问控制列表
[job:ssacl]
; ssacl-geocn = true
output = ${DEST}/ss.acl

[job:ssacl]
ssacl-geocn = true
output = ${DEST}/ss-gencn.acl

; ip输出
[job:ip]
output = ${DEST}/ip-cn.txt
Expand Down
69 changes: 18 additions & 51 deletions src/genpac/format/shadowsocks_acl.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
import re
import math
from IPy import IP, IPSet

from ..util import read_file, get_resource_path
from ..util import Namespace
from .base import formater, FmtBase
from .ip import FmtIP

_TPL = '''
_TPL_DEF = '''
#! __GENPAC__
#! Generated: __GENERATED__
#! GFWList: __GFWLIST_DETAIL__
[bypass_all]
[proxy_list]
__GFWED_RULES__
'''

_TPL_GEOCN = '''
#! __GENPAC__
#! Generated: __GENERATED__
#! GFWList: __GFWLIST_DETAIL__
[proxy_all]
[bypass_list]
__CNIPS__
'''


@formater('ssacl', desc='Shadowsocks访问控制列表.')
class FmtSSACL(FmtBase):
_default_tpl = _TPL

def __init__(self, *args, **kwargs):
super(FmtSSACL, self).__init__(*args, **kwargs)

Expand All @@ -40,19 +45,7 @@ def config(cls, options):

@property
def tpl(self):
if self.options.ssacl_geocn:
return ('#! __GENPAC__\n'
'#! Generated: __GENERATED__\n'
'[proxy_all]\n\n'
'[bypass_list]\n'
'__CNIPS__\n')
else:
return ('#! __GENPAC__\n'
'#! Generated: __GENERATED__\n'
'#! GFWList: __GFWLIST_DETAIL__\n'
'[bypass_all]\n\n'
'[proxy_list]\n'
'__GFWED_RULES__\n')
return _TPL_GEOCN if self.options.ssacl_geocn else _TPL_DEF

def generate(self, replacements):
return self.gen_by_geoip(replacements) if self.options.ssacl_geocn else \
Expand All @@ -72,39 +65,13 @@ def parse_rules(rules):
return self.replace(self.tpl, replacements)

def gen_by_geoip(self, replacements):
ips = self.fetch_cnips()
ip_data = []
for ip in ips:
ip_data.append({
'ip': ip.strNormal(wantprefixlen=0),
'prefixlen': ip.prefixlen(),
'netmask': ip.netmask(),
'broadcast': ip.broadcast(),
'net': ip.net(),
'int': ip.int(),
'hex': ip.strHex(wantprefixlen=0),
'bin': ip.strBin(wantprefixlen=0)
})
# fmt = '{ip} {prefixlen} {netmask} {broadcast} {net} {int} {hex} {bin}'
fmt = '{ip}/{prefixlen}'
ip_data = [fmt.format(**d) for d in ip_data]
print(len(ip_data))
for ip in self.fetch_cnips():
ip_data.append(ip)
replacements.update({
'__CNIPS__': '\n'.join(ip_data)})
return self.replace(self.tpl, replacements)

def fetch_cnips(self):
data = read_file(get_resource_path('res/ipdata.txt'))

cnregex = re.compile(r'apnic\|cn\|ipv4\|[0-9\.]+\|[0-9]+\|[0-9]+\|a.*',
re.IGNORECASE)

results = []
for item in cnregex.findall(data):
units = item.split('|')
start_ip = units[3]
ip_count = int(units[4])
prefixlen = 32 - int(math.log(ip_count, 2))
results.append(IP(f'{start_ip}/{prefixlen}'))

return IPSet(results)
gen_ip = FmtIP(generator=self.generator, options=Namespace(ip_family='4', ip_cc='cn'))
yield from gen_ip._fetch_data_cn(4)

0 comments on commit 49a4ece

Please sign in to comment.