Skip to content

Commit

Permalink
Add generate_fp method to AuthClient
Browse files Browse the repository at this point in the history
  • Loading branch information
seriaati committed Aug 18, 2024
1 parent f6bd51b commit e0d61cd
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
45 changes: 45 additions & 0 deletions genshin/client/components/auth/client.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
"""Main auth client."""

import asyncio
import json
import logging
import random
import time
import typing
import uuid
from string import digits

import aiohttp

Expand Down Expand Up @@ -366,3 +371,43 @@ async def os_game_login(
verification_result = await self._verify_game_email(code, result.account.device_grant_ticket)

return await self._os_game_login(result.account.uid, verification_result.game_token)

def _gen_random_fp(self) -> str:
"""Generate a random device fingerprint used for generating authentic device fingerprint."""
char = digits + "abcdef"
return "".join(random.choices(char, k=13))

def _gen_ext_fields(self, oaid: str, board: str) -> str:
oaid_key = "oaid" if self.region is types.Region.CHINESE else "adid"
ext_fields = {oaid_key: oaid, "board": board}
return json.dumps(ext_fields)

async def generate_fp(
self,
*,
device_id: str,
device_board: str,
oaid: str,
) -> str:
"""Generate an authentic device fingerprint."""
device_id_key = "bbs_device_id" if self.region is types.Region.CHINESE else "hoyolab_device_id"
payload = {
"device_id": device_id,
"device_fp": self._gen_random_fp(),
"seed_id": str(uuid.uuid4()).lower(),
"seed_time": str(int(time.time() * 1000)),
"platform": "2",
"app_name": "bbs_cn" if self.region is types.Region.CHINESE else "bbs_oversea",
"ext_fields": self._gen_ext_fields(oaid, device_board),
device_id_key: str(uuid.uuid4()).lower(),
}

async with aiohttp.ClientSession() as session, session.post(
routes.GET_FP_URL.get_url(self.region), json=payload
) as r:
data = await r.json()

if data["data"]["code"] != 200:
raise errors.GenshinException(data, data["data"]["msg"])

return data["data"]["device_fp"]
5 changes: 5 additions & 0 deletions genshin/client/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"DETAIL_LEDGER_URL",
"GACHA_URL",
"GET_COOKIE_TOKEN_BY_GAME_TOKEN_URL",
"GET_FP_URL",
"GET_STOKEN_BY_GAME_TOKEN_URL",
"GET_USER_REGION_URL",
"HK4E_URL",
Expand Down Expand Up @@ -326,3 +327,7 @@ def get_url(self, region: types.Region, game: types.Game) -> yarl.URL:
GET_USER_REGION_URL = Route(
"https://api-account-os.hoyoverse.com/account/binding/api/getUserGameRolesOfRegionByCookieToken"
)
GET_FP_URL = InternationalRoute(
overseas="https://sg-public-data-api.hoyoverse.com/device-fp/api/getFp",
chinese="https://public-data-api.mihoyo.com/device-fp/api/getFp",
)

0 comments on commit e0d61cd

Please sign in to comment.