Skip to content

Commit

Permalink
feat: 支持多个请求key
Browse files Browse the repository at this point in the history
  • Loading branch information
helloplhm-qwq committed Feb 6, 2024
1 parent 46a270f commit 6ef26f8
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion common/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class ConfigReadException(Exception):
"enable": False,
"_enable-desc": "是否开启请求key,开启后只有请求头中包含key,且值一样时可以访问API",
"ban": True,
"value": "114514",
"values": ["114514"],
},
"whitelist_host": [
"localhost",
Expand Down
6 changes: 3 additions & 3 deletions common/lx_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ async def get_script():
logger.warning('请求源脚本内容失败')

async def generate_script_response(request):
if (request.query.get('key') != config.read_config('security.key.value') and config.read_config('security.key.enable')):
if (request.query.get('key') not in config.read_config('security.key.values') and config.read_config('security.key.enable')):
return {'code': 6, 'msg': 'key验证失败', 'data': None}, 403
try:
with open('./lx-music-source-example.js', 'r', encoding='utf-8') as f:
Expand All @@ -83,13 +83,13 @@ async def generate_script_response(request):
if (line.startswith('const API_URL')):
newScriptLines.append(f'''const API_URL = "{'https' if config.read_config('common.ssl_info.is_https') else 'http'}://{request.host}"''')
elif (line.startswith('const API_KEY')):
newScriptLines.append(f'const API_KEY = "{config.read_config("security.key.value")}"')
newScriptLines.append(f"""const API_KEY = `{request.query.get("key") if request.query.get("key") else ''''''}`""")
elif (line.startswith("* @name")):
newScriptLines.append(" * @name " + config.read_config("common.download_config.name"))
elif (line.startswith("* @description")):
newScriptLines.append(" * @description " + config.read_config("common.download_config.intro"))
elif (line.startswith("* @author")):
newScriptLines.append((" * @author helloplhm-qwq & Folltoshe & " + config.read_config("common.download_config.author")) if config.read_config("common.download_config.author") else " * @author helloplhm-qwq & Folltoshe")
newScriptLines.append(" * @author " + config.read_config("common.download_config.author"))
elif (line.startswith("* @version")):
newScriptLines.append(" * @version " + config.read_config("common.download_config.version"))
elif (line.startswith("const DEV_ENABLE ")):
Expand Down
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ async def handle(request):
songId = request.match_info.get('songId')
quality = request.match_info.get('quality')
if (config.read_config("security.key.enable") and request.host.split(':')[0] not in config.read_config('security.whitelist_host')):
if (request.headers.get("X-Request-Key")) != config.read_config("security.key.value"):
if (request.headers.get("X-Request-Key")) not in config.read_config("security.key.values"):
if (config.read_config("security.key.ban")):
config.ban_ip(request.remote_addr)
return handleResult({"code": 1, "msg": "key验证失败", "data": None}, 403)
Expand Down

0 comments on commit 6ef26f8

Please # to comment.