From 92afa4b23c3caf15a0734b8feef638cae15ab389 Mon Sep 17 00:00:00 2001 From: DismissedLight <1686188646@qq.com> Date: Sun, 27 Aug 2023 22:20:28 +0800 Subject: [PATCH] add ability to customize geetest url --- .../Model/Entity/SettingEntry.Constant.cs | 8 ++ .../Resource/Localization/SH.Designer.cs | 108 ++++++++++++++++++ .../Snap.Hutao/Resource/Localization/SH.resx | 36 ++++++ .../Snap.Hutao/Service/AppOptions.cs | 7 ++ src/Snap.Hutao/Snap.Hutao/Snap.Hutao.csproj | 7 ++ .../AchievementArchiveCreateDialog.xaml | 4 +- .../AchievementArchiveCreateDialog.xaml.cs | 5 +- .../View/Dialog/CultivateProjectDialog.xaml | 2 +- .../View/Dialog/GeetestCustomUrlDialog.xaml | 89 +++++++++++++++ .../Dialog/GeetestCustomUrlDialog.xaml.cs | 28 +++++ .../Snap.Hutao/View/Page/SettingPage.xaml | 32 ++++-- .../Snap.Hutao/ViewModel/SettingViewModel.cs | 19 ++- .../Web/Hutao/Geetest/HomaGeetestClient.cs | 12 +- 13 files changed, 335 insertions(+), 22 deletions(-) create mode 100644 src/Snap.Hutao/Snap.Hutao/View/Dialog/GeetestCustomUrlDialog.xaml create mode 100644 src/Snap.Hutao/Snap.Hutao/View/Dialog/GeetestCustomUrlDialog.xaml.cs diff --git a/src/Snap.Hutao/Snap.Hutao/Model/Entity/SettingEntry.Constant.cs b/src/Snap.Hutao/Snap.Hutao/Model/Entity/SettingEntry.Constant.cs index d16b6074..73fef025 100644 --- a/src/Snap.Hutao/Snap.Hutao/Model/Entity/SettingEntry.Constant.cs +++ b/src/Snap.Hutao/Snap.Hutao/Model/Entity/SettingEntry.Constant.cs @@ -1,6 +1,8 @@ // Copyright (c) DGP Studio. All rights reserved. // Licensed under the MIT license. +using System.Runtime.Versioning; + namespace Snap.Hutao.Model.Entity; /// @@ -86,10 +88,16 @@ internal sealed partial class SettingEntry /// /// 启动游戏 多倍启动 /// + [Obsolete("不再支持多开")] public const string MultipleInstances = "Launch.MultipleInstances"; /// /// 语言 /// public const string Culture = "Culture"; + + /// + /// 自定义极验接口 + /// + public const string GeetestCustomCompositeUrl = "GeetestCustomCompositeUrl"; } diff --git a/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.Designer.cs b/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.Designer.cs index e4619b1e..0ee076d5 100644 --- a/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.Designer.cs +++ b/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.Designer.cs @@ -2274,6 +2274,87 @@ namespace Snap.Hutao.Resource.Localization { } } + /// + /// 查找类似 请输入请求接口的 Url 复合模板 的本地化字符串。 + /// + internal static string ViewDialogGeetestCustomUrlCompositInputHint { + get { + return ResourceManager.GetString("ViewDialogGeetestCustomUrlCompositInputHint", resourceCulture); + } + } + + /// + /// 查找类似 接口需要返回形如上方所示的 Json 数据,多余的数据会被忽略 的本地化字符串。 + /// + internal static string ViewDialogGeetestCustomUrlReturnDataDescription1 { + get { + return ResourceManager.GetString("ViewDialogGeetestCustomUrlReturnDataDescription1", resourceCulture); + } + } + + /// + /// 查找类似 "code" 为 0 时,指示验证成功,其他的值均视为验证失败 的本地化字符串。 + /// + internal static string ViewDialogGeetestCustomUrlReturnDataDescription2 { + get { + return ResourceManager.GetString("ViewDialogGeetestCustomUrlReturnDataDescription2", resourceCulture); + } + } + + /// + /// 查找类似 返回数据 的本地化字符串。 + /// + internal static string ViewDialogGeetestCustomUrlReturnDataHeader { + get { + return ResourceManager.GetString("ViewDialogGeetestCustomUrlReturnDataHeader", resourceCulture); + } + } + + /// + /// 查找类似 {0} 将在实际请求时替换为 gt 的本地化字符串。 + /// + internal static string ViewDialogGeetestCustomUrlSampleDescription1 { + get { + return ResourceManager.GetString("ViewDialogGeetestCustomUrlSampleDescription1", resourceCulture); + } + } + + /// + /// 查找类似 {1} 将在实际请求时替换为 challenge 的本地化字符串。 + /// + internal static string ViewDialogGeetestCustomUrlSampleDescription2 { + get { + return ResourceManager.GetString("ViewDialogGeetestCustomUrlSampleDescription2", resourceCulture); + } + } + + /// + /// 查找类似 将会通过 GET 方式对接口发送请求 的本地化字符串。 + /// + internal static string ViewDialogGeetestCustomUrlSampleDescription3 { + get { + return ResourceManager.GetString("ViewDialogGeetestCustomUrlSampleDescription3", resourceCulture); + } + } + + /// + /// 查找类似 示例 的本地化字符串。 + /// + internal static string ViewDialogGeetestCustomUrlSampleHeader { + get { + return ResourceManager.GetString("ViewDialogGeetestCustomUrlSampleHeader", resourceCulture); + } + } + + /// + /// 查找类似 配置无感验证接口 的本地化字符串。 + /// + internal static string ViewDialogGeetestCustomUrlTitle { + get { + return ResourceManager.GetString("ViewDialogGeetestCustomUrlTitle", resourceCulture); + } + } + /// /// 查找类似 导出 App 的本地化字符串。 /// @@ -4911,6 +4992,33 @@ namespace Snap.Hutao.Resource.Localization { } } + /// + /// 查找类似 配置 的本地化字符串。 + /// + internal static string ViewPageSettingGeetestCustomUrlAction { + get { + return ResourceManager.GetString("ViewPageSettingGeetestCustomUrlAction", resourceCulture); + } + } + + /// + /// 查找类似 配置当请求触发人机验证时使用的验证接口 的本地化字符串。 + /// + internal static string ViewPageSettingGeetestCustomUrlDescription { + get { + return ResourceManager.GetString("ViewPageSettingGeetestCustomUrlDescription", resourceCulture); + } + } + + /// + /// 查找类似 配置验证请求接口 的本地化字符串。 + /// + internal static string ViewPageSettingGeetestCustomUrlHeader { + get { + return ResourceManager.GetString("ViewPageSettingGeetestCustomUrlHeader", resourceCulture); + } + } + /// /// 查找类似 胡桃账号 的本地化字符串。 /// diff --git a/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.resx b/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.resx index 037f796e..ed7e20f4 100644 --- a/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.resx +++ b/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.resx @@ -911,6 +911,33 @@ 手动输入祈愿记录 Url + + 请输入请求接口的 Url 复合模板 + + + 接口需要返回形如上方所示的 Json 数据,多余的数据会被忽略 + + + "code" 为 0 时,指示验证成功,其他的值均视为验证失败 + + + 返回数据 + + + {0} 将在实际请求时替换为 gt + + + {1} 将在实际请求时替换为 challenge + + + 将会通过 GET 方式对接口发送请求 + + + 示例 + + + 配置无感验证接口 + 导出 App @@ -1790,6 +1817,15 @@ 游戏 + + 配置 + + + 配置当请求触发人机验证时使用的验证接口 + + + 配置验证请求接口 + 胡桃账号 diff --git a/src/Snap.Hutao/Snap.Hutao/Service/AppOptions.cs b/src/Snap.Hutao/Snap.Hutao/Service/AppOptions.cs index ecbd5a22..7efb137a 100644 --- a/src/Snap.Hutao/Snap.Hutao/Service/AppOptions.cs +++ b/src/Snap.Hutao/Snap.Hutao/Service/AppOptions.cs @@ -38,6 +38,7 @@ internal sealed partial class AppOptions : DbStoreOptions private BackdropType? backdropType; private CultureInfo? currentCulture; private bool? isAdvancedLaunchOptionsEnabled; + private string? geetestCustomCompositeUrl; /// /// 游戏路径 @@ -100,6 +101,12 @@ internal sealed partial class AppOptions : DbStoreOptions set => SetOption(ref isAdvancedLaunchOptionsEnabled, SettingEntry.IsAdvancedLaunchOptionsEnabled, value); } + public string GeetestCustomCompositeUrl + { + get => GetOption(ref geetestCustomCompositeUrl, SettingEntry.GeetestCustomCompositeUrl); + set => SetOption(ref geetestCustomCompositeUrl, SettingEntry.GeetestCustomCompositeUrl, value); + } + private static NameValue ToNameValue(CultureInfo info) { return new(info.NativeName, info.Name); diff --git a/src/Snap.Hutao/Snap.Hutao/Snap.Hutao.csproj b/src/Snap.Hutao/Snap.Hutao/Snap.Hutao.csproj index 90df6056..4cbe9f85 100644 --- a/src/Snap.Hutao/Snap.Hutao/Snap.Hutao.csproj +++ b/src/Snap.Hutao/Snap.Hutao/Snap.Hutao.csproj @@ -142,6 +142,7 @@ + @@ -325,6 +326,12 @@ + + + MSBuild:Compile + + + MSBuild:Compile diff --git a/src/Snap.Hutao/Snap.Hutao/View/Dialog/AchievementArchiveCreateDialog.xaml b/src/Snap.Hutao/Snap.Hutao/View/Dialog/AchievementArchiveCreateDialog.xaml index 698b0538..e92a2faa 100644 --- a/src/Snap.Hutao/Snap.Hutao/View/Dialog/AchievementArchiveCreateDialog.xaml +++ b/src/Snap.Hutao/Snap.Hutao/View/Dialog/AchievementArchiveCreateDialog.xaml @@ -14,9 +14,9 @@ + PlaceholderText="{shcm:ResourceString Name=ViewDialogAchievementArchiveCreateInputPlaceholder}" + Text="{x:Bind Text, Mode=TwoWay}"/> diff --git a/src/Snap.Hutao/Snap.Hutao/View/Dialog/AchievementArchiveCreateDialog.xaml.cs b/src/Snap.Hutao/Snap.Hutao/View/Dialog/AchievementArchiveCreateDialog.xaml.cs index e097bb92..5aa7f0af 100644 --- a/src/Snap.Hutao/Snap.Hutao/View/Dialog/AchievementArchiveCreateDialog.xaml.cs +++ b/src/Snap.Hutao/Snap.Hutao/View/Dialog/AchievementArchiveCreateDialog.xaml.cs @@ -9,6 +9,7 @@ namespace Snap.Hutao.View.Dialog; /// 成就存档创建对话框 /// [HighQuality] +[DependencyProperty("Text", typeof(string))] internal sealed partial class AchievementArchiveCreateDialog : ContentDialog { private readonly ITaskContext taskContext; @@ -33,8 +34,6 @@ internal sealed partial class AchievementArchiveCreateDialog : ContentDialog { await taskContext.SwitchToMainThreadAsync(); ContentDialogResult result = await ShowAsync(); - string text = InputText.Text ?? string.Empty; - - return new(result == ContentDialogResult.Primary, text); + return new(result == ContentDialogResult.Primary, Text ?? string.Empty); } } diff --git a/src/Snap.Hutao/Snap.Hutao/View/Dialog/CultivateProjectDialog.xaml b/src/Snap.Hutao/Snap.Hutao/View/Dialog/CultivateProjectDialog.xaml index 8d8faac4..c60c0cfc 100644 --- a/src/Snap.Hutao/Snap.Hutao/View/Dialog/CultivateProjectDialog.xaml +++ b/src/Snap.Hutao/Snap.Hutao/View/Dialog/CultivateProjectDialog.xaml @@ -16,7 +16,7 @@ + Text="{x:Bind Text, Mode=TwoWay}"/> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Snap.Hutao/Snap.Hutao/View/Dialog/GeetestCustomUrlDialog.xaml.cs b/src/Snap.Hutao/Snap.Hutao/View/Dialog/GeetestCustomUrlDialog.xaml.cs new file mode 100644 index 00000000..d6902ef1 --- /dev/null +++ b/src/Snap.Hutao/Snap.Hutao/View/Dialog/GeetestCustomUrlDialog.xaml.cs @@ -0,0 +1,28 @@ +// Copyright (c) DGP Studio. All rights reserved. +// Licensed under the MIT license. + +using Microsoft.UI.Xaml.Controls; +using Snap.Hutao.Service; + +namespace Snap.Hutao.View.Dialog; + +[DependencyProperty("Text", typeof(string))] +internal sealed partial class GeetestCustomUrlDialog : ContentDialog +{ + private readonly ITaskContext taskContext; + + public GeetestCustomUrlDialog(IServiceProvider serviceProvider) + { + InitializeComponent(); + XamlRoot = serviceProvider.GetRequiredService().Content.XamlRoot; + Text = serviceProvider.GetRequiredService().GeetestCustomCompositeUrl; + taskContext = serviceProvider.GetRequiredService(); + } + + public async ValueTask> GetUrlAsync() + { + await taskContext.SwitchToMainThreadAsync(); + ContentDialogResult result = await ShowAsync(); + return new(result == ContentDialogResult.Primary, Text ?? string.Empty); + } +} diff --git a/src/Snap.Hutao/Snap.Hutao/View/Page/SettingPage.xaml b/src/Snap.Hutao/Snap.Hutao/View/Page/SettingPage.xaml index 61c874ae..9a05f97d 100644 --- a/src/Snap.Hutao/Snap.Hutao/View/Page/SettingPage.xaml +++ b/src/Snap.Hutao/Snap.Hutao/View/Page/SettingPage.xaml @@ -112,17 +112,6 @@ SelectedItem="{Binding SelectedBackdropType, Mode=TwoWay}"/> - - - - - + + + + + + + + /// 应用程序设置 /// - public AppOptions Options { get => options; } + public AppOptions Options { get => appOptions; } /// /// 胡桃选项 @@ -231,4 +232,18 @@ internal sealed partial class SettingViewModel : Abstraction.ViewModel } } } + + [Command("ConfigureGeetestUrlCommand")] + private async Task ConfigureGeetestUrlAsync() + { + GeetestCustomUrlDialog dialog = await contentDialogFactory.CreateInstanceAsync().ConfigureAwait(false); + (bool isOk, string template) = await dialog.GetUrlAsync().ConfigureAwait(false); + + if (isOk) + { + await taskContext.SwitchToMainThreadAsync(); + appOptions.GeetestCustomCompositeUrl = template; + infoBarService.Success("无感验证复合 Url 配置成功"); + } + } } \ No newline at end of file diff --git a/src/Snap.Hutao/Snap.Hutao/Web/Hutao/Geetest/HomaGeetestClient.cs b/src/Snap.Hutao/Snap.Hutao/Web/Hutao/Geetest/HomaGeetestClient.cs index decd4b02..b02fb2cf 100644 --- a/src/Snap.Hutao/Snap.Hutao/Web/Hutao/Geetest/HomaGeetestClient.cs +++ b/src/Snap.Hutao/Snap.Hutao/Web/Hutao/Geetest/HomaGeetestClient.cs @@ -2,6 +2,7 @@ // Licensed under the MIT license. using Snap.Hutao.Core.DependencyInjection.Annotation.HttpClient; +using Snap.Hutao.Service; using Snap.Hutao.Service.Hutao; using Snap.Hutao.Web.Hutao.GachaLog; using System.Net.Http; @@ -15,14 +16,19 @@ internal sealed partial class HomaGeetestClient private readonly HttpClient httpClient; private readonly JsonSerializerOptions options; private readonly ILogger logger; - private readonly HutaoUserOptions hutaoUserOptions; + private readonly AppOptions appOptions; public async ValueTask VerifyAsync(string gt, string challenge, CancellationToken token) { - await httpClient.TrySetTokenAsync(hutaoUserOptions).ConfigureAwait(false); + string template = appOptions.GeetestCustomCompositeUrl; + + if (string.IsNullOrEmpty(template)) + { + return new() { Code = -1 }; + } GeetestResponse? resp = await httpClient - .TryCatchGetFromJsonAsync(HutaoEndpoints.GeetestVerify(gt, challenge), options, logger, token) + .TryCatchGetFromJsonAsync(template.Format(gt, challenge), options, logger, token) .ConfigureAwait(false); ArgumentNullException.ThrowIfNull(resp);