🎨 Update log output and exception handler in validation

This commit is contained in:
洛水居室 2022-11-13 13:45:01 +08:00
parent 56515a2f9f
commit ffbb224644
No known key found for this signature in database
GPG Key ID: C9DE87DA724B88FC
2 changed files with 60 additions and 18 deletions

View File

@ -12,10 +12,12 @@ from core.cookies.error import CookiesNotFoundError
from core.plugin import Plugin, handler from core.plugin import Plugin, handler
from core.user import UserService from core.user import UserService
from core.user.error import UserNotFoundError from core.user.error import UserNotFoundError
from modules.apihelper.error import ResponseException, APIHelperException
from modules.apihelper.hyperion import Verification from modules.apihelper.hyperion import Verification
from utils.decorators.error import error_callable from utils.decorators.error import error_callable
from utils.decorators.restricts import restricts from utils.decorators.restricts import restricts
from utils.helpers import get_genshin_client from utils.helpers import get_genshin_client
from utils.log import logger
class VerificationSystem: class VerificationSystem:
@ -47,6 +49,7 @@ class VerificationPlugins(Plugin, BasePlugin):
async def verify(self, update: Update, context: CallbackContext) -> None: async def verify(self, update: Update, context: CallbackContext) -> None:
user = update.effective_user user = update.effective_user
message = update.effective_message message = update.effective_message
logger.info(f"用户 %s[%s] 发出verify命令", user.full_name, user.id)
try: try:
client = await get_genshin_client(user.id) client = await get_genshin_client(user.id)
if client.region != Region.CHINESE: if client.region != Region.CHINESE:
@ -63,10 +66,16 @@ class VerificationPlugins(Plugin, BasePlugin):
validate = context.args[0] validate = context.args[0]
_, challenge = await self.system.get_challenge(client.uid) _, challenge = await self.system.get_challenge(client.uid)
if challenge: if challenge:
try:
await verification.verify(challenge, validate) await verification.verify(challenge, validate)
logger.success(f"用户 %s[%s] 验证成功", user.full_name, user.id)
await message.reply_text("验证成功") await message.reply_text("验证成功")
except ResponseException as exc:
logger.warning(f"用户 %s[%s] 验证失效 API返回 [%s]%s", user.full_name, user.id, exc.code, exc.message)
await message.reply_text(f"验证失败 错误信息为 [{exc.code}]{exc.message} 请稍后重试")
else: else:
await message.reply_text("验证失效") logger.warning(f"用户 %s[%s] 验证失效 请求已经过期", user.full_name, user.id)
await message.reply_text("验证失效 请求已经过期 请稍后重试")
return return
try: try:
await client.get_genshin_notes() await client.get_genshin_notes()
@ -76,14 +85,24 @@ class VerificationPlugins(Plugin, BasePlugin):
else: else:
await message.reply_text("账户正常,无需认证") await message.reply_text("账户正常,无需认证")
return return
try:
data = await verification.create() data = await verification.create()
logger.success(f"用户 %s[%s] 创建验证成功", user.full_name, user.id)
except ResponseException as exc:
logger.warning(f"用户 %s[%s] 创建验证失效 API返回 [%s]%s 请稍后重试", user.full_name, user.id, exc.code, exc.message)
await message.reply_text(f"创建验证失败 错误信息为 [{exc.code}]{exc.message} 请稍后重试")
return
challenge = data["challenge"] challenge = data["challenge"]
gt = data["gt"] gt = data["gt"]
try:
validate = await verification.ajax(referer="https://webstatic.mihoyo.com/", gt=gt, challenge=challenge) validate = await verification.ajax(referer="https://webstatic.mihoyo.com/", gt=gt, challenge=challenge)
if validate: if validate:
await verification.verify(challenge, validate) await verification.verify(challenge, validate)
logger.success(f"用户 %s[%s] 通过 ajax 验证", user.full_name, user.id)
await message.reply_text("验证成功") await message.reply_text("验证成功")
return return
except APIHelperException as exc:
logger.warning(f"用户 %s[%s] ajax 验证失效 错误信息为 %s", user.full_name, user.id, repr(exc))
await self.system.set_challenge(client.uid, gt, challenge) await self.system.set_challenge(client.uid, gt, challenge)
url = f"{config.pass_challenge_user_web}?username={context.bot.username}&command=verify&gt={gt}&challenge={challenge}&uid={client.uid}" url = f"{config.pass_challenge_user_web}?username={context.bot.username}&command=verify&gt={gt}&challenge={challenge}&uid={client.uid}"
button = InlineKeyboardMarkup([[InlineKeyboardButton("验证", url=url)]]) button = InlineKeyboardMarkup([[InlineKeyboardButton("验证", url=url)]])

View File

