diff --git a/mitmproxy/addons/termstatus.py b/mitmproxy/addons/termstatus.py index 7b05f4092..951ddd3c9 100644 --- a/mitmproxy/addons/termstatus.py +++ b/mitmproxy/addons/termstatus.py @@ -1,4 +1,5 @@ from mitmproxy import ctx +from mitmproxy.utils import human """ A tiny addon to print the proxy status to terminal. Eventually this could @@ -17,7 +18,7 @@ class TermStatus: def running(self): if self.server: ctx.log.info( - "Proxy server listening at http://{}:{}".format( - *ctx.master.server.address, + "Proxy server listening at http://{}".format( + human.format_address(ctx.master.server.address) ) ) diff --git a/mitmproxy/proxy/server.py b/mitmproxy/proxy/server.py index 9f783bc32..50a2b76b8 100644 --- a/mitmproxy/proxy/server.py +++ b/mitmproxy/proxy/server.py @@ -12,6 +12,7 @@ from mitmproxy.proxy import modes from mitmproxy.proxy import root_context from mitmproxy.net import tcp from mitmproxy.net.http import http1 +from mitmproxy.utils import human class DummyServer: @@ -152,5 +153,5 @@ class ConnectionHandler: self.client_conn.finish() def log(self, msg, level): - msg = "{}: {}".format(repr(self.client_conn.address), msg) + msg = "{}: {}".format(human.format_address(self.client_conn.address), msg) self.channel.tell("log", log.LogEntry(msg, level)) diff --git a/mitmproxy/tools/console/flowdetailview.py b/mitmproxy/tools/console/flowdetailview.py index 691f19a5e..30eaea90d 100644 --- a/mitmproxy/tools/console/flowdetailview.py +++ b/mitmproxy/tools/console/flowdetailview.py @@ -30,8 +30,8 @@ def flowdetails(state, flow: http.HTTPFlow): if sc is not None: text.append(urwid.Text([("head", "Server Connection:")])) parts = [ - ["Address", "{}:{}".format(sc.address[0], sc.address[1])], - ["Resolved Address", "{}:{}".format(sc.ip_address[0], sc.ip_address[1])], + ["Address", "{}".format(human.format_address(sc.address))], + ["Resolved Address", "{}".format(human.format_address(sc.ip_address))], ] if resp: parts.append(["HTTP Version", resp.http_version]) diff --git a/mitmproxy/utils/human.py b/mitmproxy/utils/human.py index 72e96d309..b3934846c 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]) diff --git a/test/mitmproxy/utils/test_human.py b/test/mitmproxy/utils/test_human.py index 3d65dfd17..76dc2f887 100644 --- a/test/mitmproxy/utils/test_human.py +++ b/test/mitmproxy/utils/test_human.py @@ -46,3 +46,10 @@ def test_pretty_duration(): assert human.pretty_duration(10000) == "10000s" assert human.pretty_duration(1.123) == "1.12s" assert human.pretty_duration(0.123) == "123ms" + + +def test_format_address(): + assert human.format_address(("::1", "54010", "0", "0")) == "[::1]:54010" + assert human.format_address(("::ffff:127.0.0.1", "54010", "0", "0")) == "127.0.0.1:54010" + assert human.format_address(("127.0.0.1", "54010")) == "127.0.0.1:54010" + assert human.format_address(("example.com", "54010")) == "example.com:54010"