Skip to content

Commit

Permalink
fix: ssacl export geocn fail
Browse files Browse the repository at this point in the history
  • Loading branch information
JinnLynn committed Jul 22, 2024
1 parent e43b2c0 commit 5034aae
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 18 deletions.
2 changes: 1 addition & 1 deletion example/config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ output = ./ss.acl

[job:ssacl]
ssacl-geocn = true
output = ./ss-gencn.acl
output = ./ss-geocn.acl

# ip输出
[job:ip]
Expand Down
12 changes: 6 additions & 6 deletions src/genpac/format/ip.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@ def iter_ip_cidr(self, family, cc):
if family not in ['4', '6', 'all']:
raise ValueError('IP family MUST BE: 4, 6, all')
if family in ['4', 'all']:
yield from self._fetch_data(4, cc)
yield from self._fetch_ip_data(4, cc)

if family in ['6', 'all']:
yield from self._fetch_data(6, cc)
yield from self._fetch_ip_data(6, cc)

def iter_ip_range(self, family, cc):
for d in self.iter_ip_cidr(family, cc):
yield str(IPAddress(d.first)), str(IPAddress(d.last))
yield IPAddress(d.first), IPAddress(d.last)

def _fetch_data(self, family, cc):
def _fetch_ip_data(self, family, cc):
if cc.lower() == 'cn':
yield from self._fetch_data_cn(family)
yield from self._fetch_ip_data_cn(family)
return

expr = re.compile(f'^[0-9a-f:,]+,{cc}' if family == 6 else f'^[0-9\\.,]+,{cc}',
Expand All @@ -63,7 +63,7 @@ def _fetch_data(self, family, cc):
yield n
logger.debug(f'IPv{family}[{cc}]: {count} => {size:.2e}')

def _fetch_data_cn(self, family):
def _fetch_ip_data_cn(self, family):
url = _IP_DATA_ASN[int(family)]
content = self.fetch(url)
if not content:
Expand Down
15 changes: 4 additions & 11 deletions src/genpac/format/shadowsocks_acl.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from ..util import Namespace
from .base import formater, FmtBase, TPL_LEAD_COMMENT
from .ip import FmtIP
from .base import formater, TPL_LEAD_COMMENT
from .ip import IPInterface

_TPL_DEF = f'''
{TPL_LEAD_COMMENT}
Expand All @@ -20,7 +19,7 @@


@formater('ssacl', desc='Shadowsocks访问控制列表')
class FmtSSACL(FmtBase):
class FmtSSACL(IPInterface):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

Expand Down Expand Up @@ -56,13 +55,7 @@ def _parse_rules(rules):
return self.replace(self.tpl, replacements)

def gen_by_geoip(self, replacements):
ip_data = []
for ip in self.fetch_cnips():
ip_data.append(ip)
ip_data = [str(ip) for ip in self.iter_ip_cidr(4, 'cn')]
replacements.update({
'__CNIPS__': '\n'.join(ip_data)})
return self.replace(self.tpl, replacements)

def fetch_cnips(self):
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 5034aae

Please sign in to comment.