From 465211ac964880156b398f21353e35e9e543fcc9 Mon Sep 17 00:00:00 2001 From: Remik1r3n Date: Wed, 11 Jun 2025 10:31:51 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=BA=E4=BA=86production=20ready=E6=9A=82?= =?UTF-8?q?=E6=97=B6=E6=81=A2=E5=A4=8D=E5=88=B02024=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E5=99=A8=20=E7=AD=89=E5=85=B3=E6=9C=8D=E5=86=8D=E8=AF=B4?= =?UTF-8?q?=E5=90=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- API_TitleServer.py | 136 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 135 insertions(+), 1 deletion(-) diff --git a/API_TitleServer.py b/API_TitleServer.py index aeafb03..d942c79 100644 --- a/API_TitleServer.py +++ b/API_TitleServer.py @@ -71,7 +71,7 @@ def getSDGBApiHash(api): # 有空做一下 Hash 的彩虹表? return hashlib.md5((api+"MaimaiChn"+ObfuscateParam).encode()).hexdigest() -def apiSDGB( +def apiSDGB2( data: str, targetApi: str, userAgentExtraData: str, @@ -222,3 +222,137 @@ def calcSpecialNumber2(): return num3 """ + + + + + + +# 舞萌DX 2024 +# omg it's leaking +AesKey = "n7bx6:@Fg_:2;5E89Phy7AyIcpxEQ:R@" +AesIV = ";;KjR1C3hgB1ovXa" +ObfuscateParam = "BEs2D5vW" + +class SDGBApiError(Exception): + pass + +class SDGBRequestError(SDGBApiError): + pass + +class SDGBResponseError(SDGBApiError): + pass + +class AESPKCS7: + # 实现了 maimai 通讯所用的 AES 加密的类 + def __init__(self, key: str, iv: str): + self.key = key.encode('utf-8') + self.iv = iv.encode('utf-8') + self.mode = AES.MODE_CBC + # 加密 + def encrypt(self, content) -> bytes: + # if content is str, convert to bytes + if isinstance(content, str): + encodedData = content.encode('utf-8') + cipher = AES.new(self.key, self.mode, self.iv) + content_padded = pad(encodedData, AES.block_size) + encrypted_bytes = cipher.encrypt(content_padded) + return encrypted_bytes + # 解密 + def decrypt(self, encrypted_content: bytes) -> str: + cipher = AES.new(self.key, self.mode, self.iv) + decrypted_padded = cipher.decrypt(encrypted_content) + decrypted = unpad(decrypted_padded, AES.block_size) + return decrypted + +def apiSDGB(data:str, targetApi:str, userAgentExtraData:str, noLog:bool=False, timeout:int=5): + """ + 舞萌DX 2024 API 通讯用函数 + :param data: 请求数据 + :param targetApi: 使用的 API + :param userAgentExtraData: UA 附加信息,机台相关则为狗号(如A63E01E9564),用户相关则为 UID + :param noLog: 是否不记录日志 + """ + maxRetries = 3 + agentExtra = str(userAgentExtraData) + aes = AESPKCS7(AesKey, AesIV) + reqData_encrypted = aes.encrypt(data) + reqData_deflated = zlib.compress(reqData_encrypted) + endpoint = "https://maimai-gm.wahlap.com:42081/Maimai2Servlet/" + if not noLog: + logger.debug(f"开始请求 {targetApi},以 {data}") + + retries = 0 + while retries < maxRetries: + try: + if useProxy: + # 使用代理 + logger.debug("使用代理") + httpClient = httpx.Client(proxy=proxyUrl, verify=False) + else: + # 不使用代理 + logger.debug("不使用代理") + httpClient = httpx.Client(verify=False) + responseOriginal = httpClient.post( + url=endpoint + getSDGBApiHash(targetApi), + headers={ + "User-Agent": f"{getSDGBApiHash(targetApi)}#{agentExtra}", + "Content-Type": "application/json", + "Mai-Encoding": "1.40", + "Accept-Encoding": "", + "Charset": "UTF-8", + "Content-Encoding": "deflate", + "Expect": "100-continue" + }, + content=reqData_deflated, + # 经测试,加 Verify 之后速度慢好多,因此建议选择性开 + #verify=certifi.where(), + #verify=False, + timeout=timeout + ) + + if not noLog: + logger.info(f"{targetApi} 请求结果: {responseOriginal.status_code}") + + if responseOriginal.status_code == 200: + logger.debug("200 OK!") + else: + errorMessage = f"请求失败: {responseOriginal.status_code}" + logger.error(errorMessage) + raise SDGBRequestError(errorMessage) + + responseRAWContent = responseOriginal.content + + try: + responseDecompressed = zlib.decompress(responseRAWContent) + logger.debug("成功解压响应!") + except: + logger.warning(f"无法解压,得到的原始响应: {responseRAWContent}") + raise SDGBResponseError("解压失败") + try: + resultResponse = aes.decrypt(responseDecompressed) + logger.debug(f"成功解密响应!") + except: + logger.warning(f"解密失败,得到的原始响应: {responseDecompressed}") + raise SDGBResponseError("解密失败") + + if not noLog: + logger.debug(f"响应: {resultResponse}") + return resultResponse + + # 异常处理 + except SDGBRequestError as e: + # 请求格式错误,不需要重试 + raise SDGBRequestError("请求格式错误") + except SDGBResponseError as e: + # 响应解析错误,这种有一定可能是我们的问题,所以只重试一次 + logger.warning(f"将重试一次 Resp Err: {e}") + retries += 2 + time.sleep(2) + except Exception as e: + # 其他错误,重试多次 + logger.warning(f"将开始重试请求. {e}") + retries += 1 + time.sleep(2) + + raise SDGBApiError("重试多次仍然无法成功请求服务器")