From 27871adc634ab1aa3d2e4a73a26786e01b38fdcf Mon Sep 17 00:00:00 2001 From: cheri Date: Mon, 22 Jun 2026 09:30:42 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BB=A3=E7=90=86=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E9=97=AE=E9=A2=98=20(CodeRabbit=20AI=20=E8=AF=84?= =?UTF-8?q?=E8=AE=BA=E4=BF=AE=E5=A4=8D)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Snap.Hutao.SourceGeneration.csproj | 4 +-- .../Core/IO/Http/Proxy/HutaoWebProxy.cs | 34 +++++++++++++++---- .../UI/Xaml/View/Page/SettingPage.xaml | 7 +++- .../Setting/SettingProxyViewModel.cs | 2 +- 4 files changed, 37 insertions(+), 10 deletions(-) diff --git a/src/Snap.Hutao/Snap.Hutao.SourceGeneration/src/Snap.Hutao.SourceGeneration/Snap.Hutao.SourceGeneration/Snap.Hutao.SourceGeneration.csproj b/src/Snap.Hutao/Snap.Hutao.SourceGeneration/src/Snap.Hutao.SourceGeneration/Snap.Hutao.SourceGeneration/Snap.Hutao.SourceGeneration.csproj index 6463874..b552a38 100644 --- a/src/Snap.Hutao/Snap.Hutao.SourceGeneration/src/Snap.Hutao.SourceGeneration/Snap.Hutao.SourceGeneration/Snap.Hutao.SourceGeneration.csproj +++ b/src/Snap.Hutao/Snap.Hutao.SourceGeneration/src/Snap.Hutao.SourceGeneration/Snap.Hutao.SourceGeneration/Snap.Hutao.SourceGeneration.csproj @@ -1,4 +1,4 @@ - + true @@ -41,7 +41,7 @@ Snap.Hutao.SourceGeneration - 1.3.14 + 1.3.15 DGP Studio false true diff --git a/src/Snap.Hutao/Snap.Hutao/Core/IO/Http/Proxy/HutaoWebProxy.cs b/src/Snap.Hutao/Snap.Hutao/Core/IO/Http/Proxy/HutaoWebProxy.cs index d89153b..cba442a 100644 --- a/src/Snap.Hutao/Snap.Hutao/Core/IO/Http/Proxy/HutaoWebProxy.cs +++ b/src/Snap.Hutao/Snap.Hutao/Core/IO/Http/Proxy/HutaoWebProxy.cs @@ -10,6 +10,7 @@ namespace Snap.Hutao.Core.IO.Http.Proxy; internal sealed class HutaoWebProxy : IWebProxy { + private static readonly IWebProxy NoProxy = new DirectWebProxy(); private readonly AppOptions appOptions; private readonly HttpProxyUsingSystemProxy systemProxy; @@ -31,7 +32,7 @@ internal sealed class HutaoWebProxy : IWebProxy { if (!appOptions.ProxyEnabled.Value) { - return systemProxy.DisplayProxyUri; + return "DIRECT"; } return appOptions.ProxyType.Value switch @@ -43,7 +44,7 @@ internal sealed class HutaoWebProxy : IWebProxy ProxyType.Socks5 => string.IsNullOrEmpty(appOptions.ProxyAddress.Value) ? systemProxy.DisplayProxyUri : $"socks5://{appOptions.ProxyAddress.Value}:{appOptions.ProxyPort.Value}", - ProxyType.None => systemProxy.DisplayProxyUri, + ProxyType.None => "DIRECT", _ => systemProxy.DisplayProxyUri, }; } @@ -55,7 +56,7 @@ internal sealed class HutaoWebProxy : IWebProxy { if (!appOptions.ProxyEnabled.Value) { - return systemProxy; + return NoProxy; } return appOptions.ProxyType.Value switch @@ -63,7 +64,7 @@ internal sealed class HutaoWebProxy : IWebProxy ProxyType.SystemProxy => systemProxy, ProxyType.Http => CreateHttpProxy(), ProxyType.Socks5 => CreateSocks5Proxy(), - ProxyType.None => systemProxy, + ProxyType.None => NoProxy, _ => systemProxy, }; } @@ -84,7 +85,7 @@ internal sealed class HutaoWebProxy : IWebProxy string address = appOptions.ProxyAddress.Value; int port = appOptions.ProxyPort.Value; - if (string.IsNullOrEmpty(address)) + if (!IsValidProxyAddress(address) || !IsValidProxyPort(port)) { return systemProxy; } @@ -102,13 +103,34 @@ internal sealed class HutaoWebProxy : IWebProxy string address = appOptions.ProxyAddress.Value; int port = appOptions.ProxyPort.Value; - if (string.IsNullOrEmpty(address)) + if (!IsValidProxyAddress(address) || !IsValidProxyPort(port)) { return systemProxy; } return new Socks5WebProxy(address, port); } + + private static bool IsValidProxyAddress(string address) + { + return !string.IsNullOrEmpty(address) && + (Uri.CheckHostName(address) != UriHostNameType.Unknown || + System.Net.IPAddress.TryParse(address, out _)); + } + + private static bool IsValidProxyPort(int port) + { + return port is >= 1 and <= 65535; + } + + private sealed class DirectWebProxy : IWebProxy + { + public ICredentials? Credentials { get; set; } + + public Uri? GetProxy(Uri destination) => destination; + + public bool IsBypassed(Uri host) => true; + } } internal sealed class Socks5WebProxy : IWebProxy diff --git a/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Page/SettingPage.xaml b/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Page/SettingPage.xaml index eba6b88..182b4d1 100644 --- a/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Page/SettingPage.xaml +++ b/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Page/SettingPage.xaml @@ -222,6 +222,7 @@ @@ -242,6 +244,7 @@ @@ -266,7 +270,8 @@ IsEnabled="{Binding IsTestingProxy, Converter={StaticResource BoolNegationConverter}, Mode=OneWay}"/>