网卡自动启动

This commit is contained in:
snltty
2024-08-04 21:42:53 +08:00
parent 559218e03a
commit 58f720b41f
2 changed files with 6 additions and 0 deletions

View File

@@ -127,6 +127,8 @@ namespace linker.tun
} }
public void SetNat() public void SetNat()
{ {
if (address == null) return;
IPAddress network = NetworkHelper.ToNetworkIp(address, NetworkHelper.MaskValue(prefixLength)); IPAddress network = NetworkHelper.ToNetworkIp(address, NetworkHelper.MaskValue(prefixLength));
CommandHelper.Linux(string.Empty, new string[] { CommandHelper.Linux(string.Empty, new string[] {
$"sysctl -w net.ipv4.ip_forward=1", $"sysctl -w net.ipv4.ip_forward=1",
@@ -135,6 +137,8 @@ namespace linker.tun
} }
public void RemoveNat() public void RemoveNat()
{ {
if (address == null) return;
IPAddress network = NetworkHelper.ToNetworkIp(address, NetworkHelper.MaskValue(prefixLength)); IPAddress network = NetworkHelper.ToNetworkIp(address, NetworkHelper.MaskValue(prefixLength));
string iptableLineNumbers = CommandHelper.Linux(string.Empty, new string[] { $"iptables -t nat -L --line-numbers | grep {network}/{prefixLength} | cut -d' ' -f1" }); string iptableLineNumbers = CommandHelper.Linux(string.Empty, new string[] { $"iptables -t nat -L --line-numbers | grep {network}/{prefixLength} | cut -d' ' -f1" });
if (string.IsNullOrWhiteSpace(iptableLineNumbers) == false) if (string.IsNullOrWhiteSpace(iptableLineNumbers) == false)

View File

@@ -108,10 +108,12 @@ namespace linker.tun
public void SetMtu(int value) public void SetMtu(int value)
{ {
if (address == null) return;
CommandHelper.Windows(string.Empty, new string[] { $"netsh interface ipv4 set subinterface {interfaceNumber} mtu={value} store=persistent" }); CommandHelper.Windows(string.Empty, new string[] { $"netsh interface ipv4 set subinterface {interfaceNumber} mtu={value} store=persistent" });
} }
public void SetNat() public void SetNat()
{ {
if (address == null) return;
IPAddress network = NetworkHelper.ToNetworkIp(this.address, NetworkHelper.MaskValue(prefixLength)); IPAddress network = NetworkHelper.ToNetworkIp(this.address, NetworkHelper.MaskValue(prefixLength));
CommandHelper.PowerShell(string.Empty, new string[] { $"New-NetNat -Name {Name} -InternalIPInterfaceAddressPrefix {network}/{prefixLength}" }); CommandHelper.PowerShell(string.Empty, new string[] { $"New-NetNat -Name {Name} -InternalIPInterfaceAddressPrefix {network}/{prefixLength}" });
} }