diff options
author | Maximilian Hils <git@maximilianhils.com> | 2017-04-10 20:24:15 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-04-10 20:24:15 +0200 |
commit | ec6be96f2ad05776127efc29fae1df51647aa1fb (patch) | |
tree | f74088499d45c3ccf32a58cb5da0a26330acf1fe | |
parent | c7b501275205e2cf82055981106336d4a0db252f (diff) | |
parent | 742127ef7bcde86bdde911a8399a05c4c4d75d5b (diff) | |
download | mitmproxy-ec6be96f2ad05776127efc29fae1df51647aa1fb.tar.gz mitmproxy-ec6be96f2ad05776127efc29fae1df51647aa1fb.tar.bz2 mitmproxy-ec6be96f2ad05776127efc29fae1df51647aa1fb.zip |
Merge pull request #2234 from ujjwal96/ip-formatting
Fixes IP address formatting #2109
-rw-r--r-- | mitmproxy/addons/termstatus.py | 5 | ||||
-rw-r--r-- | mitmproxy/proxy/server.py | 3 | ||||
-rw-r--r-- | mitmproxy/tools/console/flowdetailview.py | 4 | ||||
-rw-r--r-- | mitmproxy/utils/human.py | 19 | ||||
-rw-r--r-- | test/mitmproxy/utils/test_human.py | 7 |
5 files changed, 32 insertions, 6 deletions
diff --git a/mitmproxy/addons/termstatus.py b/mitmproxy/addons/termstatus.py index 7b05f409..951ddd3c 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 9f783bc3..50a2b76b 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 691f19a5..30eaea90 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 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]) diff --git a/test/mitmproxy/utils/test_human.py b/test/mitmproxy/utils/test_human.py index 3d65dfd1..76dc2f88 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" |