🐛 Fix device too dynamic

This commit is contained in:
omg-xtao 2023-06-03 21:48:06 +08:00 committed by GitHub
parent 560b3c1967
commit d7bef825c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 36 additions and 22 deletions

View File

@ -11,7 +11,7 @@ from qrcode.image.pure import PyPNGImage
from ...logger import logger from ...logger import logger
from ...models.genshin.cookies import CookiesModel from ...models.genshin.cookies import CookiesModel
from ...utility.helpers import get_device_id, get_ds from ...utility.helpers import get_device_id, get_ds, update_device_headers
__all__ = ("AuthClient",) __all__ = ("AuthClient",)
@ -87,13 +87,12 @@ class AuthClient:
"Accept": "application/json", "Accept": "application/json",
"x-rpc-game_biz": "bbs_cn", "x-rpc-game_biz": "bbs_cn",
"x-rpc-sys_version": "11", "x-rpc-sys_version": "11",
"x-rpc-device_id": get_device_id(self.USER_AGENT),
"x-rpc-device_fp": "".join(random.choices((ascii_letters + digits), k=13)),
"x-rpc-device_name": "Chrome 108.0.0.0", "x-rpc-device_name": "Chrome 108.0.0.0",
"x-rpc-device_model": "Windows 10 64-bit", "x-rpc-device_model": "Windows 10 64-bit",
"x-rpc-app_id": "bll8iq97cem8", "x-rpc-app_id": "bll8iq97cem8",
"User-Agent": "okhttp/4.8.0", "User-Agent": "okhttp/4.8.0",
} }
update_device_headers(self.user_id, headers)
app_version, client_type, ds_sign = get_ds(new_ds=True, data=data) app_version, client_type, ds_sign = get_ds(new_ds=True, data=data)
headers["x-rpc-app_version"] = app_version headers["x-rpc-app_version"] = app_version
headers["x-rpc-client_type"] = client_type headers["x-rpc-client_type"] = client_type

View File

@ -4,7 +4,7 @@ import time
from typing import Dict, Optional from typing import Dict, Optional
from ..base.hyperionrequest import HyperionRequest from ..base.hyperionrequest import HyperionRequest
from ...utility.helpers import get_ua, get_device_id, get_ds from ...utility.helpers import get_ua, get_ds, update_device_headers
__all__ = ("Verify",) __all__ = ("Verify",)
@ -14,6 +14,8 @@ class Verify:
VERIFICATION_HOST = "api.geetest.com" VERIFICATION_HOST = "api.geetest.com"
CREATE_VERIFICATION_URL = "/game_record/app/card/wapi/createVerification" CREATE_VERIFICATION_URL = "/game_record/app/card/wapi/createVerification"
VERIFY_VERIFICATION_URL = "/game_record/app/card/wapi/verifyVerification" VERIFY_VERIFICATION_URL = "/game_record/app/card/wapi/verifyVerification"
REFERER_URL = "/game_record/app/genshin/api/dailyNote"
GAME = "2"
AJAX_URL = "/ajax.php" AJAX_URL = "/ajax.php"
USER_AGENT = get_ua() USER_AGENT = get_ua()
@ -24,7 +26,6 @@ class Verify:
"User-Agent": USER_AGENT, "User-Agent": USER_AGENT,
"X-Requested-With": "com.mihoyo.hyperion", "X-Requested-With": "com.mihoyo.hyperion",
"Referer": "https://webstatic.mihoyo.com/", "Referer": "https://webstatic.mihoyo.com/",
"x-rpc-device_id": get_device_id(USER_AGENT),
"x-rpc-page": "3.1.3_#/ys", "x-rpc-page": "3.1.3_#/ys",
} }
@ -35,7 +36,8 @@ class Verify:
"Accept-Language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7", "Accept-Language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7",
} }
def __init__(self, cookies: Dict = None): def __init__(self, account_id: int = None, cookies: Dict = None):
self.account_id = account_id
self.client = HyperionRequest(headers=self.BBS_HEADERS, cookies=cookies) self.client = HyperionRequest(headers=self.BBS_HEADERS, cookies=cookies)
def get_verification_headers(self, referer: str): def get_verification_headers(self, referer: str):
@ -49,6 +51,9 @@ class Verify:
headers["x-rpc-app_version"] = app_version headers["x-rpc-app_version"] = app_version
headers["x-rpc-client_type"] = client_type headers["x-rpc-client_type"] = client_type
headers["DS"] = ds headers["DS"] = ds
headers["x-rpc-challenge_path"] = f"https://{self.HOST}{self.REFERER_URL}"
headers["x-rpc-challenge_game"] = self.GAME
update_device_headers(self.account_id, headers)
return headers return headers
@staticmethod @staticmethod

View File

@ -4,9 +4,9 @@ import random
import string import string
import time import time
import uuid import uuid
from typing import Any, Mapping, Optional from typing import Any, Mapping, Optional, Dict
__all__ = ("get_device_id", "hex_digest", "get_ds", "get_recognize_server", "get_ua") __all__ = ("get_device_id", "hex_digest", "get_ds", "get_recognize_server", "get_ua", "update_device_headers")
RECOGNIZE_SERVER = { RECOGNIZE_SERVER = {
"1": "cn_gf01", "1": "cn_gf01",
@ -23,6 +23,14 @@ def get_device_id(name: str = ""):
return str(uuid.uuid3(uuid.NAMESPACE_URL, name)) return str(uuid.uuid3(uuid.NAMESPACE_URL, name))
def update_device_headers(account_id: int, headers: Dict = None) -> Dict[str, str]:
account_id = account_id or 0
headers = headers or {}
headers["x-rpc-device_id"] = get_device_id(str(account_id))
headers["x-rpc-device_fp"] = hex_digest(headers["x-rpc-device_id"])[:13]
return headers
def hex_digest(text): def hex_digest(text):
_md5 = hashlib.md5() # nosec B303 _md5 = hashlib.md5() # nosec B303
_md5.update(text.encode()) _md5.update(text.encode())

View File

@ -61,7 +61,7 @@ class ChallengeSystem(Plugin):
raise exc raise exc
else: else:
raise ChallengeSystemException("账户正常,无需验证") raise ChallengeSystemException("账户正常,无需验证")
verify = Verify(cookies=client.cookie_manager.cookies) verify = Verify(account_id=client.hoyolab_id, cookies=client.cookie_manager.cookies)
try: try:
data = await verify.create() data = await verify.create()
challenge = data["challenge"] challenge = data["challenge"]
@ -91,7 +91,7 @@ class ChallengeSystem(Plugin):
_, challenge = await self.get_challenge(client.uid) _, challenge = await self.get_challenge(client.uid)
if challenge is None: if challenge is None:
raise ChallengeSystemException("验证失效 请求已经过期") raise ChallengeSystemException("验证失效 请求已经过期")
verify = Verify(cookies=client.cookie_manager.cookies) verify = Verify(account_id=client.hoyolab_id, cookies=client.cookie_manager.cookies)
try: try:
await verify.verify(challenge=challenge, validate=validate) await verify.verify(challenge=challenge, validate=validate)
except ResponseException as exc: except ResponseException as exc:

View File

@ -148,6 +148,7 @@ class SignSystem(Plugin):
gt = request_daily_reward.get("gt", "") gt = request_daily_reward.get("gt", "")
challenge = request_daily_reward.get("challenge", "") challenge = request_daily_reward.get("challenge", "")
logger.warning("UID[%s] 触发验证码\ngt[%s]\nchallenge[%s]", client.uid, gt, challenge) logger.warning("UID[%s] 触发验证码\ngt[%s]\nchallenge[%s]", client.uid, gt, challenge)
self.verify.account_id = client.hoyolab_id
validate = await self.verify.ajax( validate = await self.verify.ajax(
referer=RecognizeSystem.REFERER, referer=RecognizeSystem.REFERER,
gt=gt, gt=gt,

View File

@ -4,7 +4,7 @@ from genshin import Client
from genshin.client.routes import InternationalRoute # noqa F401 from genshin.client.routes import InternationalRoute # noqa F401
from genshin.utility import recognize_genshin_server from genshin.utility import recognize_genshin_server
from modules.apihelper.utility.helpers import hex_digest, get_ds from modules.apihelper.utility.helpers import hex_digest, get_ds, update_device_headers
AUTHKEY_API = "https://api-takumi.mihoyo.com/binding/api/genAuthKey" AUTHKEY_API = "https://api-takumi.mihoyo.com/binding/api/genAuthKey"
HK4E_LOGIN_URL = InternationalRoute( HK4E_LOGIN_URL = InternationalRoute(
@ -15,7 +15,6 @@ GACHA_HEADERS = {
"User-Agent": "okhttp/4.8.0", "User-Agent": "okhttp/4.8.0",
"x-rpc-sys_version": "12", "x-rpc-sys_version": "12",
"x-rpc-channel": "mihoyo", "x-rpc-channel": "mihoyo",
"x-rpc-device_id": "",
"x-rpc-device_name": "", "x-rpc-device_name": "",
"x-rpc-device_model": "", "x-rpc-device_model": "",
"Referer": "https://app.mihoyo.com", "Referer": "https://app.mihoyo.com",
@ -37,8 +36,8 @@ async def get_authkey_by_stoken(client: Client) -> Optional[str]:
"region": recognize_genshin_server(client.uid), "region": recognize_genshin_server(client.uid),
} }
device_id = hex_digest(str(client.uid)) device_id = hex_digest(str(client.uid))
headers["x-rpc-device_id"] = device_id
device = f"Paimon Build {device_id[:5]}" device = f"Paimon Build {device_id[:5]}"
update_device_headers(client.hoyolab_id, headers)
headers["x-rpc-device_name"] = device headers["x-rpc-device_name"] = device
headers["x-rpc-device_model"] = device headers["x-rpc-device_model"] = device
app_version, client_type, ds_sign = get_ds() app_version, client_type, ds_sign = get_ds()

View File

@ -9,7 +9,8 @@ from genshin import constants, types, utility
from genshin.client import routes from genshin.client import routes
from genshin.utility import generate_dynamic_secret, ds from genshin.utility import generate_dynamic_secret, ds
from modules.apihelper.utility.helpers import get_ds, get_ua, get_device_id, hex_digest from modules.apihelper.utility.helpers import get_ds, get_ua, get_device_id, hex_digest, update_device_headers
from utils.log import logger
from utils.patch.methods import patch, patchable from utils.patch.methods import patch, patchable
DEVICE_ID = get_device_id() DEVICE_ID = get_device_id()
@ -108,7 +109,7 @@ class CalculatorClient:
class BaseClient: class BaseClient:
@patchable @patchable
async def request_hoyolab( async def request_hoyolab(
self, self: "genshin.Client",
url: aiohttp.typedefs.StrOrURL, url: aiohttp.typedefs.StrOrURL,
*, *,
lang: typing.Optional[str] = None, lang: typing.Optional[str] = None,
@ -151,11 +152,11 @@ class BaseClient:
"User-Agent": ua, "User-Agent": ua,
"X_Requested_With": "com.mihoyo.hoyolab", "X_Requested_With": "com.mihoyo.hoyolab",
"Referer": "https://webstatic.mihoyo.com", "Referer": "https://webstatic.mihoyo.com",
"x-rpc-device_id": get_device_id(device_id),
"x-rpc-app_version": app_version, "x-rpc-app_version": app_version,
"x-rpc-client_type": client_type, "x-rpc-client_type": client_type,
"ds": ds_sign, "ds": ds_sign,
} }
update_device_headers(self.hoyolab_id, headers)
else: else:
raise TypeError(f"{region!r} is not a valid region.") raise TypeError(f"{region!r} is not a valid region.")
@ -164,7 +165,7 @@ class BaseClient:
@patchable @patchable
async def request( async def request(
self, self: "genshin.Client",
url: aiohttp.typedefs.StrOrURL, url: aiohttp.typedefs.StrOrURL,
*, *,
method: typing.Optional[str] = None, method: typing.Optional[str] = None,
@ -189,6 +190,8 @@ class BaseClient:
headers = dict(headers or {}) headers = dict(headers or {})
headers.setdefault("User-Agent", self.USER_AGENT) headers.setdefault("User-Agent", self.USER_AGENT)
update_device_headers(self.hoyolab_id, headers)
logger.debug("Account ID: %s Header: %s" % (self.hoyolab_id, headers))
if method is None: if method is None:
method = "POST" if data else "GET" method = "POST" if data else "GET"
@ -218,7 +221,7 @@ class BaseClient:
@patchable @patchable
async def request_bbs( async def request_bbs(
self, self: "genshin.Client",
url: aiohttp.typedefs.StrOrURL, url: aiohttp.typedefs.StrOrURL,
*, *,
lang: typing.Optional[str] = None, lang: typing.Optional[str] = None,
@ -256,12 +259,12 @@ class BaseClient:
add_headers = { add_headers = {
"User-Agent": ua, "User-Agent": ua,
"Referer": "https://www.miyoushe.com/ys/", "Referer": "https://www.miyoushe.com/ys/",
"x-rpc-device_id": get_device_id(device_id),
"x-rpc-app_version": app_version, "x-rpc-app_version": app_version,
"x-rpc-client_type": "4", "x-rpc-client_type": "4",
"ds": ds_sign, "ds": ds_sign,
} }
headers.update(add_headers) headers.update(add_headers)
update_device_headers(self.hoyolab_id, headers)
elif self.region == types.Region.OVERSEAS: elif self.region == types.Region.OVERSEAS:
headers.update(ds.get_ds_headers(data=data, params=params, region=region, lang=lang or self.lang)) headers.update(ds.get_ds_headers(data=data, params=params, region=region, lang=lang or self.lang))
headers["Referer"] = str(routes.BBS_REFERER_URL.get_url(self.region)) headers["Referer"] = str(routes.BBS_REFERER_URL.get_url(self.region))
@ -276,7 +279,7 @@ class BaseClient:
class DailyRewardClient: class DailyRewardClient:
@patchable @patchable
async def request_daily_reward( async def request_daily_reward(
self, self: "genshin.Client",
endpoint: str, endpoint: str,
*, *,
game: typing.Optional[types.Game] = None, game: typing.Optional[types.Game] = None,
@ -331,7 +334,6 @@ class DailyRewardClient:
"bbs_auth_required=true&act_id=e202009291139501&utm_source=bbs&utm_medium=mys&utm_campaign=icon" "bbs_auth_required=true&act_id=e202009291139501&utm_source=bbs&utm_medium=mys&utm_campaign=icon"
) )
headers["x-rpc-device_name"] = device headers["x-rpc-device_name"] = device
headers["x-rpc-device_id"] = get_device_id(device_id)
headers["x-rpc-app_version"] = app_version headers["x-rpc-app_version"] = app_version
headers["x-rpc-client_type"] = client_type headers["x-rpc-client_type"] = client_type
headers["x-rpc-sys_version"] = "12" headers["x-rpc-sys_version"] = "12"
@ -347,7 +349,7 @@ class DailyRewardClient:
headers["x-rpc-challenge"] = challenge headers["x-rpc-challenge"] = challenge
headers["x-rpc-validate"] = validate headers["x-rpc-validate"] = validate
headers["x-rpc-seccode"] = f"{validate}|jordan" headers["x-rpc-seccode"] = f"{validate}|jordan"
update_device_headers(self.hoyolab_id, headers)
else: else:
raise TypeError(f"{self.region!r} is not a valid region.") raise TypeError(f"{self.region!r} is not a valid region.")