mirror of
https://github.com/zhigang1992/mitmproxy.git
synced 2026-04-07 22:40:04 +08:00
211 lines
6.7 KiB
Python
211 lines
6.7 KiB
Python
import sys, os, argparse
|
|
import netlib.utils
|
|
import flow, filt, utils, cmdline
|
|
|
|
class DumpError(Exception): pass
|
|
|
|
def str_response(resp):
|
|
r = "%s %s"%(resp.code, resp.msg)
|
|
if resp.is_replay():
|
|
r = "[replay] " + r
|
|
return r
|
|
|
|
|
|
def str_request(req, showhost):
|
|
if req.client_conn:
|
|
c = req.client_conn.address[0]
|
|
else:
|
|
c = "[replay]"
|
|
r = "%s %s %s"%(c, req.method, req.get_url(showhost))
|
|
if req.stickycookie:
|
|
r = "[stickycookie] " + r
|
|
return r
|
|
|
|
|
|
class DumpMaster(flow.FlowMaster):
|
|
@classmethod
|
|
def add_arguments(cls,parser):
|
|
cmdline.add_common_arguments(parser)
|
|
parser.add_argument(
|
|
"--keepserving",
|
|
action="store_true", dest="keepserving", default=False,
|
|
help="Continue serving after client playback or file read. We exit by default."
|
|
)
|
|
parser.add_argument(
|
|
'filt', nargs=argparse.REMAINDER
|
|
)
|
|
|
|
def __init__(self, server, options, outfile=sys.stdout):
|
|
flow.FlowMaster.__init__(self, server, flow.State())
|
|
self.outfile = outfile
|
|
self.o = options
|
|
self.anticache = options.anticache
|
|
self.anticomp = options.anticomp
|
|
self.eventlog = options.eventlog
|
|
self.showhost = options.showhost
|
|
self.refresh_server_playback = options.refresh_server_playback
|
|
|
|
if options.filt:
|
|
self.filt = filt.parse(" ".join(options.filt))
|
|
else:
|
|
self.filt = None
|
|
|
|
if options.stickycookie:
|
|
self.set_stickycookie(options.stickycookie)
|
|
|
|
if options.stickyauth:
|
|
self.set_stickyauth(options.stickyauth)
|
|
|
|
if options.wfile:
|
|
path = os.path.expanduser(options.wfile)
|
|
try:
|
|
f = file(path, "wb")
|
|
self.start_stream(f, self.filt)
|
|
except IOError, v:
|
|
raise DumpError(v.strerror)
|
|
|
|
if options.replacements:
|
|
for i in options.replacements:
|
|
self.replacehooks.add(*i)
|
|
|
|
if options.setheaders:
|
|
for i in options.setheaders:
|
|
self.setheaders.add(*i)
|
|
|
|
if options.server_replay:
|
|
self.start_server_playback(
|
|
self._readflow(options.server_replay),
|
|
options.kill, options.rheaders,
|
|
not options.keepserving,
|
|
options.nopop
|
|
)
|
|
|
|
if options.client_replay:
|
|
self.start_client_playback(
|
|
self._readflow(options.client_replay),
|
|
not options.keepserving
|
|
)
|
|
|
|
scripts = options.scripts or []
|
|
for script_argv in scripts:
|
|
err = self.load_script(script_argv)
|
|
if err:
|
|
raise DumpError(err)
|
|
|
|
if options.rfile:
|
|
path = os.path.expanduser(options.rfile)
|
|
try:
|
|
f = file(path, "rb")
|
|
freader = flow.FlowReader(f)
|
|
except IOError, v:
|
|
raise DumpError(v.strerror)
|
|
try:
|
|
self.load_flows(freader)
|
|
except flow.FlowReadError, v:
|
|
self.add_event("Flow file corrupted. Stopped loading.")
|
|
|
|
if self.o.app:
|
|
self.start_app(self.o.app_domain, self.o.app_ip, self.o.app_auth, self.o.app_readonly)
|
|
|
|
def _readflow(self, path):
|
|
path = os.path.expanduser(path)
|
|
try:
|
|
f = file(path, "rb")
|
|
flows = list(flow.FlowReader(f).stream())
|
|
except (IOError, flow.FlowReadError), v:
|
|
raise DumpError(v.strerror)
|
|
return flows
|
|
|
|
def add_event(self, e, level="info"):
|
|
if self.eventlog:
|
|
print >> self.outfile, e
|
|
self.outfile.flush()
|
|
|
|
def indent(self, n, t):
|
|
l = str(t).strip().split("\n")
|
|
return "\n".join(" "*n + i for i in l)
|
|
|
|
def _process_flow(self, f):
|
|
if not self.o.app:
|
|
self.state.delete_flow(f)
|
|
if self.filt and not f.match(self.filt):
|
|
return
|
|
|
|
if f.response:
|
|
sz = utils.pretty_size(len(f.response.content))
|
|
if self.o.verbosity > 0:
|
|
result = " << %s %s"%(str_response(f.response), sz)
|
|
if self.o.verbosity > 1:
|
|
result = result + "\n\n" + self.indent(4, f.response.headers)
|
|
if self.o.verbosity > 2:
|
|
if utils.isBin(f.response.content):
|
|
d = netlib.utils.hexdump(f.response.content)
|
|
d = "\n".join("%s\t%s %s"%i for i in d)
|
|
cont = self.indent(4, d)
|
|
elif f.response.content:
|
|
cont = self.indent(4, f.response.content)
|
|
else:
|
|
cont = ""
|
|
result = result + "\n\n" + cont
|
|
elif f.error:
|
|
result = " << %s"%f.error.msg
|
|
|
|
if self.o.verbosity == 1:
|
|
print >> self.outfile, str_request(f.request, self.showhost)
|
|
print >> self.outfile, result
|
|
elif self.o.verbosity == 2:
|
|
print >> self.outfile, str_request(f.request, self.showhost)
|
|
print >> self.outfile, self.indent(4, f.request.headers)
|
|
print >> self.outfile
|
|
print >> self.outfile, result
|
|
print >> self.outfile, "\n"
|
|
elif self.o.verbosity >= 3:
|
|
print >> self.outfile, str_request(f.request, self.showhost)
|
|
print >> self.outfile, self.indent(4, f.request.headers)
|
|
if utils.isBin(f.request.content):
|
|
print >> self.outfile, self.indent(4, netlib.utils.hexdump(f.request.content))
|
|
elif f.request.content:
|
|
print >> self.outfile, self.indent(4, f.request.content)
|
|
print >> self.outfile
|
|
print >> self.outfile, result
|
|
print >> self.outfile, "\n"
|
|
if self.o.verbosity:
|
|
self.outfile.flush()
|
|
|
|
def handle_log(self, l):
|
|
self.add_event(l.msg)
|
|
l.reply()
|
|
|
|
def handle_request(self, r):
|
|
f = flow.FlowMaster.handle_request(self, r)
|
|
if f:
|
|
r.reply()
|
|
return f
|
|
|
|
def handle_response(self, msg):
|
|
f = flow.FlowMaster.handle_response(self, msg)
|
|
if f:
|
|
msg.reply()
|
|
self._process_flow(f)
|
|
return f
|
|
|
|
def handle_error(self, msg):
|
|
f = flow.FlowMaster.handle_error(self, msg)
|
|
if f:
|
|
self._process_flow(f)
|
|
return f
|
|
|
|
def shutdown(self): # pragma: no cover
|
|
return flow.FlowMaster.shutdown(self)
|
|
|
|
def run(self): # pragma: no cover
|
|
if self.o.rfile and not self.o.keepserving:
|
|
for script in self.scripts:
|
|
self.unload_script(script)
|
|
return
|
|
try:
|
|
return flow.FlowMaster.run(self)
|
|
except BaseException:
|
|
self.shutdown()
|
|
raise
|