1
0
mirror of https://github.com/wangdage12/Snap.Hutao.git synced 2026-07-31 04:10:25 +08:00

修复代理功能问题 (CodeRabbit AI 评论修复)

This commit is contained in:
cheri
2026-06-22 09:30:42 +08:00
parent 41cfb8c8f4
commit 27871adc63
4 changed files with 37 additions and 10 deletions
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<IsPackable>true</IsPackable> <IsPackable>true</IsPackable>
@@ -41,7 +41,7 @@
<PropertyGroup> <PropertyGroup>
<PackageId>Snap.Hutao.SourceGeneration</PackageId> <PackageId>Snap.Hutao.SourceGeneration</PackageId>
<Version>1.3.14</Version> <Version>1.3.15</Version>
<Authors>DGP Studio</Authors> <Authors>DGP Studio</Authors>
<IncludeBuildOutput>false</IncludeBuildOutput> <IncludeBuildOutput>false</IncludeBuildOutput>
<DevelopmentDependency>true</DevelopmentDependency> <DevelopmentDependency>true</DevelopmentDependency>
@@ -10,6 +10,7 @@ namespace Snap.Hutao.Core.IO.Http.Proxy;
internal sealed class HutaoWebProxy : IWebProxy internal sealed class HutaoWebProxy : IWebProxy
{ {
private static readonly IWebProxy NoProxy = new DirectWebProxy();
private readonly AppOptions appOptions; private readonly AppOptions appOptions;
private readonly HttpProxyUsingSystemProxy systemProxy; private readonly HttpProxyUsingSystemProxy systemProxy;
@@ -31,7 +32,7 @@ internal sealed class HutaoWebProxy : IWebProxy
{ {
if (!appOptions.ProxyEnabled.Value) if (!appOptions.ProxyEnabled.Value)
{ {
return systemProxy.DisplayProxyUri; return "DIRECT";
} }
return appOptions.ProxyType.Value switch return appOptions.ProxyType.Value switch
@@ -43,7 +44,7 @@ internal sealed class HutaoWebProxy : IWebProxy
ProxyType.Socks5 => string.IsNullOrEmpty(appOptions.ProxyAddress.Value) ProxyType.Socks5 => string.IsNullOrEmpty(appOptions.ProxyAddress.Value)
? systemProxy.DisplayProxyUri ? systemProxy.DisplayProxyUri
: $"socks5://{appOptions.ProxyAddress.Value}:{appOptions.ProxyPort.Value}", : $"socks5://{appOptions.ProxyAddress.Value}:{appOptions.ProxyPort.Value}",
ProxyType.None => systemProxy.DisplayProxyUri, ProxyType.None => "DIRECT",
_ => systemProxy.DisplayProxyUri, _ => systemProxy.DisplayProxyUri,
}; };
} }
@@ -55,7 +56,7 @@ internal sealed class HutaoWebProxy : IWebProxy
{ {
if (!appOptions.ProxyEnabled.Value) if (!appOptions.ProxyEnabled.Value)
{ {
return systemProxy; return NoProxy;
} }
return appOptions.ProxyType.Value switch return appOptions.ProxyType.Value switch
@@ -63,7 +64,7 @@ internal sealed class HutaoWebProxy : IWebProxy
ProxyType.SystemProxy => systemProxy, ProxyType.SystemProxy => systemProxy,
ProxyType.Http => CreateHttpProxy(), ProxyType.Http => CreateHttpProxy(),
ProxyType.Socks5 => CreateSocks5Proxy(), ProxyType.Socks5 => CreateSocks5Proxy(),
ProxyType.None => systemProxy, ProxyType.None => NoProxy,
_ => systemProxy, _ => systemProxy,
}; };
} }
@@ -84,7 +85,7 @@ internal sealed class HutaoWebProxy : IWebProxy
string address = appOptions.ProxyAddress.Value; string address = appOptions.ProxyAddress.Value;
int port = appOptions.ProxyPort.Value; int port = appOptions.ProxyPort.Value;
if (string.IsNullOrEmpty(address)) if (!IsValidProxyAddress(address) || !IsValidProxyPort(port))
{ {
return systemProxy; return systemProxy;
} }
@@ -102,13 +103,34 @@ internal sealed class HutaoWebProxy : IWebProxy
string address = appOptions.ProxyAddress.Value; string address = appOptions.ProxyAddress.Value;
int port = appOptions.ProxyPort.Value; int port = appOptions.ProxyPort.Value;
if (string.IsNullOrEmpty(address)) if (!IsValidProxyAddress(address) || !IsValidProxyPort(port))
{ {
return systemProxy; return systemProxy;
} }
return new Socks5WebProxy(address, port); 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 internal sealed class Socks5WebProxy : IWebProxy
@@ -222,6 +222,7 @@
<ToggleSwitch <ToggleSwitch
MinWidth="120" MinWidth="120"
VerticalAlignment="Center" VerticalAlignment="Center"
IsEnabled="{Binding IsTestingProxy, Converter={StaticResource BoolNegationConverter}, Mode=OneWay}"
IsOn="{Binding AppOptions.ProxyEnabled.Value, Mode=TwoWay}"/> IsOn="{Binding AppOptions.ProxyEnabled.Value, Mode=TwoWay}"/>
</cwc:SettingsCard> </cwc:SettingsCard>
<cwc:SettingsCard <cwc:SettingsCard
@@ -232,6 +233,7 @@
<ComboBox <ComboBox
DisplayMemberPath="Name" DisplayMemberPath="Name"
ItemsSource="{Binding AppOptions.ProxyTypes}" ItemsSource="{Binding AppOptions.ProxyTypes}"
IsEnabled="{Binding IsTestingProxy, Converter={StaticResource BoolNegationConverter}, Mode=OneWay}"
SelectedItem="{Binding SelectedProxyType, Mode=TwoWay}"/> SelectedItem="{Binding SelectedProxyType, Mode=TwoWay}"/>
</shuxc:SizeRestrictedContentControl> </shuxc:SizeRestrictedContentControl>
</cwc:SettingsCard> </cwc:SettingsCard>
@@ -242,6 +244,7 @@
<TextBox <TextBox
MinWidth="{ThemeResource SettingsCardContentControlMinWidth}" MinWidth="{ThemeResource SettingsCardContentControlMinWidth}"
VerticalAlignment="Center" VerticalAlignment="Center"
IsEnabled="{Binding IsTestingProxy, Converter={StaticResource BoolNegationConverter}, Mode=OneWay}"
Text="{Binding AppOptions.ProxyAddress.Value, Mode=TwoWay}"/> Text="{Binding AppOptions.ProxyAddress.Value, Mode=TwoWay}"/>
</cwc:SettingsCard> </cwc:SettingsCard>
<cwc:SettingsCard <cwc:SettingsCard
@@ -252,6 +255,7 @@
MinWidth="{ThemeResource SettingsCardContentControlMinWidth}" MinWidth="{ThemeResource SettingsCardContentControlMinWidth}"
VerticalAlignment="Center" VerticalAlignment="Center"
AcceptsExpression="False" AcceptsExpression="False"
IsEnabled="{Binding IsTestingProxy, Converter={StaticResource BoolNegationConverter}, Mode=OneWay}"
Maximum="65535" Maximum="65535"
Minimum="1" Minimum="1"
Value="{Binding AppOptions.ProxyPort.Value, Mode=TwoWay}"/> Value="{Binding AppOptions.ProxyPort.Value, Mode=TwoWay}"/>
@@ -266,7 +270,8 @@
IsEnabled="{Binding IsTestingProxy, Converter={StaticResource BoolNegationConverter}, Mode=OneWay}"/> IsEnabled="{Binding IsTestingProxy, Converter={StaticResource BoolNegationConverter}, Mode=OneWay}"/>
<Button <Button
Command="{Binding SaveProxyCommand}" Command="{Binding SaveProxyCommand}"
Content="{shuxm:ResourceString Name=ViewModelSettingProxySaveAction}"/> Content="{shuxm:ResourceString Name=ViewModelSettingProxySaveAction}"
IsEnabled="{Binding IsTestingProxy, Converter={StaticResource BoolNegationConverter}, Mode=OneWay}"/>
<TextBlock <TextBlock
VerticalAlignment="Center" VerticalAlignment="Center"
Text="{Binding ProxyTestResult}" Text="{Binding ProxyTestResult}"
@@ -63,7 +63,7 @@ internal sealed partial class SettingProxyViewModel : Abstraction.ViewModel
using HttpClient httpClient = new(new SocketsHttpHandler using HttpClient httpClient = new(new SocketsHttpHandler
{ {
Proxy = hutaoWebProxy, Proxy = hutaoWebProxy,
UseProxy = AppOptions.ProxyEnabled.Value, UseProxy = true,
}); });
httpClient.Timeout = TimeSpan.FromSeconds(10); httpClient.Timeout = TimeSpan.FromSeconds(10);