chore: totally fix lint issues
This commit is contained in:
@@ -1,18 +1,20 @@
|
||||
# 舞萌DX
|
||||
# 标题服务器通讯实现
|
||||
|
||||
import zlib
|
||||
import hashlib
|
||||
import httpx
|
||||
from loguru import logger
|
||||
import random
|
||||
import time
|
||||
|
||||
import httpx
|
||||
from loguru import logger
|
||||
from ctypes import c_int32
|
||||
from Crypto.Cipher import AES
|
||||
from Crypto.Util.Padding import pad, unpad
|
||||
from Config import *
|
||||
from typing import Optional
|
||||
import certifi
|
||||
|
||||
from Config import (
|
||||
useProxy,
|
||||
proxyUrl,
|
||||
)
|
||||
|
||||
use2024Api = False # 是否使用 2024 API
|
||||
|
||||
@@ -25,19 +27,23 @@ else:
|
||||
AesIV = "d6xHIKq]1J]Dt^ue"
|
||||
ObfuscateParam = "B44df8yT"
|
||||
|
||||
|
||||
class SDGBApiError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
|
||||
class SDGBRequestError(SDGBApiError):
|
||||
pass
|
||||
|
||||
|
||||
class SDGBResponseError(SDGBApiError):
|
||||
pass
|
||||
|
||||
|
||||
class aes_pkcs7(object):
|
||||
def __init__(self, key: str, iv: str):
|
||||
self.key = key.encode('utf-8')
|
||||
self.iv = iv.encode('utf-8')
|
||||
self.key = key.encode("utf-8")
|
||||
self.iv = iv.encode("utf-8")
|
||||
self.mode = AES.MODE_CBC
|
||||
|
||||
def encrypt(self, content: bytes) -> bytes:
|
||||
@@ -55,21 +61,23 @@ class aes_pkcs7(object):
|
||||
def pkcs7unpadding(self, text):
|
||||
length = len(text)
|
||||
unpadding = ord(text[length - 1])
|
||||
return text[0:length - unpadding]
|
||||
return text[0 : length - unpadding]
|
||||
|
||||
def pkcs7padding(self, text):
|
||||
bs = 16
|
||||
length = len(text)
|
||||
bytes_length = len(text.encode('utf-8'))
|
||||
bytes_length = len(text.encode("utf-8"))
|
||||
padding_size = length if (bytes_length == length) else bytes_length
|
||||
padding = bs - padding_size % bs
|
||||
padding_text = chr(padding) * padding
|
||||
return text + padding_text
|
||||
|
||||
|
||||
def getSDGBApiHash(api):
|
||||
# API 的 Hash 的生成
|
||||
# 有空做一下 Hash 的彩虹表?
|
||||
return hashlib.md5((api+"MaimaiChn"+ObfuscateParam).encode()).hexdigest()
|
||||
return hashlib.md5((api + "MaimaiChn" + ObfuscateParam).encode()).hexdigest()
|
||||
|
||||
|
||||
def apiSDGB(
|
||||
data: str,
|
||||
@@ -94,7 +102,7 @@ def apiSDGB(
|
||||
endpoint = "https://maimai-gm.wahlap.com:42081/Maimai2Servlet/"
|
||||
|
||||
# 准备好请求数据
|
||||
requestDataFinal = aes.encrypt(zlib.compress(data.encode('utf-8')))
|
||||
requestDataFinal = aes.encrypt(zlib.compress(data.encode("utf-8")))
|
||||
|
||||
if not noLog:
|
||||
logger.debug(f"[Stage 1] 准备开始请求 {targetApi},以 {data}")
|
||||
@@ -108,7 +116,7 @@ def apiSDGB(
|
||||
httpClient = httpx.Client(proxy=proxyUrl, verify=False)
|
||||
else:
|
||||
httpClient = httpx.Client(verify=False)
|
||||
|
||||
|
||||
# 发送请求
|
||||
response = httpClient.post(
|
||||
url=endpoint + getSDGBApiHash(targetApi),
|
||||
@@ -119,10 +127,10 @@ def apiSDGB(
|
||||
"Accept-Encoding": "",
|
||||
"Charset": "UTF-8",
|
||||
"Content-Encoding": "deflate",
|
||||
"Expect": "100-continue"
|
||||
"Expect": "100-continue",
|
||||
},
|
||||
content=requestDataFinal, #数据
|
||||
timeout=timeout
|
||||
content=requestDataFinal, # 数据
|
||||
timeout=timeout,
|
||||
)
|
||||
|
||||
if not noLog:
|
||||
@@ -142,30 +150,42 @@ def apiSDGB(
|
||||
if not noLog:
|
||||
logger.debug("[Stage 3] Decryption SUCCESS.")
|
||||
except Exception as e:
|
||||
logger.warning(f"[Stage 3] Decryption FAILED. Raw Content: {responseContentRaw}, Error: {e}")
|
||||
logger.warning(
|
||||
f"[Stage 3] Decryption FAILED. Raw Content: {responseContentRaw}, Error: {e}"
|
||||
)
|
||||
raise SDGBResponseError("Decryption failed")
|
||||
# 然后尝试解压
|
||||
try:
|
||||
# 看看文件头是否是压缩过的
|
||||
if responseContentDecrypted.startswith(b'\x78\x9c'):
|
||||
if responseContentDecrypted.startswith(b"\x78\x9c"):
|
||||
logger.debug("[Stage 4] Zlib detected, decompressing...")
|
||||
responseContentFinal = zlib.decompress(responseContentDecrypted).decode('utf-8')
|
||||
responseContentFinal = zlib.decompress(
|
||||
responseContentDecrypted
|
||||
).decode("utf-8")
|
||||
else:
|
||||
logger.warning(f"[Stage 4] Not Zlib Format!! using raw content: {responseContentDecrypted}")
|
||||
responseContentFinal = responseContentDecrypted.decode('utf-8')
|
||||
logger.warning(
|
||||
f"[Stage 4] Not Zlib Format!! using raw content: {responseContentDecrypted}"
|
||||
)
|
||||
responseContentFinal = responseContentDecrypted.decode("utf-8")
|
||||
# 完成解压
|
||||
if not noLog:
|
||||
logger.debug(f"[Stage 4] Process OK, Content: {responseContentFinal}")
|
||||
logger.debug(
|
||||
f"[Stage 4] Process OK, Content: {responseContentFinal}"
|
||||
)
|
||||
# 最终处理,检查是否是 JSON 格式
|
||||
if responseContentFinal.startswith('{') and responseContentFinal.endswith('}'):
|
||||
if responseContentFinal.startswith(
|
||||
"{"
|
||||
) and responseContentFinal.endswith("}"):
|
||||
# 如果是 JSON 格式,直接返回
|
||||
logger.debug("[Stage 5] Response is JSON, returning.")
|
||||
return responseContentFinal
|
||||
else:
|
||||
# 如果不是 JSON 格式,直接返回但是警告
|
||||
logger.warning("[Stage 5] Response is not JSON, returning as is, take care!")
|
||||
logger.warning(
|
||||
"[Stage 5] Response is not JSON, returning as is, take care!"
|
||||
)
|
||||
return responseContentFinal
|
||||
except:
|
||||
except Exception:
|
||||
logger.warning(f"解压失败,原始响应: {responseContentDecrypted}")
|
||||
raise SDGBResponseError("解压失败")
|
||||
except SDGBRequestError as e:
|
||||
@@ -181,16 +201,17 @@ def apiSDGB(
|
||||
time.sleep(2)
|
||||
|
||||
finally:
|
||||
if 'httpClient' in locals():
|
||||
if "httpClient" in locals():
|
||||
httpClient.close()
|
||||
|
||||
raise SDGBApiError("重试多次仍然无法成功请求服务器")
|
||||
|
||||
|
||||
def calcPlaySpecial():
|
||||
"""使用 c_int32 实现的 SpecialNumber 算法"""
|
||||
rng = random.SystemRandom()
|
||||
num2 = rng.randint(1, 1037933) * 2069
|
||||
num2 += 1024 #GameManager.CalcSpecialNum()
|
||||
num2 += 1024 # GameManager.CalcSpecialNum()
|
||||
num2 = c_int32(num2).value
|
||||
result = c_int32(0)
|
||||
for _ in range(32):
|
||||
@@ -199,21 +220,24 @@ def calcPlaySpecial():
|
||||
num2 >>= 1
|
||||
return c_int32(result.value).value
|
||||
|
||||
|
||||
class AESPKCS7_2024:
|
||||
# 实现了 maimai 通讯所用的 AES 加密的类
|
||||
def __init__(self, key: str, iv: str):
|
||||
self.key = key.encode('utf-8')
|
||||
self.iv = iv.encode('utf-8')
|
||||
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')
|
||||
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)
|
||||
@@ -221,7 +245,14 @@ class AESPKCS7_2024:
|
||||
decrypted = unpad(decrypted_padded, AES.block_size)
|
||||
return decrypted
|
||||
|
||||
def apiSDGB_2024(data:str, targetApi:str, userAgentExtraData:str, noLog:bool=False, timeout:int=5):
|
||||
|
||||
def apiSDGB_2024(
|
||||
data: str,
|
||||
targetApi: str,
|
||||
userAgentExtraData: str,
|
||||
noLog: bool = False,
|
||||
timeout: int = 5,
|
||||
):
|
||||
"""
|
||||
舞萌DX 2024 API 通讯用函数
|
||||
:param data: 请求数据
|
||||
@@ -258,13 +289,13 @@ def apiSDGB_2024(data:str, targetApi:str, userAgentExtraData:str, noLog:bool=Fal
|
||||
"Accept-Encoding": "",
|
||||
"Charset": "UTF-8",
|
||||
"Content-Encoding": "deflate",
|
||||
"Expect": "100-continue"
|
||||
"Expect": "100-continue",
|
||||
},
|
||||
content=reqData_deflated,
|
||||
# 经测试,加 Verify 之后速度慢好多,因此建议选择性开
|
||||
#verify=certifi.where(),
|
||||
#verify=False,
|
||||
timeout=timeout
|
||||
# verify=certifi.where(),
|
||||
# verify=False,
|
||||
timeout=timeout,
|
||||
)
|
||||
|
||||
if not noLog:
|
||||
@@ -282,22 +313,22 @@ def apiSDGB_2024(data:str, targetApi:str, userAgentExtraData:str, noLog:bool=Fal
|
||||
try:
|
||||
responseDecompressed = zlib.decompress(responseRAWContent)
|
||||
logger.debug("成功解压响应!")
|
||||
except:
|
||||
except Exception:
|
||||
logger.warning(f"无法解压,得到的原始响应: {responseRAWContent}")
|
||||
raise SDGBResponseError("解压失败")
|
||||
try:
|
||||
resultResponse = aes.decrypt(responseDecompressed)
|
||||
logger.debug(f"成功解密响应!")
|
||||
except:
|
||||
logger.debug("成功解密响应!")
|
||||
except Exception:
|
||||
logger.warning(f"解密失败,得到的原始响应: {responseDecompressed}")
|
||||
raise SDGBResponseError("解密失败")
|
||||
|
||||
|
||||
if not noLog:
|
||||
logger.debug(f"响应: {resultResponse}")
|
||||
return resultResponse
|
||||
|
||||
|
||||
# 异常处理
|
||||
except SDGBRequestError as e:
|
||||
except SDGBRequestError:
|
||||
# 请求格式错误,不需要重试
|
||||
raise SDGBRequestError("请求格式错误")
|
||||
except SDGBResponseError as e:
|
||||
|
||||
Reference in New Issue
Block a user