File tree 4 files changed +18
-12
lines changed
4 files changed +18
-12
lines changed Original file line number Diff line number Diff line change 1
- import time
2
-
3
1
import os
2
+ import time
4
3
import typing
5
4
import uuid
6
5
7
- from mitmproxy import stateobject , exceptions
8
6
from mitmproxy import certs
7
+ from mitmproxy import exceptions
8
+ from mitmproxy import stateobject
9
9
from mitmproxy .net import tcp
10
10
from mitmproxy .net import tls
11
+ from mitmproxy .utils import human
11
12
from mitmproxy .utils import strutils
12
13
13
14
14
15
class ClientConnection (tcp .BaseHandler , stateobject .StateObject ):
15
-
16
16
"""
17
17
A client connection
18
18
@@ -72,11 +72,10 @@ def __repr__(self):
72
72
else :
73
73
alpn = ""
74
74
75
- return "<ClientConnection: {tls}{alpn}{host}:{port }>" .format (
75
+ return "<ClientConnection: {tls}{alpn}{address }>" .format (
76
76
tls = tls ,
77
77
alpn = alpn ,
78
- host = self .address [0 ],
79
- port = self .address [1 ],
78
+ address = human .format_address (self .address ),
80
79
)
81
80
82
81
def __eq__ (self , other ):
@@ -161,7 +160,6 @@ def finish(self):
161
160
162
161
163
162
class ServerConnection (tcp .TCPClient , stateobject .StateObject ):
164
-
165
163
"""
166
164
A server connection
167
165
@@ -209,11 +207,10 @@ def __repr__(self):
209
207
)
210
208
else :
211
209
alpn = ""
212
- return "<ServerConnection: {tls}{alpn}{host}:{port }>" .format (
210
+ return "<ServerConnection: {tls}{alpn}{address }>" .format (
213
211
tls = tls ,
214
212
alpn = alpn ,
215
- host = self .address [0 ],
216
- port = self .address [1 ],
213
+ address = human .format_address (self .address ),
217
214
)
218
215
219
216
def __eq__ (self , other ):
Original file line number Diff line number Diff line change @@ -73,11 +73,13 @@ def format_timestamp_with_milli(s):
73
73
return d .strftime ("%Y-%m-%d %H:%M:%S.%f" )[:- 3 ]
74
74
75
75
76
- def format_address (address : tuple ) -> str :
76
+ def format_address (address : typing . Optional [ tuple ] ) -> str :
77
77
"""
78
78
This function accepts IPv4/IPv6 tuples and
79
79
returns the formatted address string with port number
80
80
"""
81
+ if address is None :
82
+ return "<no address>"
81
83
try :
82
84
host = ipaddress .ip_address (address [0 ])
83
85
if host .is_unspecified :
Original file line number Diff line number Diff line change @@ -38,6 +38,9 @@ def test_repr(self):
38
38
assert 'ALPN' not in repr (c )
39
39
assert 'TLS' in repr (c )
40
40
41
+ c .address = None
42
+ assert repr (c )
43
+
41
44
def test_tls_established_property (self ):
42
45
c = tflow .tclient_conn ()
43
46
c .tls_established = True
@@ -110,6 +113,9 @@ def test_repr(self):
110
113
c .tls_established = False
111
114
assert 'TLS' not in repr (c )
112
115
116
+ c .address = None
117
+ assert repr (c )
118
+
113
119
def test_tls_established_property (self ):
114
120
c = tflow .tserver_conn ()
115
121
c .tls_established = True
Original file line number Diff line number Diff line change @@ -56,3 +56,4 @@ def test_format_address():
56
56
assert human .format_address (("example.com" , "54010" )) == "example.com:54010"
57
57
assert human .format_address (("::" , "8080" )) == "*:8080"
58
58
assert human .format_address (("0.0.0.0" , "8080" )) == "*:8080"
59
+ assert human .format_address (None ) == "<no address>"
You can’t perform that action at this time.
0 commit comments