mirror of
https://github.com/2dust/v2rayN.git
synced 2025-12-26 08:32:43 +00:00
Compare commits
No commits in common. "72ff947d953e2c700566d622bb316d7841d80e13" and "aeddbc1dccf456d5409d58794dc74f115759522a" have entirely different histories.
72ff947d95
...
aeddbc1dcc
4 changed files with 82 additions and 95 deletions
|
|
@ -38,7 +38,6 @@ namespace ServiceLib
|
||||||
public const string ClashTunYaml = NamespaceSample + "clash_tun_yaml";
|
public const string ClashTunYaml = NamespaceSample + "clash_tun_yaml";
|
||||||
public const string LinuxAutostartConfig = NamespaceSample + "linux_autostart_config";
|
public const string LinuxAutostartConfig = NamespaceSample + "linux_autostart_config";
|
||||||
public const string PacFileName = NamespaceSample + "pac";
|
public const string PacFileName = NamespaceSample + "pac";
|
||||||
public const string ProxySetOSXShellFileName = NamespaceSample + "proxy_set_osx_sh";
|
|
||||||
|
|
||||||
public const string DefaultSecurity = "auto";
|
public const string DefaultSecurity = "auto";
|
||||||
public const string DefaultNetwork = "tcp";
|
public const string DefaultNetwork = "tcp";
|
||||||
|
|
|
||||||
|
|
@ -1,38 +1,101 @@
|
||||||
namespace ServiceLib.Handler.SysProxy
|
namespace ServiceLib.Handler.SysProxy
|
||||||
{
|
{
|
||||||
public class ProxySettingOSX
|
public class ProxySettingOSX
|
||||||
{
|
{
|
||||||
private static readonly string _proxySetFileName = $"{Global.ProxySetOSXShellFileName.Replace(Global.NamespaceSample, "")}.sh";
|
/// <summary>
|
||||||
|
/// 应用接口类型
|
||||||
|
/// </summary>
|
||||||
|
private static readonly List<string> LstInterface = ["Ethernet", "Wi-Fi", "Thunderbolt Bridge", "USB 10/100/1000 LAN"];
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 代理类型,对应 http,https,socks
|
||||||
|
/// </summary>
|
||||||
|
private static readonly List<string> LstTypes = ["setwebproxy", "setsecurewebproxy", "setsocksfirewallproxy"];
|
||||||
|
|
||||||
public static async Task SetProxy(string host, int port, string exceptions)
|
public static async Task SetProxy(string host, int port, string exceptions)
|
||||||
{
|
{
|
||||||
List<string> args = ["set", host, port.ToString()];
|
var lstInterface = await GetListNetworkServices();
|
||||||
if (exceptions.IsNotEmpty())
|
var lstCmd = GetSetCmds(lstInterface, host, port, exceptions);
|
||||||
{
|
await ExecCmd(lstCmd);
|
||||||
args.AddRange(exceptions.Split(','));
|
|
||||||
}
|
|
||||||
|
|
||||||
await ExecCmd(args);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async Task UnsetProxy()
|
public static async Task UnsetProxy()
|
||||||
{
|
{
|
||||||
List<string> args = ["clear"];
|
var lstInterface = await GetListNetworkServices();
|
||||||
await ExecCmd(args);
|
var lstCmd = GetUnsetCmds(lstInterface);
|
||||||
|
await ExecCmd(lstCmd);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static async Task ExecCmd(List<string> args)
|
private static async Task ExecCmd(List<CmdItem> lstCmd)
|
||||||
{
|
{
|
||||||
var fileName = Utils.GetBinConfigPath(_proxySetFileName);
|
foreach (var cmd in lstCmd)
|
||||||
if (!File.Exists(fileName))
|
|
||||||
{
|
{
|
||||||
var contents = EmbedUtils.GetEmbedText(Global.ProxySetOSXShellFileName);
|
if (cmd is null || cmd.Cmd.IsNullOrEmpty() || cmd.Arguments is null)
|
||||||
await File.AppendAllTextAsync(fileName, contents);
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
await Utils.SetLinuxChmod(fileName);
|
await Task.Delay(10);
|
||||||
|
await Utils.GetCliWrapOutput(cmd.Cmd, cmd.Arguments);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static List<CmdItem> GetSetCmds(List<string> lstInterface, string host, int port, string exceptions)
|
||||||
|
{
|
||||||
|
List<CmdItem> 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<string> args = [$"-setproxybypassdomains", interf];
|
||||||
|
args.AddRange(exceptions.Split(','));
|
||||||
|
lstCmd.Add(new CmdItem()
|
||||||
|
{
|
||||||
|
Cmd = "networksetup",
|
||||||
|
Arguments = args
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
await Utils.GetCliWrapOutput(fileName, args);
|
return lstCmd;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static List<CmdItem> GetUnsetCmds(List<string> lstInterface)
|
||||||
|
{
|
||||||
|
List<CmdItem> 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<List<string>> 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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,74 +0,0 @@
|
||||||
#!/bin/bash
|
|
||||||
|
|
||||||
# Function to set proxy
|
|
||||||
set_proxy() {
|
|
||||||
IP=$1
|
|
||||||
PORT=$2
|
|
||||||
|
|
||||||
shift 2
|
|
||||||
BYPASS_DOMAINS=("$@")
|
|
||||||
# If no bypass domains are provided, set it to empty by default
|
|
||||||
if [ ${#BYPASS_DOMAINS[@]} -eq 0 ]; then
|
|
||||||
BYPASS_DOMAINS=("")
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Get all network service names
|
|
||||||
SERVICES=$(networksetup -listallnetworkservices | grep -v '*')
|
|
||||||
|
|
||||||
# Loop through each network service
|
|
||||||
echo "$SERVICES" | while read -r SERVICE; do
|
|
||||||
echo "Setting proxy for network service '$SERVICE'..."
|
|
||||||
# Set HTTP proxy
|
|
||||||
networksetup -setwebproxy "$SERVICE" "$IP" "$PORT"
|
|
||||||
|
|
||||||
# Set HTTPS proxy
|
|
||||||
networksetup -setsecurewebproxy "$SERVICE" "$IP" "$PORT"
|
|
||||||
|
|
||||||
# Set SOCKS proxy
|
|
||||||
networksetup -setsocksfirewallproxy "$SERVICE" "$IP" "$PORT"
|
|
||||||
|
|
||||||
# Set bypass domains
|
|
||||||
networksetup -setproxybypassdomains "$SERVICE" "${BYPASS_DOMAINS[@]}"
|
|
||||||
echo "Proxy for network service '$SERVICE' has been set to $IP:$PORT"
|
|
||||||
done
|
|
||||||
echo "Proxy settings for all network services are complete!"
|
|
||||||
}
|
|
||||||
|
|
||||||
# Function to disable proxy
|
|
||||||
clear_proxy() {
|
|
||||||
# Get all network service names
|
|
||||||
SERVICES=$(networksetup -listallnetworkservices | grep -v '*')
|
|
||||||
|
|
||||||
# Loop through each network service
|
|
||||||
echo "$SERVICES" | while read -r SERVICE; do
|
|
||||||
echo "Disabling proxy and clearing bypass domains for network service '$SERVICE'..."
|
|
||||||
# Disable HTTP proxy
|
|
||||||
networksetup -setwebproxystate "$SERVICE" off
|
|
||||||
|
|
||||||
# Disable HTTPS proxy
|
|
||||||
networksetup -setsecurewebproxystate "$SERVICE" off
|
|
||||||
|
|
||||||
# Disable SOCKS proxy
|
|
||||||
networksetup -setsocksfirewallproxystate "$SERVICE" off
|
|
||||||
|
|
||||||
echo "Proxy for network service '$SERVICE' has been disabled"
|
|
||||||
done
|
|
||||||
echo "Proxy for all network services has been disabled!"
|
|
||||||
}
|
|
||||||
|
|
||||||
# Main script logic
|
|
||||||
if [ "$1" == "set" ]; then
|
|
||||||
# Check if enough parameters are passed for setting proxy
|
|
||||||
if [ "$#" -lt 3 ]; then
|
|
||||||
echo "Usage: $0 set <IP Address> <Port> [Bypass Domain 1 Bypass Domain 2 ...]"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
set_proxy "$2" "$3" "${@:4}"
|
|
||||||
elif [ "$1" == "clear" ]; then
|
|
||||||
clear_proxy
|
|
||||||
else
|
|
||||||
echo "Usage:"
|
|
||||||
echo " To set proxy: $0 set <IP Address> <Port> [Bypass Domain 1 Bypass Domain 2 ...]"
|
|
||||||
echo " To clear proxy: $0 clear"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
@ -29,7 +29,6 @@
|
||||||
<EmbeddedResource Include="Sample\dns_singbox_normal" />
|
<EmbeddedResource Include="Sample\dns_singbox_normal" />
|
||||||
<EmbeddedResource Include="Sample\dns_v2ray_normal" />
|
<EmbeddedResource Include="Sample\dns_v2ray_normal" />
|
||||||
<EmbeddedResource Include="Sample\pac" />
|
<EmbeddedResource Include="Sample\pac" />
|
||||||
<EmbeddedResource Include="Sample\proxy_set_osx_sh" />
|
|
||||||
<EmbeddedResource Include="Sample\SampleClientConfig" />
|
<EmbeddedResource Include="Sample\SampleClientConfig" />
|
||||||
<EmbeddedResource Include="Sample\SampleHttpRequest" />
|
<EmbeddedResource Include="Sample\SampleHttpRequest" />
|
||||||
<EmbeddedResource Include="Sample\SampleHttpResponse" />
|
<EmbeddedResource Include="Sample\SampleHttpResponse" />
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue