♻️ Refactor sign system code

This commit is contained in:
xtaodada 2024-11-03 16:22:47 +08:00
parent 666311e625
commit 0566adeac3
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659
2 changed files with 49 additions and 97 deletions

View File

@ -17,6 +17,8 @@ class RecognizeSystem:
async def recognize(gt: str, challenge: str, referer: str = None, uid: int = None) -> Optional[str]: async def recognize(gt: str, challenge: str, referer: str = None, uid: int = None) -> Optional[str]:
if not referer: if not referer:
referer = RecognizeSystem.REFERER referer = RecognizeSystem.REFERER
if not config.pass_challenge_api:
return None
if not gt or not challenge or not uid: if not gt or not challenge or not uid:
return None return None
pass_challenge_params = { pass_challenge_params = {
@ -40,11 +42,8 @@ class RecognizeSystem:
) )
logger.debug("recognize 请求返回:%s", resp.text) logger.debug("recognize 请求返回:%s", resp.text)
data = resp.json() data = resp.json()
status = data.get("status")
if status != 0:
logger.error("recognize 解析错误:[%s]%s", data.get("code"), data.get("msg"))
if data.get("code", 0) != 0: if data.get("code", 0) != 0:
raise RuntimeError logger.error("recognize 解析错误:[%s]%s", data.get("code"), data.get("msg"))
logger.info("recognize 解析成功") logger.info("recognize 解析成功")
return data["data"]["validate"] return data["data"]["validate"]
except JSONDecodeError: except JSONDecodeError:

View File

