mirror of
https://github.com/lifegpc/pythonscript.git
synced 2026-06-05 11:08:49 +08:00
uPDATE
This commit is contained in:
5
.gitignore
vendored
5
.gitignore
vendored
@@ -137,4 +137,7 @@ dmypy.json
|
||||
!*.example.jsonc
|
||||
*.epub
|
||||
!*.schema.json
|
||||
update_clash.yaml
|
||||
*.yaml
|
||||
*.yml
|
||||
*.conf
|
||||
!*.example.yaml
|
||||
|
||||
58
ip-cidr-detect.py
Normal file
58
ip-cidr-detect.py
Normal file
@@ -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
|
||||
45
update_clash.example.yaml
Normal file
45
update_clash.example.yaml
Normal file
@@ -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
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user