🐛 fix cookie parser

This commit is contained in:
Karako 2022-12-08 22:22:59 +08:00 committed by GitHub
parent 330a7b22e8
commit 8beb2cf442
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,9 +1,8 @@
import contextlib import contextlib
import re
from http.cookies import CookieError, SimpleCookie
from typing import Dict, Optional from typing import Dict, Optional
import genshin import genshin
from arkowrapper import ArkoWrapper
from genshin import DataNotPublic, GenshinException, InvalidCookies, types from genshin import DataNotPublic, GenshinException, InvalidCookies, types
from genshin.models import GenshinAccount from genshin.models import GenshinAccount
from telegram import ReplyKeyboardMarkup, ReplyKeyboardRemove, TelegramObject, Update from telegram import ReplyKeyboardMarkup, ReplyKeyboardRemove, TelegramObject, Update
@ -45,43 +44,26 @@ class SetUserCookies(Plugin.Conversation, BasePlugin.Conversation):
self.cookies_service = cookies_service self.cookies_service = cookies_service
self.user_service = user_service self.user_service = user_service
# noinspection SpellCheckingInspection
@staticmethod @staticmethod
def parse_cookie(cookie: SimpleCookie) -> Dict[str, str]: def parse_cookie(cookie: Dict[str, str]) -> Dict[str, str]:
cookies = {} cookies = {}
ltoken = cookie.get("ltoken")
if ltoken: v1_keys = ["ltoken", "ltuid", "login_uid", "cookie_token"]
cookies["ltoken"] = ltoken.value v2_keys = ["ltoken_v2", "ltmid_v2", "account_mid_v2", "cookie_token", "cookie_token_v2", "login_ticket"]
ltuid = cookie.get("ltuid")
login_uid = cookie.get("login_uid") cookie_is_v1 = None
if ltuid:
cookies["ltuid"] = ltuid.value for k in v1_keys + v2_keys:
cookies["account_id"] = ltuid.value v = cookie.get(k)
if login_uid: if v is not None and cookie_is_v1 is None:
cookies["ltuid"] = login_uid.value cookie_is_v1 = k not in v2_keys
if ltuid: cookies[k] = cookie.get(k)
cookies["account_id"] = ltuid.value
cookie_token = cookie.get("cookie_token") if cookie_is_v1:
cookie_token_v2 = cookie.get("cookie_token_v2") cookies["account_id"] = cookies["ltuid"]
if cookie_token:
cookies["cookie_token"] = cookie_token.value return {k: v for k, v in cookies.items() if v is not None}
if cookie_token_v2:
cookies["cookie_token"] = cookie_token_v2.value
account_mid_v2 = cookie.get("account_mid_v2")
if account_mid_v2:
cookies["account_mid_v2"] = account_mid_v2.value
cookie_token_v2 = cookie.get("cookie_token_v2")
if cookie_token_v2:
cookies["cookie_token_v2"] = cookie_token_v2.value
ltoken_v2 = cookie.get("ltoken_v2")
if ltoken_v2:
cookies["ltoken_v2"] = ltoken_v2.value
ltmid_v2 = cookie.get("ltmid_v2")
if ltmid_v2:
cookies["ltmid_v2"] = ltmid_v2.value
login_ticket = cookie.get("login_ticket")
if login_ticket:
cookies["login_ticket"] = login_ticket.value
return cookies
@conversation.entry_point @conversation.entry_point
@handler.command(command="setcookie", filters=filters.ChatType.PRIVATE, block=True) @handler.command(command="setcookie", filters=filters.ChatType.PRIVATE, block=True)
@ -278,19 +260,13 @@ class SetUserCookies(Plugin.Conversation, BasePlugin.Conversation):
if message.text == "退出": if message.text == "退出":
await message.reply_text("退出任务", reply_markup=ReplyKeyboardRemove()) await message.reply_text("退出任务", reply_markup=ReplyKeyboardRemove())
return ConversationHandler.END return ConversationHandler.END
str_cookies = re.sub(r"(\s*\S*?=\{.*?};?\s*)", " ", message.text, 0)
cookie = SimpleCookie() # cookie str to dict
try: wrapped = (
cookie.load(str_cookies) ArkoWrapper(message.text.split(";")).map(lambda x: x.strip()).map(lambda x: ((y := x.split("="))[0], y[1]))
except CookieError: )
logger.info("用户 %s[%s] Cookies格式有误", user.full_name, user.id) cookie = {x[0]: x[1] for x in wrapped}
await message.reply_text("Cookies格式有误请检查", reply_markup=ReplyKeyboardRemove())
return ConversationHandler.END
if len(cookie) == 0:
logger.info("用户 %s[%s] Cookies格式有误", user.full_name, user.id)
await message.reply_text("Cookies格式有误请检查", reply_markup=ReplyKeyboardRemove())
return ConversationHandler.END
try: try:
cookies = self.parse_cookie(cookie) cookies = self.parse_cookie(cookie)
except (AttributeError, ValueError) as exc: except (AttributeError, ValueError) as exc: