mitmproxy/test/tservers.py

314 lines
8.6 KiB
Python
Raw Normal View History

import os.path
import threading, Queue
import shutil, tempfile
import flask
2014-08-08 02:43:44 +00:00
import mock
2014-03-09 20:51:24 +00:00
from libmproxy.proxy.config import ProxyConfig
from libmproxy.proxy.server import ProxyServer
from libmproxy.proxy.primitives import TransparentUpstreamServerResolver
import libpathod.test, libpathod.pathoc
2014-03-09 20:51:24 +00:00
from libmproxy import flow, controller
from libmproxy.cmdline import APP_HOST, APP_PORT
import tutils
testapp = flask.Flask(__name__)
@testapp.route("/")
def hello():
return "testapp"
@testapp.route("/error")
def error():
raise ValueError("An exception...")
def errapp(environ, start_response):
raise ValueError("errapp")
class TestMaster(flow.FlowMaster):
2014-02-07 06:08:59 +00:00
def __init__(self, config):
2014-03-09 20:51:24 +00:00
s = ProxyServer(config, 0)
state = flow.State()
flow.FlowMaster.__init__(self, s, state)
self.apps.add(testapp, "testapp", 80)
self.apps.add(errapp, "errapp", 80)
self.clear_log()
def handle_request(self, f):
flow.FlowMaster.handle_request(self, f)
f.reply()
2013-02-23 03:34:59 +00:00
def handle_response(self, f):
flow.FlowMaster.handle_response(self, f)
f.reply()
def clear_log(self):
self.log = []
def handle_log(self, l):
self.log.append(l.msg)
l.reply()
class ProxyThread(threading.Thread):
2013-02-23 03:34:59 +00:00
def __init__(self, tmaster):
threading.Thread.__init__(self)
2013-02-23 03:34:59 +00:00
self.tmaster = tmaster
2014-02-07 06:08:59 +00:00
self.name = "ProxyThread (%s:%s)" % (tmaster.server.address.host, tmaster.server.address.port)
2013-02-23 03:34:59 +00:00
controller.should_exit = False
@property
def port(self):
return self.tmaster.server.address.port
@property
def log(self):
return self.tmaster.log
def run(self):
self.tmaster.run()
def shutdown(self):
self.tmaster.shutdown()
2014-02-07 06:08:59 +00:00
class ProxTestBase(object):
# Test Configuration
ssl = None
ssloptions = False
clientcerts = False
2013-03-02 03:59:16 +00:00
no_upstream_cert = False
2013-03-02 22:04:33 +00:00
authenticator = None
2013-02-23 03:34:59 +00:00
masterclass = TestMaster
2014-03-02 09:15:53 +00:00
externalapp = False
2014-03-11 00:02:10 +00:00
certforward = False
@classmethod
def setupAll(cls):
cls.server = libpathod.test.Daemon(ssl=cls.ssl, ssloptions=cls.ssloptions)
cls.server2 = libpathod.test.Daemon(ssl=cls.ssl, ssloptions=cls.ssloptions)
pconf = cls.get_proxy_config()
cls.confdir = os.path.join(tempfile.gettempdir(), "mitmproxy")
2014-08-08 02:43:44 +00:00
cls.config = ProxyConfig(
2013-03-02 03:59:16 +00:00
no_upstream_cert = cls.no_upstream_cert,
confdir = cls.confdir,
2013-03-02 22:04:33 +00:00
authenticator = cls.authenticator,
2014-03-11 00:02:10 +00:00
certforward = cls.certforward,
2014-08-30 18:15:19 +00:00
ssl_ports=([cls.server.port, cls.server2.port] if cls.ssl else []),
**pconf
)
2014-08-08 02:43:44 +00:00
tmaster = cls.masterclass(cls.config)
2014-03-02 09:15:53 +00:00
tmaster.start_app(APP_HOST, APP_PORT, cls.externalapp)
2013-02-23 03:34:59 +00:00
cls.proxy = ProxyThread(tmaster)
cls.proxy.start()
@property
def master(cls):
return cls.proxy.tmaster
@classmethod
def teardownAll(cls):
2014-08-08 02:43:44 +00:00
shutil.rmtree(cls.confdir)
cls.proxy.shutdown()
cls.server.shutdown()
cls.server2.shutdown()
def setUp(self):
self.master.clear_log()
self.master.state.clear()
self.server.clear_log()
self.server2.clear_log()
@property
def scheme(self):
return "https" if self.ssl else "http"
@property
def proxies(self):
"""
The URL base for the server instance.
"""
return (
(self.scheme, ("127.0.0.1", self.proxy.port))
)
@classmethod
def get_proxy_config(cls):
d = dict()
if cls.clientcerts:
d["clientcerts"] = tutils.test_data.path("data/clientcert")
return d
class HTTPProxTest(ProxTestBase):
2013-03-02 03:59:16 +00:00
def pathoc_raw(self):
return libpathod.pathoc.Pathoc(("127.0.0.1", self.proxy.port))
2013-03-02 03:59:16 +00:00
def pathoc(self, sni=None):
"""
Returns a connected Pathoc instance.
"""
p = libpathod.pathoc.Pathoc(("localhost", self.proxy.port), ssl=self.ssl, sni=sni)
2013-03-02 03:59:16 +00:00
if self.ssl:
p.connect(("127.0.0.1", self.server.port))
else:
p.connect()
return p
def pathod(self, spec, sni=None):
"""
2013-02-28 20:05:39 +00:00
Constructs a pathod GET request, with the appropriate base and proxy.
"""
2013-03-02 03:59:16 +00:00
p = self.pathoc(sni=sni)
spec = spec.encode("string_escape")
2013-02-28 20:05:39 +00:00
if self.ssl:
q = "get:'/p/%s'"%spec
else:
q = "get:'%s/p/%s'"%(self.server.urlbase, spec)
return p.request(q)
2013-03-24 20:20:26 +00:00
def app(self, page):
if self.ssl:
p = libpathod.pathoc.Pathoc(("127.0.0.1", self.proxy.port), True)
2013-12-08 14:46:11 +00:00
p.connect((APP_HOST, APP_PORT))
2013-03-24 20:20:26 +00:00
return p.request("get:'/%s'"%page)
else:
p = self.pathoc()
2013-12-08 14:46:11 +00:00
return p.request("get:'http://%s/%s'"%(APP_HOST, page))
2013-03-24 20:20:26 +00:00
class TResolver:
def __init__(self, port):
self.port = port
def original_addr(self, sock):
return ("127.0.0.1", self.port)
class TransparentProxTest(ProxTestBase):
ssl = None
resolver = TResolver
2014-08-08 02:43:44 +00:00
@classmethod
2014-08-08 02:43:44 +00:00
@mock.patch("libmproxy.platform.resolver")
def setupAll(cls, _):
super(TransparentProxTest, cls).setupAll()
2013-02-28 20:05:39 +00:00
if cls.ssl:
ports = [cls.server.port, cls.server2.port]
else:
ports = []
2014-08-08 02:43:44 +00:00
cls.config.get_upstream_server = TransparentUpstreamServerResolver(cls.resolver(cls.server.port), ports)
@classmethod
def get_proxy_config(cls):
d = ProxTestBase.get_proxy_config()
d["mode"] = "transparent"
return d
2013-03-02 02:06:49 +00:00
def pathod(self, spec, sni=None):
"""
2013-03-02 02:06:49 +00:00
Constructs a pathod GET request, with the appropriate base and proxy.
"""
2013-03-02 02:06:49 +00:00
if self.ssl:
p = self.pathoc(sni=sni)
q = "get:'/p/%s'"%spec
else:
p = self.pathoc()
q = "get:'/p/%s'"%spec
return p.request(q)
2013-03-02 02:06:49 +00:00
def pathoc(self, sni=None):
2013-02-28 20:05:39 +00:00
"""
Returns a connected Pathoc instance.
"""
p = libpathod.pathoc.Pathoc(("localhost", self.proxy.port), ssl=self.ssl, sni=sni)
2013-03-02 02:06:49 +00:00
p.connect()
2013-02-28 20:05:39 +00:00
return p
class ReverseProxTest(ProxTestBase):
ssl = None
@classmethod
def get_proxy_config(cls):
d = ProxTestBase.get_proxy_config()
2014-08-08 02:43:44 +00:00
d["upstream_server"] = (
True if cls.ssl else False,
True if cls.ssl else False,
"127.0.0.1",
cls.server.port
)
d["http_form_in"] = "relative"
d["http_form_out"] = "relative"
return d
def pathoc(self, sni=None):
"""
Returns a connected Pathoc instance.
"""
p = libpathod.pathoc.Pathoc(("localhost", self.proxy.port), ssl=self.ssl, sni=sni)
p.connect()
return p
def pathod(self, spec, sni=None):
"""
Constructs a pathod GET request, with the appropriate base and proxy.
"""
if self.ssl:
p = self.pathoc(sni=sni)
q = "get:'/p/%s'"%spec
else:
p = self.pathoc()
q = "get:'/p/%s'"%spec
return p.request(q)
2013-03-24 20:20:26 +00:00
2014-02-07 06:08:59 +00:00
class ChainProxTest(ProxTestBase):
"""
Chain n instances of mitmproxy in a row - because we can.
"""
n = 2
2014-08-30 18:15:19 +00:00
chain_config = [lambda port, sslports: ProxyConfig(
2014-08-08 02:43:44 +00:00
upstream_server= (False, False, "127.0.0.1", port),
http_form_in = "absolute",
2014-08-30 18:15:19 +00:00
http_form_out = "absolute",
ssl_ports=sslports
)] * n
2014-02-07 06:08:59 +00:00
@classmethod
def setupAll(cls):
super(ChainProxTest, cls).setupAll()
cls.chain = []
2014-02-07 06:08:59 +00:00
for i in range(cls.n):
2014-08-30 18:15:19 +00:00
sslports = [cls.server.port, cls.server2.port]
config = cls.chain_config[i](cls.proxy.port if i == 0 else cls.chain[-1].port,
sslports)
2014-02-07 06:08:59 +00:00
tmaster = cls.masterclass(config)
2014-03-02 09:15:53 +00:00
tmaster.start_app(APP_HOST, APP_PORT, cls.externalapp)
2014-02-07 06:08:59 +00:00
cls.chain.append(ProxyThread(tmaster))
cls.chain[-1].start()
@classmethod
def teardownAll(cls):
super(ChainProxTest, cls).teardownAll()
for p in cls.chain:
p.tmaster.shutdown()
2013-03-24 20:20:26 +00:00
def setUp(self):
super(ChainProxTest, self).setUp()
for p in self.chain:
p.tmaster.clear_log()
p.tmaster.state.clear()
2013-03-24 20:20:26 +00:00
2014-02-07 06:08:59 +00:00
class HTTPChainProxyTest(ChainProxTest):
def pathoc(self, sni=None):
"""
Returns a connected Pathoc instance.
"""
p = libpathod.pathoc.Pathoc(("localhost", self.chain[-1].port), ssl=self.ssl, sni=sni)
if self.ssl:
p.connect(("127.0.0.1", self.server.port))
else:
p.connect()
return p