2016-06-09 22:47:28 +00:00
|
|
|
import time
|
2015-05-31 05:25:36 +00:00
|
|
|
|
2016-10-19 20:45:18 +00:00
|
|
|
from netlib import strutils
|
|
|
|
from mitmproxy.utils import human
|
2015-05-30 05:43:01 +00:00
|
|
|
|
2015-05-31 06:03:41 +00:00
|
|
|
|
2016-06-09 22:47:28 +00:00
|
|
|
def write_raw(fp, lines, timestamp=True):
|
2015-05-31 06:03:41 +00:00
|
|
|
if fp:
|
2016-06-09 22:47:28 +00:00
|
|
|
if timestamp:
|
|
|
|
fp.write(human.format_timestamp(time.time()))
|
2015-05-31 06:03:41 +00:00
|
|
|
for i in lines:
|
|
|
|
fp.write(i)
|
2015-06-08 02:01:04 +00:00
|
|
|
fp.write("\n")
|
2015-05-31 06:03:41 +00:00
|
|
|
fp.flush()
|
|
|
|
|
2015-05-30 05:43:01 +00:00
|
|
|
|
2016-10-17 04:29:45 +00:00
|
|
|
class LogCtx:
|
2015-05-31 05:25:36 +00:00
|
|
|
|
2016-06-09 22:47:28 +00:00
|
|
|
def __init__(self, fp, hex, timestamp, rfile, wfile):
|
2015-05-30 05:43:01 +00:00
|
|
|
self.lines = []
|
|
|
|
self.fp = fp
|
|
|
|
self.suppressed = False
|
|
|
|
self.hex = hex
|
2016-06-09 22:47:28 +00:00
|
|
|
self.timestamp = timestamp
|
2015-05-30 05:43:01 +00:00
|
|
|
self.rfile, self.wfile = rfile, wfile
|
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
if self.wfile:
|
|
|
|
self.wfile.start_log()
|
|
|
|
if self.rfile:
|
|
|
|
self.rfile.start_log()
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __exit__(self, exc_type, exc_value, traceback):
|
|
|
|
wlog = self.wfile.get_log() if self.wfile else None
|
|
|
|
rlog = self.rfile.get_log() if self.rfile else None
|
|
|
|
if self.suppressed or not self.fp:
|
|
|
|
return
|
|
|
|
if wlog:
|
|
|
|
self("Bytes written:")
|
|
|
|
self.dump(wlog, self.hex)
|
|
|
|
if rlog:
|
|
|
|
self("Bytes read:")
|
|
|
|
self.dump(rlog, self.hex)
|
2015-06-08 02:01:04 +00:00
|
|
|
if self.lines:
|
2015-06-28 00:16:02 +00:00
|
|
|
write_raw(
|
2015-06-08 02:01:04 +00:00
|
|
|
self.fp,
|
|
|
|
[
|
|
|
|
"\n".join(self.lines),
|
2016-06-09 22:47:28 +00:00
|
|
|
],
|
|
|
|
timestamp = self.timestamp
|
2015-06-08 02:01:04 +00:00
|
|
|
)
|
2015-05-31 06:12:39 +00:00
|
|
|
if exc_value:
|
2016-10-17 02:43:38 +00:00
|
|
|
raise exc_value
|
2015-05-30 05:43:01 +00:00
|
|
|
|
|
|
|
def suppress(self):
|
|
|
|
self.suppressed = True
|
|
|
|
|
|
|
|
def dump(self, data, hexdump):
|
|
|
|
if hexdump:
|
2016-06-02 01:03:37 +00:00
|
|
|
for line in strutils.hexdump(data):
|
2015-05-30 05:43:01 +00:00
|
|
|
self("\t%s %s %s" % line)
|
|
|
|
else:
|
2016-07-06 23:47:32 +00:00
|
|
|
data = strutils.native(
|
|
|
|
strutils.escape_control_characters(
|
|
|
|
data
|
|
|
|
.decode("ascii", "replace")
|
|
|
|
.replace(u"\ufffd", u".")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
for i in data.split("\n"):
|
|
|
|
self("\t%s" % i)
|
2015-05-30 05:43:01 +00:00
|
|
|
|
|
|
|
def __call__(self, line):
|
|
|
|
self.lines.append(line)
|
2015-06-28 00:16:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ConnectionLogger:
|
2016-06-09 22:47:28 +00:00
|
|
|
def __init__(self, fp, hex, timestamp, rfile, wfile):
|
2015-06-28 00:16:02 +00:00
|
|
|
self.fp = fp
|
|
|
|
self.hex = hex
|
|
|
|
self.rfile, self.wfile = rfile, wfile
|
2016-06-09 22:47:28 +00:00
|
|
|
self.timestamp = timestamp
|
2015-06-28 00:16:02 +00:00
|
|
|
|
|
|
|
def ctx(self):
|
2016-06-09 22:47:28 +00:00
|
|
|
return LogCtx(self.fp, self.hex, self.timestamp, self.rfile, self.wfile)
|
2015-06-28 00:16:02 +00:00
|
|
|
|
|
|
|
def write(self, lines):
|
2016-06-09 22:47:28 +00:00
|
|
|
write_raw(self.fp, lines, timestamp=self.timestamp)
|