From 9089310c84c6a164130110ece7255733d34edd25 Mon Sep 17 00:00:00 2001 From: lifegpc Date: Wed, 20 Dec 2023 17:48:57 +0800 Subject: [PATCH] uPDATE --- .gitignore | 5 +++- ip-cidr-detect.py | 58 +++++++++++++++++++++++++++++++++++++++ update_clash.example.yaml | 45 ++++++++++++++++++++++++++++++ update_clash.py | 2 ++ 4 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 ip-cidr-detect.py create mode 100644 update_clash.example.yaml diff --git a/.gitignore b/.gitignore index 3b7b513..e4c7c4b 100644 --- a/.gitignore +++ b/.gitignore @@ -137,4 +137,7 @@ dmypy.json !*.example.jsonc *.epub !*.schema.json -update_clash.yaml +*.yaml +*.yml +*.conf +!*.example.yaml diff --git a/ip-cidr-detect.py b/ip-cidr-detect.py new file mode 100644 index 0000000..a3b337b --- /dev/null +++ b/ip-cidr-detect.py @@ -0,0 +1,58 @@ +from argparse import ArgumentParser +import ipaddress +import os +from os.path import join +import sys +import geoip2.database +import netaddr + + +def try_parse(ip): + try: + return ipaddress.ip_network(ip) + except ValueError: + return None + + +def get_home() -> str: + if sys.platform == "win32": + return join(os.environ["USERPROFILE"], "AppData", "Roaming") + else: + return os.environ["HOME"] + + +p = ArgumentParser() +p.add_argument("CONFIG", help="Configuration files", nargs="+") +p.add_argument("-c", "--country", help="Country code", default="CN") +p.add_argument("-o", "--output", help="Output file", + default="ip-cidr-detect.yaml") +p.add_argument("--resolve", help="Resolve domain name", action="store_true", + default=False) +p.add_argument("-r", "--rule", help="Clash rule target", default="PROXY") +p.add_argument("-d", "--db", help="GeoIP database", + default=join(get_home(), ".config/clash/Country.mmdb")) +arg = p.parse_intermixed_args() +with geoip2.database.Reader(arg.db) as reader: + ips = [] + for config in arg.CONFIG: + with open(config, "r", encoding="UTF-8") as i: + tmp = i.readline() + while tmp: + tmp = tmp.split("#")[0].strip() + print(tmp) + sl = tmp.split("=") + if len(sl) == 1 and not tmp.startswith("["): + network = try_parse(tmp) + if network is not None: + for ip in network: + re = reader.country(ip) + if re.country.iso_code == arg.country: + ips.append(str(ip)) + tmp = i.readline() + ips = netaddr.cidr_merge(ips) + print(ips) + with open(arg.output, "w", encoding="UTF-8") as o: + o.write("# Generated by ip-cidr-detect.py\n") + o.write("rules:\n") + for r in ips: + o.write(f"- IP-CIDR,{r},{arg.rule}{'' if arg.resolve else ',no-resolve'}\n") # noqa: E501 diff --git a/update_clash.example.yaml b/update_clash.example.yaml new file mode 100644 index 0000000..12dec8f --- /dev/null +++ b/update_clash.example.yaml @@ -0,0 +1,45 @@ +# 订阅来源 +src: + # 来源名称(可选) + - name: TAG + # 是否用该来源的节点生成一个代理组(名称使用来源名称) + # 默认 false + add-group: true + # 订阅地址 + url: URL + # HTTP 头部 (可选) + headers: + User-Agent: ClashforWindows/0.13.8 + # 订阅地址 + - URL +# 输出文件位置(需要文件本身存在) +dest: output.yaml +# 添加自定义节点 +proxies: + - name: WARP + type: socks5 + server: localhost + port: 7891 +# 添加自定义规则组 +proxy-groups: + - name: all + type: select + proxies: + - group1 + - group2 + # 是否将DIRECT加至列表 + add-direct: true + - name: JP + type: select + # 将符合下列正则的加至列表 + match: + - .*JP + - .*日本.* + - name: All + type: select + # 将所有节点/组加至列表 + match-all: true + add-direct: true +# 自定义节点(默认覆盖) +rules: + - MATCH,All diff --git a/update_clash.py b/update_clash.py index 3e7a52a..6a515fc 100644 --- a/update_clash.py +++ b/update_clash.py @@ -39,6 +39,8 @@ for s in src: if "add-group" in s and s["add-group"] and "name" in s and s["name"]: add_group = True ori = load(get_src(s), Loader=SafeLoader) + if ori is None: + raise ValueError(f"Failed to download src: {s}") dest["proxies"] += ori["proxies"] dest["proxy-groups"] += ori["proxy-groups"] if add_group: