Skip to content

Commit

Permalink
Merge pull request #160 from JokelBaf/master
Browse files Browse the repository at this point in the history
Fixes and improvements in geetest module
  • Loading branch information
jokelbaf authored Mar 4, 2024
2 parents a424187 + 9b31827 commit 5a4a840
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 22 deletions.
31 changes: 20 additions & 11 deletions genshin/client/components/geetest/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
class GeetestClient(base.BaseClient):
"""Geetest client component."""

async def web_login(
async def _web_login(
self,
account: str,
password: str,
Expand Down Expand Up @@ -69,12 +69,13 @@ async def web_login(

return cookies

async def app_login(
async def _app_login(
self,
account: str,
password: str,
*,
geetest: typing.Optional[typing.Dict[str, typing.Any]] = None,
ticket: typing.Optional[typing.Dict[str, typing.Any]] = None,
) -> typing.Dict[str, typing.Any]:
"""Login with a password using HoYoLab app endpoint.
Expand All @@ -89,6 +90,10 @@ async def app_login(
session_id = geetest["session_id"]
headers["x-rpc-aigis"] = geetest_utility.get_aigis_header(session_id, mmt_data)

if ticket:
ticket["verify_str"] = json.dumps(ticket["verify_str"])
headers["x-rpc-verify"] = json.dumps(ticket)

payload = {
"account": geetest_utility.encrypt_geetest_credentials(account),
"password": geetest_utility.encrypt_geetest_credentials(password),
Expand All @@ -110,7 +115,9 @@ async def app_login(

if data["retcode"] == -3239:
# Email verification required
return json.loads(r.headers["x-rpc-verify"])
verify = json.loads(r.headers["x-rpc-verify"])
verify["verify_str"] = json.loads(verify["verify_str"])
return verify

if not data["data"]:
errors.raise_for_retcode(data)
Expand All @@ -126,7 +133,7 @@ async def app_login(

return cookies

async def send_verification_email(
async def _send_verification_email(
self,
ticket: typing.Dict[str, typing.Any],
*,
Expand Down Expand Up @@ -164,7 +171,7 @@ async def send_verification_email(

return None

async def verify_email(self, code: str, ticket: typing.Dict[str, typing.Any]) -> None:
async def _verify_email(self, code: str, ticket: typing.Dict[str, typing.Any]) -> None:
"""Verify email."""
async with aiohttp.ClientSession() as session:
async with session.post(
Expand Down Expand Up @@ -200,7 +207,7 @@ async def login_with_password(
Note that this will start a webserver if captcha is
triggered and `geetest_solver` is not passed.
"""
result = await self.web_login(account, password, tokenType=tokenType)
result = await self._web_login(account, password, tokenType=tokenType)

if "session_id" not in result:
# Captcha not triggered
Expand All @@ -211,7 +218,7 @@ async def login_with_password(
else:
geetest = await server.solve_geetest(result, port=port)

return await self.web_login(account, password, tokenType=tokenType, geetest=geetest)
return await self._web_login(account, password, tokenType=tokenType, geetest=geetest)

async def login_with_app_password(
self,
Expand All @@ -232,7 +239,7 @@ async def login_with_app_password(
2. Email verification is triggered (can happen if you
first login with a new device).
"""
result = await self.app_login(account, password)
result = await self._app_login(account, password)

if "session_id" in result:
# Captcha triggered
Expand All @@ -241,18 +248,20 @@ async def login_with_app_password(
else:
geetest = await server.solve_geetest(result, port=port)

result = await self.app_login(account, password, geetest=geetest)
result = await self._app_login(account, password, geetest=geetest)

if "risk_ticket" in result:
# Email verification required
mmt = await self.send_verification_email(result)
mmt = await self._send_verification_email(result)
if mmt:
if geetest_solver:
geetest = await geetest_solver(mmt)
else:
geetest = await server.solve_geetest(mmt, port=port)

await self._send_verification_email(result, geetest=geetest)

await server.verify_email(self, result, port=port)
result = await self.app_login(account, password)
result = await self._app_login(account, password, ticket=result)

return result
5 changes: 3 additions & 2 deletions genshin/client/components/geetest/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def get_page(page: typing.Literal["captcha", "verify-email"]) -> str:
<button id="verify">Send</button>
</body>
<script>
document.getElementById("verify").onClick = () => {
document.getElementById("verify").onclick = () => {
fetch("/send-data", {
method: "POST",
body: JSON.stringify({
Expand Down Expand Up @@ -137,6 +137,7 @@ async def send_data_endpoint(request: web.Request) -> web.Response:
finally:
await asyncio.sleep(0.3)
await runner.shutdown()
await runner.cleanup()

return data

Expand All @@ -160,4 +161,4 @@ async def verify_email(
data = await launch_webapp("verify-email", port=port)
code = data["code"]

return await client.verify_email(code, ticket)
return await client._verify_email(code, ticket)
16 changes: 7 additions & 9 deletions genshin/utility/geetest.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,29 @@
WEB_LOGIN_HEADERS = {
"x-rpc-app_id": "c9oqaq3s3gu8",
"x-rpc-client_type": "4",
"x-rpc-sdk_version": "2.14.1",
"x-rpc-game_biz": "bbs_oversea",
"x-rpc-source": "v2.webLogin",
"x-rpc-referrer": "https://www.hoyolab.com",
# If not equal account.hoyolab.com It's will return retcode 1200 [Unauthorized]
"Origin": "https://account.hoyolab.com",
"Referer": "https://account.hoyolab.com/",
}

APP_LOGIN_HEADERS = {
"x-rpc-app_id": "c9oqaq3s3gu8",
"x-rpc-app_version": "2.47.0",
"x-rpc-client_type": "2",
"x-rpc-sdk_version": "2.22.0",
"x-rpc-game_biz": "bbs_oversea",
"Origin": "https://account.hoyoverse.com",
"Referer": "https://account.hoyoverse.com/",
# Passing "x-rpc-device_id" header will trigger email verification
# (unless the device_id is already verified).
#
# For some reason, without this header, email verification is not triggered.
# "x-rpc-device_id": "1c33337bd45c1bfs",
}

EMAIL_SEND_HEADERS = {
"x-rpc-app_id": "c9oqaq3s3gu8",
"x-rpc-client_type": "2",
}

EMAIL_VERIFY_HEADERS = {
"x-rpc-app_id": "c9oqaq3s3gu8",
"x-rpc-client_type": "2",
}


Expand Down

0 comments on commit 5a4a840

Please sign in to comment.