2022-11-12 12:59:42 +00:00
|
|
|
|
from typing import Tuple, Optional
|
|
|
|
|
|
2022-11-12 15:08:25 +00:00
|
|
|
|
from genshin import Region, GenshinException
|
2022-11-20 11:32:56 +00:00
|
|
|
|
from telegram import Update, WebAppInfo, KeyboardButton, ReplyKeyboardMarkup
|
2022-11-21 12:50:08 +00:00
|
|
|
|
from telegram.ext import CallbackContext, filters
|
2022-11-12 12:59:42 +00:00
|
|
|
|
|
|
|
|
|
from core.base.redisdb import RedisDB
|
|
|
|
|
from core.baseplugin import BasePlugin
|
|
|
|
|
from core.config import config
|
|
|
|
|
from core.cookies import CookiesService
|
2022-11-12 15:08:25 +00:00
|
|
|
|
from core.cookies.error import CookiesNotFoundError
|
2022-11-12 12:59:42 +00:00
|
|
|
|
from core.plugin import Plugin, handler
|
|
|
|
|
from core.user import UserService
|
2022-11-12 15:08:25 +00:00
|
|
|
|
from core.user.error import UserNotFoundError
|
2022-12-10 12:37:43 +00:00
|
|
|
|
from modules.apihelper.client.components.verify import Verify
|
2022-11-20 11:32:56 +00:00
|
|
|
|
from modules.apihelper.error import ResponseException
|
2022-11-12 12:59:42 +00:00
|
|
|
|
from utils.decorators.error import error_callable
|
|
|
|
|
from utils.decorators.restricts import restricts
|
2022-11-12 15:08:25 +00:00
|
|
|
|
from utils.helpers import get_genshin_client
|
2022-11-13 05:45:01 +00:00
|
|
|
|
from utils.log import logger
|
2022-11-12 12:59:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class VerificationSystem:
|
|
|
|
|
def __init__(self, redis: RedisDB = None):
|
|
|
|
|
self.cache = redis.client
|
|
|
|
|
self.qname = "plugin:verification:"
|
|
|
|
|
|
|
|
|
|
async def get_challenge(self, uid: int) -> Tuple[Optional[str], Optional[str]]:
|
|
|
|
|
data = await self.cache.get(f"{self.qname}{uid}")
|
|
|
|
|
if not data:
|
|
|
|
|
return None, None
|
|
|
|
|
data = data.decode("utf-8").split("|")
|
|
|
|
|
return data[0], data[1]
|
|
|
|
|
|
|
|
|
|
async def set_challenge(self, uid: int, gt: str, challenge: str):
|
|
|
|
|
await self.cache.set(f"{self.qname}{uid}", f"{gt}|{challenge}")
|
|
|
|
|
await self.cache.expire(f"{self.qname}{uid}", 10 * 60)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class VerificationPlugins(Plugin, BasePlugin):
|
|
|
|
|
def __init__(self, user_service: UserService = None, cookies_service: CookiesService = None, redis: RedisDB = None):
|
|
|
|
|
self.cookies_service = cookies_service
|
|
|
|
|
self.user_service = user_service
|
|
|
|
|
self.system = VerificationSystem(redis)
|
|
|
|
|
|
2022-11-21 12:50:08 +00:00
|
|
|
|
@handler.command("verify", filters=filters.ChatType.PRIVATE, block=False)
|
2022-11-12 12:59:42 +00:00
|
|
|
|
@restricts(restricts_time=60)
|
|
|
|
|
@error_callable
|
|
|
|
|
async def verify(self, update: Update, context: CallbackContext) -> None:
|
|
|
|
|
user = update.effective_user
|
|
|
|
|
message = update.effective_message
|
2022-11-13 05:45:01 +00:00
|
|
|
|
logger.info(f"用户 %s[%s] 发出verify命令", user.full_name, user.id)
|
2022-11-12 15:08:25 +00:00
|
|
|
|
try:
|
|
|
|
|
client = await get_genshin_client(user.id)
|
|
|
|
|
if client.region != Region.CHINESE:
|
|
|
|
|
await message.reply_text("非法用户")
|
|
|
|
|
return
|
|
|
|
|
except UserNotFoundError:
|
|
|
|
|
await message.reply_text("用户未找到")
|
2022-11-12 12:59:42 +00:00
|
|
|
|
return
|
2022-11-12 15:08:25 +00:00
|
|
|
|
except CookiesNotFoundError:
|
|
|
|
|
await message.reply_text("检测到用户为UID绑定,无需认证")
|
|
|
|
|
return
|
2022-11-20 11:32:56 +00:00
|
|
|
|
is_high: bool = False
|
2022-12-10 12:37:43 +00:00
|
|
|
|
verification = Verify(cookies=client.cookie_manager.cookies)
|
2022-11-20 11:32:56 +00:00
|
|
|
|
if not context.args:
|
|
|
|
|
try:
|
|
|
|
|
await client.get_genshin_notes()
|
|
|
|
|
except GenshinException as exc:
|
|
|
|
|
if exc.retcode == 1034:
|
|
|
|
|
is_high = True
|
|
|
|
|
else:
|
|
|
|
|
raise exc
|
2022-11-12 12:59:42 +00:00
|
|
|
|
else:
|
2022-11-20 11:32:56 +00:00
|
|
|
|
await message.reply_text("账户正常,无需认证")
|
|
|
|
|
return
|
2022-11-13 05:45:01 +00:00
|
|
|
|
try:
|
2022-11-20 11:32:56 +00:00
|
|
|
|
data = await verification.create(is_high=is_high)
|
2022-11-17 02:16:45 +00:00
|
|
|
|
challenge = data["challenge"]
|
|
|
|
|
gt = data["gt"]
|
2022-11-21 12:50:08 +00:00
|
|
|
|
logger.success("用户 %s[%s] 创建验证成功\ngt[%s]\nchallenge[%s]", user.full_name, user.id, gt, challenge)
|
2022-11-13 05:45:01 +00:00
|
|
|
|
except ResponseException as exc:
|
2022-11-17 02:16:45 +00:00
|
|
|
|
logger.warning("用户 %s[%s] 创建验证失效 API返回 [%s]%s", user.full_name, user.id, exc.code, exc.message)
|
2022-11-13 05:45:01 +00:00
|
|
|
|
await message.reply_text(f"创建验证失败 错误信息为 [{exc.code}]{exc.message} 请稍后重试")
|
|
|
|
|
return
|
2022-11-12 15:08:25 +00:00
|
|
|
|
await self.system.set_challenge(client.uid, gt, challenge)
|
2022-11-20 11:32:56 +00:00
|
|
|
|
url = f"{config.pass_challenge_user_web}/webapp?username={context.bot.username}&command=verify>={gt}&challenge={challenge}&uid={client.uid}"
|
|
|
|
|
await message.reply_text(
|
|
|
|
|
"请尽快在10秒内完成手动验证\n或发送 /web_cancel 取消操作",
|
|
|
|
|
reply_markup=ReplyKeyboardMarkup.from_button(
|
|
|
|
|
KeyboardButton(
|
|
|
|
|
text="点我手动验证",
|
|
|
|
|
web_app=WebAppInfo(url=url),
|
|
|
|
|
)
|
|
|
|
|
),
|
|
|
|
|
)
|