diff --git a/v2rayN/ServiceLib/Handler/SysProxy/ProxySettingOSX.cs b/v2rayN/ServiceLib/Handler/SysProxy/ProxySettingOSX.cs index e2eb4e26..8ba1e043 100644 --- a/v2rayN/ServiceLib/Handler/SysProxy/ProxySettingOSX.cs +++ b/v2rayN/ServiceLib/Handler/SysProxy/ProxySettingOSX.cs @@ -2,12 +2,81 @@ { public class ProxySettingOSX { + /* + * 仅测试了,MacOS 13.7.1 x86 版本,其他版本有待确认 + */ + /// + /// 应用接口类型 + /// + private static readonly List LstInterface = ["Ethernet", "Wi-Fi", "Thunderbolt Bridge"]; + + /// + /// 代理类型,对应 http,https,socks + /// + private static readonly List LstTypes = ["setwebproxy", "setsecurewebproxy", "setsocksfirewallproxy"]; + public static async Task SetProxy(string host, int port) { + var lstCmd = GetSetCmds(host, port); + 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 lstCmd = []; + foreach (var interf in LstInterface) + { + foreach (var type in LstTypes) + { + lstCmd.Add(new CmdItem() + { + Cmd = "networksetup", + Arguments = [$"-{type}", interf, host, (type.Contains("socks") ? (port - 1) : port).ToString()] + }); + } + } + + return lstCmd; + } + + private static List GetUnsetCmds() + { + List lstCmd = []; + foreach (var interf in LstInterface) + { + foreach (var type in LstTypes) + { + lstCmd.Add(new CmdItem() + { + Cmd = "networksetup", + Arguments = [$"-{type}state", interf, "off"] + }); + } + } + + return lstCmd; } } } \ No newline at end of file