feat: 接入黄豆短剧独立清单与下载

This commit is contained in:
Nixevol
2026-10-21 22:54:56 +08:00
parent e31e80384c
commit 5b4a948e5f
25 changed files with 2093 additions and 132 deletions
+115
View File
@@ -0,0 +1,115 @@
import gzip
import hashlib
import hmac
import json
import os
import re
import uuid
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.primitives.padding import PKCS7
WEB_AES_KEY = "7961beb44246e3012ce228d6b5ced05a"
IMAGE_AES_KEY = "525202f9149e061d"
SIGN_PREFIX = "Dart"
APP_VERSION = "2.0.0"
DEVICE_TYPE = "web"
LANG = "zh-CN"
USER_AGENT = (
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
"(KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36"
)
_SCHEME = re.compile(r"^https?://", re.I)
_IMAGE_MAGIC = (
b"\xff\xd8\xff",
b"\x89PNG",
b"GIF8",
b"RIFF",
b"\x00\x00\x00 ftyp",
)
def new_uuid() -> str:
return str(uuid.uuid4())
def session_id_of(value: str) -> str:
text = (value or "").strip()
if not text:
text = new_uuid()
return text.replace("-", "")
def derive_aes_key(request_id: str, secret: str = WEB_AES_KEY) -> bytes:
raw = bytes.fromhex(request_id.replace("-", ""))
return hmac.new(secret.encode("utf-8"), raw, hashlib.sha256).digest()
def sign_header(session_id: str, request_id: str, time_text: str, url: str) -> str:
path = _SCHEME.sub("", url)
payload = f"{SIGN_PREFIX}|{session_id}|{request_id}|{time_text}|{path}"
digest = hashlib.md5(payload.encode("utf-8")).hexdigest()
return f"{digest}-{time_text}"
def _aes_cbc(key: bytes, iv: bytes, data: bytes, encrypt: bool) -> bytes:
cipher = Cipher(algorithms.AES(key), modes.CBC(iv))
if encrypt:
padder = PKCS7(128).padder()
blob = padder.update(data) + padder.finalize()
engine = cipher.encryptor()
return engine.update(blob) + engine.finalize()
engine = cipher.decryptor()
blob = engine.update(data) + engine.finalize()
unpadder = PKCS7(128).unpadder()
return unpadder.update(blob) + unpadder.finalize()
def encrypt_body(payload: dict, request_id: str, secret: str = WEB_AES_KEY) -> bytes:
plain = json.dumps(payload, ensure_ascii=False, separators=(",", ":")).encode("utf-8")
packed = gzip.compress(plain)
iv = os.urandom(16)
key = derive_aes_key(request_id, secret)
return iv + _aes_cbc(key, iv, packed, True)
def decrypt_body(blob: bytes, request_id: str, secret: str = WEB_AES_KEY) -> dict:
if len(blob) < 32:
raise RuntimeError("响应数据格式错误")
iv, data = blob[:16], blob[16:]
key = derive_aes_key(request_id, secret)
try:
raw = _aes_cbc(key, iv, data, False)
except ValueError as exc:
raise RuntimeError("响应数据格式错误") from exc
try:
text = gzip.decompress(raw).decode("utf-8")
except OSError:
text = raw.decode("utf-8")
try:
parsed = json.loads(text)
except json.JSONDecodeError as exc:
raise RuntimeError("响应数据格式错误") from exc
if not isinstance(parsed, dict):
raise RuntimeError("响应数据格式错误")
return parsed
def decrypt_resource(data: bytes, key: str = IMAGE_AES_KEY) -> bytes:
if _looks_like_image(data) or len(data) < 16 or len(data) % 16 != 0:
return data
secret = key.encode("utf-8")
cipher = Cipher(algorithms.AES(secret), modes.ECB())
engine = cipher.decryptor()
blob = engine.update(data) + engine.finalize()
try:
unpadder = PKCS7(128).unpadder()
plain = unpadder.update(blob) + unpadder.finalize()
except ValueError:
return data
return plain if _looks_like_image(plain) else data
def _looks_like_image(data: bytes) -> bool:
return any(data.startswith(magic) for magic in _IMAGE_MAGIC)