mitmproxy/libmproxy/proxy/root_context.py

130 lines
4.4 KiB
Python
Raw Normal View History

2015-08-14 08:41:11 +00:00
from __future__ import (absolute_import, print_function, division)
import string
import sys
2015-08-14 08:41:11 +00:00
import six
from libmproxy.exceptions import ProtocolException
2015-08-27 13:26:21 +00:00
from netlib.http.http1 import HTTP1Protocol
from netlib.http.http2 import HTTP2Protocol
from netlib.tcp import NetLibError
2015-08-30 13:27:29 +00:00
from ..protocol import (
RawTCPLayer, TlsLayer, Http1Layer, Http2Layer, is_tls_record_magic, ServerConnectionMixin
)
from .modes import HttpProxy, HttpUpstreamProxy, ReverseProxy
2015-08-16 21:25:02 +00:00
2015-08-30 11:40:23 +00:00
2015-08-11 18:27:34 +00:00
class RootContext(object):
"""
2015-09-03 16:55:38 +00:00
The outermost context provided to the root layer.
As a consequence, every layer has access to methods and attributes defined here.
Attributes:
client_conn:
The :py:class:`client connection <libmproxy.models.ClientConnection>`.
channel:
A :py:class:`~libmproxy.controller.Channel` to communicate with the FlowMaster.
Provides :py:meth:`.ask() <libmproxy.controller.Channel.ask>` and
:py:meth:`.tell() <libmproxy.controller.Channel.tell>` methods.
config:
The :py:class:`proxy server's configuration <libmproxy.proxy.ProxyConfig>`
2015-08-11 18:27:34 +00:00
"""
def __init__(self, client_conn, config, channel):
2015-09-03 16:55:38 +00:00
self.client_conn = client_conn
self.channel = channel
self.config = config
2015-08-11 18:27:34 +00:00
def next_layer(self, top_layer):
"""
This function determines the next layer in the protocol stack.
2015-08-27 13:26:21 +00:00
Arguments:
2015-09-03 16:55:38 +00:00
top_layer: the current innermost layer.
2015-08-27 13:26:21 +00:00
Returns:
The next layer
2015-08-11 18:27:34 +00:00
"""
2015-09-07 11:51:46 +00:00
layer = self._next_layer(top_layer)
return self.channel.ask("next_layer", layer)
2015-08-11 18:27:34 +00:00
2015-09-07 11:51:46 +00:00
def _next_layer(self, top_layer):
2015-08-27 13:26:21 +00:00
# 1. Check for --ignore.
if self.config.check_ignore(top_layer.server_conn.address):
2015-08-30 13:27:29 +00:00
return RawTCPLayer(top_layer, logging=False)
2015-08-14 08:41:11 +00:00
try:
d = top_layer.client_conn.rfile.peek(3)
except NetLibError as e:
six.reraise(ProtocolException, ProtocolException(str(e)), sys.exc_info()[2])
client_tls = is_tls_record_magic(d)
# 2. Always insert a TLS layer, even if there's neither client nor server tls.
# An inline script may upgrade from http to https,
# in which case we need some form of TLS layer.
if isinstance(top_layer, ReverseProxy):
return TlsLayer(top_layer, client_tls, top_layer.server_tls)
if isinstance(top_layer, ServerConnectionMixin):
return TlsLayer(top_layer, client_tls, client_tls)
# 3. In Http Proxy mode and Upstream Proxy mode, the next layer is fixed.
if isinstance(top_layer, TlsLayer):
if isinstance(top_layer.ctx, HttpProxy):
return Http1Layer(top_layer, "regular")
if isinstance(top_layer.ctx, HttpUpstreamProxy):
return Http1Layer(top_layer, "upstream")
# 4. Check for other TLS cases (e.g. after CONNECT).
if client_tls:
2015-08-27 13:26:21 +00:00
return TlsLayer(top_layer, True, True)
2015-08-11 18:27:34 +00:00
# 4. Check for --tcp
2015-08-27 13:26:21 +00:00
if self.config.check_tcp(top_layer.server_conn.address):
2015-08-30 13:27:29 +00:00
return RawTCPLayer(top_layer)
2015-08-18 12:15:08 +00:00
# 5. Check for TLS ALPN (HTTP1/HTTP2)
2015-08-27 13:26:21 +00:00
if isinstance(top_layer, TlsLayer):
alpn = top_layer.client_conn.get_alpn_proto_negotiated()
if alpn == HTTP2Protocol.ALPN_PROTO_H2:
return Http2Layer(top_layer, 'transparent')
if alpn == HTTP1Protocol.ALPN_PROTO_HTTP1:
return Http1Layer(top_layer, 'transparent')
# 6. Check for raw tcp mode
is_ascii = (
len(d) == 3 and
# better be safe here and don't expect uppercase...
all(x in string.ascii_letters for x in d)
)
if self.config.rawtcp and not is_ascii:
return RawTCPLayer(top_layer)
2015-08-14 08:41:11 +00:00
# 7. Assume HTTP1 by default
return Http1Layer(top_layer, 'transparent')
2015-08-15 18:20:46 +00:00
2015-09-03 15:01:25 +00:00
def log(self, msg, level, subs=()):
"""
Send a log message to the master.
"""
full_msg = [
"{}: {}".format(repr(self.client_conn.address), msg)
]
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 []
def __repr__(self):
return "RootContext"
2015-09-03 15:01:25 +00:00
class Log(object):
def __init__(self, msg, level="info"):
self.msg = msg
2015-09-07 11:51:46 +00:00
self.level = level