aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--mitmproxy/connections.py19
-rw-r--r--mitmproxy/utils/human.py4
-rw-r--r--test/mitmproxy/test_connections.py6
-rw-r--r--test/mitmproxy/utils/test_human.py1
4 files changed, 18 insertions, 12 deletions
diff --git a/mitmproxy/connections.py b/mitmproxy/connections.py
index 29ab6ab5..9c26b44f 100644
--- a/mitmproxy/connections.py
+++ b/mitmproxy/connections.py
@@ -1,18 +1,18 @@
-import time
-
import os
+import time
import typing
import uuid
-from mitmproxy import stateobject, exceptions
from mitmproxy import certs
+from mitmproxy import exceptions
+from mitmproxy import stateobject
from mitmproxy.net import tcp
from mitmproxy.net import tls
+from mitmproxy.utils import human
from mitmproxy.utils import strutils
class ClientConnection(tcp.BaseHandler, stateobject.StateObject):
-
"""
A client connection
@@ -72,11 +72,10 @@ class ClientConnection(tcp.BaseHandler, stateobject.StateObject):
else:
alpn = ""
- return "<ClientConnection: {tls}{alpn}{host}:{port}>".format(
+ return "<ClientConnection: {tls}{alpn}{address}>".format(
tls=tls,
alpn=alpn,
- host=self.address[0],
- port=self.address[1],
+ address=human.format_address(self.address),
)
def __eq__(self, other):
@@ -161,7 +160,6 @@ class ClientConnection(tcp.BaseHandler, stateobject.StateObject):
class ServerConnection(tcp.TCPClient, stateobject.StateObject):
-
"""
A server connection
@@ -209,11 +207,10 @@ class ServerConnection(tcp.TCPClient, stateobject.StateObject):
)
else:
alpn = ""
- return "<ServerConnection: {tls}{alpn}{host}:{port}>".format(
+ return "<ServerConnection: {tls}{alpn}{address}>".format(
tls=tls,
alpn=alpn,
- host=self.address[0],
- port=self.address[1],
+ address=human.format_address(self.address),
)
def __eq__(self, other):
diff --git a/mitmproxy/utils/human.py b/mitmproxy/utils/human.py
index b21ac0b8..5c02b072 100644
--- a/mitmproxy/utils/human.py
+++ b/mitmproxy/utils/human.py
@@ -73,11 +73,13 @@ def format_timestamp_with_milli(s):
return d.strftime("%Y-%m-%d %H:%M:%S.%f")[:-3]
-def format_address(address: tuple) -> str:
+def format_address(address: typing.Optional[tuple]) -> str:
"""
This function accepts IPv4/IPv6 tuples and
returns the formatted address string with port number
"""
+ if address is None:
+ return "<no address>"
try:
host = ipaddress.ip_address(address[0])
if host.is_unspecified:
diff --git a/test/mitmproxy/test_connections.py b/test/mitmproxy/test_connections.py
index 00cdbc87..845a9043 100644
--- a/test/mitmproxy/test_connections.py
+++ b/test/mitmproxy/test_connections.py
@@ -38,6 +38,9 @@ class TestClientConnection:
assert 'ALPN' not in repr(c)
assert 'TLS' in repr(c)
+ c.address = None
+ assert repr(c)
+
def test_tls_established_property(self):
c = tflow.tclient_conn()
c.tls_established = True
@@ -110,6 +113,9 @@ class TestServerConnection:
c.tls_established = False
assert 'TLS' not in repr(c)
+ c.address = None
+ assert repr(c)
+
def test_tls_established_property(self):
c = tflow.tserver_conn()
c.tls_established = True
diff --git a/test/mitmproxy/utils/test_human.py b/test/mitmproxy/utils/test_human.py
index 947cfa4a..faf35f72 100644
--- a/test/mitmproxy/utils/test_human.py
+++ b/test/mitmproxy/utils/test_human.py
@@ -56,3 +56,4 @@ def test_format_address():
assert human.format_address(("example.com", "54010")) == "example.com:54010"
assert human.format_address(("::", "8080")) == "*:8080"
assert human.format_address(("0.0.0.0", "8080")) == "*:8080"
+ assert human.format_address(None) == "<no address>"