From b24689ba25a5198449f3cdfa6782cc2f84d4ce39 Mon Sep 17 00:00:00 2001 From: Connection Refused Date: Wed, 29 Jan 2020 19:48:02 +0800 Subject: [PATCH] Change system proxy settings with C++ Windows API --- Netch/Controllers/HTTPController.cs | 8 ++++++++ Netch/NativeMethods.cs | 30 ++++++++++++++++++++++++++--- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/Netch/Controllers/HTTPController.cs b/Netch/Controllers/HTTPController.cs index 852f6355..87f8b4eb 100644 --- a/Netch/Controllers/HTTPController.cs +++ b/Netch/Controllers/HTTPController.cs @@ -36,7 +36,10 @@ namespace Netch.Controllers if (mode.Type != 5) { + NativeMethods.SetGlobal($"127.0.0.1:{Global.Settings.HTTPLocalPort}", ""); + // HTTP 系统代理模式,启动系统代理 + /* using (var registry = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true)) { registry.SetValue("ProxyEnable", 1); @@ -45,6 +48,7 @@ namespace Netch.Controllers Win32Native.InternetSetOption(IntPtr.Zero, 39, IntPtr.Zero, 0); Win32Native.InternetSetOption(IntPtr.Zero, 37, IntPtr.Zero, 0); } + */ } } catch (Exception e) @@ -72,6 +76,9 @@ namespace Netch.Controllers Utils.Logging.Info(e.ToString()); } + NativeMethods.SetDIRECT(); + + /* using (var registry = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true)) { registry.SetValue("ProxyEnable", 0); @@ -80,6 +87,7 @@ namespace Netch.Controllers Win32Native.InternetSetOption(IntPtr.Zero, 39, IntPtr.Zero, 0); Win32Native.InternetSetOption(IntPtr.Zero, 37, IntPtr.Zero, 0); } + */ } catch (Exception e) { diff --git a/Netch/NativeMethods.cs b/Netch/NativeMethods.cs index 3cbff8d1..0fbfd77d 100644 --- a/Netch/NativeMethods.cs +++ b/Netch/NativeMethods.cs @@ -13,7 +13,7 @@ namespace Netch /// 适配器索引 /// 跃点数 /// 是否成功 - [DllImport("bin\\NetchCore.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "CreateRoute")] + [DllImport("bin\\NetchCore", CallingConvention = CallingConvention.Cdecl, EntryPoint = "CreateRoute")] public static extern bool CreateRoute(string address, int netmask, string gateway, int index, int metric = 0); /// @@ -25,7 +25,7 @@ namespace Netch /// 适配器索引 /// 跃点数 /// 是否成功 - [DllImport("bin\\NetchCore.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "ChangeRoute")] + [DllImport("bin\\NetchCore", CallingConvention = CallingConvention.Cdecl, EntryPoint = "ChangeRoute")] public static extern bool ChangeRoute(string address, int netmask, string gateway, int index, int metric = 0); /// @@ -37,7 +37,31 @@ namespace Netch /// 适配器索引 /// 跃点数 /// 是否成功 - [DllImport("bin\\NetchCore.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "DeleteRoute")] + [DllImport("bin\\NetchCore", CallingConvention = CallingConvention.Cdecl, EntryPoint = "DeleteRoute")] public static extern bool DeleteRoute(string address, int netmask, string gateway, int index, int metric = 0); + + /// + /// 设置直连 + /// + /// 是否成功 + [DllImport("bin\\sysproxy", CallingConvention = CallingConvention.Cdecl)] + public static extern bool SetDIRECT(); + + /// + /// 设置全局 + /// + /// 地址 + /// 绕过 + /// 是否成功 + [DllImport("bin\\sysproxy", CallingConvention = CallingConvention.Cdecl)] + public static extern bool SetGlobal([MarshalAs(UnmanagedType.LPTStr)] string remote, [MarshalAs(UnmanagedType.LPTStr)] string bypass); + + /// + /// 设置自动代理 + /// + /// URL + /// 是否成功 + [DllImport("bin\\sysproxy", CallingConvention = CallingConvention.Cdecl)] + public static extern bool SetURL([MarshalAs(UnmanagedType.LPTStr)] string remote); } }