mitmproxy/libmproxy/proxy/connection.py

181 lines
5.7 KiB
Python
Raw Normal View History

from __future__ import absolute_import
2014-03-09 20:13:08 +00:00
import copy
import os
from netlib import tcp, certutils
2014-03-09 20:13:08 +00:00
from .. import stateobject, utils
2014-03-09 20:51:24 +00:00
class ClientConnection(tcp.BaseHandler, stateobject.StateObject):
2014-03-09 20:13:08 +00:00
def __init__(self, client_connection, address, server):
2015-05-30 00:03:28 +00:00
# Eventually, this object is restored from state. We don't have a
# connection then.
if client_connection:
2014-03-09 20:13:08 +00:00
tcp.BaseHandler.__init__(self, client_connection, address, server)
else:
self.connection = None
self.server = None
self.wfile = None
self.rfile = None
self.address = None
self.clientcert = None
2014-08-17 23:47:39 +00:00
self.ssl_established = None
2014-03-09 20:13:08 +00:00
self.timestamp_start = utils.timestamp()
self.timestamp_end = None
self.timestamp_ssl_setup = None
2014-08-17 22:55:30 +00:00
def __repr__(self):
return "<ClientConnection: {ssl}{host}:{port}>".format(
ssl="[ssl] " if self.ssl_established else "",
host=self.address.host,
port=self.address.port
)
2014-03-09 20:13:08 +00:00
_stateobject_attributes = dict(
2014-08-17 23:47:39 +00:00
ssl_established=bool,
2014-03-09 20:13:08 +00:00
timestamp_start=float,
timestamp_end=float,
timestamp_ssl_setup=float
)
def get_state(self, short=False):
d = super(ClientConnection, self).get_state(short)
2014-03-09 20:13:08 +00:00
d.update(
2015-05-30 00:03:28 +00:00
address={
"address": self.address(),
"use_ipv6": self.address.use_ipv6},
clientcert=self.cert.to_pem() if self.clientcert else None)
2014-03-09 20:13:08 +00:00
return d
def load_state(self, state):
super(ClientConnection, self).load_state(state)
2015-05-30 00:03:28 +00:00
self.address = tcp.Address(
**state["address"]) if state["address"] else None
self.clientcert = certutils.SSLCert.from_pem(
state["clientcert"]) if state["clientcert"] else None
2014-03-09 20:13:08 +00:00
def copy(self):
return copy.copy(self)
def send(self, message):
self.wfile.write(message)
self.wfile.flush()
@classmethod
def from_state(cls, state):
2014-03-09 20:13:08 +00:00
f = cls(None, tuple(), None)
f.load_state(state)
2014-03-09 20:13:08 +00:00
return f
def convert_to_ssl(self, *args, **kwargs):
def alpn_select_callback(conn_, options):
if alpn_select in options:
return bytes(alpn_select)
else: # pragma no cover
return options[0]
# TODO: read ALPN from server and select same proto for client conn
tcp.BaseHandler.convert_to_ssl(self, alpn_select=alpn_select_callback, *args, **kwargs)
2014-03-09 20:13:08 +00:00
self.timestamp_ssl_setup = utils.timestamp()
def finish(self):
tcp.BaseHandler.finish(self)
self.timestamp_end = utils.timestamp()
class ServerConnection(tcp.TCPClient, stateobject.StateObject):
def __init__(self, address):
2014-03-09 20:13:08 +00:00
tcp.TCPClient.__init__(self, address)
self.state = [] # a list containing (conntype, state) tuples
2014-03-09 20:13:08 +00:00
self.timestamp_start = None
self.timestamp_end = None
self.timestamp_tcp_setup = None
self.timestamp_ssl_setup = None
2014-08-17 22:55:30 +00:00
def __repr__(self):
if self.ssl_established and self.sni:
ssl = "[ssl: {0}] ".format(self.sni)
elif self.ssl_established:
ssl = "[ssl] "
else:
ssl = ""
return "<ServerConnection: {ssl}{host}:{port}>".format(
ssl=ssl,
host=self.address.host,
port=self.address.port
)
2014-03-09 20:13:08 +00:00
_stateobject_attributes = dict(
state=list,
2014-03-09 20:13:08 +00:00
timestamp_start=float,
timestamp_end=float,
timestamp_tcp_setup=float,
timestamp_ssl_setup=float,
address=tcp.Address,
source_address=tcp.Address,
cert=certutils.SSLCert,
ssl_established=bool,
sni=str
)
2014-09-17 15:30:19 +00:00
_stateobject_long_attributes = {"cert"}
2014-03-09 20:13:08 +00:00
def get_state(self, short=False):
d = super(ServerConnection, self).get_state(short)
2014-03-09 20:13:08 +00:00
d.update(
2014-09-04 14:37:50 +00:00
address={"address": self.address(),
"use_ipv6": self.address.use_ipv6},
source_address= ({"address": self.source_address(),
2015-05-30 00:03:28 +00:00
"use_ipv6": self.source_address.use_ipv6} if self.source_address else None),
2014-03-09 20:13:08 +00:00
cert=self.cert.to_pem() if self.cert else None
)
return d
def load_state(self, state):
super(ServerConnection, self).load_state(state)
2014-03-09 20:13:08 +00:00
2015-05-30 00:03:28 +00:00
self.address = tcp.Address(
**state["address"]) if state["address"] else None
self.source_address = tcp.Address(
**state["source_address"]) if state["source_address"] else None
self.cert = certutils.SSLCert.from_pem(
state["cert"]) if state["cert"] else None
2014-03-09 20:13:08 +00:00
@classmethod
def from_state(cls, state):
f = cls(tuple())
f.load_state(state)
2014-03-09 20:13:08 +00:00
return f
def copy(self):
return copy.copy(self)
def connect(self):
self.timestamp_start = utils.timestamp()
tcp.TCPClient.connect(self)
self.timestamp_tcp_setup = utils.timestamp()
def send(self, message):
self.wfile.write(message)
self.wfile.flush()
def establish_ssl(self, clientcerts, sni, **kwargs):
2014-03-09 20:13:08 +00:00
clientcert = None
if clientcerts:
2015-05-30 00:03:28 +00:00
path = os.path.join(
clientcerts,
self.address.host.encode("idna")) + ".pem"
2014-03-09 20:13:08 +00:00
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=['h2'], **kwargs)
self.sni = sni
2014-09-02 16:13:18 +00:00
self.timestamp_ssl_setup = utils.timestamp()
2014-03-09 20:13:08 +00:00
def finish(self):
tcp.TCPClient.finish(self)
self.timestamp_end = utils.timestamp()