forked from 1c3z/ARL-Finger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
101 lines (69 loc) · 2.34 KB
/
main.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
import json
from enum import Enum
import yaml
class Method(Enum):
KEYWORD = "keyword"
FAVICON_HASH = "faviconhash"
class Location(Enum):
BODY = "body"
TITLE = "title"
HEADER = "header"
def validate_keyword(keyword):
if not isinstance(keyword, list):
raise ValueError("keyword must be a list")
if len(keyword) == 0:
raise ValueError("empty keyword")
# ARL 支持的字段, body, title, header,icon_hash
class FingerPrint:
def __init__(self, cms, method, location, keyword):
validate_keyword(keyword)
self.cms = cms
self.method = Method(method)
self.location = Location(location)
self.keyword = keyword
def __str__(self):
return self.cms
def transfer_rule(self) -> str:
items = []
if self.method == Method.KEYWORD:
for keyword in self.keyword:
msg = f'{self.location.value}="{self.quote_keyword(keyword)}"'
items.append(msg)
return " && ".join(items)
if self.method == Method.FAVICON_HASH:
return f'icon_hash="{self.keyword[0]}"'
@staticmethod
def quote_keyword(keyword):
keyword = keyword.replace('\\', r'\\')
return keyword.replace('"', r'\"')
def transfer(self) -> dict:
item = {
"name": self.cms,
"rule": self.transfer_rule()
}
return item
def load_fingers() -> [FingerPrint]:
items = []
with open("ehole-finger.json", encoding="utf-8") as f:
for item in json.load(f)["fingerprint"]:
items.append(FingerPrint(**item))
return items
def main():
transferred_fingers = []
loaded_fingers = load_fingers()
print(f"Loaded fingers: {len(loaded_fingers)}")
unique_rules = set()
for finger in loaded_fingers:
rule = finger.transfer()
if rule["rule"] in unique_rules:
print("Repeated rule", rule)
continue
unique_rules.add(rule["rule"])
transferred_fingers.append(rule)
print(f"Transferred fingers: {len(transferred_fingers)}")
finger_data = yaml.dump(transferred_fingers, default_flow_style=False, sort_keys=False, allow_unicode=True)
with open("finger.yml", "w", encoding="utf-8") as file:
file.write(finger_data)
print("Done")
if __name__ == "__main__":
main()