mirror of https://github.com/2dust/v2rayN
				
				
				
			Improves private IP address detection (#7657)
							parent
							
								
									82f9698c0d
								
							
						
					
					
						commit
						74bb01d044
					
				| 
						 | 
					@ -390,7 +390,14 @@ public class Utils
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        if (IPAddress.TryParse(ip, out var address))
 | 
					        if (IPAddress.TryParse(ip, out var address))
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
 | 
					            // Loopback address check (127.0.0.1 for IPv4, ::1 for IPv6)
 | 
				
			||||||
 | 
					            if (IPAddress.IsLoopback(address))
 | 
				
			||||||
 | 
					                return true;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            var ipBytes = address.GetAddressBytes();
 | 
					            var ipBytes = address.GetAddressBytes();
 | 
				
			||||||
 | 
					            if (address.AddressFamily == AddressFamily.InterNetwork)
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                // IPv4 private address check
 | 
				
			||||||
                if (ipBytes[0] == 10)
 | 
					                if (ipBytes[0] == 10)
 | 
				
			||||||
                    return true;
 | 
					                    return true;
 | 
				
			||||||
                if (ipBytes[0] == 172 && ipBytes[1] >= 16 && ipBytes[1] <= 31)
 | 
					                if (ipBytes[0] == 172 && ipBytes[1] >= 16 && ipBytes[1] <= 31)
 | 
				
			||||||
| 
						 | 
					@ -398,6 +405,30 @@ public class Utils
 | 
				
			||||||
                if (ipBytes[0] == 192 && ipBytes[1] == 168)
 | 
					                if (ipBytes[0] == 192 && ipBytes[1] == 168)
 | 
				
			||||||
                    return true;
 | 
					                    return true;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					            else if (address.AddressFamily == AddressFamily.InterNetworkV6)
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                // IPv6 private address check
 | 
				
			||||||
 | 
					                // Link-local address fe80::/10
 | 
				
			||||||
 | 
					                if (ipBytes[0] == 0xfe && (ipBytes[1] & 0xc0) == 0x80)
 | 
				
			||||||
 | 
					                    return true;
 | 
				
			||||||
 | 
					                
 | 
				
			||||||
 | 
					                // Unique local address fc00::/7 (typically fd00::/8)
 | 
				
			||||||
 | 
					                if ((ipBytes[0] & 0xfe) == 0xfc)
 | 
				
			||||||
 | 
					                    return true;
 | 
				
			||||||
 | 
					                
 | 
				
			||||||
 | 
					                // Private portion in IPv4-mapped addresses ::ffff:0:0/96
 | 
				
			||||||
 | 
					                if (address.IsIPv4MappedToIPv6)
 | 
				
			||||||
 | 
					                {
 | 
				
			||||||
 | 
					                    var ipv4Bytes = ipBytes.Skip(12).ToArray();
 | 
				
			||||||
 | 
					                    if (ipv4Bytes[0] == 10)
 | 
				
			||||||
 | 
					                        return true;
 | 
				
			||||||
 | 
					                    if (ipv4Bytes[0] == 172 && ipv4Bytes[1] >= 16 && ipv4Bytes[1] <= 31)
 | 
				
			||||||
 | 
					                        return true;
 | 
				
			||||||
 | 
					                    if (ipv4Bytes[0] == 192 && ipv4Bytes[1] == 168)
 | 
				
			||||||
 | 
					                        return true;
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return false;
 | 
					        return false;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue