2015-08-14 08:41:11 +00:00
|
|
|
from __future__ import (absolute_import, print_function, division)
|
2015-09-10 22:00:00 +00:00
|
|
|
import string
|
|
|
|
import sys
|
2015-08-14 08:41:11 +00:00
|
|
|
|
2015-09-10 22:00:00 +00:00
|
|
|
import six
|
|
|
|
|
2016-01-24 02:29:14 +00:00
|
|
|
from libmproxy.exceptions import ProtocolException, TlsProtocolException
|
2015-09-17 00:13:28 +00:00
|
|
|
from netlib.exceptions import TcpException
|
2015-08-30 13:27:29 +00:00
|
|
|
from ..protocol import (
|
2015-09-18 16:07:38 +00:00
|
|
|
RawTCPLayer, TlsLayer, Http1Layer, Http2Layer, is_tls_record_magic, ServerConnectionMixin,
|
2016-01-24 02:29:14 +00:00
|
|
|
UpstreamConnectLayer, TlsClientHello
|
2015-08-30 13:27:29 +00:00
|
|
|
)
|
|
|
|
from .modes import HttpProxy, HttpUpstreamProxy, ReverseProxy
|
2015-08-16 21:25:02 +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-09-10 22:00:00 +00:00
|
|
|
try:
|
|
|
|
d = top_layer.client_conn.rfile.peek(3)
|
2015-09-17 00:13:28 +00:00
|
|
|
except TcpException as e:
|
2015-09-10 22:00:00 +00:00
|
|
|
six.reraise(ProtocolException, ProtocolException(str(e)), sys.exc_info()[2])
|
2016-01-25 22:49:31 +00:00
|
|
|
client_tls = is_tls_record_magic(d)
|
2015-08-30 00:27:38 +00:00
|
|
|
|
2016-01-26 16:44:32 +00:00
|
|
|
# 1. check for --ignore
|
|
|
|
if self.config.check_ignore:
|
2016-01-26 18:09:22 +00:00
|
|
|
ignore = self.config.check_ignore(top_layer.server_conn.address)
|
|
|
|
if not ignore and client_tls:
|
|
|
|
try:
|
|
|
|
client_hello = TlsClientHello.from_client_conn(self.client_conn)
|
|
|
|
except TlsProtocolException as e:
|
|
|
|
self.log("Cannot parse Client Hello: %s" % repr(e), "error")
|
|
|
|
else:
|
|
|
|
ignore = self.config.check_ignore((client_hello.client_sni, 443))
|
|
|
|
if ignore:
|
2016-01-26 16:44:32 +00:00
|
|
|
return RawTCPLayer(top_layer, logging=False)
|
2016-01-26 16:12:46 +00:00
|
|
|
|
2015-08-30 00:27:38 +00:00
|
|
|
# 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):
|
2016-01-25 22:49:31 +00:00
|
|
|
return TlsLayer(top_layer, client_tls, top_layer.server_tls)
|
2015-09-18 16:07:38 +00:00
|
|
|
if isinstance(top_layer, ServerConnectionMixin) or isinstance(top_layer, UpstreamConnectLayer):
|
2016-01-25 22:49:31 +00:00
|
|
|
return TlsLayer(top_layer, client_tls, client_tls)
|
2015-08-30 00:27:38 +00:00
|
|
|
|
|
|
|
# 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).
|
2016-01-25 22:49:31 +00:00
|
|
|
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
|
|
|
|
2015-08-30 00:27:38 +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
|
|
|
|
2015-08-30 00:27:38 +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()
|
2015-09-26 15:41:14 +00:00
|
|
|
if alpn == b'h2':
|
2015-08-27 13:26:21 +00:00
|
|
|
return Http2Layer(top_layer, 'transparent')
|
2015-09-26 15:41:14 +00:00
|
|
|
if alpn == b'http/1.1':
|
2015-08-27 13:26:21 +00:00
|
|
|
return Http1Layer(top_layer, 'transparent')
|
2015-08-16 13:19:11 +00:00
|
|
|
|
2015-09-10 22:00:00 +00:00
|
|
|
# 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
|
|
|
|
2015-09-10 22:00:00 +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
|