This commit is contained in:
Ujjwal Verma 2017-04-09 18:55:20 +05:30
parent c7b5012752
commit 742127ef7b
5 changed files with 32 additions and 6 deletions

View File

@ -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)
)
)

View File

@ -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))

View File

@ -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])

View File

@ -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])

View File

@ -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"