添加:MacOS x86下socks自动系统代理

This commit is contained in:
fonaix 2024-11-07 19:41:58 +08:00
parent 12dee27b0b
commit 4d8bfb213e

View file

@ -1,13 +1,89 @@
namespace ServiceLib.Handler.SysProxy
{
/// <summary>
/// 开启代理
/// 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
/// </summary>
public class ProxySettingOSX
{
/// <summary>
///
/// </summary>
/// <param name="host"></param>
/// <param name="port">此处传入的是HTTP端口SOCKS端口需要-1</param>
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<CmdItem> 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<CmdItem> GetSetCmds(string host, int port)
{
List<string> lstType = ["Ethernet", "Wi-Fi", "Thunderbolt Bridge", "iPhone USB"];
List<CmdItem> lstCmd = [];
foreach (var type in lstType)
{
lstCmd.AddRange(GetSetCmd4Intel(host, port, type));
}
return lstCmd;
}
private static List<CmdItem> GetUnsetCmds()
{
List<string> lstType = ["Ethernet", "Wi-Fi", "Thunderbolt Bridge", "iPhone USB"];
List<CmdItem> lstCmd = [];
foreach (var type in lstType)
{
lstCmd.Add(new CmdItem()
{
Cmd = "networksetup",
Arguments = ["-setsocksfirewallproxy", type, "off"]
});
}
return lstCmd;
}
private static List<CmdItem> GetSetCmd4Intel(string host, int port, string type)
{
List<CmdItem> lstCmd = [];
lstCmd.Add(new CmdItem()
{
Cmd = "networksetup",
Arguments = ["-setsocksfirewallproxy", type, host, port.ToString()]
});
return lstCmd;
}
}
}