Init: v1 release
This commit is contained in:
61
sdgb/sdgb.py
Normal file
61
sdgb/sdgb.py
Normal file
@@ -0,0 +1,61 @@
|
||||
import asyncio
|
||||
import httpx
|
||||
import json
|
||||
import logging
|
||||
import time
|
||||
from encrypt import *
|
||||
|
||||
# 配置日志,方便调试看请求顺序
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format='%(asctime)s - %(levelname)s - %(message)s'
|
||||
)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class MaimaiClient:
|
||||
def __init__(self):
|
||||
self.base_url = f"https://maimai-gm.wahlap.com:42081/Maimai2Servlet/"
|
||||
self.aes = aes_pkcs7(AesKey, AesIV)
|
||||
|
||||
async def call_api(self, client: httpx.AsyncClient, ApiType: str, data: dict, userId: int):
|
||||
"""
|
||||
先压缩再加密请求数据,发送请求后解密再解压响应数据
|
||||
这里的 client 需要传入外部创建的 httpx.AsyncClient 实例
|
||||
"""
|
||||
|
||||
ApiTypeHash = get_hash_api(ApiType)
|
||||
url = f"{self.base_url}{ApiTypeHash}"
|
||||
|
||||
headers = {
|
||||
"User-Agent": f"{ApiTypeHash}#{userId}",
|
||||
"Content-Type": "application/json",
|
||||
"Mai-Encoding": "1.50",
|
||||
"Accept-Encoding": "",
|
||||
"Charset": "UTF-8",
|
||||
"Content-Encoding": "deflate"
|
||||
}
|
||||
|
||||
data = bytes(json.dumps(data), encoding="utf-8")
|
||||
CompressedData = zlib.compress(data)
|
||||
AESEncrptedData = self.aes.encrypt(CompressedData)
|
||||
|
||||
try:
|
||||
# 这里的 timeout 设置稍微长一点,防止服务端处理慢
|
||||
resp = await client.post(url, headers=headers, data=AESEncrptedData, timeout=10.0)
|
||||
resp.raise_for_status() # 如果状态码不是 2xx 则抛出异常
|
||||
|
||||
AESEncrptedResponse = resp.content
|
||||
DecryptedData = self.aes.decrypt(AESEncrptedResponse)
|
||||
UncompressedData = zlib.decompress(DecryptedData).decode('utf-8')
|
||||
|
||||
logger.info(f"✅ [SUCCESS] {ApiType} - {UncompressedData}")
|
||||
|
||||
return UncompressedData
|
||||
|
||||
|
||||
except httpx.HTTPStatusError as e:
|
||||
logger.error(f"❌ [HTTP ERROR] {ApiType}: {e.response.status_code}")
|
||||
return None
|
||||
except Exception as e:
|
||||
logger.error(f"❌ [ERROR] {ApiType}: {str(e)}")
|
||||
return None
|
||||
Reference in New Issue
Block a user