-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrule_community_server.py
More file actions
303 lines (303 loc) · 10.8 KB
/
Copy pathrule_community_server.py
File metadata and controls
303 lines (303 loc) · 10.8 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
from flask import Flask, request, jsonify
import json
import threading
import os
app = Flask(__name__)
DATA_FILE = "community_rules.json"
SCORE_FILE = "community_scores.json"
USER_FILE = "community_users.json"
TAG_FILE = "community_tags.json"
REPORT_FILE = "community_reports.json"
lock = threading.Lock()
def load_rules():
if not os.path.exists(DATA_FILE):
return []
with open(DATA_FILE, "r", encoding="utf-8") as f:
try:
return json.load(f)
except Exception:
return []
def save_rules(rules):
#保存rule
with open(DATA_FILE, "w", encoding="utf-8") as f:
json.dump(rules, f, ensure_ascii=False, indent=2)
def load_scores():
#加载用户评分
if not os.path.exists(SCORE_FILE):
return {}
with open(SCORE_FILE, "r", encoding="utf-8") as f:
try:
return json.load(f)
except Exception:
return {}
def save_scores(scores):
with open(SCORE_FILE, "w", encoding="utf-8") as f:
json.dump(scores, f, ensure_ascii=False, indent=2)
def load_users():
if not os.path.exists(USER_FILE):
return {}
with open(USER_FILE, "r", encoding="utf-8") as f:
try:
return json.load(f)
except Exception:
return {}
def save_users(users):
with open(USER_FILE, "w", encoding="utf-8") as f:
json.dump(users, f, ensure_ascii=False, indent=2)
def load_tags():
#加载label
if not os.path.exists(TAG_FILE):
return {}
with open(TAG_FILE, "r", encoding="utf-8") as f:
try:
return json.load(f)
except Exception:
return {}
def save_tags(tags):
# 保存label
with open(TAG_FILE, "w", encoding="utf-8") as f:
json.dump(tags, f, ensure_ascii=False, indent=2)
def load_reports():
#加载举报
if not os.path.exists(REPORT_FILE):
return []
with open(REPORT_FILE, "r", encoding="utf-8") as f:
try:
return json.load(f)
except Exception:
return []
def save_reports(reports):
with open(REPORT_FILE, "w", encoding="utf-8") as f:
json.dump(reports, f, ensure_ascii=False, indent=2)
@app.route("/register", methods=["POST"])
def register():
#注册
data = request.get_json()
username = data.get("username")
password = data.get("password")
if not username or not password:
return jsonify({"error": "用户名和密码不能为空"}), 400
with lock:
users = load_users()
if username in users:
return jsonify({"error": "用户名已存在"}), 409
users[username] = {"password": password}
save_users(users)
return jsonify({"msg": "注册成功"})
@app.route("/login", methods=["POST"])
def login():
#登录
data = request.get_json()
username = data.get("username")
password = data.get("password")
if not username or not password:
return jsonify({"error": "用户名和密码不能为空"}), 400
users = load_users()
if username not in users or users[username]["password"] != password:
return jsonify({"error": "用户名或密码错误"}), 401
return jsonify({"msg": "登录成功"})
@app.route("/change_password", methods=["POST"])
def change_password():
#用户更改自己的登录密码
data = request.get_json()
username = data.get("username")
old_password = data.get("old_password")
new_password = data.get("new_password")
if not username or not old_password or not new_password:
return jsonify({"error": "参数不能为空"}), 400
with lock:
users = load_users()
if username not in users or users[username]["password"] != old_password:
return jsonify({"error": "用户名或原密码错误"}), 401
users[username]["password"] = new_password
save_users(users)
return jsonify({"msg": "密码修改成功"})
@app.route("/rules", methods=["GET"])
def get_rules():
# 加载规则列表
rules = load_rules()
scores = load_scores()
tags = load_tags()
# 支持标签筛选和搜索
tag_filter = request.args.get("tag", "").strip()
search = request.args.get("search", "").strip().lower()
rule_list = []
for i, r in enumerate(rules):
score_info = scores.get(str(i), {})
if isinstance(score_info, dict):
score_count = len(score_info)
avg_score = round(sum(score_info.values())/score_count, 2) if score_count > 0 else 0
elif isinstance(score_info, list):
score_count = len(score_info)
avg_score = round(sum(score_info)/score_count, 2) if score_count > 0 else 0
else:
score_count = 0
avg_score = 0
rule_tags = tags.get(str(i), [])
# 标签筛选
if tag_filter and tag_filter not in rule_tags:
continue
# 搜索(规则名、简介、标签、作者)
if search:
if (search not in r.get("name", "").lower() and
search not in r.get("desc", "").lower() and
search not in r.get("username", "").lower() and
not any(search in t.lower() for t in rule_tags)):
continue
rule_list.append({
"id": i,
"name": r.get("name", ""),
"desc": r.get("desc", ""),
"username": r.get("username", ""),
"score_count": score_count,
"avg_score": avg_score,
"tags": rule_tags
})
rule_list.sort(key=lambda x: x["avg_score"], reverse=True)
return jsonify({
"server_ip": "116.62.80.32",
"rules": rule_list
})
@app.route("/rule/<int:rule_id>", methods=["GET"])
def get_rule(rule_id):
rules = load_rules()
tags = load_tags()
if 0 <= rule_id < len(rules):
rule = rules[rule_id]
return jsonify({
"username": rule.get("username", ""),
"name": rule.get("name", ""),
"desc": rule.get("desc", ""),
"rules": rule.get("rules", []),
"tags": tags.get(str(rule_id), [])
})
return jsonify({"error": "not found"}), 404
@app.route("/upload", methods=["POST"])
def upload_rule():
data = request.get_json()
username = data.get("username")
password = data.get("password")
rule_name = data.get("name", "")
tags = data.get("tags", [])
if not data or not username or not password or not rule_name or "rules" not in data:
return jsonify({"error": "invalid data"}), 400
with lock:
users = load_users()
if username not in users or users[username]["password"] != password:
return jsonify({"error": "用户未登录或密码错误"}), 401
rules = load_rules()
for r in rules:
if r.get("username") == username and r.get("name") == rule_name:
return jsonify({"error": "同一用户下规则名称已存在"}), 409
rules.append({
"username": username,
"name": rule_name,
"desc": data.get("desc", ""),
"rules": data.get("rules", [])
})
save_rules(rules)
# 保存标签
tags_map = load_tags()
tags_map[str(len(rules)-1)] = tags if isinstance(tags, list) else []
save_tags(tags_map)
return jsonify({"msg": "ok"})
@app.route("/set_tags", methods=["POST"])
def set_tags():
data = request.get_json()
rule_id = str(data.get("rule_id"))
username = data.get("username")
tags = data.get("tags", [])
rules = load_rules()
if not rule_id or not username or not isinstance(tags, list):
return jsonify({"error": "invalid data"}), 400
rule_id_int = int(rule_id)
if not (0 <= rule_id_int < len(rules)):
return jsonify({"error": "rule not found"}), 404
if rules[rule_id_int].get("username") != username:
return jsonify({"error": "只能修改自己上传的规则标签"}), 403
with lock:
tags_map = load_tags()
tags_map[rule_id] = tags
save_tags(tags_map)
return jsonify({"msg": "ok"})
@app.route("/report", methods=["POST"])
def report_rule():
data = request.get_json()
rule_id = str(data.get("rule_id"))
username = data.get("username", "")
reason = data.get("reason", "")
if not rule_id or not reason:
return jsonify({"error": "invalid data"}), 400
with lock:
reports = load_reports()
reports.append({
"rule_id": rule_id,
"username": username,
"reason": reason
})
save_reports(reports)
return jsonify({"msg": "举报成功"})
@app.route("/rate", methods=["POST"])
def rate_rule():
data = request.get_json()
rule_id = str(data.get("rule_id"))
score = int(data.get("score", 0))
username = data.get("username", None)
if not rule_id or not (1 <= score <= 5) or not username:
return jsonify({"error": "invalid data"}), 400
with lock:
scores = load_scores()
if rule_id not in scores:
scores[rule_id] = {}
# 只保留每个用户最后一次评分
scores[rule_id][username] = score
save_scores(scores)
return jsonify({"msg": "ok"})
@app.route("/delete_rule", methods=["POST"])
def delete_rule():
data = request.get_json()
rule_id = data.get("rule_id")
username = data.get("username")
if rule_id is None or username is None:
return jsonify({"error": "参数不完整"}), 400
with lock:
rules = load_rules()
try:
rule_id_int = int(rule_id)
except Exception:
return jsonify({"error": "rule_id格式错误"}), 400
if not (0 <= rule_id_int < len(rules)):
return jsonify({"error": "规则不存在"}), 404
if rules[rule_id_int].get("username") != username:
return jsonify({"error": "只能删除自己上传的规则"}), 403
# 删除规则
del rules[rule_id_int]
save_rules(rules)
tags = load_tags()
scores = load_scores()
reports = load_reports()
tags.pop(str(rule_id), None)
scores.pop(str(rule_id), None)
reports = [r for r in reports if r.get("rule_id") != str(rule_id)]
def reindex_dict(d):
return {str(i): d.get(str(old_i), []) for i, old_i in enumerate(sorted(map(int, d.keys()))) if str(old_i) in d}
tags_new = {}
scores_new = {}
for i, rule in enumerate(rules):
tags_new[str(i)] = tags.get(str(i), [])
scores_new[str(i)] = scores.get(str(i), {})
save_tags(tags_new)
save_scores(scores_new)
save_reports(reports)
return jsonify({"msg": "删除成功"})
@app.route("/stats", methods=["GET"])
def stats():
"""统计总规则数和注册总人数"""
rules = load_rules()
users = load_users()
return jsonify({
"rule_count": len(rules),
"user_count": len(users)
})
if __name__ == "__main__":
app.run(host="0.0.0.0", port=80, debug=False)