Move IP version check earlier. Property ipv4_mapped
is only defined on IPv6 addrs.
This commit is contained in:
parent
8794ff48e0
commit
d495ce9f0a
1 changed files with 8 additions and 5 deletions
|
@ -610,11 +610,13 @@ IPV4_TO_6_RELAY_SUBNET = ipaddress.ip_network('192.88.99.0/24')
|
||||||
def is_valid_public_ipv4(address, allow_localhost: bool = False, allow_lan: bool = False):
|
def is_valid_public_ipv4(address, allow_localhost: bool = False, allow_lan: bool = False):
|
||||||
try:
|
try:
|
||||||
parsed_ip = ipaddress.ip_address(address)
|
parsed_ip = ipaddress.ip_address(address)
|
||||||
|
if parsed_ip.version != 4:
|
||||||
|
return False
|
||||||
if parsed_ip.is_loopback:
|
if parsed_ip.is_loopback:
|
||||||
return allow_localhost
|
return allow_localhost
|
||||||
if parsed_ip.is_private:
|
if parsed_ip.is_private:
|
||||||
return allow_lan
|
return allow_lan
|
||||||
if any((parsed_ip.version != 4, parsed_ip.is_unspecified, parsed_ip.is_link_local, parsed_ip.is_loopback,
|
if any((parsed_ip.is_unspecified, parsed_ip.is_link_local, parsed_ip.is_loopback,
|
||||||
parsed_ip.is_multicast, parsed_ip.is_reserved, parsed_ip.is_private)):
|
parsed_ip.is_multicast, parsed_ip.is_reserved, parsed_ip.is_private)):
|
||||||
return False
|
return False
|
||||||
else:
|
else:
|
||||||
|
@ -626,14 +628,15 @@ def is_valid_public_ipv4(address, allow_localhost: bool = False, allow_lan: bool
|
||||||
def is_valid_public_ipv6(address, allow_localhost: bool = False, allow_lan: bool = False):
|
def is_valid_public_ipv6(address, allow_localhost: bool = False, allow_lan: bool = False):
|
||||||
try:
|
try:
|
||||||
parsed_ip = ipaddress.ip_address(address)
|
parsed_ip = ipaddress.ip_address(address)
|
||||||
|
if parsed_ip.version != 6:
|
||||||
|
return False
|
||||||
if parsed_ip.is_loopback:
|
if parsed_ip.is_loopback:
|
||||||
return allow_localhost
|
return allow_localhost
|
||||||
if parsed_ip.is_private:
|
if parsed_ip.is_private:
|
||||||
return allow_lan
|
return allow_lan
|
||||||
return not any((parsed_ip.version != 6, parsed_ip.is_unspecified,
|
return not any((parsed_ip.is_unspecified, parsed_ip.is_link_local, parsed_ip.is_loopback,
|
||||||
parsed_ip.is_link_local, parsed_ip.is_loopback,
|
parsed_ip.is_multicast, parsed_ip.is_reserved, parsed_ip.is_private,
|
||||||
parsed_ip.is_multicast, parsed_ip.is_reserved,
|
parsed_ip.ipv4_mapped))
|
||||||
parsed_ip.is_private, parsed_ip.ipv4_mapped))
|
|
||||||
except (ipaddress.AddressValueError, ValueError):
|
except (ipaddress.AddressValueError, ValueError):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue