diff options
Diffstat (limited to 'mitmproxy/utils/human.py')
-rw-r--r-- | mitmproxy/utils/human.py | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/mitmproxy/utils/human.py b/mitmproxy/utils/human.py index 72e96d30..b3934846 100644 --- a/mitmproxy/utils/human.py +++ b/mitmproxy/utils/human.py @@ -1,7 +1,7 @@ import datetime +import ipaddress import time - SIZE_TABLE = [ ("b", 1024 ** 0), ("k", 1024 ** 1), @@ -62,3 +62,20 @@ def format_timestamp(s): def format_timestamp_with_milli(s): d = datetime.datetime.fromtimestamp(s) return d.strftime("%Y-%m-%d %H:%M:%S.%f")[:-3] + + +def format_address(address: tuple) -> str: + """ + This function accepts IPv4/IPv6 tuples and + returns the formatted address string with port number + """ + try: + host = ipaddress.ip_address(address[0]) + if host.version == 4: + return "{}:{}".format(str(host), address[1]) + # If IPv6 is mapped to IPv4 + elif host.ipv4_mapped: + return "{}:{}".format(str(host.ipv4_mapped), address[1]) + return "[{}]:{}".format(str(host), address[1]) + except ValueError: + return "{}:{}".format(address[0], address[1]) |