2024-10-16 01:20:05 +00:00
|
|
|
|
namespace ServiceLib.Handler.SysProxy
|
2024-09-28 02:41:30 +00:00
|
|
|
|
{
|
|
|
|
|
public class ProxySettingOSX
|
|
|
|
|
{
|
2024-11-08 01:21:43 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 应用接口类型
|
|
|
|
|
/// </summary>
|
2024-12-31 06:55:35 +00:00
|
|
|
|
private static readonly List<string> LstInterface = ["Ethernet", "Wi-Fi", "Thunderbolt Bridge", "USB 10/100/1000 LAN"];
|
2024-11-08 01:21:43 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 代理类型,对应 http,https,socks
|
|
|
|
|
/// </summary>
|
|
|
|
|
private static readonly List<string> LstTypes = ["setwebproxy", "setsecurewebproxy", "setsocksfirewallproxy"];
|
|
|
|
|
|
2024-12-19 03:40:16 +00:00
|
|
|
|
public static async Task SetProxy(string host, int port, string exceptions)
|
2024-09-28 02:41:30 +00:00
|
|
|
|
{
|
2025-01-01 06:24:23 +00:00
|
|
|
|
var lstInterface = await GetListNetworkServices();
|
|
|
|
|
var lstCmd = GetSetCmds(lstInterface, host, port, exceptions);
|
2024-11-08 01:21:43 +00:00
|
|
|
|
await ExecCmd(lstCmd);
|
2024-09-28 02:41:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static async Task UnsetProxy()
|
|
|
|
|
{
|
2025-01-01 06:24:23 +00:00
|
|
|
|
var lstInterface = await GetListNetworkServices();
|
|
|
|
|
var lstCmd = GetUnsetCmds(lstInterface);
|
2024-11-08 01:21:43 +00:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-01 06:24:23 +00:00
|
|
|
|
private static List<CmdItem> GetSetCmds(List<string> lstInterface, string host, int port, string exceptions)
|
2024-11-08 01:21:43 +00:00
|
|
|
|
{
|
|
|
|
|
List<CmdItem> lstCmd = [];
|
2025-01-01 06:24:23 +00:00
|
|
|
|
foreach (var interf in lstInterface)
|
2024-11-08 01:21:43 +00:00
|
|
|
|
{
|
|
|
|
|
foreach (var type in LstTypes)
|
|
|
|
|
{
|
|
|
|
|
lstCmd.Add(new CmdItem()
|
|
|
|
|
{
|
|
|
|
|
Cmd = "networksetup",
|
2024-12-19 05:20:12 +00:00
|
|
|
|
Arguments = [$"-{type}", interf, host, port.ToString()]
|
2024-11-08 01:21:43 +00:00
|
|
|
|
});
|
|
|
|
|
}
|
2024-12-19 03:40:16 +00:00
|
|
|
|
if (exceptions.IsNotEmpty())
|
|
|
|
|
{
|
|
|
|
|
List<string> args = [$"-setproxybypassdomains", interf];
|
|
|
|
|
args.AddRange(exceptions.Split(','));
|
|
|
|
|
lstCmd.Add(new CmdItem()
|
|
|
|
|
{
|
|
|
|
|
Cmd = "networksetup",
|
|
|
|
|
Arguments = args
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-11-08 01:21:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return lstCmd;
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-01 06:24:23 +00:00
|
|
|
|
private static List<CmdItem> GetUnsetCmds(List<string> lstInterface)
|
2024-11-08 01:21:43 +00:00
|
|
|
|
{
|
|
|
|
|
List<CmdItem> lstCmd = [];
|
2025-01-01 06:24:23 +00:00
|
|
|
|
foreach (var interf in lstInterface)
|
2024-11-08 01:21:43 +00:00
|
|
|
|
{
|
|
|
|
|
foreach (var type in LstTypes)
|
|
|
|
|
{
|
|
|
|
|
lstCmd.Add(new CmdItem()
|
|
|
|
|
{
|
|
|
|
|
Cmd = "networksetup",
|
|
|
|
|
Arguments = [$"-{type}state", interf, "off"]
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return lstCmd;
|
2024-09-28 02:41:30 +00:00
|
|
|
|
}
|
2025-01-01 06:24:23 +00:00
|
|
|
|
|
|
|
|
|
public static async Task<List<string>> GetListNetworkServices()
|
|
|
|
|
{
|
|
|
|
|
var services = await Utils.GetListNetworkServices();
|
|
|
|
|
if (services.IsNullOrEmpty())
|
|
|
|
|
{
|
|
|
|
|
return LstInterface;
|
|
|
|
|
}
|
2025-01-05 04:24:14 +00:00
|
|
|
|
|
|
|
|
|
var lst = services.Split(Environment.NewLine).Where(t => t.Length > 0 && t.Contains('*') == false);
|
|
|
|
|
return lst.ToList();
|
2025-01-01 06:24:23 +00:00
|
|
|
|
}
|
2024-09-28 02:41:30 +00:00
|
|
|
|
}
|
|
|
|
|
}
|