diff --git a/v2rayN/ServiceLib/Global.cs b/v2rayN/ServiceLib/Global.cs index fbda8abf..126e7fea 100644 --- a/v2rayN/ServiceLib/Global.cs +++ b/v2rayN/ServiceLib/Global.cs @@ -38,6 +38,7 @@ namespace ServiceLib public const string ClashTunYaml = NamespaceSample + "clash_tun_yaml"; public const string LinuxAutostartConfig = NamespaceSample + "linux_autostart_config"; public const string PacFileName = NamespaceSample + "pac"; + public const string ProxySetOSXShellFileName = NamespaceSample + "proxy_set_osx_sh"; public const string DefaultSecurity = "auto"; public const string DefaultNetwork = "tcp"; diff --git a/v2rayN/ServiceLib/Handler/SysProxy/ProxySettingOSX.cs b/v2rayN/ServiceLib/Handler/SysProxy/ProxySettingOSX.cs index 33f6f36b..f7ec5585 100644 --- a/v2rayN/ServiceLib/Handler/SysProxy/ProxySettingOSX.cs +++ b/v2rayN/ServiceLib/Handler/SysProxy/ProxySettingOSX.cs @@ -1,101 +1,38 @@ -namespace ServiceLib.Handler.SysProxy +namespace ServiceLib.Handler.SysProxy { public class ProxySettingOSX { - /// - /// 应用接口类型 - /// - private static readonly List LstInterface = ["Ethernet", "Wi-Fi", "Thunderbolt Bridge", "USB 10/100/1000 LAN"]; - - /// - /// 代理类型,对应 http,https,socks - /// - private static readonly List LstTypes = ["setwebproxy", "setsecurewebproxy", "setsocksfirewallproxy"]; + private static readonly string _proxySetFileName = $"{Global.ProxySetOSXShellFileName.Replace(Global.NamespaceSample, "")}.sh"; public static async Task SetProxy(string host, int port, string exceptions) { - var lstInterface = await GetListNetworkServices(); - var lstCmd = GetSetCmds(lstInterface, host, port, exceptions); - await ExecCmd(lstCmd); + List args = ["set", host, port.ToString()]; + if (exceptions.IsNotEmpty()) + { + args.AddRange(exceptions.Split(',')); + } + + await ExecCmd(args); } public static async Task UnsetProxy() { - var lstInterface = await GetListNetworkServices(); - var lstCmd = GetUnsetCmds(lstInterface); - await ExecCmd(lstCmd); + List args = ["clear"]; + await ExecCmd(args); } - private static async Task ExecCmd(List lstCmd) + private static async Task ExecCmd(List args) { - foreach (var cmd in lstCmd) + var fileName = Utils.GetBinConfigPath(_proxySetFileName); + if (!File.Exists(fileName)) { - 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(List lstInterface, string host, int port, string exceptions) - { - List lstCmd = []; - foreach (var interf in lstInterface) - { - foreach (var type in LstTypes) - { - lstCmd.Add(new CmdItem() - { - Cmd = "networksetup", - Arguments = [$"-{type}", interf, host, port.ToString()] - }); - } - if (exceptions.IsNotEmpty()) - { - List args = [$"-setproxybypassdomains", interf]; - args.AddRange(exceptions.Split(',')); - lstCmd.Add(new CmdItem() - { - Cmd = "networksetup", - Arguments = args - }); - } + var contents = EmbedUtils.GetEmbedText(Global.ProxySetOSXShellFileName); + await File.AppendAllTextAsync(fileName, contents); } - return lstCmd; - } + await Utils.SetLinuxChmod(fileName); - private static List GetUnsetCmds(List lstInterface) - { - 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; - } - - public static async Task> GetListNetworkServices() - { - var services = await Utils.GetListNetworkServices(); - if (services.IsNullOrEmpty()) - { - return LstInterface; - } - - var lst = services.Split(Environment.NewLine).Where(t => t.Length > 0 && t.Contains('*') == false); - return lst.ToList(); + await Utils.GetCliWrapOutput(Global.LinuxBash, args); } } -} \ No newline at end of file +} diff --git a/v2rayN/ServiceLib/ServiceLib.csproj b/v2rayN/ServiceLib/ServiceLib.csproj index 4def8966..c0889ec3 100644 --- a/v2rayN/ServiceLib/ServiceLib.csproj +++ b/v2rayN/ServiceLib/ServiceLib.csproj @@ -29,6 +29,7 @@ +