2018-09-30 12:45:48 +00:00
|
|
|
"""Network utilities."""
|
|
|
|
from ipaddress import IPv4Address, IPv6Address, ip_address, ip_network
|
|
|
|
from typing import Union
|
|
|
|
|
|
|
|
# IP addresses of loopback interfaces
|
2019-07-31 19:25:30 +00:00
|
|
|
LOCAL_IPS = (ip_address("127.0.0.1"), ip_address("::1"))
|
2018-09-30 12:45:48 +00:00
|
|
|
|
|
|
|
# RFC1918 - Address allocation for Private Internets
|
|
|
|
LOCAL_NETWORKS = (
|
2019-07-31 19:25:30 +00:00
|
|
|
ip_network("10.0.0.0/8"),
|
|
|
|
ip_network("172.16.0.0/12"),
|
|
|
|
ip_network("192.168.0.0/16"),
|
2018-09-30 12:45:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def is_local(address: Union[IPv4Address, IPv6Address]) -> bool:
|
|
|
|
"""Check if an address is local."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return address in LOCAL_IPS or any(address in network for network in LOCAL_NETWORKS)
|