aboutsummaryrefslogtreecommitdiffstats
path: root/libmproxy/protocol2/root_context.py
blob: cbe596aa888ad9d4031e7d542d6262c7be555d26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from .rawtcp import TcpLayer
from .tls import TlsLayer


class RootContext(object):
    """
    The outmost context provided to the root layer.
    As a consequence, every layer has .client_conn, .channel, .next_layer() and .config.
    """

    def __init__(self, client_conn, config, channel):
        self.client_conn = client_conn  # Client Connection
        self.channel = channel  # provides .ask() method to communicate with FlowMaster
        self.config = config  # Proxy Configuration

    def next_layer(self, top_layer):
        """
        This function determines the next layer in the protocol stack.
        :param top_layer: the current top layer
        :return: The next layer.
        """

        d = top_layer.client_conn.rfile.peek(1)

        if not d:
            return
        # TLS ClientHello magic, see http://www.moserware.com/2009/06/first-few-milliseconds-of-https.html#client-hello
        if d[0] == "\x16":
            layer = TlsLayer(top_layer, True, True)
        else:
            layer = TcpLayer(top_layer)
        return layer