-
-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2067 from DGP-Studio/feat/1616
- Loading branch information
Showing
19 changed files
with
255 additions
and
355 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright (c) DGP Studio. All rights reserved. | ||
// Licensed under the MIT license. | ||
|
||
namespace Snap.Hutao.Service.Geetest; | ||
|
||
internal sealed class AigisData | ||
{ | ||
[JsonPropertyName("success")] | ||
public int Success { get; set; } | ||
|
||
[JsonPropertyName("gt")] | ||
public string GT { get; set; } = default!; | ||
|
||
[JsonPropertyName("challenge")] | ||
public string Challenge { get; set; } = default!; | ||
|
||
[JsonPropertyName("new_captcha")] | ||
public int NewCaptcha { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright (c) DGP Studio. All rights reserved. | ||
// Licensed under the MIT license. | ||
|
||
namespace Snap.Hutao.Service.Geetest; | ||
|
||
internal sealed class AigisSession | ||
{ | ||
[JsonPropertyName("session_id")] | ||
public string SessionId { get; set; } = default!; | ||
|
||
[JsonPropertyName("mmt_type")] | ||
public int MmtType { get; set; } | ||
|
||
[JsonPropertyName("data")] | ||
public string Data { get; set; } = default!; | ||
} |
109 changes: 109 additions & 0 deletions
109
src/Snap.Hutao/Snap.Hutao/Service/Geetest/GeetestService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// Copyright (c) DGP Studio. All rights reserved. | ||
// Licensed under the MIT license. | ||
|
||
using Snap.Hutao.Core.LifeCycle; | ||
using Snap.Hutao.Service.Notification; | ||
using Snap.Hutao.UI.Xaml.Behavior.Action; | ||
using Snap.Hutao.UI.Xaml.View.Window.WebView2; | ||
using Snap.Hutao.Web.Hoyolab.Passport; | ||
using Snap.Hutao.Web.Hoyolab.Takumi.GameRecord; | ||
using Snap.Hutao.Web.Hoyolab.Takumi.GameRecord.Verification; | ||
using Snap.Hutao.Web.Hutao.Geetest; | ||
using Snap.Hutao.Web.Response; | ||
using System.Text; | ||
|
||
namespace Snap.Hutao.Service.Geetest; | ||
|
||
[ConstructorGenerated] | ||
[Injection(InjectAs.Transient, typeof(IGeetestService))] | ||
internal sealed partial class GeetestService : IGeetestService | ||
{ | ||
private readonly ICurrentXamlWindowReference currentXamlWindowReference; | ||
private readonly CustomGeetestClient customGeetestClient; | ||
private readonly IInfoBarService infoBarService; | ||
private readonly ITaskContext taskContext; | ||
private readonly CardClient cardClient; | ||
|
||
public async ValueTask<GeetestData?> TryVerifyGtChallengeAsync(string gt, string challenge, CancellationToken token = default) | ||
{ | ||
GeetestResponse response = await customGeetestClient.VerifyAsync(gt, challenge, token).ConfigureAwait(false); | ||
|
||
if (response is { Code: 0, Data: { } data }) | ||
{ | ||
return data; | ||
} | ||
|
||
string? result = await PrivateVerifyByWebViewAsync(gt, challenge, false, token).ConfigureAwait(false); | ||
if (string.IsNullOrEmpty(result)) | ||
{ | ||
return default; | ||
} | ||
|
||
GeetestWebResponse? webResponse = JsonSerializer.Deserialize<GeetestWebResponse>(result); | ||
ArgumentNullException.ThrowIfNull(webResponse); | ||
|
||
return new GeetestData() | ||
{ | ||
Gt = gt, | ||
Challenge = webResponse.Challenge, | ||
Validate = webResponse.Validate, | ||
}; | ||
} | ||
|
||
public async ValueTask<string?> TryVerifyXrpcChallengeAsync(Model.Entity.User user, CardVerifiationHeaders headers, CancellationToken token = default) | ||
{ | ||
Response<VerificationRegistration> registrationResponse = await cardClient.CreateVerificationAsync(user, headers, token).ConfigureAwait(false); | ||
if (!ResponseValidator.TryValidate(registrationResponse, infoBarService, out VerificationRegistration? registration)) | ||
{ | ||
return default; | ||
} | ||
|
||
if (await TryVerifyGtChallengeAsync(registration.Gt, registration.Challenge, token).ConfigureAwait(false) is not { } data) | ||
{ | ||
return default; | ||
} | ||
|
||
Response<VerificationResult> verifyResponse = await cardClient.VerifyVerificationAsync(user, headers, registration.Challenge, data.Validate, token).ConfigureAwait(false); | ||
if (!ResponseValidator.TryValidate(verifyResponse, infoBarService, out VerificationResult? result)) | ||
{ | ||
return default; | ||
} | ||
|
||
return result.Challenge; | ||
} | ||
|
||
public async ValueTask<bool> TryVerifyAigisSessionAsync(IAigisProvider provider, string? rawSession, bool isOversea, CancellationToken token = default) | ||
{ | ||
if (string.IsNullOrEmpty(rawSession)) | ||
{ | ||
return false; | ||
} | ||
|
||
AigisSession? session = JsonSerializer.Deserialize<AigisSession>(rawSession); | ||
ArgumentNullException.ThrowIfNull(session); | ||
|
||
AigisData? sessionData = JsonSerializer.Deserialize<AigisData>(session.Data); | ||
ArgumentNullException.ThrowIfNull(sessionData); | ||
|
||
string? result = await PrivateVerifyByWebViewAsync(sessionData.GT, sessionData.Challenge, isOversea, token).ConfigureAwait(false); | ||
|
||
if (string.IsNullOrEmpty(result)) | ||
{ | ||
// User closed the window without completing the verification | ||
return false; | ||
} | ||
|
||
provider.Aigis = $"{session.SessionId};{Convert.ToBase64String(Encoding.UTF8.GetBytes(result))}"; | ||
return true; | ||
} | ||
|
||
private async ValueTask<string?> PrivateVerifyByWebViewAsync(string gt, string challenge, bool isOversea, CancellationToken token) | ||
{ | ||
await taskContext.SwitchToMainThreadAsync(); | ||
GeetestWebView2ContentProvider contentProvider = new(gt, challenge, isOversea); | ||
ShowWebView2WindowAction.Show(contentProvider, currentXamlWindowReference.GetXamlRoot()); | ||
|
||
await taskContext.SwitchToBackgroundAsync(); | ||
return await contentProvider.GetResultAsync().ConfigureAwait(false); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/Snap.Hutao/Snap.Hutao/Service/Geetest/GeetestWebResponse.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright (c) DGP Studio. All rights reserved. | ||
// Licensed under the MIT license. | ||
|
||
namespace Snap.Hutao.Service.Geetest; | ||
|
||
internal sealed class GeetestWebResponse | ||
{ | ||
[JsonPropertyName("geetest_challenge")] | ||
public string Challenge { get; set; } = default!; | ||
|
||
[JsonPropertyName("geetest_validate")] | ||
public string Validate { get; set; } = default!; | ||
|
||
[JsonPropertyName("geetest_seccode")] | ||
public string Seccode { get; set; } = default!; | ||
} |
17 changes: 17 additions & 0 deletions
17
src/Snap.Hutao/Snap.Hutao/Service/Geetest/IGeetestService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright (c) DGP Studio. All rights reserved. | ||
// Licensed under the MIT license. | ||
|
||
using Snap.Hutao.Web.Hoyolab.Passport; | ||
using Snap.Hutao.Web.Hoyolab.Takumi.GameRecord; | ||
using Snap.Hutao.Web.Hutao.Geetest; | ||
|
||
namespace Snap.Hutao.Service.Geetest; | ||
|
||
internal interface IGeetestService | ||
{ | ||
ValueTask<GeetestData?> TryVerifyGtChallengeAsync(string gt, string challenge, CancellationToken token = default); | ||
|
||
ValueTask<string?> TryVerifyXrpcChallengeAsync(Model.Entity.User user, CardVerifiationHeaders headers, CancellationToken token = default); | ||
|
||
ValueTask<bool> TryVerifyAigisSessionAsync(IAigisProvider provider, string? rawSession, bool isOversea, CancellationToken token = default); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.