Skip to content

Commit fed54fa

Browse files
committedMar 23, 2018
don't crash if server address is unknown, fix mitmproxy#2969
1 parent 623f9b6 commit fed54fa

File tree

4 files changed

+18
-12
lines changed

4 files changed

+18
-12
lines changed
 

‎mitmproxy/connections.py

+8-11
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
import time
2-
31
import os
2+
import time
43
import typing
54
import uuid
65

7-
from mitmproxy import stateobject, exceptions
86
from mitmproxy import certs
7+
from mitmproxy import exceptions
8+
from mitmproxy import stateobject
99
from mitmproxy.net import tcp
1010
from mitmproxy.net import tls
11+
from mitmproxy.utils import human
1112
from mitmproxy.utils import strutils
1213

1314

1415
class ClientConnection(tcp.BaseHandler, stateobject.StateObject):
15-
1616
"""
1717
A client connection
1818
@@ -72,11 +72,10 @@ def __repr__(self):
7272
else:
7373
alpn = ""
7474

75-
return "<ClientConnection: {tls}{alpn}{host}:{port}>".format(
75+
return "<ClientConnection: {tls}{alpn}{address}>".format(
7676
tls=tls,
7777
alpn=alpn,
78-
host=self.address[0],
79-
port=self.address[1],
78+
address=human.format_address(self.address),
8079
)
8180

8281
def __eq__(self, other):
@@ -161,7 +160,6 @@ def finish(self):
161160

162161

163162
class ServerConnection(tcp.TCPClient, stateobject.StateObject):
164-
165163
"""
166164
A server connection
167165
@@ -209,11 +207,10 @@ def __repr__(self):
209207
)
210208
else:
211209
alpn = ""
212-
return "<ServerConnection: {tls}{alpn}{host}:{port}>".format(
210+
return "<ServerConnection: {tls}{alpn}{address}>".format(
213211
tls=tls,
214212
alpn=alpn,
215-
host=self.address[0],
216-
port=self.address[1],
213+
address=human.format_address(self.address),
217214
)
218215

219216
def __eq__(self, other):

‎mitmproxy/utils/human.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,13 @@ def format_timestamp_with_milli(s):
7373
return d.strftime("%Y-%m-%d %H:%M:%S.%f")[:-3]
7474

7575

76-
def format_address(address: tuple) -> str:
76+
def format_address(address: typing.Optional[tuple]) -> str:
7777
"""
7878
This function accepts IPv4/IPv6 tuples and
7979
returns the formatted address string with port number
8080
"""
81+
if address is None:
82+
return "<no address>"
8183
try:
8284
host = ipaddress.ip_address(address[0])
8385
if host.is_unspecified:

‎test/mitmproxy/test_connections.py

+6
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ def test_repr(self):
3838
assert 'ALPN' not in repr(c)
3939
assert 'TLS' in repr(c)
4040

41+
c.address = None
42+
assert repr(c)
43+
4144
def test_tls_established_property(self):
4245
c = tflow.tclient_conn()
4346
c.tls_established = True
@@ -110,6 +113,9 @@ def test_repr(self):
110113
c.tls_established = False
111114
assert 'TLS' not in repr(c)
112115

116+
c.address = None
117+
assert repr(c)
118+
113119
def test_tls_established_property(self):
114120
c = tflow.tserver_conn()
115121
c.tls_established = True

‎test/mitmproxy/utils/test_human.py

+1
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,4 @@ def test_format_address():
5656
assert human.format_address(("example.com", "54010")) == "example.com:54010"
5757
assert human.format_address(("::", "8080")) == "*:8080"
5858
assert human.format_address(("0.0.0.0", "8080")) == "*:8080"
59+
assert human.format_address(None) == "<no address>"

0 commit comments

Comments
 (0)
Please sign in to comment.