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>
<IsPackable>true</IsPackable>
@@ -41,7 +41,7 @@
<PropertyGroup>
<PackageId>Snap.Hutao.SourceGeneration</PackageId>
<Version>1.3.14</Version>
<Version>1.3.15</Version>
<Authors>DGP Studio</Authors>
<IncludeBuildOutput>false</IncludeBuildOutput>
<DevelopmentDependency>true</DevelopmentDependency>
@@ -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
@@ -222,6 +222,7 @@
<ToggleSwitch
MinWidth="120"
VerticalAlignment="Center"
IsEnabled="{Binding IsTestingProxy, Converter={StaticResource BoolNegationConverter}, Mode=OneWay}"
IsOn="{Binding AppOptions.ProxyEnabled.Value, Mode=TwoWay}"/>
</cwc:SettingsCard>
<cwc:SettingsCard
@@ -232,6 +233,7 @@
<ComboBox
DisplayMemberPath="Name"
ItemsSource="{Binding AppOptions.ProxyTypes}"
IsEnabled="{Binding IsTestingProxy, Converter={StaticResource BoolNegationConverter}, Mode=OneWay}"
SelectedItem="{Binding SelectedProxyType, Mode=TwoWay}"/>
</shuxc:SizeRestrictedContentControl>
</cwc:SettingsCard>
@@ -242,6 +244,7 @@
<TextBox
MinWidth="{ThemeResource SettingsCardContentControlMinWidth}"
VerticalAlignment="Center"
IsEnabled="{Binding IsTestingProxy, Converter={StaticResource BoolNegationConverter}, Mode=OneWay}"
Text="{Binding AppOptions.ProxyAddress.Value, Mode=TwoWay}"/>
</cwc:SettingsCard>
<cwc:SettingsCard
@@ -252,6 +255,7 @@
MinWidth="{ThemeResource SettingsCardContentControlMinWidth}"
VerticalAlignment="Center"
AcceptsExpression="False"
IsEnabled="{Binding IsTestingProxy, Converter={StaticResource BoolNegationConverter}, Mode=OneWay}"
Maximum="65535"
Minimum="1"
Value="{Binding AppOptions.ProxyPort.Value, Mode=TwoWay}"/>
@@ -266,7 +270,8 @@
IsEnabled="{Binding IsTestingProxy, Converter={StaticResource BoolNegationConverter}, Mode=OneWay}"/>
<Button
Command="{Binding SaveProxyCommand}"
Content="{shuxm:ResourceString Name=ViewModelSettingProxySaveAction}"/>
Content="{shuxm:ResourceString Name=ViewModelSettingProxySaveAction}"
IsEnabled="{Binding IsTestingProxy, Converter={StaticResource BoolNegationConverter}, Mode=OneWay}"/>
<TextBlock
VerticalAlignment="Center"
Text="{Binding ProxyTestResult}"
@@ -63,7 +63,7 @@ internal sealed partial class SettingProxyViewModel : Abstraction.ViewModel
using HttpClient httpClient = new(new SocketsHttpHandler
{
Proxy = hutaoWebProxy,
UseProxy = AppOptions.ProxyEnabled.Value,
UseProxy = true,
});
httpClient.Timeout = TimeSpan.FromSeconds(10);