Skip to content

Commit

Permalink
feat: 久违的更新awa
Browse files Browse the repository at this point in the history
  • Loading branch information
helloplhm-qwq committed Jun 7, 2024
1 parent f93a888 commit f8b0126
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 5 deletions.
9 changes: 9 additions & 0 deletions common/default_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,15 @@
headers:
User-Agent: okhttp/3.10.0
gcsp: # 歌词适配后端配置
# 请注意只允许私用,不要给原作者带来麻烦,谢谢
enable: false # 是否启用歌词适配后端
path: /client/cgi-bin/api.fcg # 后端接口地址
enable_verify: false # 是否启用后端验证
package_md5: "" # apk包的md5值,用于验证
salt_1: "NDRjZGIzNzliNzEe" # 后端验证参数1
salt_2: "6562653262383463363633646364306534333668" # 后端验证参数2
cookiepool:
kg:
- userid: '0'
Expand Down
86 changes: 86 additions & 0 deletions common/gcsp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# ----------------------------------------
# - mode: python -
# - author: helloplhm-qwq -
# - name: gcsp.py -
# - project: moonsea_api(private) -
# - license: MIT -
# ----------------------------------------
# This file is part of the "moonsea_api" project and featured for the "lx-music-api-server" project.

import zlib
import binascii
import time
import ujson as json
import modules
from .utils import createMD5 as hashMd5
from . import config
from aiohttp.web import Response

PACKAGE = config.read_config("module.gcsp.package_md5") # pkg md5
SALT_1 = config.read_config("module.gcsp.salt_1") # salt 1
SALT_2 = config.read_config("module.gcsp.salt_2") # salt 2
NEED_VERIFY = config.read_config("module.gcsp.enable_verify") # need verify

qm = {
'mp3': '128k',
'hq': '320k',
'sq': 'flac',
"hr": "flac24bit",
'hires': 'flac24bit'
}

pm = {
'qq': 'tx',
'wyy': 'wy',
'kugou': 'kg',
"kuwo": "kw",
"mgu": "mg"
}

internal_trans = {
"time": "请求检验失败,请检查系统时间是否为标准时间",
"sign": "请求检验失败,请检查应用是否被修改或更新到最新版本",
}

def decode(indata):
return json.loads(binascii.unhexlify(zlib.decompress(indata)))

def verify(data):
if (not NEED_VERIFY):
return "success"
sign_1 = hashMd5(PACKAGE + data["time"] + SALT_2)
sign_2 = hashMd5((json.dumps(data["text_1"]) + json.dumps(data["text_2"]) + sign_1 + data["time"] + SALT_1).replace("\\", "").replace("}\"", "}").replace("\"{", "{"))

if (data["sign_1"] != sign_1 or data["sign_2"] != sign_2):
return "sign"
if int(time.time()) - int(data["time"]) > 10:
return "time"
return "success"

async def handleGcspBody(body):
data = decode(body)
result = verify(data)
if (result != "success"):
return zlib.compress(json.dumps({"code": "403", "error_msg": internal_trans[result], "data": None}, ensure_ascii = False).encode("utf-8")), 200

data["te"] = json.loads(data["text_1"])

body = modules.url(pm[data["te"]["platform"]], data["te"]["t1"], qm[data["te"]["t2"]])

if (body["code"] == 0):
return zlib.compress(json.dumps({"code": "200", "error_msg": "success", "data": body["data"] if (pm[data["te"]["platform"]] != "kw") else {"bitrate": "123", "url": body["data"]}}, ensure_ascii = False).encode("utf-8")), 200
else:
return zlib.compress(json.dumps({"code": "403", "error_msg": "内部系统错误,请稍后再试", "data": None}, ensure_ascii = False).encode("utf-8")), 200

async def handle_request(request):
if (request.method == "POST"):
body = await request.body()
return Response(
body = await handleGcspBody(body),
content_type = "application/octet-stream"
)
else:
return Response(
body = "Method Not Allowed",
status = 405
)
2 changes: 1 addition & 1 deletion common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def filterFileName(filename):
# 将不合法字符替换为下划线
return re.sub(illegal_chars, '_', filename)

def createMD5(s: (str, bytes)):
def createMD5(s: (str | bytes)):
if (isinstance(s, str)):
s = s.encode("utf-8")
return hashlib.md5(s).hexdigest()
Expand Down
10 changes: 7 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import traceback
import threading
import ujson as json
from aiohttp.web import Response, FileResponse, StreamResponse
from aiohttp.web import Response, FileResponse, StreamResponse, Application
from io import TextIOWrapper
import sys
import os
Expand All @@ -35,6 +35,7 @@
from common import variable
from common import scheduler
from common import lx_script
from common import gcsp
import modules

def handleResult(dic, status=200) -> Response:
Expand Down Expand Up @@ -209,7 +210,7 @@ async def handle_local(request):
'data': localMusic.checkLocalMusic(data['p'])
}

app = aiohttp.web.Application(middlewares=[handle_before_request])
app = Application(middlewares=[handle_before_request])
utils.setGlobal(app, "app")

# mainpage
Expand All @@ -223,6 +224,9 @@ async def handle_local(request):
if (config.read_config('common.allow_download_script')):
app.router.add_get('/script', lx_script.generate_script_response)

if (config.read_config('module.gcsp.enable')):
app.router.add_route('*', config.read_config('module.gcsp.path'), gcsp.handle_gcsp)

# 404
app.router.add_route('*', '/{tail:.*}', handle_404)

Expand Down Expand Up @@ -286,7 +290,7 @@ async def run_app_host(host):
https_runner, host, port, ssl_context=ssl_context)
await https_site.start()
variable.running_ports.append(f'{host}_{port}')
logger.info(f"""监听 -> http://{
logger.info(f"""监听 -> https://{
host if (':' not in host)
else '[' + host + ']'
}:{port}""")
Expand Down
2 changes: 1 addition & 1 deletion modules/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
}


async def url(source, songId, quality, query):
async def url(source, songId, quality, query = {}):
if (not quality):
return {
'code': 2,
Expand Down
6 changes: 6 additions & 0 deletions termux.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# for installing the dependencies on Termux.
# ruamel-yaml-clib need to build from source, but it will throw an error during
# the build process in default configuration. To fix this, we need to set
# CFLAGS environment variable to "-Wno-incompatible-function-pointer-types".
# Resolution from: https://github.com/termux/termux-packages/issues/16746

import os
import subprocess

Expand Down

0 comments on commit f8b0126

Please sign in to comment.