-
Notifications
You must be signed in to change notification settings - Fork 0
/
clash_api.py
88 lines (68 loc) · 2.83 KB
/
clash_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env python3
# coding: utf-8
import requests
from pathlib import Path
import yaml
from yaml import CSafeLoader as Loader, CSafeDumper as Dumper
def get_header(secret=""):
if secret == "":
return ""
return {"Authorization": f"Bearer {secret}"}
def set_udp(path="./profiles/proxies"):
for i in Path(path).glob("*.yaml"):
with open(i, "r", encoding="utf8") as f:
data = yaml.load(f, Loader=Loader)
for item in data["proxies"]:
item["udp"] = True
with open(i, "w", encoding="utf8") as f:
yaml.dump(data, f, allow_unicode=True, Dumper=Dumper)
def reload_config(path, url="http://127.0.0.1:9090/configs", secret=""):
requests.put(url, json={"path": path}, headers=get_header(secret=secret))
def get_rules(url="http://127.0.0.1:9090/rules", secret=""):
return requests.get(url, headers=get_header(secret=secret))
def connections(name, url="http://127.0.0.1:9090/connections", secret=""):
return requests.delete(url + f"/{name}", headers=get_header(secret=secret))
def all_connections(mode, url="http://127.0.0.1:9090/connections", secret=""):
if mode == "get":
r = requests.get(url, headers=get_header(secret=secret))
if r.status_code == 200:
return r.json()
if mode == "delete":
requests.delete(url, headers=get_header(secret=secret))
if __name__ == "__main__":
set_udp()
with open("config.custom.yaml", "r", encoding="utf8") as f:
custom_data = yaml.load(f, Loader=Loader)
with open("config.template.yaml", "r", encoding="utf8") as f:
template_data = yaml.load(f, Loader=Loader)
template_providers = template_data["proxy-providers"]
custom_providers = custom_data["proxy-providers"]
for template_name in template_providers:
template_item = template_providers[template_name]
if template_item["type"] == "file":
continue
custom_item = custom_providers[template_name]
for custom_k, custom_v in custom_item.items():
template_item[custom_k] = custom_v
fields = [
"mixed-port",
"allow-lan",
"mode",
"log-level",
"external-controller",
"dns",
"tun",
"proxies",
"rules",
]
assert "rules" in custom_data, "请定义rules字段"
for i in fields:
if i in template_data and i in custom_data:
template_data[i] = custom_data[i]
with open("config.yaml", "w", encoding="utf8") as f:
yaml.dump(template_data, f, allow_unicode=True, Dumper=Dumper)
configPath = Path("config.yaml").absolute()
reload_config(configPath.as_posix())
print("已重载配置", configPath)
all_connections("delete")
print("已清空连接")