From 58f720b41f28d0ea61f34889a921cdcfb561384f Mon Sep 17 00:00:00 2001 From: snltty <1069410172@qq.com> Date: Sun, 4 Aug 2024 21:42:53 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BD=91=E5=8D=A1=E8=87=AA=E5=8A=A8=E5=90=AF?= =?UTF-8?q?=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- linker.tun/LinkerLinuxTunDevice.cs | 4 ++++ linker.tun/LinkerWinTunDevice.cs | 2 ++ 2 files changed, 6 insertions(+) diff --git a/linker.tun/LinkerLinuxTunDevice.cs b/linker.tun/LinkerLinuxTunDevice.cs index 671d8086..a3e63a44 100644 --- a/linker.tun/LinkerLinuxTunDevice.cs +++ b/linker.tun/LinkerLinuxTunDevice.cs @@ -127,6 +127,8 @@ namespace linker.tun } public void SetNat() { + if (address == null) return; + IPAddress network = NetworkHelper.ToNetworkIp(address, NetworkHelper.MaskValue(prefixLength)); CommandHelper.Linux(string.Empty, new string[] { $"sysctl -w net.ipv4.ip_forward=1", @@ -135,6 +137,8 @@ namespace linker.tun } public void RemoveNat() { + if (address == null) return; + 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" }); if (string.IsNullOrWhiteSpace(iptableLineNumbers) == false) diff --git a/linker.tun/LinkerWinTunDevice.cs b/linker.tun/LinkerWinTunDevice.cs index 5ac0b664..b7abf2c8 100644 --- a/linker.tun/LinkerWinTunDevice.cs +++ b/linker.tun/LinkerWinTunDevice.cs @@ -108,10 +108,12 @@ namespace linker.tun 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" }); } public void SetNat() { + if (address == null) return; IPAddress network = NetworkHelper.ToNetworkIp(this.address, NetworkHelper.MaskValue(prefixLength)); CommandHelper.PowerShell(string.Empty, new string[] { $"New-NetNat -Name {Name} -InternalIPInterfaceAddressPrefix {network}/{prefixLength}" }); }