diff --git a/src/Snap.Hutao/Snap.Hutao.Win32/PInvoke.cs b/src/Snap.Hutao/Snap.Hutao.Win32/PInvoke.cs index fc570f6e..a54fa034 100644 --- a/src/Snap.Hutao/Snap.Hutao.Win32/PInvoke.cs +++ b/src/Snap.Hutao/Snap.Hutao.Win32/PInvoke.cs @@ -1,7 +1,6 @@ using System; using Windows.Win32.Foundation; using Windows.Win32.System.Com; -using Windows.Win32.System.Registry; namespace Windows.Win32; diff --git a/src/Snap.Hutao/Snap.Hutao/Core/IO/Http/DynamicProxy/DynamicHttpProxy.cs b/src/Snap.Hutao/Snap.Hutao/Core/IO/Http/DynamicProxy/DynamicHttpProxy.cs index 65861589..3485479c 100644 --- a/src/Snap.Hutao/Snap.Hutao/Core/IO/Http/DynamicProxy/DynamicHttpProxy.cs +++ b/src/Snap.Hutao/Snap.Hutao/Core/IO/Http/DynamicProxy/DynamicHttpProxy.cs @@ -1,7 +1,7 @@ // Copyright (c) DGP Studio. All rights reserved. // Licensed under the MIT license. -using Snap.Hutao.Core.Shell; +using Snap.Hutao.Win32.Registry; using System.Net; using System.Reflection; @@ -10,15 +10,29 @@ namespace Snap.Hutao.Core.IO.Http.DynamicProxy; [Injection(InjectAs.Singleton)] internal sealed partial class DynamicHttpProxy : IWebProxy, IDisposable { + private const string ProxySettingPath = @"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections"; + + private static readonly MethodInfo ConstructSystemProxyMethod; + private readonly RegistryWatcher watcher; private IWebProxy innerProxy = default!; + static DynamicHttpProxy() + { + Type? systemProxyInfoType = typeof(System.Net.Http.SocketsHttpHandler).Assembly.GetType("System.Net.Http.SystemProxyInfo"); + ArgumentNullException.ThrowIfNull(systemProxyInfoType); + + MethodInfo? constructSystemProxyMethod = systemProxyInfoType.GetMethod("ConstructSystemProxy", BindingFlags.Static | BindingFlags.Public); + ArgumentNullException.ThrowIfNull(constructSystemProxyMethod); + ConstructSystemProxyMethod = constructSystemProxyMethod; + } + public DynamicHttpProxy() { UpdateProxy(); - watcher = new (@"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections"); + watcher = new(ProxySettingPath, UpdateProxy); watcher.Start(); } @@ -32,6 +46,8 @@ internal sealed partial class DynamicHttpProxy : IWebProxy, IDisposable private IWebProxy InnerProxy { get => innerProxy; + + [MemberNotNull(nameof(innerProxy))] set { if (ReferenceEquals(innerProxy, value)) @@ -39,24 +55,17 @@ internal sealed partial class DynamicHttpProxy : IWebProxy, IDisposable return; } - if (innerProxy is IDisposable disposable) - { - disposable.Dispose(); - } - + (innerProxy as IDisposable)?.Dispose(); innerProxy = value; } } + [MemberNotNull(nameof(innerProxy))] public void UpdateProxy() { - Assembly httpNamespace = Assembly.Load("System.Net.Http"); - Type? systemProxyInfoType = httpNamespace.GetType("System.Net.Http.SystemProxyInfo"); - ArgumentNullException.ThrowIfNull(systemProxyInfoType); - MethodInfo? constructSystemProxyMethod = systemProxyInfoType.GetMethod("ConstructSystemProxy", BindingFlags.Static | BindingFlags.Public); - ArgumentNullException.ThrowIfNull(constructSystemProxyMethod); - IWebProxy? proxy = (IWebProxy?)constructSystemProxyMethod.Invoke(null, null); + IWebProxy? proxy = ConstructSystemProxyMethod.Invoke(default, default) as IWebProxy; ArgumentNullException.ThrowIfNull(proxy); + InnerProxy = proxy; } @@ -72,16 +81,7 @@ internal sealed partial class DynamicHttpProxy : IWebProxy, IDisposable public void Dispose() { - if (innerProxy is IDisposable disposable) - { - disposable.Dispose(); - } - + (innerProxy as IDisposable)?.Dispose(); watcher.Dispose(); } - - private void OnRegistryChanged(object? sender, EventArgs e) - { - UpdateProxy(); - } } \ No newline at end of file diff --git a/src/Snap.Hutao/Snap.Hutao/Core/Shell/RegistryWatcher.cs b/src/Snap.Hutao/Snap.Hutao/Win32/Registry/RegistryWatcher.cs similarity index 96% rename from src/Snap.Hutao/Snap.Hutao/Core/Shell/RegistryWatcher.cs rename to src/Snap.Hutao/Snap.Hutao/Win32/Registry/RegistryWatcher.cs index 4ad120af..43fa8020 100644 --- a/src/Snap.Hutao/Snap.Hutao/Core/Shell/RegistryWatcher.cs +++ b/src/Snap.Hutao/Snap.Hutao/Win32/Registry/RegistryWatcher.cs @@ -6,7 +6,7 @@ using Windows.Win32.Foundation; using Windows.Win32.System.Registry; using static Windows.Win32.PInvoke; -namespace Snap.Hutao.Core.Shell; +namespace Snap.Hutao.Win32.Registry; internal sealed partial class RegistryWatcher : IDisposable { @@ -32,7 +32,6 @@ internal sealed partial class RegistryWatcher : IDisposable public RegistryWatcher(string keyName, Action valueChangedCallback) { - ArgumentException.ThrowIfNullOrEmpty(keyName); string[] pathArray = keyName.Split('\\'); hKey = pathArray[0] switch @@ -143,8 +142,11 @@ internal sealed partial class RegistryWatcher : IDisposable } } - // Before exiting, signal the Dispose method. - disposeEvent.Reset(); + if (!disposed) + { + // Before exiting, signal the Dispose method. + disposeEvent.Reset(); + } } catch (OperationCanceledException) {