2015-08-14 08:41:11 +00:00
|
|
|
from __future__ import (absolute_import, print_function, division)
|
|
|
|
|
|
|
|
from .rawtcp import RawTcpLayer
|
2015-08-11 18:27:34 +00:00
|
|
|
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.
|
|
|
|
"""
|
|
|
|
|
2015-08-14 08:41:11 +00:00
|
|
|
d = top_layer.client_conn.rfile.peek(3)
|
|
|
|
|
|
|
|
# TODO: Handle ignore and tcp passthrough
|
|
|
|
|
|
|
|
# TLS ClientHello magic, see http://www.moserware.com/2009/06/first-few-milliseconds-of-https.html#client-hello
|
|
|
|
is_tls_client_hello = (
|
|
|
|
len(d) == 3 and
|
|
|
|
d[0] == '\x16' and
|
|
|
|
d[1] == '\x03' and
|
|
|
|
d[2] in ('\x00', '\x01', '\x02', '\x03')
|
|
|
|
)
|
2015-08-11 18:27:34 +00:00
|
|
|
|
|
|
|
if not d:
|
|
|
|
return
|
2015-08-14 08:41:11 +00:00
|
|
|
|
|
|
|
if is_tls_client_hello:
|
2015-08-11 18:27:34 +00:00
|
|
|
layer = TlsLayer(top_layer, True, True)
|
|
|
|
else:
|
2015-08-14 08:41:11 +00:00
|
|
|
layer = RawTcpLayer(top_layer)
|
2015-08-11 18:27:34 +00:00
|
|
|
return layer
|
2015-08-14 08:41:11 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def layers(self):
|
|
|
|
return []
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "RootContext"
|