-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
194 lines (150 loc) · 5.68 KB
/
server.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import os
import quart, json, reds_simple_logger, colorama, sys, time, string
from quart import Quart, request
from colorama import Fore, Style
from Levenshtein import distance
import hashlib, time, uuid
print(Fore.MAGENTA, "Programm developed by Red_wolf2467")
print(Fore.MAGENTA, "Starting up chatfilter server...")
print(Fore.RESET, "")
server = Quart(__name__)
logger = reds_simple_logger.Logger()
logger.info("Server init complete! Continuing startup...")
def load_data(file_path):
try:
with open(file_path, "r", encoding="utf-8") as file:
data = json.load(file)
return data
except FileNotFoundError:
return {}
def save_data(file_path, data):
with open(file_path, "w", encoding="utf-8") as file:
json.dump(data, file, indent=4, ensure_ascii=False)
files_and_functions = [
("json/badwords.json", load_data, save_data),
("json/goodwords.json", load_data, save_data),
("json/ids.json", load_data, save_data),
("json/key_hash.json", load_data, save_data),
("json/admin_key_hash.json", load_data, save_data),
]
def check_chatfilter(input_str: str, badwords, goodwords, cid: int, gid: int):
threshold: int = 1 if len(input_str) < 150 else 2
input_data = input_str.lower().split()
for word in input_data:
cleaned_word = word.strip(string.punctuation)
if cleaned_word in goodwords:
continue
best_match = None
best_distance = float("inf")
for badword in badwords:
current_distance = distance(cleaned_word, badword)
if current_distance <= threshold and current_distance < best_distance:
best_match = {
"input_word": cleaned_word,
"matched_badword": badword,
"distance": current_distance,
"cid": cid,
"gid": gid
}
best_distance = current_distance
if best_match:
return best_match
return {}
def check_user_db(input_id: int, ids_list):
rt_data = {}
name: str = None
id: int = input_id
reason: str = None
flagged: bool = False
if str(input_id) in ids_list:
name: str = ids_list[str(input_id)]["name"]
id: int = ids_list[str(input_id)]["id"]
reason: str = ids_list[str(input_id)]["reason"]
flagged: bool = True
rt_data = {"name": name, "id": id, "reason": reason, "flagged": flagged}
return rt_data
def hash_string(string: str):
hashed = hashlib.sha256(string.encode()).hexdigest()
return hashed
@server.route("/chatfilter", methods=["GET", "POST"])
async def check_message():
print("")
start_time = time.time()
id = os.urandom(15).hex()
logger.info(f"Start processing order number chatfilter-{id}")
data = await request.get_json()
badwords = load_data("json/badwords.json")
goodwords = load_data("json/goodwords.json")
key_hash_list = load_data("json/key_hash.json")
if hash_string(data["key"]) not in key_hash_list:
return {"error": "access denied"}
message = data["message"]
try:
cid = data["cid"]
gid = data["gid"]
except:
cid = None
gid = None
results = check_chatfilter(message, badwords, goodwords, cid, gid)
end_time = time.time()
processing_time = end_time - start_time
logger.success(
f"Processing of order number chatfilter-{id} in {processing_time}s completed."
)
return results
@server.route("/user", methods=["GET", "POST"])
async def check_user():
print("")
start_time = time.time()
id = os.urandom(15).hex()
logger.info(f"Start processing order number user-{id}")
data = await request.get_json()
key_hash_list = load_data("json/key_hash.json")
ids_list = load_data("json/ids.json")
if hash_string(data["key"]) not in key_hash_list:
return {"error": "access denied"}
user_id = data["id"]
result = check_user_db(int(user_id), ids_list)
end_time = time.time()
processing_time = end_time - start_time
logger.success(
f"Processing of order number user-{id} in {processing_time}s completed."
)
return result
@server.route("/flagg_user", methods=["GET", "POST"])
async def add_flagged_user():
data = await request.get_json()
admin_key_hash_list = load_data("json/admin_key_hash.json")
ids_list = load_data("json/ids.json")
if hash_string(data["key"]) not in admin_key_hash_list:
return {"error": "access denied"}
user_id = data[str(user_id)]["id"]
user_name = data[str(user_id)]["name"]
reason = data[str(user_id)]["reason"]
if str(user_id) in ids_list:
return {"success": False, "message": "User already in Database!"}
else:
ids_list[str(user_id)] = {
"id": int(user_id),
"name": str(user_name),
"reason": str(reason),
"flagged": bool(True),
}
save_data("json/ids.json", ids_list)
return {"success": True, "message": "Used was flagged"}
@server.route("/deflag_user", methods=["GET", "POST"])
async def remove_flagged_user():
data = await request.get_json()
admin_key_hash_list = load_data("json/admin_key_hash.json")
ids_list = load_data("json/ids.json")
if hash_string(data["key"]) not in admin_key_hash_list:
return {"error": "access denied"}
user_id = data[str(user_id)]["id"]
if str(user_id) in ids_list:
del ids_list[str(user_id)]
save_data("json/ids.json", ids_list)
return {"success": True, "message": "Used was deflagged"}
else:
return {"success": False, "message": "User not flagged!"}
if __name__ == "__main__":
server.run(host="0.0.0.0", port=1652)