-
Notifications
You must be signed in to change notification settings - Fork 129
/
Copy pathconfig.py
567 lines (461 loc) · 17 KB
/
config.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
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
# ----------------------------------------
# - mode: python -
# - author: helloplhm-qwq -
# - name: config.py -
# - project: lx-music-api-server -
# - license: MIT -
# ----------------------------------------
# This file is part of the "lx-music-api-server" project.
import ujson as json
import time
import os
import traceback
import sys
import sqlite3
import shutil
import ruamel.yaml as yaml_
from . import variable
from .log import log
from . import default_config
import threading
import redis
logger = log("config_manager")
# 创建线程本地存储对象
local_data = threading.local()
local_cache = threading.local()
local_redis = threading.local()
def get_data_connection():
return local_data.connection
def get_cache_connection():
return local_cache.connection
def get_redis_connection():
return local_redis.connection
def handle_connect_db():
try:
local_data.connection = sqlite3.connect("./config/data.db")
if read_config("common.cache.adapter") == "redis":
host = read_config("common.cache.redis.host")
port = read_config("common.cache.redis.port")
user = read_config("common.cache.redis.user")
password = read_config("common.cache.redis.password")
db = read_config("common.cache.redis.db")
client = redis.Redis(host=host, port=port, username=user, password=password, db=db)
if not client.ping():
raise
local_redis.connection = client
else:
local_cache.connection = sqlite3.connect("./cache.db")
except:
logger.error("连接数据库失败")
sys.exit(1)
class ConfigReadException(Exception):
pass
yaml = yaml_.YAML()
default_str = default_config.default
default = yaml.load(default_str)
def handle_default_config():
with open("./config/config.yml", "w", encoding="utf-8") as f:
f.write(default_str)
if not os.getenv("build"):
logger.info(
f"首次启动或配置文件被删除,已创建默认配置文件\n建议您到{variable.workdir + os.path.sep}config.yml修改配置后重新启动服务器"
)
return default
class ConfigReadException(Exception):
pass
def load_data():
config_data = {}
try:
# Connect to the database
conn = get_data_connection()
cursor = conn.cursor()
# Retrieve all configuration data from the 'config' table
cursor.execute("SELECT key, value FROM data")
rows = cursor.fetchall()
for row in rows:
key, value = row
config_data[key] = json.loads(value)
except Exception as e:
logger.error(f"Error loading config: {str(e)}")
logger.error(traceback.format_exc())
return config_data
def save_data(config_data):
try:
# Connect to the database
conn = get_data_connection()
cursor = conn.cursor()
# Clear existing data in the 'data' table
cursor.execute("DELETE FROM data")
# Insert the new configuration data into the 'data' table
for key, value in config_data.items():
cursor.execute("INSERT INTO data (key, value) VALUES (?, ?)", (key, json.dumps(value)))
conn.commit()
except Exception as e:
logger.error(f"Error saving config: {str(e)}")
logger.error(traceback.format_exc())
def handleBuildRedisKey(module, key):
prefix = read_config("common.cache.redis.key_prefix")
return f"{prefix}:{module}:{key}"
def getCache(module, key):
try:
if read_config("common.cache.adapter") == "redis":
redis = get_redis_connection()
key = handleBuildRedisKey(module, key)
result = redis.get(key)
if result:
cache_data = json.loads(result)
return cache_data
else:
# 连接到数据库(如果数据库不存在,则会自动创建)
conn = get_cache_connection()
# 创建一个游标对象
cursor = conn.cursor()
cursor.execute("SELECT data FROM cache WHERE module=? AND key=?", (module, key))
result = cursor.fetchone()
if result:
cache_data = json.loads(result[0])
cache_data["time"] = int(cache_data["time"])
if not cache_data["expire"]:
return cache_data
if int(time.time()) < int(cache_data["time"]):
return cache_data
except:
pass
# traceback.print_exc()
return None
def updateCache(module, key, data, expire=None):
try:
if read_config("common.cache.adapter") == "redis":
redis = get_redis_connection()
key = handleBuildRedisKey(module, key)
redis.set(key, json.dumps(data), ex=expire if expire and expire > 0 else None)
else:
# 连接到数据库(如果数据库不存在,则会自动创建)
conn = get_cache_connection()
# 创建一个游标对象
cursor = conn.cursor()
cursor.execute("SELECT data FROM cache WHERE module=? AND key=?", (module, key))
result = cursor.fetchone()
if result:
cursor.execute(
"UPDATE cache SET data = ? WHERE module = ? AND key = ?", (json.dumps(data), module, key)
)
else:
cursor.execute(
"INSERT INTO cache (module, key, data) VALUES (?, ?, ?)", (module, key, json.dumps(data))
)
conn.commit()
except:
logger.error("缓存写入遇到错误…")
logger.error(traceback.format_exc())
def resetRequestTime(ip):
config_data = load_data()
try:
try:
config_data["requestTime"][ip] = 0
except KeyError:
config_data["requestTime"] = {}
config_data["requestTime"][ip] = 0
save_data(config_data)
except:
logger.error("配置写入遇到错误…")
logger.error(traceback.format_exc())
def updateRequestTime(ip):
try:
config_data = load_data()
try:
config_data["requestTime"][ip] = time.time()
except KeyError:
config_data["requestTime"] = {}
config_data["requestTime"][ip] = time.time()
save_data(config_data)
except:
logger.error("配置写入遇到错误...")
logger.error(traceback.format_exc())
def getRequestTime(ip):
config_data = load_data()
try:
value = config_data["requestTime"][ip]
except:
value = 0
return value
def read_data(key):
config = load_data()
keys = key.split(".")
value = config
for k in keys:
if k not in value and keys.index(k) != len(keys) - 1:
value[k] = {}
elif k not in value and keys.index(k) == len(keys) - 1:
value = None
value = value[k]
return value
def write_data(key, value):
config = load_data()
keys = key.split(".")
current = config
for k in keys[:-1]:
if k not in current:
current[k] = {}
current = current[k]
current[keys[-1]] = value
save_data(config)
def push_to_list(key, obj):
config = load_data()
keys = key.split(".")
current = config
for k in keys[:-1]:
if k not in current:
current[k] = {}
current = current[k]
if keys[-1] not in current:
current[keys[-1]] = []
current[keys[-1]].append(obj)
save_data(config)
def write_config(key, value):
config = None
with open("./config/config.yml", "r", encoding="utf-8") as f:
config = yaml_.YAML().load(f)
keys = key.split(".")
current = config
for k in keys[:-1]:
if k not in current:
current[k] = {}
current = current[k]
current[keys[-1]] = value
# 设置保留注释和空行的参数
y = yaml_.YAML()
y.preserve_quotes = True
y.preserve_blank_lines = True
# 写入配置并保留注释和空行
with open("./config/config.yml", "w", encoding="utf-8") as f:
y.dump(config, f)
def read_default_config(key):
try:
config = default
keys = key.split(".")
value = config
for k in keys:
if isinstance(value, dict):
if k not in value and keys.index(k) != len(keys) - 1:
value[k] = {}
elif k not in value and keys.index(k) == len(keys) - 1:
value = None
value = value[k]
else:
value = None
break
return value
except:
return None
def _read_config(key):
try:
config = variable.config
keys = key.split(".")
value = config
for k in keys:
if isinstance(value, dict):
if k not in value and keys.index(k) != len(keys) - 1:
value[k] = None
elif k not in value and keys.index(k) == len(keys) - 1:
value = None
value = value[k]
else:
value = None
break
return value
except (KeyError, TypeError):
return None
def read_config(key):
try:
config = variable.config
keys = key.split(".")
value = config
for k in keys:
if isinstance(value, dict):
if k not in value and keys.index(k) != len(keys) - 1:
value[k] = {}
elif k not in value and keys.index(k) == len(keys) - 1:
value = None
value = value[k]
else:
value = None
break
return value
except:
default_value = read_default_config(key)
if isinstance(default_value, type(None)):
logger.warning(f"配置文件{key}不存在")
else:
for i in range(len(keys)):
tk = ".".join(keys[: (i + 1)])
tkvalue = _read_config(tk)
logger.debug(f"configfix: 读取配置文件{tk}的值:{tkvalue}")
if (tkvalue is None) or (tkvalue == {}):
write_config(tk, read_default_config(tk))
logger.info(f"配置文件{tk}不存在,已创建")
return default_value
def write_data(key, value):
config = load_data()
keys = key.split(".")
current = config
for k in keys[:-1]:
if k not in current:
current[k] = {}
current = current[k]
current[keys[-1]] = value
save_data(config)
def init_config():
if not os.path.exists("./config"):
os.mkdir("config")
if os.path.exists("./config.json"):
shutil.move("config.json", "./config")
if os.path.exists("./data.db"):
shutil.move("./data.db", "./config")
if os.path.exists("./config/config.json"):
os.rename("./config/config.json", "./config/config.json.bak")
handle_default_config()
logger.warning("json配置文件已不再使用,已将其重命名为config.json.bak")
logger.warning("配置文件不会自动更新(因为变化太大),请手动修改配置文件重启服务器")
sys.exit(0)
try:
with open("./config/config.yml", "r", encoding="utf-8") as f:
try:
variable.config = yaml.load(f.read())
if not isinstance(variable.config, dict):
logger.warning("配置文件并不是一个有效的字典,使用默认值")
variable.config = default
with open("./config/config.yml", "w", encoding="utf-8") as f:
yaml.dump(variable.config, f)
f.close()
except:
if os.path.getsize("./config/config.yml") != 0:
logger.error("配置文件加载失败,请检查是否遵循YAML语法规范")
sys.exit(1)
else:
variable.config = handle_default_config()
except FileNotFoundError:
variable.config = handle_default_config()
# print(variable.config)
variable.log_length_limit = read_config("common.log_length_limit")
variable.debug_mode = read_config("common.debug_mode")
logger.debug("配置文件加载成功")
# 尝试连接数据库
handle_connect_db()
conn = sqlite3.connect("./cache.db")
# 创建一个游标对象
cursor = conn.cursor()
# 创建一个表来存储缓存数据
cursor.execute(
"""CREATE TABLE IF NOT EXISTS cache
(id INTEGER PRIMARY KEY AUTOINCREMENT,
module TEXT NOT NULL,
key TEXT NOT NULL,
data TEXT NOT NULL)"""
)
conn.close()
conn2 = sqlite3.connect("./config/data.db")
# 创建一个游标对象
cursor2 = conn2.cursor()
cursor2.execute(
"""CREATE TABLE IF NOT EXISTS data
(key TEXT PRIMARY KEY,
value TEXT)"""
)
conn2.close()
logger.debug("数据库初始化成功")
# handle data
all_data_keys = {"banList": [], "requestTime": {}, "banListRaw": []}
data = load_data()
if data == {}:
write_data("banList", [])
write_data("requestTime", {})
logger.info("数据库内容为空,已写入默认值")
for k, v in all_data_keys.items():
if k not in data:
write_data(k, v)
logger.info(f"数据库中不存在{k},已创建")
# 处理代理配置
if read_config("common.proxy.enable"):
if read_config("common.proxy.http_value"):
os.environ["http_proxy"] = read_config("common.proxy.http_value")
logger.info("HTTP协议代理地址: " + read_config("common.proxy.http_value"))
if read_config("common.proxy.https_value"):
os.environ["https_proxy"] = read_config("common.proxy.https_value")
logger.info("HTTPS协议代理地址: " + read_config("common.proxy.https_value"))
logger.info("代理功能已开启,请确保代理地址正确,否则无法连接网络")
# cookie池
if read_config("common.cookiepool"):
logger.info("已启用cookie池功能,请确定配置的cookie都能正确获取链接")
logger.info("传统的源 - 单用户cookie配置将被忽略")
logger.info("所以即使某个源你只有一个cookie,也请填写到cookiepool对应的源中,否则将无法使用该cookie")
variable.use_cookie_pool = True
# 移除已经过期的封禁数据
banlist = read_data("banList")
banlistRaw = read_data("banListRaw")
count = 0
for b in banlist:
if b["expire"] and (time.time() > b["expire_time"]):
count += 1
banlist.remove(b)
if b["ip"] in banlistRaw:
banlistRaw.remove(b["ip"])
write_data("banList", banlist)
write_data("banListRaw", banlistRaw)
if count != 0:
logger.info(f"已移除{count}条过期封禁数据")
# 处理旧版数据库的banListRaw
banlist = read_data("banList")
banlistRaw = read_data("banListRaw")
if banlist != [] and banlistRaw == []:
for b in banlist:
banlistRaw.append(b["ip"])
return
def ban_ip(ip_addr, ban_time=-1):
if read_config("security.banlist.enable"):
banList = read_data("banList")
banList.append(
{
"ip": ip_addr,
"expire": read_config("security.banlist.expire.enable"),
"expire_time": read_config("security.banlist.expire.length") if (ban_time == -1) else ban_time,
}
)
write_data("banList", banList)
banListRaw = read_data("banListRaw")
if ip_addr not in banListRaw:
banListRaw.append(ip_addr)
write_data("banListRaw", banListRaw)
else:
if variable.banList_suggest < 10:
variable.banList_suggest += 1
logger.warning("黑名单功能已被关闭,我们墙裂建议你开启这个功能以防止恶意请求")
def check_ip_banned(ip_addr):
if read_config("security.banlist.enable"):
banList = read_data("banList")
banlistRaw = read_data("banListRaw")
if ip_addr in banlistRaw:
for b in banList:
if b["ip"] == ip_addr:
if b["expire"]:
if b["expire_time"] > int(time.time()):
return True
else:
banList.remove(b)
banlistRaw.remove(b["ip"])
write_data("banListRaw", banlistRaw)
write_data("banList", banList)
return False
else:
return True
else:
return False
return False
else:
return False
else:
if variable.banList_suggest <= 10:
variable.banList_suggest += 1
logger.warning("黑名单功能已被关闭,我们墙裂建议你开启这个功能以防止恶意请求")
return False
init_config()