-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathv1.py
135 lines (95 loc) · 3.87 KB
/
v1.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import requests
import utils
import yaml
import re
from collections import OrderedDict
def handle_v1(data: OrderedDict) -> OrderedDict:
preprocessor: OrderedDict = data["preprocessor"]
if preprocessor is None or preprocessor["version"] != 1:
raise utils.ParseException("Version != 1")
result: OrderedDict = OrderedDict()
general_block: OrderedDict = data["clash-general"]
result.update(general_block)
proxy_sources_dicts: list = data["proxy-sources"]
proxies: list = []
for item in proxy_sources_dicts:
if item["type"] == "url":
proxies += load_url_proxies(item["url"])
elif item["type"] == "file":
proxies += load_file_proxies(item["path"])
elif item["type"] == "plain":
proxies.append(load_plain_proxies(item))
proxy_group_dispatch_dicts: list = data["proxy-group-dispatch"]
proxy_groups: list = []
for item in proxy_group_dispatch_dicts:
group_data: OrderedDict = item.copy()
ps: list = []
black_regex = re.compile(item["proxies-filters"]["black-regex"])
white_regex = re.compile(item["proxies-filters"]["white-regex"])
for p in proxies:
p_name: str = p["name"]
if white_regex.fullmatch(p_name) and not black_regex.fullmatch(p_name):
ps.append(p_name)
if "flat-proxies" in item:
ps.extend(item["flat-proxies"])
group_data.pop("proxies-filters", None)
group_data.pop("flat-proxies", None)
group_data["proxies"] = ps
proxy_groups.append(group_data)
rule_sets_dicts: list = data["rule-sets"]
rule_sets: dict = {}
if not rule_sets_dicts is None:
for item in rule_sets_dicts:
item_name: str = item["name"]
item_type: str = item["type"]
item_map: dict = {}
for target_map_element in item.get("target-map", {}):
kv: list = target_map_element.split(",")
item_map[kv[0]] = kv[1]
if item_type == "url":
rule_sets[item_name] = load_url_rule_set(item["url"], item_map)
elif item_type == "file":
rule_sets[item_name] = load_file_rule_set(item["path"], item_map)
rules: list = []
for rule in data["rule"]:
if str(rule).startswith("RULE-SET"):
rules.extend(rule_sets[str(rule).split(",")[1]])
else:
rules.append(rule)
result["Proxy"] = proxies
result["Proxy Group"] = proxy_groups
result["Rule"] = rules
return result
def load_url_proxies(url: str) -> OrderedDict:
data = requests.get(url)
data_yaml: OrderedDict = yaml.load(data.content.decode(), Loader=yaml.Loader)
return data_yaml["Proxy"]
def load_file_proxies(path: str) -> OrderedDict:
with open(path, "r") as f:
data_yaml: OrderedDict = yaml.load(f, Loader=yaml.Loader)
return data_yaml["Proxy"]
def load_plain_proxies(data: OrderedDict) -> OrderedDict:
return data["data"]
def load_url_rule_set(url: str, targetMap: dict) -> list:
data = yaml.load(requests.get(url).content, Loader=yaml.Loader)
result: list = []
for rule in data["Rule"]:
original_target = str(rule).split(",")[-1]
map_to: str = targetMap.get(original_target)
if not map_to is None:
result.append(str(rule).replace(original_target, map_to))
else:
result.append(rule)
return result
def load_file_rule_set(path: str, targetMap: dict) -> list:
with open(path, "r") as f:
data = yaml.load(f, Loader=yaml.Loader)
result: list = []
for rule in data["Rule"]:
original_target = str(rule).split(",")[-1]
map_to: str = targetMap.get(original_target)
if not map_to is None:
result.append(str(rule).replace(original_target, map_to))
else:
result.append(rule)
return result