From 4d8bfb213eebe392ac24e02337d95c57fbb27092 Mon Sep 17 00:00:00 2001 From: fonaix Date: Thu, 7 Nov 2024 19:41:58 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=9AMacOS=20x86=E4=B8=8B?= =?UTF-8?q?socks=E8=87=AA=E5=8A=A8=E7=B3=BB=E7=BB=9F=E4=BB=A3=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Handler/SysProxy/ProxySettingOSX.cs | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/v2rayN/ServiceLib/Handler/SysProxy/ProxySettingOSX.cs b/v2rayN/ServiceLib/Handler/SysProxy/ProxySettingOSX.cs index e2eb4e26..d71b54b4 100644 --- a/v2rayN/ServiceLib/Handler/SysProxy/ProxySettingOSX.cs +++ b/v2rayN/ServiceLib/Handler/SysProxy/ProxySettingOSX.cs @@ -1,13 +1,89 @@ namespace ServiceLib.Handler.SysProxy { + /// + /// 开启代理 + /// networksetup -setsocksfirewallproxy "Ethernet" 127.0.0.1 10808 + /// networksetup -setsocksfirewallproxy "Wi-Fi" 127.0.0.1 10808 + /// networksetup -setsocksfirewallproxy "Thunderbolt Bridge" 127.0.0.1 10808 + /// 关闭代理 + /// networksetup -setsocksfirewallproxy "Ethernet" off + /// networksetup -setsocksfirewallproxy "Wi-Fi" off + /// networksetup -setsocksfirewallproxy "Thunderbolt Bridge" off + /// + /// 只测试过 x86 MacOS 13.7.1 + /// public class ProxySettingOSX { + /// + /// + /// + /// + /// 此处传入的是HTTP端口,SOCKS端口需要-1 public static async Task SetProxy(string host, int port) { + var lstCmd = GetSetCmds(host, port - 1); + await ExecCmd(lstCmd); } + public static async Task UnsetProxy() { + var lstCmd = GetUnsetCmds(); + await ExecCmd(lstCmd); + } + + + private static async Task ExecCmd(List lstCmd) + { + foreach (var cmd in lstCmd) + { + if (cmd is null || cmd.Cmd.IsNullOrEmpty() || cmd.Arguments is null) + { + continue; + } + + await Task.Delay(10); + await Utils.GetCliWrapOutput(cmd.Cmd, cmd.Arguments); + } + } + + private static List GetSetCmds(string host, int port) + { + List lstType = ["Ethernet", "Wi-Fi", "Thunderbolt Bridge", "iPhone USB"]; + List lstCmd = []; + foreach (var type in lstType) + { + lstCmd.AddRange(GetSetCmd4Intel(host, port, type)); + } + + return lstCmd; + } + + private static List GetUnsetCmds() + { + List lstType = ["Ethernet", "Wi-Fi", "Thunderbolt Bridge", "iPhone USB"]; + List lstCmd = []; + foreach (var type in lstType) + { + lstCmd.Add(new CmdItem() + { + Cmd = "networksetup", + Arguments = ["-setsocksfirewallproxy", type, "off"] + }); + } + + return lstCmd; + } + + private static List GetSetCmd4Intel(string host, int port, string type) + { + List lstCmd = []; + lstCmd.Add(new CmdItem() + { + Cmd = "networksetup", + Arguments = ["-setsocksfirewallproxy", type, host, port.ToString()] + }); + return lstCmd; } } } \ No newline at end of file