2016-10-17 02:43:38 +00:00
|
|
|
import io
|
2016-06-02 05:46:37 +00:00
|
|
|
import time
|
2016-10-17 02:43:38 +00:00
|
|
|
import queue
|
2015-06-04 07:09:38 +00:00
|
|
|
from . import pathod
|
2016-10-19 21:22:23 +00:00
|
|
|
from mitmproxy.types import basethread
|
2017-05-01 23:49:25 +00:00
|
|
|
import typing # noqa
|
2012-06-06 23:23:23 +00:00
|
|
|
|
2014-09-06 23:38:44 +00:00
|
|
|
|
2012-06-06 23:23:23 +00:00
|
|
|
class Daemon:
|
2012-07-30 05:29:36 +00:00
|
|
|
IFACE = "127.0.0.1"
|
2015-05-30 00:03:13 +00:00
|
|
|
|
2017-05-01 23:49:25 +00:00
|
|
|
def __init__(self, ssl=None, **daemonargs) -> None:
|
|
|
|
self.q = queue.Queue() # type: queue.Queue
|
2016-10-17 02:43:38 +00:00
|
|
|
self.logfp = io.StringIO()
|
2015-06-08 03:23:56 +00:00
|
|
|
daemonargs["logfp"] = self.logfp
|
2012-07-30 05:29:36 +00:00
|
|
|
self.thread = _PaThread(self.IFACE, self.q, ssl, daemonargs)
|
2012-06-06 23:23:23 +00:00
|
|
|
self.thread.start()
|
|
|
|
self.port = self.q.get(True, 5)
|
2015-05-30 00:03:13 +00:00
|
|
|
self.urlbase = "%s://%s:%s" % (
|
2014-10-24 01:01:34 +00:00
|
|
|
"https" if ssl else "http",
|
|
|
|
self.IFACE,
|
|
|
|
self.port
|
|
|
|
)
|
2012-07-30 05:29:36 +00:00
|
|
|
|
2012-07-30 08:58:59 +00:00
|
|
|
def __enter__(self):
|
|
|
|
return self
|
|
|
|
|
2017-05-01 23:49:25 +00:00
|
|
|
def __exit__(self, type, value, traceback) -> bool:
|
2015-06-08 03:23:56 +00:00
|
|
|
self.logfp.truncate(0)
|
2012-07-30 08:58:59 +00:00
|
|
|
self.shutdown()
|
|
|
|
return False
|
|
|
|
|
2017-05-01 23:49:25 +00:00
|
|
|
def p(self, spec: str) -> str:
|
2012-07-30 05:29:36 +00:00
|
|
|
"""
|
|
|
|
Return a URL that will render the response in spec.
|
|
|
|
"""
|
2015-05-30 00:03:13 +00:00
|
|
|
return "%s/p/%s" % (self.urlbase, spec)
|
2012-06-06 23:23:23 +00:00
|
|
|
|
2017-05-01 23:49:25 +00:00
|
|
|
def text_log(self) -> str:
|
2015-06-08 03:23:56 +00:00
|
|
|
return self.logfp.getvalue()
|
|
|
|
|
2016-06-02 23:47:07 +00:00
|
|
|
def wait_for_silence(self, timeout=5):
|
2016-06-12 00:15:00 +00:00
|
|
|
self.thread.server.wait_for_silence(timeout=timeout)
|
2016-06-02 23:47:07 +00:00
|
|
|
|
2016-06-02 05:55:11 +00:00
|
|
|
def expect_log(self, n, timeout=5):
|
2016-06-02 05:46:37 +00:00
|
|
|
l = []
|
|
|
|
start = time.time()
|
|
|
|
while True:
|
|
|
|
l = self.log()
|
|
|
|
if time.time() - start >= timeout:
|
|
|
|
return None
|
|
|
|
if len(l) >= n:
|
|
|
|
break
|
|
|
|
return l
|
|
|
|
|
2012-07-30 05:29:36 +00:00
|
|
|
def last_log(self):
|
|
|
|
"""
|
2013-02-23 08:46:01 +00:00
|
|
|
Returns the last logged request, or None.
|
2012-07-30 05:29:36 +00:00
|
|
|
"""
|
2016-06-02 05:46:37 +00:00
|
|
|
l = self.expect_log(1)
|
2012-07-30 05:29:36 +00:00
|
|
|
if not l:
|
2013-02-23 08:46:01 +00:00
|
|
|
return None
|
2016-06-02 07:45:18 +00:00
|
|
|
return l[-1]
|
2012-07-30 05:29:36 +00:00
|
|
|
|
2017-05-01 23:49:25 +00:00
|
|
|
def log(self) -> typing.List[typing.Dict]:
|
2012-06-21 03:39:40 +00:00
|
|
|
"""
|
|
|
|
Return the log buffer as a list of dictionaries.
|
|
|
|
"""
|
2016-06-02 06:10:36 +00:00
|
|
|
return self.thread.server.get_log()
|
2012-06-21 03:39:40 +00:00
|
|
|
|
|
|
|
def clear_log(self):
|
|
|
|
"""
|
|
|
|
Clear the log.
|
|
|
|
"""
|
2016-06-02 06:10:36 +00:00
|
|
|
return self.thread.server.clear_log()
|
2012-06-21 03:39:40 +00:00
|
|
|
|
2012-06-06 23:23:23 +00:00
|
|
|
def shutdown(self):
|
2012-06-21 03:39:40 +00:00
|
|
|
"""
|
|
|
|
Shut the daemon down, return after the thread has exited.
|
|
|
|
"""
|
2012-06-19 01:23:07 +00:00
|
|
|
self.thread.server.shutdown()
|
2012-06-09 08:42:43 +00:00
|
|
|
self.thread.join()
|
2012-06-07 04:35:54 +00:00
|
|
|
|
|
|
|
|
2016-06-11 07:52:24 +00:00
|
|
|
class _PaThread(basethread.BaseThread):
|
2015-06-18 16:12:11 +00:00
|
|
|
|
2012-07-30 05:29:36 +00:00
|
|
|
def __init__(self, iface, q, ssl, daemonargs):
|
2016-06-11 07:52:24 +00:00
|
|
|
basethread.BaseThread.__init__(self, "PathodThread")
|
2012-07-30 05:29:36 +00:00
|
|
|
self.iface, self.q, self.ssl = iface, q, ssl
|
2012-07-24 22:34:57 +00:00
|
|
|
self.daemonargs = daemonargs
|
2016-06-02 06:10:36 +00:00
|
|
|
self.server = None
|
2012-06-07 04:35:54 +00:00
|
|
|
|
|
|
|
def run(self):
|
2012-06-24 03:12:31 +00:00
|
|
|
self.server = pathod.Pathod(
|
2012-07-30 05:29:36 +00:00
|
|
|
(self.iface, 0),
|
2015-06-18 16:12:11 +00:00
|
|
|
ssl=self.ssl,
|
2012-07-24 22:34:57 +00:00
|
|
|
**self.daemonargs
|
2012-06-24 03:12:31 +00:00
|
|
|
)
|
2014-10-24 01:01:34 +00:00
|
|
|
self.name = "PathodThread (%s:%s)" % (
|
2017-02-20 23:20:44 +00:00
|
|
|
self.server.address[0],
|
|
|
|
self.server.address[1],
|
2014-10-24 01:01:34 +00:00
|
|
|
)
|
2017-02-20 23:20:44 +00:00
|
|
|
self.q.put(self.server.address[1])
|
2012-06-19 01:23:07 +00:00
|
|
|
self.server.serve_forever()
|