2013-02-16 03:46:16 +00:00
|
|
|
import threading, Queue
|
|
|
|
import flask
|
|
|
|
import libpathod.test, libpathod.pathoc
|
|
|
|
from libmproxy import proxy, flow, controller
|
|
|
|
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):
|
|
|
|
def __init__(self, testq, config):
|
|
|
|
s = proxy.ProxyServer(config, 0)
|
|
|
|
s.apps.add(testapp, "testapp", 80)
|
|
|
|
s.apps.add(errapp, "errapp", 80)
|
|
|
|
state = flow.State()
|
|
|
|
flow.FlowMaster.__init__(self, s, state)
|
|
|
|
self.testq = testq
|
2013-03-02 01:52:05 +00:00
|
|
|
self.clear_log()
|
2013-02-16 03:46:16 +00:00
|
|
|
|
2013-02-23 03:34:59 +00:00
|
|
|
def handle_request(self, m):
|
|
|
|
flow.FlowMaster.handle_request(self, m)
|
|
|
|
m.reply()
|
|
|
|
|
|
|
|
def handle_response(self, m):
|
|
|
|
flow.FlowMaster.handle_response(self, m)
|
2013-02-16 23:42:48 +00:00
|
|
|
m.reply()
|
2013-02-16 03:46:16 +00:00
|
|
|
|
2013-03-02 01:52:05 +00:00
|
|
|
def clear_log(self):
|
|
|
|
self.log = []
|
|
|
|
|
2013-02-24 09:24:21 +00:00
|
|
|
def handle_log(self, l):
|
|
|
|
self.log.append(l.msg)
|
|
|
|
l.reply()
|
|
|
|
|
2013-02-16 03:46:16 +00:00
|
|
|
|
|
|
|
class ProxyThread(threading.Thread):
|
2013-02-23 03:34:59 +00:00
|
|
|
def __init__(self, tmaster):
|
2013-02-16 03:46:16 +00:00
|
|
|
threading.Thread.__init__(self)
|
2013-02-23 03:34:59 +00:00
|
|
|
self.tmaster = tmaster
|
|
|
|
controller.should_exit = False
|
2013-02-16 03:46:16 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def port(self):
|
|
|
|
return self.tmaster.server.port
|
|
|
|
|
2013-02-24 09:24:21 +00:00
|
|
|
@property
|
|
|
|
def log(self):
|
|
|
|
return self.tmaster.log
|
|
|
|
|
2013-02-16 03:46:16 +00:00
|
|
|
def run(self):
|
|
|
|
self.tmaster.run()
|
|
|
|
|
|
|
|
def shutdown(self):
|
|
|
|
self.tmaster.shutdown()
|
|
|
|
|
|
|
|
|
|
|
|
class ProxTestBase:
|
2013-02-24 09:52:59 +00:00
|
|
|
# Test Configuration
|
|
|
|
ssl = None
|
|
|
|
clientcerts = False
|
|
|
|
certfile = None
|
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
|
2013-02-16 03:46:16 +00:00
|
|
|
@classmethod
|
|
|
|
def setupAll(cls):
|
|
|
|
cls.tqueue = Queue.Queue()
|
|
|
|
cls.server = libpathod.test.Daemon(ssl=cls.ssl)
|
2013-02-24 09:24:21 +00:00
|
|
|
cls.server2 = libpathod.test.Daemon(ssl=cls.ssl)
|
2013-02-16 03:46:16 +00:00
|
|
|
pconf = cls.get_proxy_config()
|
|
|
|
config = proxy.ProxyConfig(
|
2013-03-02 03:59:16 +00:00
|
|
|
no_upstream_cert = cls.no_upstream_cert,
|
2013-02-23 22:34:01 +00:00
|
|
|
cacert = tutils.test_data.path("data/serverkey.pem"),
|
2013-03-02 22:04:33 +00:00
|
|
|
authenticator = cls.authenticator,
|
2013-02-16 03:46:16 +00:00
|
|
|
**pconf
|
|
|
|
)
|
2013-02-23 03:34:59 +00:00
|
|
|
tmaster = cls.masterclass(cls.tqueue, config)
|
|
|
|
cls.proxy = ProxyThread(tmaster)
|
2013-02-16 03:46:16 +00:00
|
|
|
cls.proxy.start()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def master(cls):
|
|
|
|
return cls.proxy.tmaster
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def teardownAll(cls):
|
|
|
|
cls.proxy.shutdown()
|
|
|
|
cls.server.shutdown()
|
2013-02-24 09:24:21 +00:00
|
|
|
cls.server2.shutdown()
|
2013-02-16 03:46:16 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
2013-03-02 01:52:05 +00:00
|
|
|
self.master.clear_log()
|
2013-02-16 03:46:16 +00:00
|
|
|
self.master.state.clear()
|
2013-03-02 01:52:05 +00:00
|
|
|
self.server.clear_log()
|
|
|
|
self.server2.clear_log()
|
2013-02-16 03:46:16 +00:00
|
|
|
|
|
|
|
@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")
|
2013-02-24 09:52:59 +00:00
|
|
|
if cls.certfile:
|
|
|
|
d["certfile"] =tutils.test_data.path("data/testkey.pem")
|
2013-02-16 03:46:16 +00:00
|
|
|
return d
|
|
|
|
|
2013-02-24 09:52:59 +00:00
|
|
|
|
|
|
|
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 09:42:36 +00:00
|
|
|
|
2013-03-02 03:59:16 +00:00
|
|
|
def pathoc(self, sni=None):
|
2013-02-23 21:51:14 +00:00
|
|
|
"""
|
|
|
|
Returns a connected Pathoc instance.
|
|
|
|
"""
|
2013-03-02 01:52:05 +00:00
|
|
|
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()
|
2013-02-16 03:46:16 +00:00
|
|
|
return p
|
|
|
|
|
2013-03-02 01:52:05 +00:00
|
|
|
def pathod(self, spec, sni=None):
|
2013-02-16 03:46:16 +00:00
|
|
|
"""
|
2013-02-28 20:05:39 +00:00
|
|
|
Constructs a pathod GET request, with the appropriate base and proxy.
|
2013-02-16 03:46:16 +00:00
|
|
|
"""
|
2013-03-02 03:59:16 +00:00
|
|
|
p = self.pathoc(sni=sni)
|
2013-03-02 09:42:36 +00:00
|
|
|
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-02-16 03:46:16 +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
|
2013-03-02 09:42:36 +00:00
|
|
|
resolver = TResolver
|
2013-02-16 03:46:16 +00:00
|
|
|
@classmethod
|
|
|
|
def get_proxy_config(cls):
|
2013-02-24 09:52:59 +00:00
|
|
|
d = ProxTestBase.get_proxy_config()
|
2013-02-28 20:05:39 +00:00
|
|
|
if cls.ssl:
|
|
|
|
ports = [cls.server.port, cls.server2.port]
|
|
|
|
else:
|
|
|
|
ports = []
|
2013-02-24 09:52:59 +00:00
|
|
|
d["transparent_proxy"] = dict(
|
2013-03-02 09:42:36 +00:00
|
|
|
resolver = cls.resolver(cls.server.port),
|
2013-02-28 20:05:39 +00:00
|
|
|
sslports = ports
|
2013-02-24 09:52:59 +00:00
|
|
|
)
|
|
|
|
return d
|
2013-02-16 03:46:16 +00:00
|
|
|
|
2013-03-02 02:06:49 +00:00
|
|
|
def pathod(self, spec, sni=None):
|
2013-02-16 03:46:16 +00:00
|
|
|
"""
|
2013-03-02 02:06:49 +00:00
|
|
|
Constructs a pathod GET request, with the appropriate base and proxy.
|
2013-02-16 03:46:16 +00:00
|
|
|
"""
|
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-02-16 03:46:16 +00:00
|
|
|
|
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.
|
|
|
|
"""
|
2013-03-02 02:06:49 +00:00
|
|
|
p = libpathod.pathoc.Pathoc("localhost", self.proxy.port, ssl=self.ssl, sni=sni)
|
|
|
|
p.connect()
|
2013-02-28 20:05:39 +00:00
|
|
|
return p
|
|
|
|
|
2013-02-16 03:46:16 +00:00
|
|
|
|
|
|
|
class ReverseProxTest(ProxTestBase):
|
|
|
|
ssl = None
|
|
|
|
@classmethod
|
|
|
|
def get_proxy_config(cls):
|
2013-02-24 09:52:59 +00:00
|
|
|
d = ProxTestBase.get_proxy_config()
|
|
|
|
d["reverse_proxy"] = (
|
2013-02-16 03:46:16 +00:00
|
|
|
"https" if cls.ssl else "http",
|
|
|
|
"127.0.0.1",
|
|
|
|
cls.server.port
|
|
|
|
)
|
2013-02-24 09:52:59 +00:00
|
|
|
return d
|
2013-02-16 03:46:16 +00:00
|
|
|
|
2013-03-02 02:09:22 +00:00
|
|
|
def pathoc(self, sni=None):
|
2013-02-16 03:46:16 +00:00
|
|
|
"""
|
2013-03-02 02:09:22 +00:00
|
|
|
Returns a connected Pathoc instance.
|
2013-02-16 03:46:16 +00:00
|
|
|
"""
|
2013-03-02 02:09:22 +00:00
|
|
|
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-02-16 03:46:16 +00:00
|
|
|
|