From 7c89853e41c737f5284b8627abe1b7795f95f2ee Mon Sep 17 00:00:00 2001 From: snltty <1069410172@qq.com> Date: Mon, 19 Aug 2024 17:11:27 +0800 Subject: [PATCH] sync --- linker.libs/NetworkHelper.cs | 35 ++++++-------- linker.libs/extends/IPEndPointExtends.cs | 53 ---------------------- linker.tun/ILinkerTunDevice.cs | 3 ++ linker.tun/LinkerLinuxTunDevice.cs | 18 ++++---- linker.tun/LinkerOsxTunDevice.cs | 4 +- linker.tun/LinkerWinTunDevice.cs | 10 ++-- linker/plugins/tunnel/TunnelAdapter.cs | 2 +- linker/plugins/tuntap/TuntapTransfer.cs | 2 +- linker/plugins/tuntap/proxy/TuntapProxy.cs | 4 +- 9 files changed, 38 insertions(+), 93 deletions(-) diff --git a/linker.libs/NetworkHelper.cs b/linker.libs/NetworkHelper.cs index b6582566..bfa42780 100644 --- a/linker.libs/NetworkHelper.cs +++ b/linker.libs/NetworkHelper.cs @@ -133,7 +133,7 @@ namespace linker.libs try { return Dns.GetHostAddresses(Dns.GetHostName()) - .Where(c => c.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6) + .Where(c => c.AddressFamily == AddressFamily.InterNetworkV6) .Where(c => c.GetAddressBytes().AsSpan(0, 8).SequenceEqual(ipv6LocalBytes) == false).Distinct().ToArray(); } catch (Exception) @@ -157,7 +157,7 @@ namespace linker.libs return Array.Empty(); } - public static byte MaskLength(uint ip) + public static byte GetPrefixLength(uint ip) { byte maskLength = 32; for (int i = 0; i < sizeof(uint); i++) @@ -171,42 +171,37 @@ namespace linker.libs return maskLength; } - public static uint MaskValue(byte maskLength) + public static uint GetPrefixIP(byte prefixLength) { //最多<<31 所以0需要单独计算 - if (maskLength < 1) return 0; - return 0xffffffff << (32 - maskLength); + if (prefixLength < 1) return 0; + return 0xffffffff << (32 - prefixLength); } - public static IPAddress GetMaskIp(uint maskValue) + public static IPAddress GetPrefixIp(uint prefixIP) { - return new IPAddress(BitConverter.GetBytes(BinaryPrimitives.ReverseEndianness(maskValue))); + return new IPAddress(BitConverter.GetBytes(BinaryPrimitives.ReverseEndianness(prefixIP))); } - public static IPAddress ToNetworkIp(IPAddress ip, uint maskvalue) + public static IPAddress ToNetworkIp(IPAddress ip, uint prefixIP) { - return ToNetworkIp(BinaryPrimitives.ReadUInt32BigEndian(ip.GetAddressBytes()), maskvalue); + return ToNetworkIp(BinaryPrimitives.ReadUInt32BigEndian(ip.GetAddressBytes()), prefixIP); } - public static IPAddress ToNetworkIp(uint ip, uint maskvalue) + public static IPAddress ToNetworkIp(uint ip, uint prefixIP) { - return new IPAddress(BinaryPrimitives.ReverseEndianness(ip & maskvalue).ToBytes()); + return new IPAddress(BinaryPrimitives.ReverseEndianness(ip & prefixIP).ToBytes()); } - public static IPAddress ToGatewayIP(IPAddress ip, byte maskLength) + public static IPAddress ToGatewayIP(IPAddress ip, byte prefixLength) { - uint network = BinaryPrimitives.ReadUInt32BigEndian(ToNetworkIp(ip, NetworkHelper.MaskValue(maskLength)).GetAddressBytes()); + uint network = BinaryPrimitives.ReadUInt32BigEndian(ToNetworkIp(ip, NetworkHelper.GetPrefixIP(prefixLength)).GetAddressBytes()); IPAddress gateway = new IPAddress(BitConverter.GetBytes(BinaryPrimitives.ReverseEndianness(network + 1))); return gateway; } - public static IPAddress ToGatewayIP(uint ip, uint maskValue) + public static IPAddress ToGatewayIP(uint ip, uint prefixIP) { - uint network = BinaryPrimitives.ReadUInt32BigEndian(ToNetworkIp(ip, maskValue).GetAddressBytes()); + uint network = BinaryPrimitives.ReadUInt32BigEndian(ToNetworkIp(ip, prefixIP).GetAddressBytes()); IPAddress gateway = new IPAddress(BitConverter.GetBytes(BinaryPrimitives.ReverseEndianness(network + 1))); return gateway; } - public static bool NotIPv6Support(IPAddress ip) - { - return ip.AddressFamily == AddressFamily.InterNetworkV6 && (IPv6Support == false); - } - #if DISABLE_IPV6 || (!UNITY_EDITOR && ENABLE_IL2CPP && !UNITY_2018_3_OR_NEWER) public static bool IPv6Support = false; diff --git a/linker.libs/extends/IPEndPointExtends.cs b/linker.libs/extends/IPEndPointExtends.cs index 5f326960..3630e765 100644 --- a/linker.libs/extends/IPEndPointExtends.cs +++ b/linker.libs/extends/IPEndPointExtends.cs @@ -1,49 +1,11 @@ using System; using System.Buffers.Binary; -using System.Linq; using System.Net; namespace linker.libs.extends { public static class IPEndPointExtends { - public static Memory ipv6Loopback = IPAddress.IPv6Loopback.GetAddressBytes(); - public static Memory ipv6Multicast = IPAddress.Parse("ff00::").GetAddressBytes(); - public static Memory ipv6Local = IPAddress.Parse("fe80::").GetAddressBytes(); - public static byte[] anyIpArray = IPAddress.Any.GetAddressBytes(); - public static byte[] anyIpv6Array = IPAddress.IPv6Any.GetAddressBytes(); - - public static bool IsLan(this IPEndPoint endPoint) - { - if (endPoint == null) return false; - return endPoint.Address.IsLan(); - } - public static bool IsLan(this IPAddress address) - { - if (address == null) return false; - - return IsLan(address.GetAddressBytes().AsSpan()); - } - public static bool IsLan(this Memory address) - { - return IsLan(address.Span); - } - public static bool IsLan(Span address) - { - if (address.Length < 4) return false; - if (address.Length == 4) - { - return address[0] == 127 - || address[0] == 10 - || (address[0] == 172 && address[1] >= 16 && address[1] <= 31) - || (address[0] == 192 && address[1] == 168); - } - return address.Length == ipv6Loopback.Length && (address.SequenceEqual(ipv6Loopback.Span) - || address.SequenceEqual(ipv6Multicast.Span) - || (address[0] == ipv6Local.Span[0] && address[1] == ipv6Local.Span[1])); - } - - public static bool GetIsBroadcastAddress(this IPAddress address) { return new ReadOnlySpan(address.GetAddressBytes()).GetIsBroadcastAddress(); @@ -57,20 +19,5 @@ namespace linker.libs.extends uint ip = BinaryPrimitives.ReadUInt32BigEndian(address); return address[3] == 255 || (ip >= 0xE0000000 && ip <= 0xEFFFFFFF) || ip == 0xFFFFFFFF; } - - - public static bool GetIsAnyAddress(this IPAddress address) - { - return address.GetAddressBytes().AsSpan().GetIsAnyAddress(); - } - public static bool GetIsAnyAddress(this Memory address) - { - return address.Span.GetIsAnyAddress(); - } - public static bool GetIsAnyAddress(this Span address) - { - return (address.Length == 4 && address.SequenceEqual(anyIpArray)) - || (address.Length == 6 && address.SequenceEqual(anyIpv6Array)); - } } } diff --git a/linker.tun/ILinkerTunDevice.cs b/linker.tun/ILinkerTunDevice.cs index 07e328ef..afc46721 100644 --- a/linker.tun/ILinkerTunDevice.cs +++ b/linker.tun/ILinkerTunDevice.cs @@ -165,6 +165,9 @@ namespace linker.tun /// public readonly IPEndPoint Dist => new IPEndPoint(new IPAddress(DistIPAddress.Span), DistPort); + public readonly bool IPV4Broadcast => Version == 4 && DistIPAddress.GetIsBroadcastAddress(); + public readonly bool IPV6Multicast => Version == 6 && (DistIPAddress.Span[0] & 0xFF) == 0xFF; + public void Unpacket(ReadOnlyMemory buffer) { Packet = buffer; diff --git a/linker.tun/LinkerLinuxTunDevice.cs b/linker.tun/LinkerLinuxTunDevice.cs index 655a91a2..adb465cf 100644 --- a/linker.tun/LinkerLinuxTunDevice.cs +++ b/linker.tun/LinkerLinuxTunDevice.cs @@ -133,7 +133,7 @@ namespace linker.tun error = string.Empty; try { - IPAddress network = NetworkHelper.ToNetworkIp(address, NetworkHelper.MaskValue(prefixLength)); + IPAddress network = NetworkHelper.ToNetworkIp(address, NetworkHelper.GetPrefixIP(prefixLength)); CommandHelper.Linux(string.Empty, new string[] { $"sysctl -w net.ipv4.ip_forward=1", $"iptables -A FORWARD -i {Name} -j ACCEPT", @@ -156,7 +156,7 @@ namespace linker.tun $"iptables -D FORWARD -o {Name} -m state --state ESTABLISHED,RELATED -j ACCEPT" }); - IPAddress network = NetworkHelper.ToNetworkIp(address, NetworkHelper.MaskValue(prefixLength)); + IPAddress network = NetworkHelper.ToNetworkIp(address, NetworkHelper.GetPrefixIP(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) { @@ -211,8 +211,8 @@ namespace linker.tun { var commands = ips.Select(c => { - uint maskValue = NetworkHelper.MaskValue(c.PrefixLength); - IPAddress network = NetworkHelper.ToNetworkIp(c.Address, maskValue); + uint prefixValue = NetworkHelper.GetPrefixIP(c.PrefixLength); + IPAddress network = NetworkHelper.ToNetworkIp(c.Address, prefixValue); return $"iptables -t nat -A POSTROUTING -o {Name} -s {network}/{c.PrefixLength} -j MASQUERADE"; }).ToList(); commands.Insert(0, "sysctl -w net.ipv4.ip_forward=1"); @@ -223,8 +223,8 @@ namespace linker.tun { string[] commands = ips.Select(item => { - uint maskValue = NetworkHelper.MaskValue(item.PrefixLength); - IPAddress network = NetworkHelper.ToNetworkIp(item.Address, maskValue); + uint prefixValue = NetworkHelper.GetPrefixIP(item.PrefixLength); + IPAddress network = NetworkHelper.ToNetworkIp(item.Address, prefixValue); return $"ip route add {network}/{item.PrefixLength} via {ip} dev {Name} metric 1 "; }).ToArray(); @@ -240,7 +240,7 @@ namespace linker.tun { foreach (var item in ip) { - IPAddress network = NetworkHelper.ToNetworkIp(item.Address, NetworkHelper.MaskValue(item.PrefixLength)); + IPAddress network = NetworkHelper.ToNetworkIp(item.Address, NetworkHelper.GetPrefixIP(item.PrefixLength)); string iptableLineNumbers = CommandHelper.Linux(string.Empty, new string[] { $"iptables -t nat -L --line-numbers | grep {network}/{item.PrefixLength} | cut -d' ' -f1" }); if (string.IsNullOrWhiteSpace(iptableLineNumbers) == false) { @@ -255,8 +255,8 @@ namespace linker.tun { string[] commands = ip.Select(item => { - uint maskValue = NetworkHelper.MaskValue(item.PrefixLength); - IPAddress network = NetworkHelper.ToNetworkIp(item.Address, maskValue); + uint prefixValue = NetworkHelper.GetPrefixIP(item.PrefixLength); + IPAddress network = NetworkHelper.ToNetworkIp(item.Address, prefixValue); return $"ip route del {network}/{item.PrefixLength}"; }).ToArray(); CommandHelper.Linux(string.Empty, commands); diff --git a/linker.tun/LinkerOsxTunDevice.cs b/linker.tun/LinkerOsxTunDevice.cs index b705230f..df0e405b 100644 --- a/linker.tun/LinkerOsxTunDevice.cs +++ b/linker.tun/LinkerOsxTunDevice.cs @@ -35,7 +35,7 @@ namespace linker.tun safeFileHandle = File.OpenHandle($"/dev/{Name}", FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite, FileOptions.Asynchronous); fs = new FileStream(safeFileHandle, FileAccess.ReadWrite, 1500); - IPAddress network = NetworkHelper.ToNetworkIp(address, NetworkHelper.MaskValue(prefixLength)); + IPAddress network = NetworkHelper.ToNetworkIp(address, NetworkHelper.GetPrefixIP(prefixLength)); CommandHelper.Osx(string.Empty, new string[] { $"route delete -net {network}/{prefixLength} {address}", $"ifconfig {Name} {address} {address} up", @@ -55,7 +55,7 @@ namespace linker.tun fs.Dispose(); fs = null; } - IPAddress network = NetworkHelper.ToNetworkIp(address, NetworkHelper.MaskValue(this.prefixLength)); + IPAddress network = NetworkHelper.ToNetworkIp(address, NetworkHelper.GetPrefixIP(this.prefixLength)); CommandHelper.Osx(string.Empty, new string[] { $"route delete -net {network}/{prefixLength} {address}" }); } diff --git a/linker.tun/LinkerWinTunDevice.cs b/linker.tun/LinkerWinTunDevice.cs index 64a1e054..6544a46b 100644 --- a/linker.tun/LinkerWinTunDevice.cs +++ b/linker.tun/LinkerWinTunDevice.cs @@ -153,7 +153,7 @@ namespace linker.tun try { CommandHelper.PowerShell($"start-service WinNat", [], out error); - IPAddress network = NetworkHelper.ToNetworkIp(this.address, NetworkHelper.MaskValue(prefixLength)); + IPAddress network = NetworkHelper.ToNetworkIp(this.address, NetworkHelper.GetPrefixIP(prefixLength)); CommandHelper.PowerShell($"New-NetNat -Name {Name} -InternalIPInterfaceAddressPrefix {network}/{prefixLength}", [], out error); if (string.IsNullOrWhiteSpace(error) == false) @@ -215,8 +215,8 @@ namespace linker.tun { string[] commands = ips.Select(item => { - uint maskValue = NetworkHelper.MaskValue(item.PrefixLength); - IPAddress mask = NetworkHelper.GetMaskIp(maskValue); + uint maskValue = NetworkHelper.GetPrefixIP(item.PrefixLength); + IPAddress mask = NetworkHelper.GetPrefixIp(maskValue); IPAddress _ip = NetworkHelper.ToNetworkIp(item.Address, maskValue); return $"route add {_ip} mask {mask} {ip} metric 5 if {interfaceNumber}"; @@ -231,8 +231,8 @@ namespace linker.tun { string[] commands = ip.Select(item => { - uint maskValue = NetworkHelper.MaskValue(item.PrefixLength); - IPAddress mask = NetworkHelper.GetMaskIp(maskValue); + uint maskValue = NetworkHelper.GetPrefixIP(item.PrefixLength); + IPAddress mask = NetworkHelper.GetPrefixIp(maskValue); IPAddress _ip = NetworkHelper.ToNetworkIp(item.Address, maskValue); return $"route delete {_ip}"; }).ToArray(); diff --git a/linker/plugins/tunnel/TunnelAdapter.cs b/linker/plugins/tunnel/TunnelAdapter.cs index 08b3fcf3..37dafcd4 100644 --- a/linker/plugins/tunnel/TunnelAdapter.cs +++ b/linker/plugins/tunnel/TunnelAdapter.cs @@ -116,7 +116,7 @@ namespace linker.plugins.tunnel uint ip = BinaryPrimitives.ReadUInt32BigEndian(c.GetAddressBytes()); foreach (var item in excludeips) { - uint maskValue = NetworkHelper.MaskValue(item.Mask); + uint maskValue = NetworkHelper.GetPrefixIP(item.Mask); uint ip1 = BinaryPrimitives.ReadUInt32BigEndian(item.IPAddress.GetAddressBytes()); if ((ip & maskValue) == (ip1 & maskValue)) { diff --git a/linker/plugins/tuntap/TuntapTransfer.cs b/linker/plugins/tuntap/TuntapTransfer.cs index 9f007b87..0f69f84b 100644 --- a/linker/plugins/tuntap/TuntapTransfer.cs +++ b/linker/plugins/tuntap/TuntapTransfer.cs @@ -390,7 +390,7 @@ namespace linker.plugins.tuntap { uint ipInt = BinaryPrimitives.ReadUInt32BigEndian(ip.GetAddressBytes()); //掩码十进制 - uint maskValue = NetworkHelper.MaskValue(maskLength); + uint maskValue = NetworkHelper.GetPrefixIP(maskLength); return new TuntapVeaLanIPAddress { IPAddress = ipInt, diff --git a/linker/plugins/tuntap/proxy/TuntapProxy.cs b/linker/plugins/tuntap/proxy/TuntapProxy.cs index d303c2fa..60b91f0b 100644 --- a/linker/plugins/tuntap/proxy/TuntapProxy.cs +++ b/linker/plugins/tuntap/proxy/TuntapProxy.cs @@ -79,7 +79,7 @@ namespace linker.plugins.tuntap.proxy public async Task Callback(LinkerTunDevicPacket packet) { //IPV4广播组播 - if (packet.Version == 4 && packet.DistIPAddress.GetIsBroadcastAddress()) + if (packet.IPV4Broadcast) { if (connections.IsEmpty == false) { @@ -88,7 +88,7 @@ namespace linker.plugins.tuntap.proxy return; } //IPV6 多播 - else if (packet.Version == 6 && (packet.DistIPAddress.Span[0] & 0xFF) == 0xFF) + else if (packet.IPV6Multicast) { if (connections.IsEmpty == false) {