@ -13,6 +13,7 @@ from core.cookies.error import CookiesNotFoundError
from core.plugin import handler, Plugin from core.plugin import handler, Plugin
from core.user import UserService from core.user import UserService
from core.user.error import UserNotFoundError from core.user.error import UserNotFoundError
from modules.apihelper.error import ResponseException, APIHelperException
from modules.apihelper.hyperion import Verification from modules.apihelper.hyperion import Verification
from plugins.genshin.sign import SignSystem, NeedChallenge from plugins.genshin.sign import SignSystem, NeedChallenge
from plugins.genshin.verification import VerificationSystem from plugins.genshin.verification import VerificationSystem
@ -53,20 +54,25 @@ class StartPlugin(Plugin):
f"{escape_markdown('发送 /setuid 或 /setcookie 命令进入绑定账号流程')}" f"{escape_markdown('发送 /setuid 或 /setcookie 命令进入绑定账号流程')}"
) )
elif args[0] == "verify_verification": elif args[0] == "verify_verification":
logger.info(f"用户 %s[%s] 通过start命令 获取认证信息", user.full_name, user.id)
await self.process_validate(message, user, bot_username=context.bot.username) await self.process_validate(message, user, bot_username=context.bot.username)
elif args[0] == "sign": elif args[0] == "sign":
logger.info(f"用户 %s[%s] 通过start命令 获取签到信息", user.full_name, user.id)
await self.gen_sign_button(message, user) await self.gen_sign_button(message, user)
elif args[0].startswith("challenge_"): elif args[0].startswith("challenge_"):
_data = args[0].split("_") _data = args[0].split("_")
_command = _data[1] _command = _data[1]
_challenge = _data[2] _challenge = _data[2]
if _command == "sign": if _command == "sign":
logger.info(f"用户 %s[%s] 通过start命令 进入签到流程", user.full_name, user.id)
await self.process_sign_validate(message, user, _challenge) await self.process_sign_validate(message, user, _challenge)
elif _command == "verify": elif _command == "verify":
logger.info(f"用户 %s[%s] 通过start命令 进入认证流程", user.full_name, user.id)
await self.process_validate(message, user, validate=_challenge) await self.process_validate(message, user, validate=_challenge)
else: else:
await message.reply_html(f"你好 {user.mention_html()} !我是派蒙 \n请点击 /{args[0]} 命令进入对应流程") await message.reply_html(f"你好 {user.mention_html()} !我是派蒙 \n请点击 /{args[0]} 命令进入对应流程")
return return
logger.info(f"用户 %s[%s] 发出start命令", user.full_name, user.id)
await message.reply_markdown_v2(f"你好 {user.mention_markdown_v2()} {escape_markdown('!我是派蒙 ')}") await message.reply_markdown_v2(f"你好 {user.mention_markdown_v2()} {escape_markdown('!我是派蒙 ')}")
@staticmethod @staticmethod
@ -142,19 +148,36 @@ class StartPlugin(Plugin):
if validate: if validate:
_, challenge = await self.verification_system.get_challenge(client.uid) _, challenge = await self.verification_system.get_challenge(client.uid)
if challenge: if challenge:
try:
await verification.verify(challenge, validate) await verification.verify(challenge, validate)
logger.success(f"用户 %s[%s] 验证成功", user.full_name, user.id)
await message.reply_text("验证成功") await message.reply_text("验证成功")
except ResponseException as exc:
logger.warning(f"用户 %s[%s] 验证失效 API返回 [%s]%s", user.full_name, user.id, exc.code, exc.message)
await message.reply_text(f"验证失败 错误信息为 [{exc.code}]{exc.message} 请稍后重试")
else: else:
await message.reply_text("验证失效") logger.warning(f"用户 %s[%s] 验证失效 请求已经过期", user.full_name, user.id)
await message.reply_text("验证失效 请求已经过期 请稍后重试")
return
if bot_username: if bot_username:
try:
data = await verification.create() data = await verification.create()
logger.success(f"用户 %s[%s] 创建验证成功", user.full_name, user.id)
except ResponseException as exc:
logger.warning(f"用户 %s[%s] 创建验证失效 API返回 [%s]%s", user.full_name, user.id, exc.code, exc.message)
await message.reply_text(f"创建验证失败 错误信息为 [{exc.code}]{exc.message} 请稍后重试")
return
challenge = data["challenge"] challenge = data["challenge"]
gt = data["gt"] gt = data["gt"]
try:
validate = await verification.ajax(referer="https://webstatic.mihoyo.com/", gt=gt, challenge=challenge) validate = await verification.ajax(referer="https://webstatic.mihoyo.com/", gt=gt, challenge=challenge)
if validate: if validate:
await verification.verify(challenge, validate) await verification.verify(challenge, validate)
logger.success(f"用户 %s[%s] 通过 ajax 验证", user.full_name, user.id)
await message.reply_text("验证成功") await message.reply_text("验证成功")
return return
except APIHelperException as exc:
logger.warning(f"用户 %s[%s] ajax 验证失效 错误信息为 %s", user.full_name, user.id, repr(exc))
await self.sign_system.set_challenge(client.uid, gt, challenge) await self.sign_system.set_challenge(client.uid, gt, challenge)
url = f"{config.pass_challenge_user_web}?username={bot_username}&command=verify&gt={gt}&challenge={challenge}&uid={client.uid}" url = f"{config.pass_challenge_user_web}?username={bot_username}&command=verify&gt={gt}&challenge={challenge}&uid={client.uid}"
button = InlineKeyboardMarkup([[InlineKeyboardButton("验证", url=url)]]) button = InlineKeyboardMarkup([[InlineKeyboardButton("验证", url=url)]])