aboutsummaryrefslogtreecommitdiffstats
path: root/libmproxy/proxy/connection.py
diff options
context:
space:
mode:
Diffstat (limited to 'libmproxy/proxy/connection.py')
-rw-r--r--libmproxy/proxy/connection.py43
1 files changed, 24 insertions, 19 deletions
diff --git a/libmproxy/proxy/connection.py b/libmproxy/proxy/connection.py
index 9e03157a..94f318f6 100644
--- a/libmproxy/proxy/connection.py
+++ b/libmproxy/proxy/connection.py
@@ -1,6 +1,8 @@
from __future__ import absolute_import
+
import copy
import os
+
from netlib import tcp, certutils
from .. import stateobject, utils
@@ -10,7 +12,7 @@ class ClientConnection(tcp.BaseHandler, stateobject.StateObject):
# Eventually, this object is restored from state. We don't have a
# connection then.
if client_connection:
- tcp.BaseHandler.__init__(self, client_connection, address, server)
+ super(ClientConnection, self).__init__(client_connection, address, server)
else:
self.connection = None
self.server = None
@@ -25,6 +27,9 @@ class ClientConnection(tcp.BaseHandler, stateobject.StateObject):
self.timestamp_ssl_setup = None
self.protocol = None
+ def __nonzero__(self):
+ return bool(self.connection) and not self.finished
+
def __repr__(self):
return "<ClientConnection: {ssl}{host}:{port}>".format(
ssl="[ssl] " if self.ssl_established else "",
@@ -32,6 +37,10 @@ class ClientConnection(tcp.BaseHandler, stateobject.StateObject):
port=self.address.port
)
+ @property
+ def tls_established(self):
+ return self.ssl_established
+
_stateobject_attributes = dict(
ssl_established=bool,
timestamp_start=float,
@@ -71,20 +80,11 @@ class ClientConnection(tcp.BaseHandler, stateobject.StateObject):
return f
def convert_to_ssl(self, *args, **kwargs):
- # TODO: read ALPN from server and select same proto for client conn
- # alpn_select = 'h2'
- # def alpn_select_callback(conn_, options):
- # if alpn_select in options:
- # return bytes(alpn_select)
- # else: # pragma no cover
- # return options[0]
- # tcp.BaseHandler.convert_to_ssl(self, alpn_select=alpn_select_callback, *args, **kwargs)
-
- tcp.BaseHandler.convert_to_ssl(self, *args, **kwargs)
+ super(ClientConnection, self).convert_to_ssl(*args, **kwargs)
self.timestamp_ssl_setup = utils.timestamp()
def finish(self):
- tcp.BaseHandler.finish(self)
+ super(ClientConnection, self).finish()
self.timestamp_end = utils.timestamp()
@@ -92,13 +92,16 @@ class ServerConnection(tcp.TCPClient, stateobject.StateObject):
def __init__(self, address):
tcp.TCPClient.__init__(self, address)
- self.state = [] # a list containing (conntype, state) tuples
+ self.via = None
self.timestamp_start = None
self.timestamp_end = None
self.timestamp_tcp_setup = None
self.timestamp_ssl_setup = None
self.protocol = None
+ def __nonzero__(self):
+ return bool(self.connection) and not self.finished
+
def __repr__(self):
if self.ssl_established and self.sni:
ssl = "[ssl: {0}] ".format(self.sni)
@@ -112,8 +115,11 @@ class ServerConnection(tcp.TCPClient, stateobject.StateObject):
port=self.address.port
)
+ @property
+ def tls_established(self):
+ return self.ssl_established
+
_stateobject_attributes = dict(
- state=list,
timestamp_start=float,
timestamp_end=float,
timestamp_tcp_setup=float,
@@ -131,8 +137,8 @@ class ServerConnection(tcp.TCPClient, stateobject.StateObject):
d.update(
address={"address": self.address(),
"use_ipv6": self.address.use_ipv6},
- source_address= ({"address": self.source_address(),
- "use_ipv6": self.source_address.use_ipv6} if self.source_address else None),
+ source_address=({"address": self.source_address(),
+ "use_ipv6": self.source_address.use_ipv6} if self.source_address else None),
cert=self.cert.to_pem() if self.cert else None
)
return d
@@ -176,9 +182,6 @@ class ServerConnection(tcp.TCPClient, stateobject.StateObject):
if os.path.exists(path):
clientcert = path
- # TODO: read ALPN from client and use same list for server conn
- # self.convert_to_ssl(cert=clientcert, sni=sni, alpn_protos=[netlib.http.http2.HTTP2Protocol.ALPN_PROTO_H2], **kwargs)
-
self.convert_to_ssl(cert=clientcert, sni=sni, **kwargs)
self.sni = sni
self.timestamp_ssl_setup = utils.timestamp()
@@ -186,3 +189,5 @@ class ServerConnection(tcp.TCPClient, stateobject.StateObject):
def finish(self):
tcp.TCPClient.finish(self)
self.timestamp_end = utils.timestamp()
+
+ServerConnection._stateobject_attributes["via"] = ServerConnection