152 lines
5.6 KiB
C#
152 lines
5.6 KiB
C#
|
|
using System.Runtime.InteropServices;
|
||
|
|
using System.Net;
|
||
|
|
using System.Net.Sockets;
|
||
|
|
using System.Text.Json;
|
||
|
|
using Microsoft.Win32;
|
||
|
|
|
||
|
|
namespace UCVL.VPNPlus;
|
||
|
|
|
||
|
|
internal static class SystemProxy
|
||
|
|
{
|
||
|
|
private const string InternetSettings = @"Software\Microsoft\Windows\CurrentVersion\Internet Settings";
|
||
|
|
private const int InternetOptionSettingsChanged = 39;
|
||
|
|
private const int InternetOptionRefresh = 37;
|
||
|
|
|
||
|
|
[DllImport("wininet.dll", SetLastError = true)]
|
||
|
|
private static extern bool InternetSetOption(IntPtr internet, int option, IntPtr buffer, int bufferLength);
|
||
|
|
|
||
|
|
private sealed class ProxyState
|
||
|
|
{
|
||
|
|
public int ProxyEnable { get; init; }
|
||
|
|
public bool HadProxyServer { get; init; }
|
||
|
|
public string? ProxyServer { get; init; }
|
||
|
|
public bool HadProxyOverride { get; init; }
|
||
|
|
public string? ProxyOverride { get; init; }
|
||
|
|
}
|
||
|
|
|
||
|
|
internal static void Enable()
|
||
|
|
{
|
||
|
|
AppPaths.EnsureDirectory();
|
||
|
|
using var key = Registry.CurrentUser.OpenSubKey(InternetSettings, writable: true)
|
||
|
|
?? throw new InvalidOperationException("无法打开 Windows 系统代理设置。");
|
||
|
|
if (!File.Exists(AppPaths.ProxyStateFile))
|
||
|
|
{
|
||
|
|
var server = key.GetValue("ProxyServer");
|
||
|
|
var bypass = key.GetValue("ProxyOverride");
|
||
|
|
var state = new ProxyState
|
||
|
|
{
|
||
|
|
ProxyEnable = NormalizeProxyEnable(
|
||
|
|
Convert.ToInt32(key.GetValue("ProxyEnable", 0)),
|
||
|
|
server?.ToString()),
|
||
|
|
HadProxyServer = server is not null,
|
||
|
|
ProxyServer = server?.ToString(),
|
||
|
|
HadProxyOverride = bypass is not null,
|
||
|
|
ProxyOverride = bypass?.ToString(),
|
||
|
|
};
|
||
|
|
File.WriteAllText(AppPaths.ProxyStateFile, JsonSerializer.Serialize(state));
|
||
|
|
}
|
||
|
|
|
||
|
|
key.SetValue("ProxyEnable", 1, RegistryValueKind.DWord);
|
||
|
|
key.SetValue("ProxyServer", "http=127.0.0.1:10809;https=127.0.0.1:10809;socks=127.0.0.1:10808", RegistryValueKind.String);
|
||
|
|
key.SetValue("ProxyOverride", "<local>;localhost;127.*;10.*;192.168.*;172.16.*;172.17.*;172.18.*;172.19.*;172.2?.*;172.30.*;172.31.*", RegistryValueKind.String);
|
||
|
|
NotifyChanged();
|
||
|
|
}
|
||
|
|
|
||
|
|
internal static bool Restore()
|
||
|
|
{
|
||
|
|
if (!File.Exists(AppPaths.ProxyStateFile))
|
||
|
|
{
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
var restored = false;
|
||
|
|
try
|
||
|
|
{
|
||
|
|
var state = JsonSerializer.Deserialize<ProxyState>(File.ReadAllText(AppPaths.ProxyStateFile));
|
||
|
|
if (state is null)
|
||
|
|
{
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
using var key = Registry.CurrentUser.OpenSubKey(InternetSettings, writable: true);
|
||
|
|
if (key is null)
|
||
|
|
{
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
key.SetValue(
|
||
|
|
"ProxyEnable",
|
||
|
|
NormalizeProxyEnable(state.ProxyEnable, state.ProxyServer),
|
||
|
|
RegistryValueKind.DWord);
|
||
|
|
RestoreValue(key, "ProxyServer", state.HadProxyServer, state.ProxyServer);
|
||
|
|
RestoreValue(key, "ProxyOverride", state.HadProxyOverride, state.ProxyOverride);
|
||
|
|
NotifyChanged();
|
||
|
|
restored = true;
|
||
|
|
}
|
||
|
|
catch (Exception exception)
|
||
|
|
{
|
||
|
|
AppPaths.WriteError(exception);
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
finally
|
||
|
|
{
|
||
|
|
if (restored)
|
||
|
|
{
|
||
|
|
try { File.Delete(AppPaths.ProxyStateFile); } catch { }
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return restored;
|
||
|
|
}
|
||
|
|
|
||
|
|
private static void RestoreValue(RegistryKey key, string name, bool existed, string? value)
|
||
|
|
{
|
||
|
|
if (existed)
|
||
|
|
{
|
||
|
|
key.SetValue(name, value ?? "", RegistryValueKind.String);
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
key.DeleteValue(name, throwOnMissingValue: false);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private static int NormalizeProxyEnable(int enabled, string? proxyServer)
|
||
|
|
{
|
||
|
|
if (enabled == 0 || string.IsNullOrWhiteSpace(proxyServer)) return 0;
|
||
|
|
var foundLocalEndpoint = false;
|
||
|
|
foreach (var item in proxyServer.Split(';', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries))
|
||
|
|
{
|
||
|
|
var endpoint = item.Contains('=') ? item[(item.IndexOf('=') + 1)..] : item;
|
||
|
|
if (!endpoint.Contains("://", StringComparison.Ordinal)) endpoint = "http://" + endpoint;
|
||
|
|
if (!Uri.TryCreate(endpoint, UriKind.Absolute, out var uri) || uri.Port is < 1 or > 65535)
|
||
|
|
{
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
var isLoopback = string.Equals(uri.Host, "localhost", StringComparison.OrdinalIgnoreCase)
|
||
|
|
|| (IPAddress.TryParse(uri.Host, out var address) && IPAddress.IsLoopback(address));
|
||
|
|
if (!isLoopback) return enabled;
|
||
|
|
foundLocalEndpoint = true;
|
||
|
|
try
|
||
|
|
{
|
||
|
|
using var client = new TcpClient();
|
||
|
|
using var timeout = new CancellationTokenSource(TimeSpan.FromMilliseconds(300));
|
||
|
|
client.ConnectAsync(uri.Host, uri.Port, timeout.Token).AsTask().GetAwaiter().GetResult();
|
||
|
|
if (client.Connected) return enabled;
|
||
|
|
}
|
||
|
|
catch
|
||
|
|
{
|
||
|
|
// Try the next configured loopback endpoint.
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if (foundLocalEndpoint)
|
||
|
|
{
|
||
|
|
AppPaths.WriteLog($"检测到未监听的残留系统代理,恢复时自动关闭:{proxyServer}");
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
return enabled;
|
||
|
|
}
|
||
|
|
|
||
|
|
private static void NotifyChanged()
|
||
|
|
{
|
||
|
|
InternetSetOption(IntPtr.Zero, InternetOptionSettingsChanged, IntPtr.Zero, 0);
|
||
|
|
InternetSetOption(IntPtr.Zero, InternetOptionRefresh, IntPtr.Zero, 0);
|
||
|
|
}
|
||
|
|
}
|