@ -102,6 +102,37 @@ class SignSystem(Plugin):
) )
return InlineKeyboardMarkup([[InlineKeyboardButton("请尽快点我进行手动验证", url=url)]]) return InlineKeyboardMarkup([[InlineKeyboardButton("请尽快点我进行手动验证", url=url)]])
@staticmethod
async def sign_with_recognize(
client: "ZZZClient",
use_recognize: bool = True,
_challenge: str = None,
_validate: str = None,
):
_request_daily_reward = await client.request_daily_reward(
"sign",
method="POST",
lang="zh-cn",
challenge=_challenge,
validate=_validate,
)
logger.debug("request_daily_reward 返回\n%s", _request_daily_reward)
if _request_daily_reward and _request_daily_reward.get("success", 0) == 1:
_gt = _request_daily_reward.get("gt", "")
_challenge = _request_daily_reward.get("challenge", "")
logger.info("UID[%s] 创建验证码 gt[%s] challenge[%s]", client.player_id, _gt, _challenge)
_validate = (
await RecognizeSystem.recognize(_gt, _challenge, uid=client.player_id) if use_recognize else None
)
if _validate:
logger.success("recognize 通过验证成功 challenge[%s] validate[%s]", _challenge, _validate)
return await SignSystem.sign_with_recognize(client, False, _challenge, _validate)
else:
raise NeedChallenge(uid=client.player_id, gt=_gt, challenge=_challenge)
else:
logger.success("UID[%s] 签到成功", client.player_id)
return _request_daily_reward
async def start_sign( async def start_sign(
self, self,
client: "ZZZClient", client: "ZZZClient",
@ -129,103 +160,25 @@ class SignSystem(Plugin):
return f"获取签到状态失败API返回信息为 {str(error)}" return f"获取签到状态失败API返回信息为 {str(error)}"
if not daily_reward_info.signed_in: if not daily_reward_info.signed_in:
try: try:
if validate: if challenge and validate:
logger.info( logger.info(
"UID[%s] 正在尝试通过验证码\nchallenge[%s]\nvalidate[%s]", client.player_id, challenge, validate "UID[%s] 正在尝试通过验证码 challenge[%s] validate[%s]", client.player_id, challenge, validate
) )
request_daily_reward = await client.request_daily_reward( await self.sign_with_recognize(
"sign", client, use_recognize=False, _challenge=challenge, _validate=validate
method="POST",
lang="zh-cn",
challenge=challenge,
validate=validate,
) )
logger.debug("request_daily_reward 返回 %s", request_daily_reward)
if request_daily_reward and request_daily_reward.get("success", 0) == 1:
# 尝试通过 ajax 请求绕过签到
gt = request_daily_reward.get("gt", "")
challenge = request_daily_reward.get("challenge", "")
logger.warning("UID[%s] 触发验证码\ngt[%s]\nchallenge[%s]", client.player_id, gt, challenge)
self.verify.account_id = client.account_id
validate = await self.verify.ajax(
referer=RecognizeSystem.REFERER,
gt=gt,
challenge=challenge,
)
if validate:
logger.success("ajax 通过验证成功\nchallenge[%s]\nvalidate[%s]", challenge, validate)
request_daily_reward = await client.request_daily_reward(
"sign",
method="POST",
lang="zh-cn",
challenge=challenge,
validate=validate,
)
logger.debug("request_daily_reward 返回 %s", request_daily_reward)
if request_daily_reward and request_daily_reward.get("success", 0) == 1:
logger.warning("UID[%s] 触发验证码\nchallenge[%s]", client.player_id, challenge)
raise NeedChallenge(
uid=client.player_id,
gt=request_daily_reward.get("gt", ""),
challenge=request_daily_reward.get("challenge", ""),
)
elif config.pass_challenge_app_key:
# 如果无法绕过 检查配置文件是否配置识别 API 尝试请求绕过
# 注意 需要重新获取没有进行任何请求的 Challenge
logger.info("UID[%s] 正在使用 recognize 重新请求签到", client.player_id)
_request_daily_reward = await client.request_daily_reward(
"sign",
method="POST",
lang="zh-cn",
)
logger.debug("request_daily_reward 返回\n%s", _request_daily_reward)
if _request_daily_reward and _request_daily_reward.get("success", 0) == 1:
_gt = _request_daily_reward.get("gt", "")
_challenge = _request_daily_reward.get("challenge", "")
logger.info("UID[%s] 创建验证码\ngt[%s]\nchallenge[%s]", client.player_id, _gt, _challenge)
_validate = await RecognizeSystem.recognize(_gt, _challenge, uid=client.player_id)
if _validate:
logger.success(
"recognize 通过验证成功\nchallenge[%s]\nvalidate[%s]", _challenge, _validate
)
request_daily_reward = await client.request_daily_reward(
"sign",
method="POST",
lang="zh-cn",
challenge=_challenge,
validate=_validate,
)
if request_daily_reward and request_daily_reward.get("success", 0) == 1:
logger.warning("UID[%s] 触发验证码\nchallenge[%s]", client.player_id, _challenge)
gt = request_daily_reward.get("gt", "")
challenge = request_daily_reward.get("challenge", "")
logger.success(
"UID[%s] 创建验证成功\ngt[%s]\nchallenge[%s]", client.player_id, gt, challenge
)
raise NeedChallenge(
uid=client.player_id,
gt=gt,
challenge=challenge,
)
logger.success("UID[%s] 通过 recognize 签到成功", client.player_id)
else: else:
request_daily_reward = await client.request_daily_reward( retry = 3 if config.pass_challenge_api else 1
"sign", method="POST", lang="zh-cn" for ret in range(3):
) try:
gt = request_daily_reward.get("gt", "") await self.sign_with_recognize(client)
challenge = request_daily_reward.get("challenge", "") break
logger.success( except NeedChallenge as e:
"UID[%s] 创建验证成功\ngt[%s]\nchallenge[%s]", client.player_id, gt, challenge if ret == retry - 1:
) # 重试次数用完,抛出异常
raise NeedChallenge(uid=client.player_id, gt=gt, challenge=challenge) if not config.pass_challenge_api:
else: raise e
request_daily_reward = await client.request_daily_reward("sign", method="POST", lang="zh-cn") await self.sign_with_recognize(client, False)
gt = request_daily_reward.get("gt", "")
challenge = request_daily_reward.get("challenge", "")
logger.success("UID[%s] 创建验证成功\ngt[%s]\nchallenge[%s]", client.player_id, gt, challenge)
raise NeedChallenge(uid=client.player_id, gt=gt, challenge=challenge)
else:
logger.success("UID[%s] 签到成功", client.player_id)
except TimeoutException as error: except TimeoutException as error:
logger.warning("UID[%s] 签到请求超时", client.player_id) logger.warning("UID[%s] 签到请求超时", client.player_id)
if is_raise: if is_raise: