mitmproxy/libmproxy/protocol/base.py

153 lines
4.5 KiB
Python
Raw Normal View History

2015-07-25 11:31:55 +00:00
"""
mitmproxy protocol architecture
In mitmproxy, protocols are implemented as a set of layers, which are composed on top each other.
2015-08-30 13:27:29 +00:00
For example, the following scenarios depict possible settings (lowest layer first):
2015-07-25 11:31:55 +00:00
Transparent HTTP proxy, no SSL:
2015-08-30 13:27:29 +00:00
TransparentProxy
Http1Layer
2015-07-25 11:31:55 +00:00
HttpLayer
Regular proxy, CONNECT request with WebSockets over SSL:
2015-08-30 13:27:29 +00:00
HttpProxy
Http1Layer
2015-07-25 11:31:55 +00:00
HttpLayer
SslLayer
WebsocketLayer (or TcpLayer)
Automated protocol detection by peeking into the buffer:
2015-08-30 13:27:29 +00:00
TransparentProxy
TLSLayer
2015-07-25 11:31:55 +00:00
Http2Layer
2015-08-30 13:27:29 +00:00
HttpLayer
2015-07-25 11:31:55 +00:00
Communication between layers is done as follows:
- lower layers provide context information to higher layers
2015-08-18 13:59:44 +00:00
- higher layers can call functions provided by lower layers,
2015-07-25 11:31:55 +00:00
which are propagated until they reach a suitable layer.
Further goals:
- Connections should always be peekable to make automatic protocol detection work.
- Upstream connections should be established as late as possible;
inline scripts shall have a chance to handle everything locally.
"""
from __future__ import (absolute_import, print_function, division)
2015-07-25 11:31:55 +00:00
from netlib import tcp
2015-08-30 13:27:29 +00:00
from ..models import ServerConnection
from ..exceptions import ProtocolException
2015-07-25 11:31:55 +00:00
2015-07-25 12:48:50 +00:00
2015-07-25 11:31:55 +00:00
class _LayerCodeCompletion(object):
"""
Dummy class that provides type hinting in PyCharm, which simplifies development a lot.
"""
2015-08-16 21:25:02 +00:00
def __init__(self, *args, **kwargs):
super(_LayerCodeCompletion, self).__init__(*args, **kwargs)
2015-07-25 11:31:55 +00:00
if True:
return
self.config = None
"""@type: libmproxy.proxy.config.ProxyConfig"""
self.client_conn = None
"""@type: libmproxy.proxy.connection.ClientConnection"""
self.channel = None
"""@type: libmproxy.controller.Channel"""
class Layer(_LayerCodeCompletion):
2015-08-16 21:25:02 +00:00
def __init__(self, ctx, *args, **kwargs):
2015-07-25 11:31:55 +00:00
"""
Args:
ctx: The (read-only) higher layer.
"""
2015-08-16 21:25:02 +00:00
super(Layer, self).__init__(*args, **kwargs)
2015-07-25 11:31:55 +00:00
self.ctx = ctx
def __call__(self):
"""
Logic of the layer.
Raises:
2015-08-18 13:59:44 +00:00
ProtocolException in case of protocol exceptions.
2015-07-25 11:31:55 +00:00
"""
raise NotImplementedError
def __getattr__(self, name):
"""
Attributes not present on the current layer may exist on a higher layer.
"""
return getattr(self.ctx, name)
def log(self, msg, level, subs=()):
full_msg = [
2015-08-29 23:21:58 +00:00
"{}: {}".format(repr(self.client_conn.address), msg)
]
2015-07-25 11:31:55 +00:00
for i in subs:
full_msg.append(" -> " + i)
full_msg = "\n".join(full_msg)
self.channel.tell("log", Log(full_msg, level))
2015-08-14 08:41:11 +00:00
@property
def layers(self):
return [self] + self.ctx.layers
def __repr__(self):
2015-08-15 14:26:12 +00:00
return type(self).__name__
2015-08-14 08:41:11 +00:00
2015-07-25 11:31:55 +00:00
class ServerConnectionMixin(object):
"""
Mixin that provides a layer with the capabilities to manage a server connection.
"""
2015-08-16 21:25:02 +00:00
def __init__(self, server_address=None):
2015-08-15 14:26:12 +00:00
super(ServerConnectionMixin, self).__init__()
2015-08-16 21:25:02 +00:00
self.server_conn = ServerConnection(server_address)
2015-07-25 11:31:55 +00:00
2015-08-18 13:59:44 +00:00
def reconnect(self):
address = self.server_conn.address
self._disconnect()
self.server_conn.address = address
self.connect()
2015-08-27 15:35:53 +00:00
def set_server(self, address, server_tls=None, sni=None, depth=1):
2015-08-18 13:59:44 +00:00
if depth == 1:
if self.server_conn:
self._disconnect()
self.log("Set new server address: " + repr(address), "debug")
2015-08-18 12:15:08 +00:00
self.server_conn.address = address
2015-08-18 13:59:44 +00:00
else:
2015-08-29 23:21:58 +00:00
self.ctx.set_server(address, server_tls, sni, depth - 1)
2015-07-25 11:31:55 +00:00
def _disconnect(self):
"""
Deletes (and closes) an existing server connection.
"""
2015-08-16 21:25:02 +00:00
self.log("serverdisconnect", "debug", [repr(self.server_conn.address)])
2015-07-25 11:31:55 +00:00
self.server_conn.finish()
self.server_conn.close()
# self.channel.tell("serverdisconnect", self)
2015-08-16 21:25:02 +00:00
self.server_conn = ServerConnection(None)
2015-07-25 11:31:55 +00:00
2015-08-18 13:59:44 +00:00
def connect(self):
2015-08-16 21:25:02 +00:00
if not self.server_conn.address:
2015-08-14 08:41:11 +00:00
raise ProtocolException("Cannot connect to server, no server address given.")
2015-08-16 21:25:02 +00:00
self.log("serverconnect", "debug", [repr(self.server_conn.address)])
2015-07-25 11:31:55 +00:00
try:
self.server_conn.connect()
except tcp.NetLibError as e:
2015-08-29 23:21:58 +00:00
raise ProtocolException(
"Server connection to '%s' failed: %s" % (self.server_conn.address, e), e)
2015-08-30 13:27:29 +00:00
class Log(object):
def __init__(self, msg, level="info"):
self.msg = msg
self.level = level
class Kill(Exception):
"""
Kill a connection.
"""