From 11134b669e649231d7ad79642fea80b7e9f796d7 Mon Sep 17 00:00:00 2001 From: Ujjwal Verma Date: Mon, 20 Feb 2017 21:02:43 +0530 Subject: Absolute IPv6 addresses supported --- mitmproxy/net/check.py | 9 ++++++++- test/mitmproxy/net/test_check.py | 1 + 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/mitmproxy/net/check.py b/mitmproxy/net/check.py index f793d397..ac2b6830 100644 --- a/mitmproxy/net/check.py +++ b/mitmproxy/net/check.py @@ -1,4 +1,5 @@ import re +from ipaddress import ip_address # Allow underscore in host name _label_valid = re.compile(b"(?!-)[A-Z\d\-_]{1,63}(? bool: return False if host and host[-1:] == b".": host = host[:-1] - return all(_label_valid.match(x) for x in host.split(b".")) + if all(_label_valid.match(x) for x in host.split(b".")): + return True + try: + ip_address(host.decode('idna')) + return True + except ValueError: + return False def is_valid_port(port): diff --git a/test/mitmproxy/net/test_check.py b/test/mitmproxy/net/test_check.py index 9dbc02e0..0ffd6b2e 100644 --- a/test/mitmproxy/net/test_check.py +++ b/test/mitmproxy/net/test_check.py @@ -11,3 +11,4 @@ def test_is_valid_host(): assert check.is_valid_host(b"one.two.") # Allow underscore assert check.is_valid_host(b"one_two") + assert check.is_valid_host(b"::1") -- cgit v1.2.3 From aa6b0f299e7cebb70a7e5b1f8b63d7d45683e31f Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Tue, 21 Feb 2017 13:00:04 +0100 Subject: minor changes --- mitmproxy/net/check.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/mitmproxy/net/check.py b/mitmproxy/net/check.py index ac2b6830..d30c1df6 100644 --- a/mitmproxy/net/check.py +++ b/mitmproxy/net/check.py @@ -1,5 +1,5 @@ +import ipaddress import re -from ipaddress import ip_address # Allow underscore in host name _label_valid = re.compile(b"(?!-)[A-Z\d\-_]{1,63}(? bool: """ - Checks if a hostname is valid. + Checks if the passed bytes are a valid DNS hostname or an IPv4/IPv6 address. """ try: host.decode("idna") except ValueError: return False + # RFC1035: 255 bytes or less. if len(host) > 255: return False if host and host[-1:] == b".": host = host[:-1] + # DNS hostname if all(_label_valid.match(x) for x in host.split(b".")): return True + # IPv4/IPv6 address try: - ip_address(host.decode('idna')) + ipaddress.ip_address(host.decode('idna')) return True except ValueError: return False -- cgit v1.2.3