don't crash if server address is unknown, fix #2969

This commit is contained in:
Maximilian Hils 2018-03-23 04:26:36 +01:00
parent 623f9b694d
commit fed54fa3d0
4 changed files with 18 additions and 12 deletions

View File

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

View File

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

View File

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

View File

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