diff --git a/example/config.ini b/example/config.ini index cfa759d6..4cd02fe0 100644 --- a/example/config.ini +++ b/example/config.ini @@ -163,7 +163,7 @@ output = ./ss.acl [job:ssacl] ssacl-geocn = true -output = ./ss-gencn.acl +output = ./ss-geocn.acl # ip输出 [job:ip] diff --git a/src/genpac/format/ip.py b/src/genpac/format/ip.py index 37bd24df..a740548f 100644 --- a/src/genpac/format/ip.py +++ b/src/genpac/format/ip.py @@ -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}', @@ -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: diff --git a/src/genpac/format/shadowsocks_acl.py b/src/genpac/format/shadowsocks_acl.py index 087bc934..b5518c1c 100644 --- a/src/genpac/format/shadowsocks_acl.py +++ b/src/genpac/format/shadowsocks_acl.py @@ -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} @@ -20,7 +19,7 @@ @formater('ssacl', desc='Shadowsocks访问控制列表') -class FmtSSACL(FmtBase): +class FmtSSACL(IPInterface): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) @@ -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)