2014-03-10 21:36:47 +00:00
|
|
|
from __future__ import absolute_import
|
2015-03-13 11:07:35 +00:00
|
|
|
|
|
|
|
import mailcap
|
|
|
|
import mimetypes
|
|
|
|
import tempfile
|
|
|
|
import os
|
|
|
|
import os.path
|
|
|
|
import shlex
|
2015-03-19 21:02:34 +00:00
|
|
|
import signal
|
2015-03-13 11:07:35 +00:00
|
|
|
import stat
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
import traceback
|
2012-02-07 03:39:37 +00:00
|
|
|
import urwid
|
2015-03-13 11:07:35 +00:00
|
|
|
import weakref
|
|
|
|
|
2015-09-12 11:49:16 +00:00
|
|
|
from .. import controller, flow, script, contentviews
|
2015-04-03 01:10:57 +00:00
|
|
|
from . import flowlist, flowview, help, window, signals, options
|
2015-09-11 11:37:52 +00:00
|
|
|
from . import grideditor, palettes, statusbar, palettepicker
|
2012-02-07 03:39:37 +00:00
|
|
|
|
|
|
|
EVENTLOG_SIZE = 500
|
|
|
|
|
|
|
|
|
|
|
|
class ConsoleState(flow.State):
|
2016-01-27 09:12:18 +00:00
|
|
|
|
2012-02-07 03:39:37 +00:00
|
|
|
def __init__(self):
|
|
|
|
flow.State.__init__(self)
|
|
|
|
self.focus = None
|
2012-08-02 06:57:56 +00:00
|
|
|
self.follow_focus = None
|
2015-09-12 11:49:16 +00:00
|
|
|
self.default_body_view = contentviews.get("Auto")
|
2012-04-01 21:30:38 +00:00
|
|
|
self.flowsettings = weakref.WeakKeyDictionary()
|
2015-03-29 02:14:56 +00:00
|
|
|
self.last_search = None
|
2012-04-01 21:30:38 +00:00
|
|
|
|
2015-03-22 02:11:54 +00:00
|
|
|
def __setattr__(self, name, value):
|
|
|
|
self.__dict__[name] = value
|
|
|
|
signals.update_settings.send(self)
|
|
|
|
|
2012-04-01 21:30:38 +00:00
|
|
|
def add_flow_setting(self, flow, key, value):
|
|
|
|
d = self.flowsettings.setdefault(flow, {})
|
|
|
|
d[key] = value
|
|
|
|
|
|
|
|
def get_flow_setting(self, flow, key, default=None):
|
|
|
|
d = self.flowsettings.get(flow, {})
|
|
|
|
return d.get(key, default)
|
2012-02-07 03:39:37 +00:00
|
|
|
|
2014-11-26 03:18:21 +00:00
|
|
|
def add_flow(self, f):
|
|
|
|
super(ConsoleState, self).add_flow(f)
|
2012-02-07 03:39:37 +00:00
|
|
|
if self.focus is None:
|
|
|
|
self.set_focus(0)
|
2012-08-02 06:57:56 +00:00
|
|
|
elif self.follow_focus:
|
|
|
|
self.set_focus(len(self.view) - 1)
|
2015-06-12 01:27:33 +00:00
|
|
|
self.set_flow_marked(f, False)
|
2012-02-07 03:39:37 +00:00
|
|
|
return f
|
|
|
|
|
2014-11-26 03:18:21 +00:00
|
|
|
def update_flow(self, f):
|
|
|
|
super(ConsoleState, self).update_flow(f)
|
2012-02-07 03:39:37 +00:00
|
|
|
if self.focus is None:
|
|
|
|
self.set_focus(0)
|
|
|
|
return f
|
|
|
|
|
|
|
|
def set_limit(self, limit):
|
|
|
|
ret = flow.State.set_limit(self, limit)
|
|
|
|
self.set_focus(self.focus)
|
|
|
|
return ret
|
|
|
|
|
|
|
|
def get_focus(self):
|
|
|
|
if not self.view or self.focus is None:
|
|
|
|
return None, None
|
|
|
|
return self.view[self.focus], self.focus
|
|
|
|
|
|
|
|
def set_focus(self, idx):
|
|
|
|
if self.view:
|
|
|
|
if idx >= len(self.view):
|
|
|
|
idx = len(self.view) - 1
|
|
|
|
elif idx < 0:
|
|
|
|
idx = 0
|
|
|
|
self.focus = idx
|
2015-03-31 21:47:28 +00:00
|
|
|
else:
|
|
|
|
self.focus = None
|
2012-02-07 03:39:37 +00:00
|
|
|
|
2013-07-28 06:00:49 +00:00
|
|
|
def set_focus_flow(self, f):
|
|
|
|
self.set_focus(self.view.index(f))
|
|
|
|
|
2012-02-07 03:39:37 +00:00
|
|
|
def get_from_pos(self, pos):
|
|
|
|
if len(self.view) <= pos or pos < 0:
|
|
|
|
return None, None
|
|
|
|
return self.view[pos], pos
|
|
|
|
|
|
|
|
def get_next(self, pos):
|
2015-05-30 00:03:28 +00:00
|
|
|
return self.get_from_pos(pos + 1)
|
2012-02-07 03:39:37 +00:00
|
|
|
|
|
|
|
def get_prev(self, pos):
|
2015-05-30 00:03:28 +00:00
|
|
|
return self.get_from_pos(pos - 1)
|
2012-02-07 03:39:37 +00:00
|
|
|
|
|
|
|
def delete_flow(self, f):
|
2013-07-28 06:00:49 +00:00
|
|
|
if f in self.view and self.view.index(f) <= self.focus:
|
|
|
|
self.focus -= 1
|
2013-07-29 00:38:41 +00:00
|
|
|
if self.focus < 0:
|
|
|
|
self.focus = None
|
2012-02-07 03:39:37 +00:00
|
|
|
ret = flow.State.delete_flow(self, f)
|
|
|
|
self.set_focus(self.focus)
|
|
|
|
return ret
|
|
|
|
|
2014-12-29 13:56:35 +00:00
|
|
|
def clear(self):
|
2015-06-12 01:27:33 +00:00
|
|
|
marked_flows = []
|
|
|
|
for f in self.flows:
|
|
|
|
if self.flow_marked(f):
|
|
|
|
marked_flows.append(f)
|
2016-01-22 21:11:03 +00:00
|
|
|
|
2014-12-29 13:56:35 +00:00
|
|
|
super(ConsoleState, self).clear()
|
2016-01-22 21:11:03 +00:00
|
|
|
|
2015-06-12 01:27:33 +00:00
|
|
|
for f in marked_flows:
|
|
|
|
self.add_flow(f)
|
|
|
|
self.set_flow_marked(f, True)
|
2016-01-22 21:11:03 +00:00
|
|
|
|
2015-06-11 16:49:23 +00:00
|
|
|
if len(self.flows.views) == 0:
|
|
|
|
self.focus = None
|
|
|
|
else:
|
|
|
|
self.focus = 0
|
|
|
|
self.set_focus(self.focus)
|
2016-01-22 21:11:03 +00:00
|
|
|
|
2015-06-12 01:27:33 +00:00
|
|
|
def flow_marked(self, flow):
|
|
|
|
return self.get_flow_setting(flow, "marked", False)
|
2016-01-22 21:11:03 +00:00
|
|
|
|
2015-06-12 01:27:33 +00:00
|
|
|
def set_flow_marked(self, flow, marked):
|
|
|
|
self.add_flow_setting(flow, "marked", marked)
|
2016-01-22 21:11:03 +00:00
|
|
|
|
2012-02-07 03:39:37 +00:00
|
|
|
|
|
|
|
class Options(object):
|
2013-03-02 23:26:20 +00:00
|
|
|
attributes = [
|
2013-07-23 22:32:56 +00:00
|
|
|
"app",
|
|
|
|
"app_domain",
|
|
|
|
"app_ip",
|
2012-02-07 03:39:37 +00:00
|
|
|
"anticache",
|
|
|
|
"anticomp",
|
|
|
|
"client_replay",
|
|
|
|
"eventlog",
|
2016-01-22 21:11:03 +00:00
|
|
|
"follow",
|
2012-02-07 03:39:37 +00:00
|
|
|
"keepserving",
|
|
|
|
"kill",
|
|
|
|
"intercept",
|
2015-04-15 00:56:43 +00:00
|
|
|
"limit",
|
2012-02-07 03:39:37 +00:00
|
|
|
"no_server",
|
|
|
|
"refresh_server_playback",
|
|
|
|
"rfile",
|
2014-01-03 02:29:32 +00:00
|
|
|
"scripts",
|
2013-03-17 04:31:35 +00:00
|
|
|
"showhost",
|
2012-03-17 04:20:34 +00:00
|
|
|
"replacements",
|
2012-02-07 03:39:37 +00:00
|
|
|
"rheaders",
|
2012-08-18 12:14:16 +00:00
|
|
|
"setheaders",
|
2012-02-07 03:39:37 +00:00
|
|
|
"server_replay",
|
|
|
|
"stickycookie",
|
|
|
|
"stickyauth",
|
2014-07-21 19:06:55 +00:00
|
|
|
"stream_large_bodies",
|
2012-02-07 03:39:37 +00:00
|
|
|
"verbosity",
|
|
|
|
"wfile",
|
2012-03-05 09:05:11 +00:00
|
|
|
"nopop",
|
2012-06-30 05:37:38 +00:00
|
|
|
"palette",
|
2015-08-19 20:07:39 +00:00
|
|
|
"palette_transparent",
|
|
|
|
"no_mouse"
|
2012-02-07 03:39:37 +00:00
|
|
|
]
|
2015-03-13 11:07:35 +00:00
|
|
|
|
2012-02-07 03:39:37 +00:00
|
|
|
def __init__(self, **kwargs):
|
|
|
|
for k, v in kwargs.items():
|
|
|
|
setattr(self, k, v)
|
2013-03-02 23:26:20 +00:00
|
|
|
for i in self.attributes:
|
2012-02-07 03:39:37 +00:00
|
|
|
if not hasattr(self, i):
|
|
|
|
setattr(self, i, None)
|
|
|
|
|
|
|
|
|
|
|
|
class ConsoleMaster(flow.FlowMaster):
|
|
|
|
palette = []
|
2015-03-13 11:07:35 +00:00
|
|
|
|
2012-02-07 03:39:37 +00:00
|
|
|
def __init__(self, server, options):
|
|
|
|
flow.FlowMaster.__init__(self, server, ConsoleState())
|
2012-07-08 22:22:14 +00:00
|
|
|
self.stream_path = None
|
2012-02-07 03:39:37 +00:00
|
|
|
self.options = options
|
|
|
|
|
2012-03-17 04:20:34 +00:00
|
|
|
for i in options.replacements:
|
|
|
|
self.replacehooks.add(*i)
|
|
|
|
|
2012-08-18 12:14:16 +00:00
|
|
|
for i in options.setheaders:
|
|
|
|
self.setheaders.add(*i)
|
|
|
|
|
2012-02-07 03:39:37 +00:00
|
|
|
r = self.set_intercept(options.intercept)
|
|
|
|
if r:
|
|
|
|
print >> sys.stderr, "Intercept error:", r
|
|
|
|
sys.exit(1)
|
|
|
|
|
2015-04-15 00:56:43 +00:00
|
|
|
if options.limit:
|
|
|
|
self.set_limit(options.limit)
|
|
|
|
|
2012-02-07 03:39:37 +00:00
|
|
|
r = self.set_stickycookie(options.stickycookie)
|
|
|
|
if r:
|
|
|
|
print >> sys.stderr, "Sticky cookies error:", r
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
r = self.set_stickyauth(options.stickyauth)
|
|
|
|
if r:
|
|
|
|
print >> sys.stderr, "Sticky auth error:", r
|
|
|
|
sys.exit(1)
|
|
|
|
|
2014-07-21 19:06:55 +00:00
|
|
|
self.set_stream_large_bodies(options.stream_large_bodies)
|
|
|
|
|
2012-02-07 03:39:37 +00:00
|
|
|
self.refresh_server_playback = options.refresh_server_playback
|
|
|
|
self.anticache = options.anticache
|
|
|
|
self.anticomp = options.anticomp
|
|
|
|
self.killextra = options.kill
|
|
|
|
self.rheaders = options.rheaders
|
2012-03-05 09:05:11 +00:00
|
|
|
self.nopop = options.nopop
|
2013-03-17 04:31:35 +00:00
|
|
|
self.showhost = options.showhost
|
2015-04-06 20:42:40 +00:00
|
|
|
self.palette = options.palette
|
2015-04-06 22:01:18 +00:00
|
|
|
self.palette_transparent = options.palette_transparent
|
2012-02-07 03:39:37 +00:00
|
|
|
|
|
|
|
self.eventlog = options.eventlog
|
|
|
|
self.eventlist = urwid.SimpleListWalker([])
|
2016-01-22 21:11:03 +00:00
|
|
|
self.follow = options.follow
|
2012-02-07 03:39:37 +00:00
|
|
|
|
|
|
|
if options.client_replay:
|
|
|
|
self.client_playback_path(options.client_replay)
|
|
|
|
|
|
|
|
if options.server_replay:
|
|
|
|
self.server_playback_path(options.server_replay)
|
|
|
|
|
2014-01-03 02:29:32 +00:00
|
|
|
if options.scripts:
|
|
|
|
for i in options.scripts:
|
|
|
|
err = self.load_script(i)
|
|
|
|
if err:
|
|
|
|
print >> sys.stderr, "Script load error:", err
|
|
|
|
sys.exit(1)
|
2012-02-07 03:39:37 +00:00
|
|
|
|
2014-12-29 13:40:34 +00:00
|
|
|
if options.outfile:
|
2015-03-13 11:07:35 +00:00
|
|
|
err = self.start_stream_to_path(
|
|
|
|
options.outfile[0],
|
|
|
|
options.outfile[1]
|
|
|
|
)
|
2012-07-06 02:41:10 +00:00
|
|
|
if err:
|
2014-12-29 13:40:34 +00:00
|
|
|
print >> sys.stderr, "Stream file error:", err
|
2012-07-06 02:41:10 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
2015-03-22 04:18:53 +00:00
|
|
|
self.view_stack = []
|
|
|
|
|
2013-07-23 22:32:56 +00:00
|
|
|
if options.app:
|
2014-09-13 23:46:01 +00:00
|
|
|
self.start_app(self.options.app_host, self.options.app_port)
|
2015-03-20 23:37:00 +00:00
|
|
|
signals.call_in.connect(self.sig_call_in)
|
2015-03-22 04:18:53 +00:00
|
|
|
signals.pop_view_state.connect(self.sig_pop_view_state)
|
|
|
|
signals.push_view_state.connect(self.sig_push_view_state)
|
2015-06-02 00:09:07 +00:00
|
|
|
signals.sig_add_event.connect(self.sig_add_event)
|
2015-03-20 23:37:00 +00:00
|
|
|
|
2015-03-22 02:11:54 +00:00
|
|
|
def __setattr__(self, name, value):
|
|
|
|
self.__dict__[name] = value
|
|
|
|
signals.update_settings.send(self)
|
|
|
|
|
2016-02-02 13:33:36 +00:00
|
|
|
def load_script(self, command, use_reloader=True):
|
|
|
|
# We default to using the reloader in the console ui.
|
|
|
|
super(ConsoleMaster, self).load_script(command, use_reloader)
|
|
|
|
|
2015-06-02 00:09:07 +00:00
|
|
|
def sig_add_event(self, sender, e, level):
|
|
|
|
needed = dict(error=0, info=1, debug=2).get(level, 1)
|
|
|
|
if self.options.verbosity < needed:
|
|
|
|
return
|
|
|
|
|
|
|
|
if level == "error":
|
|
|
|
e = urwid.Text(("error", str(e)))
|
|
|
|
else:
|
|
|
|
e = urwid.Text(str(e))
|
|
|
|
self.eventlist.append(e)
|
|
|
|
if len(self.eventlist) > EVENTLOG_SIZE:
|
|
|
|
self.eventlist.pop(0)
|
|
|
|
self.eventlist.set_focus(len(self.eventlist) - 1)
|
|
|
|
|
|
|
|
def add_event(self, e, level):
|
|
|
|
signals.add_event(e, level)
|
|
|
|
|
2015-03-20 23:37:00 +00:00
|
|
|
def sig_call_in(self, sender, seconds, callback, args=()):
|
|
|
|
def cb(*_):
|
|
|
|
return callback(*args)
|
|
|
|
self.loop.set_alarm_in(seconds, cb)
|
2013-07-23 22:32:56 +00:00
|
|
|
|
2015-03-22 04:18:53 +00:00
|
|
|
def sig_pop_view_state(self, sender):
|
2015-04-17 01:06:45 +00:00
|
|
|
if len(self.view_stack) > 1:
|
|
|
|
self.view_stack.pop()
|
|
|
|
self.loop.widget = self.view_stack[-1]
|
2015-03-31 20:25:50 +00:00
|
|
|
else:
|
|
|
|
signals.status_prompt_onekey.send(
|
|
|
|
self,
|
|
|
|
prompt = "Quit",
|
|
|
|
keys = (
|
|
|
|
("yes", "y"),
|
|
|
|
("no", "n"),
|
|
|
|
),
|
|
|
|
callback = self.quit,
|
|
|
|
)
|
2015-03-22 04:18:53 +00:00
|
|
|
|
2015-04-17 01:06:45 +00:00
|
|
|
def sig_push_view_state(self, sender, window):
|
|
|
|
self.view_stack.append(window)
|
|
|
|
self.loop.widget = window
|
|
|
|
self.loop.draw_screen()
|
2015-03-22 04:18:53 +00:00
|
|
|
|
2012-10-10 22:12:06 +00:00
|
|
|
def _run_script_method(self, method, s, f):
|
|
|
|
status, val = s.run(method, f)
|
|
|
|
if val:
|
|
|
|
if status:
|
2015-06-02 00:09:07 +00:00
|
|
|
signals.add_event("Method %s return: %s" % (method, val), "debug")
|
2012-10-10 22:12:06 +00:00
|
|
|
else:
|
2015-06-02 00:09:07 +00:00
|
|
|
signals.add_event(
|
2015-05-30 00:03:28 +00:00
|
|
|
"Method %s error: %s" %
|
|
|
|
(method, val[1]), "error")
|
2012-10-10 22:12:06 +00:00
|
|
|
|
2014-01-12 10:01:59 +00:00
|
|
|
def run_script_once(self, command, f):
|
|
|
|
if not command:
|
2012-02-08 09:28:15 +00:00
|
|
|
return
|
2015-06-02 00:09:07 +00:00
|
|
|
signals.add_event("Running script on flow: %s" % command, "debug")
|
2014-01-12 00:59:32 +00:00
|
|
|
|
|
|
|
try:
|
2015-11-14 03:21:38 +00:00
|
|
|
s = script.Script(command, script.ScriptContext(self))
|
|
|
|
except script.ScriptException as v:
|
2015-03-20 22:19:20 +00:00
|
|
|
signals.status_message.send(
|
|
|
|
message = "Error loading script."
|
|
|
|
)
|
2015-06-02 00:09:07 +00:00
|
|
|
signals.add_event("Error loading script:\n%s" % v.args[0], "error")
|
2012-02-08 09:28:15 +00:00
|
|
|
return
|
2014-01-12 00:59:32 +00:00
|
|
|
|
2012-02-07 03:39:37 +00:00
|
|
|
if f.request:
|
2012-10-10 22:12:06 +00:00
|
|
|
self._run_script_method("request", s, f)
|
2012-02-07 03:39:37 +00:00
|
|
|
if f.response:
|
2012-10-10 22:12:06 +00:00
|
|
|
self._run_script_method("response", s, f)
|
2012-02-07 03:39:37 +00:00
|
|
|
if f.error:
|
2012-10-10 22:12:06 +00:00
|
|
|
self._run_script_method("error", s, f)
|
2013-06-13 14:04:04 +00:00
|
|
|
s.unload()
|
2015-03-22 02:58:32 +00:00
|
|
|
signals.flow_change.send(self, flow = f)
|
2012-02-07 03:39:37 +00:00
|
|
|
|
2014-01-12 10:01:59 +00:00
|
|
|
def set_script(self, command):
|
|
|
|
if not command:
|
2012-02-07 03:39:37 +00:00
|
|
|
return
|
2014-01-12 10:01:59 +00:00
|
|
|
ret = self.load_script(command)
|
2012-02-07 03:39:37 +00:00
|
|
|
if ret:
|
2015-03-20 22:19:20 +00:00
|
|
|
signals.status_message.send(message=ret)
|
2012-02-07 03:39:37 +00:00
|
|
|
|
|
|
|
def toggle_eventlog(self):
|
|
|
|
self.eventlog = not self.eventlog
|
2015-04-26 16:41:27 +00:00
|
|
|
signals.pop_view_state.send(self)
|
2012-02-20 23:42:43 +00:00
|
|
|
self.view_flowlist()
|
2012-02-07 03:39:37 +00:00
|
|
|
|
2015-03-22 00:32:24 +00:00
|
|
|
def _readflows(self, path):
|
2015-02-05 13:44:45 +00:00
|
|
|
"""
|
|
|
|
Utitility function that reads a list of flows
|
|
|
|
or prints an error to the UI if that fails.
|
|
|
|
Returns
|
|
|
|
- None, if there was an error.
|
|
|
|
- a list of flows, otherwise.
|
|
|
|
"""
|
2012-02-07 03:39:37 +00:00
|
|
|
try:
|
2015-03-25 19:57:28 +00:00
|
|
|
return flow.read_flows_from_paths(path)
|
2015-02-05 13:44:45 +00:00
|
|
|
except flow.FlowReadError as e:
|
2015-03-22 03:59:11 +00:00
|
|
|
signals.status_message.send(message=e.strerror)
|
2015-02-05 13:44:45 +00:00
|
|
|
|
|
|
|
def client_playback_path(self, path):
|
2015-03-25 20:07:31 +00:00
|
|
|
if not isinstance(path, list):
|
|
|
|
path = [path]
|
2015-03-22 00:32:24 +00:00
|
|
|
flows = self._readflows(path)
|
2015-02-05 13:44:45 +00:00
|
|
|
if flows:
|
|
|
|
self.start_client_playback(flows, False)
|
2012-02-07 03:39:37 +00:00
|
|
|
|
|
|
|
def server_playback_path(self, path):
|
2015-03-25 20:12:38 +00:00
|
|
|
if not isinstance(path, list):
|
|
|
|
path = [path]
|
2015-03-22 00:32:24 +00:00
|
|
|
flows = self._readflows(path)
|
2015-02-05 13:44:45 +00:00
|
|
|
if flows:
|
2012-02-07 03:39:37 +00:00
|
|
|
self.start_server_playback(
|
2015-02-05 13:44:45 +00:00
|
|
|
flows,
|
2012-02-07 03:39:37 +00:00
|
|
|
self.killextra, self.rheaders,
|
2014-11-06 10:25:03 +00:00
|
|
|
False, self.nopop,
|
2015-03-13 11:07:35 +00:00
|
|
|
self.options.replay_ignore_params,
|
|
|
|
self.options.replay_ignore_content,
|
2015-03-07 16:38:18 +00:00
|
|
|
self.options.replay_ignore_payload_params,
|
|
|
|
self.options.replay_ignore_host
|
2012-02-07 03:39:37 +00:00
|
|
|
)
|
|
|
|
|
2012-02-08 05:25:00 +00:00
|
|
|
def spawn_editor(self, data):
|
|
|
|
fd, name = tempfile.mkstemp('', "mproxy")
|
|
|
|
os.write(fd, data)
|
|
|
|
os.close(fd)
|
|
|
|
c = os.environ.get("EDITOR")
|
2012-12-23 00:26:15 +00:00
|
|
|
# if no EDITOR is set, assume 'vi'
|
2012-02-08 05:25:00 +00:00
|
|
|
if not c:
|
|
|
|
c = "vi"
|
2012-02-24 23:43:00 +00:00
|
|
|
cmd = shlex.split(c)
|
|
|
|
cmd.append(name)
|
2012-02-08 05:25:00 +00:00
|
|
|
self.ui.stop()
|
|
|
|
try:
|
|
|
|
subprocess.call(cmd)
|
|
|
|
except:
|
2015-03-22 03:59:11 +00:00
|
|
|
signals.status_message.send(
|
|
|
|
message = "Can't start editor: %s" % " ".join(c)
|
|
|
|
)
|
2012-12-23 00:26:15 +00:00
|
|
|
else:
|
2015-03-13 11:07:35 +00:00
|
|
|
data = open(name, "rb").read()
|
2012-02-08 05:25:00 +00:00
|
|
|
self.ui.start()
|
|
|
|
os.unlink(name)
|
|
|
|
return data
|
|
|
|
|
2012-02-07 03:39:37 +00:00
|
|
|
def spawn_external_viewer(self, data, contenttype):
|
|
|
|
if contenttype:
|
2013-06-08 23:26:44 +00:00
|
|
|
contenttype = contenttype.split(";")[0]
|
2012-02-07 03:39:37 +00:00
|
|
|
ext = mimetypes.guess_extension(contenttype) or ""
|
|
|
|
else:
|
|
|
|
ext = ""
|
|
|
|
fd, name = tempfile.mkstemp(ext, "mproxy")
|
|
|
|
os.write(fd, data)
|
|
|
|
os.close(fd)
|
|
|
|
|
2012-12-23 00:26:15 +00:00
|
|
|
# read-only to remind the user that this is a view function
|
|
|
|
os.chmod(name, stat.S_IREAD)
|
|
|
|
|
2012-02-07 03:39:37 +00:00
|
|
|
cmd = None
|
|
|
|
shell = False
|
|
|
|
|
|
|
|
if contenttype:
|
|
|
|
c = mailcap.getcaps()
|
|
|
|
cmd, _ = mailcap.findmatch(c, contenttype, filename=name)
|
|
|
|
if cmd:
|
|
|
|
shell = True
|
|
|
|
if not cmd:
|
2012-12-23 00:26:15 +00:00
|
|
|
# hm which one should get priority?
|
2012-02-07 03:39:37 +00:00
|
|
|
c = os.environ.get("PAGER") or os.environ.get("EDITOR")
|
2012-12-23 00:26:15 +00:00
|
|
|
if not c:
|
|
|
|
c = "less"
|
|
|
|
cmd = shlex.split(c)
|
|
|
|
cmd.append(name)
|
2012-02-07 03:39:37 +00:00
|
|
|
self.ui.stop()
|
2012-12-23 00:26:15 +00:00
|
|
|
try:
|
|
|
|
subprocess.call(cmd, shell=shell)
|
|
|
|
except:
|
2015-03-20 22:19:20 +00:00
|
|
|
signals.status_message.send(
|
|
|
|
message="Can't start external viewer: %s" % " ".join(c)
|
2015-03-13 11:07:35 +00:00
|
|
|
)
|
2012-02-07 03:39:37 +00:00
|
|
|
self.ui.start()
|
|
|
|
os.unlink(name)
|
|
|
|
|
2012-06-30 05:37:38 +00:00
|
|
|
def set_palette(self, name):
|
2015-04-06 20:42:40 +00:00
|
|
|
self.palette = name
|
|
|
|
self.ui.register_palette(
|
2015-04-06 22:01:18 +00:00
|
|
|
palettes.palettes[name].palette(self.palette_transparent)
|
2015-04-06 20:42:40 +00:00
|
|
|
)
|
|
|
|
self.ui.clear()
|
2012-02-07 03:39:37 +00:00
|
|
|
|
2015-03-13 10:58:22 +00:00
|
|
|
def ticker(self, *userdata):
|
|
|
|
changed = self.tick(self.masterq, timeout=0)
|
|
|
|
if changed:
|
|
|
|
self.loop.draw_screen()
|
2015-03-22 02:58:32 +00:00
|
|
|
signals.update_settings.send()
|
2015-03-13 10:58:22 +00:00
|
|
|
self.loop.set_alarm_in(0.01, self.ticker)
|
|
|
|
|
2012-02-07 03:39:37 +00:00
|
|
|
def run(self):
|
|
|
|
self.ui = urwid.raw_display.Screen()
|
2012-02-17 23:12:01 +00:00
|
|
|
self.ui.set_terminal_properties(256)
|
2015-04-06 20:42:40 +00:00
|
|
|
self.set_palette(self.palette)
|
2015-03-13 10:58:22 +00:00
|
|
|
self.loop = urwid.MainLoop(
|
2015-03-22 03:59:11 +00:00
|
|
|
urwid.SolidFill("x"),
|
2015-03-13 10:58:22 +00:00
|
|
|
screen = self.ui,
|
2015-08-19 20:07:39 +00:00
|
|
|
handle_mouse = not self.options.no_mouse,
|
2015-03-13 10:58:22 +00:00
|
|
|
)
|
2012-02-07 03:39:37 +00:00
|
|
|
|
2015-01-02 00:26:22 +00:00
|
|
|
self.server.start_slave(
|
|
|
|
controller.Slave,
|
|
|
|
controller.Channel(self.masterq, self.should_exit)
|
|
|
|
)
|
2012-02-07 03:39:37 +00:00
|
|
|
|
|
|
|
if self.options.rfile:
|
2015-01-02 00:26:22 +00:00
|
|
|
ret = self.load_flows_path(self.options.rfile)
|
2012-07-24 03:15:41 +00:00
|
|
|
if ret and self.state.flow_count():
|
2015-06-02 00:09:07 +00:00
|
|
|
signals.add_event(
|
2015-01-02 00:26:22 +00:00
|
|
|
"File truncated or corrupted. "
|
|
|
|
"Loaded as many flows as possible.",
|
|
|
|
"error"
|
|
|
|
)
|
|
|
|
elif ret and not self.state.flow_count():
|
2012-02-07 03:39:37 +00:00
|
|
|
self.shutdown()
|
|
|
|
print >> sys.stderr, "Could not load file:", ret
|
|
|
|
sys.exit(1)
|
|
|
|
|
2015-03-13 10:58:22 +00:00
|
|
|
self.loop.set_alarm_in(0.01, self.ticker)
|
2015-03-19 21:02:34 +00:00
|
|
|
|
|
|
|
# It's not clear why we need to handle this explicitly - without this,
|
|
|
|
# mitmproxy hangs on keyboard interrupt. Remove if we ever figure it
|
|
|
|
# out.
|
|
|
|
def exit(s, f):
|
|
|
|
raise urwid.ExitMainLoop
|
|
|
|
signal.signal(signal.SIGINT, exit)
|
|
|
|
|
2015-03-22 03:59:11 +00:00
|
|
|
self.loop.set_alarm_in(
|
|
|
|
0.0001,
|
|
|
|
lambda *args: self.view_flowlist()
|
|
|
|
)
|
|
|
|
|
2014-02-08 01:15:19 +00:00
|
|
|
try:
|
2015-03-13 10:58:22 +00:00
|
|
|
self.loop.run()
|
2014-02-08 01:15:19 +00:00
|
|
|
except Exception:
|
2015-03-13 10:58:22 +00:00
|
|
|
self.loop.stop()
|
2014-09-04 09:47:27 +00:00
|
|
|
sys.stdout.flush()
|
2014-02-08 01:15:19 +00:00
|
|
|
print >> sys.stderr, traceback.format_exc()
|
|
|
|
print >> sys.stderr, "mitmproxy has crashed!"
|
2015-03-13 11:07:35 +00:00
|
|
|
print >> sys.stderr, "Please lodge a bug report at:"
|
|
|
|
print >> sys.stderr, "\thttps://github.com/mitmproxy/mitmproxy"
|
2015-03-13 10:58:22 +00:00
|
|
|
print >> sys.stderr, "Shutting down..."
|
2012-02-07 03:39:37 +00:00
|
|
|
sys.stderr.flush()
|
|
|
|
self.shutdown()
|
|
|
|
|
2015-03-31 20:25:50 +00:00
|
|
|
def view_help(self, helpctx):
|
2015-04-17 01:06:45 +00:00
|
|
|
signals.push_view_state.send(
|
2015-03-13 10:58:22 +00:00
|
|
|
self,
|
2015-04-17 01:06:45 +00:00
|
|
|
window = window.Window(
|
|
|
|
self,
|
|
|
|
help.HelpView(helpctx),
|
|
|
|
None,
|
|
|
|
statusbar.StatusBar(self, help.footer),
|
|
|
|
None
|
|
|
|
)
|
2015-03-13 10:58:22 +00:00
|
|
|
)
|
2012-04-02 23:10:25 +00:00
|
|
|
|
2015-04-03 01:10:57 +00:00
|
|
|
def view_options(self):
|
2015-04-07 00:26:56 +00:00
|
|
|
for i in self.view_stack:
|
|
|
|
if isinstance(i["body"], options.Options):
|
|
|
|
return
|
2015-04-17 01:06:45 +00:00
|
|
|
signals.push_view_state.send(
|
2015-04-03 01:10:57 +00:00
|
|
|
self,
|
2015-04-17 01:06:45 +00:00
|
|
|
window = window.Window(
|
|
|
|
self,
|
|
|
|
options.Options(self),
|
|
|
|
None,
|
|
|
|
statusbar.StatusBar(self, options.footer),
|
|
|
|
options.help_context,
|
|
|
|
)
|
2015-04-03 01:10:57 +00:00
|
|
|
)
|
|
|
|
|
2015-04-06 20:42:40 +00:00
|
|
|
def view_palette_picker(self):
|
2015-04-17 01:06:45 +00:00
|
|
|
signals.push_view_state.send(
|
2015-04-06 20:42:40 +00:00
|
|
|
self,
|
2015-04-17 01:06:45 +00:00
|
|
|
window = window.Window(
|
|
|
|
self,
|
|
|
|
palettepicker.PalettePicker(self),
|
|
|
|
None,
|
|
|
|
statusbar.StatusBar(self, palettepicker.footer),
|
|
|
|
palettepicker.help_context,
|
|
|
|
)
|
2015-04-06 20:42:40 +00:00
|
|
|
)
|
|
|
|
|
2012-03-18 21:12:06 +00:00
|
|
|
def view_grideditor(self, ge):
|
2015-04-17 01:06:45 +00:00
|
|
|
signals.push_view_state.send(
|
2015-03-22 03:59:11 +00:00
|
|
|
self,
|
2015-04-17 01:06:45 +00:00
|
|
|
window = window.Window(
|
|
|
|
self,
|
|
|
|
ge,
|
|
|
|
None,
|
|
|
|
statusbar.StatusBar(self, grideditor.FOOTER),
|
|
|
|
ge.make_help()
|
|
|
|
)
|
2015-03-22 03:59:11 +00:00
|
|
|
)
|
2012-02-07 03:39:37 +00:00
|
|
|
|
2012-02-20 23:42:43 +00:00
|
|
|
def view_flowlist(self):
|
2012-02-07 03:39:37 +00:00
|
|
|
if self.ui.started:
|
|
|
|
self.ui.clear()
|
2012-08-02 07:06:34 +00:00
|
|
|
if self.state.follow_focus:
|
|
|
|
self.state.set_focus(self.state.flow_count())
|
|
|
|
|
2012-02-07 03:39:37 +00:00
|
|
|
if self.eventlog:
|
2015-03-22 03:59:11 +00:00
|
|
|
body = flowlist.BodyPile(self)
|
2012-02-07 03:39:37 +00:00
|
|
|
else:
|
2015-03-22 03:59:11 +00:00
|
|
|
body = flowlist.FlowListBox(self)
|
2012-02-08 08:47:39 +00:00
|
|
|
|
2016-01-22 21:11:03 +00:00
|
|
|
if self.follow:
|
|
|
|
self.toggle_follow_flows()
|
|
|
|
|
2015-04-17 01:06:45 +00:00
|
|
|
signals.push_view_state.send(
|
2015-03-22 03:59:11 +00:00
|
|
|
self,
|
2015-04-17 01:06:45 +00:00
|
|
|
window = window.Window(
|
|
|
|
self,
|
|
|
|
body,
|
|
|
|
None,
|
|
|
|
statusbar.StatusBar(self, flowlist.footer),
|
|
|
|
flowlist.help_context
|
|
|
|
)
|
2015-03-22 03:59:11 +00:00
|
|
|
)
|
2012-02-07 03:39:37 +00:00
|
|
|
|
2015-03-29 04:40:43 +00:00
|
|
|
def view_flow(self, flow, tab_offset=0):
|
2013-07-28 06:00:49 +00:00
|
|
|
self.state.set_focus_flow(flow)
|
2015-04-17 01:06:45 +00:00
|
|
|
signals.push_view_state.send(
|
2015-03-22 03:59:11 +00:00
|
|
|
self,
|
2015-04-17 01:06:45 +00:00
|
|
|
window = window.Window(
|
|
|
|
self,
|
|
|
|
flowview.FlowView(self, self.state, flow, tab_offset),
|
|
|
|
flowview.FlowViewHeader(self, flow),
|
|
|
|
statusbar.StatusBar(self, flowview.footer),
|
|
|
|
flowview.help_context
|
|
|
|
)
|
2015-03-22 03:59:11 +00:00
|
|
|
)
|
2012-02-07 03:39:37 +00:00
|
|
|
|
|
|
|
def _write_flows(self, path, flows):
|
|
|
|
if not path:
|
|
|
|
return
|
|
|
|
path = os.path.expanduser(path)
|
|
|
|
try:
|
|
|
|
f = file(path, "wb")
|
|
|
|
fw = flow.FlowWriter(f)
|
|
|
|
for i in flows:
|
|
|
|
fw.add(i)
|
|
|
|
f.close()
|
2015-05-30 00:03:28 +00:00
|
|
|
except IOError as v:
|
2015-03-20 22:19:20 +00:00
|
|
|
signals.status_message.send(message=v.strerror)
|
2012-02-07 03:39:37 +00:00
|
|
|
|
|
|
|
def save_one_flow(self, path, flow):
|
|
|
|
return self._write_flows(path, [flow])
|
|
|
|
|
|
|
|
def save_flows(self, path):
|
|
|
|
return self._write_flows(path, self.state.view)
|
2016-01-22 21:11:03 +00:00
|
|
|
|
2015-06-11 21:15:24 +00:00
|
|
|
def save_marked_flows(self, path):
|
|
|
|
marked_flows = []
|
|
|
|
for f in self.state.view:
|
2015-06-12 01:27:33 +00:00
|
|
|
if self.state.flow_marked(f):
|
2015-06-11 21:15:24 +00:00
|
|
|
marked_flows.append(f)
|
|
|
|
return self._write_flows(path, marked_flows)
|
2012-02-07 03:39:37 +00:00
|
|
|
|
|
|
|
def load_flows_callback(self, path):
|
|
|
|
if not path:
|
|
|
|
return
|
2015-01-02 00:26:22 +00:00
|
|
|
ret = self.load_flows_path(path)
|
2015-05-30 00:03:28 +00:00
|
|
|
return ret or "Flows loaded from %s" % path
|
2012-02-07 03:39:37 +00:00
|
|
|
|
2015-01-02 00:26:22 +00:00
|
|
|
def load_flows_path(self, path):
|
2012-07-24 03:15:41 +00:00
|
|
|
reterr = None
|
2012-02-08 23:09:40 +00:00
|
|
|
try:
|
2015-01-02 00:26:22 +00:00
|
|
|
flow.FlowMaster.load_flows_file(self, path)
|
2015-05-30 00:03:28 +00:00
|
|
|
except flow.FlowReadError as v:
|
2015-01-02 00:26:22 +00:00
|
|
|
reterr = str(v)
|
2015-04-07 03:59:38 +00:00
|
|
|
signals.flowlist_change.send(self)
|
2012-07-24 03:15:41 +00:00
|
|
|
return reterr
|
2012-02-07 03:39:37 +00:00
|
|
|
|
|
|
|
def accept_all(self):
|
2014-12-23 19:33:42 +00:00
|
|
|
self.state.accept_all(self)
|
2012-02-07 03:39:37 +00:00
|
|
|
|
|
|
|
def set_limit(self, txt):
|
2012-04-08 22:05:13 +00:00
|
|
|
v = self.state.set_limit(txt)
|
2015-04-07 03:59:38 +00:00
|
|
|
signals.flowlist_change.send(self)
|
2012-04-08 22:05:13 +00:00
|
|
|
return v
|
2012-02-07 03:39:37 +00:00
|
|
|
|
|
|
|
def set_intercept(self, txt):
|
|
|
|
return self.state.set_intercept(txt)
|
|
|
|
|
2012-04-01 22:49:57 +00:00
|
|
|
def change_default_display_mode(self, t):
|
2015-09-12 11:49:16 +00:00
|
|
|
v = contentviews.get_by_shortcut(t)
|
2012-04-01 22:49:57 +00:00
|
|
|
self.state.default_body_view = v
|
2013-07-28 06:00:49 +00:00
|
|
|
self.refresh_focus()
|
2012-04-01 22:49:57 +00:00
|
|
|
|
2014-09-07 13:57:36 +00:00
|
|
|
def edit_scripts(self, scripts):
|
|
|
|
commands = [x[0] for x in scripts] # remove outer array
|
|
|
|
if commands == [s.command for s in self.scripts]:
|
|
|
|
return
|
|
|
|
|
|
|
|
self.unload_scripts()
|
|
|
|
for command in commands:
|
|
|
|
self.load_script(command)
|
2015-04-06 05:11:02 +00:00
|
|
|
signals.update_settings.send(self)
|
2014-01-11 23:49:19 +00:00
|
|
|
|
2012-02-07 03:39:37 +00:00
|
|
|
def stop_client_playback_prompt(self, a):
|
|
|
|
if a != "n":
|
|
|
|
self.stop_client_playback()
|
|
|
|
|
|
|
|
def stop_server_playback_prompt(self, a):
|
|
|
|
if a != "n":
|
|
|
|
self.stop_server_playback()
|
|
|
|
|
|
|
|
def quit(self, a):
|
|
|
|
if a != "n":
|
2015-03-13 10:58:22 +00:00
|
|
|
raise urwid.ExitMainLoop
|
2012-02-07 03:39:37 +00:00
|
|
|
|
|
|
|
def shutdown(self):
|
|
|
|
self.state.killall(self)
|
2012-07-08 22:18:37 +00:00
|
|
|
flow.FlowMaster.shutdown(self)
|
2012-02-07 03:39:37 +00:00
|
|
|
|
2012-02-20 23:42:43 +00:00
|
|
|
def clear_flows(self):
|
2012-02-07 03:39:37 +00:00
|
|
|
self.state.clear()
|
2015-04-07 03:59:38 +00:00
|
|
|
signals.flowlist_change.send(self)
|
2012-02-07 03:39:37 +00:00
|
|
|
|
2012-08-02 06:57:56 +00:00
|
|
|
def toggle_follow_flows(self):
|
|
|
|
# toggle flow follow
|
|
|
|
self.state.follow_focus = not self.state.follow_focus
|
|
|
|
# jump to most recent flow if follow is now on
|
|
|
|
if self.state.follow_focus:
|
|
|
|
self.state.set_focus(self.state.flow_count())
|
2015-04-07 03:59:38 +00:00
|
|
|
signals.flowlist_change.send(self)
|
2012-08-02 06:57:56 +00:00
|
|
|
|
2012-02-20 23:42:43 +00:00
|
|
|
def delete_flow(self, f):
|
2012-02-07 03:39:37 +00:00
|
|
|
self.state.delete_flow(f)
|
2015-04-07 03:59:38 +00:00
|
|
|
signals.flowlist_change.send(self)
|
2012-02-07 03:39:37 +00:00
|
|
|
|
2013-07-28 06:00:49 +00:00
|
|
|
def refresh_focus(self):
|
|
|
|
if self.state.view:
|
2015-03-22 02:58:32 +00:00
|
|
|
signals.flow_change.send(
|
|
|
|
self,
|
|
|
|
flow = self.state.view[self.state.focus]
|
|
|
|
)
|
2012-02-07 03:39:37 +00:00
|
|
|
|
2014-09-03 14:57:56 +00:00
|
|
|
def process_flow(self, f):
|
2015-05-30 00:03:28 +00:00
|
|
|
if self.state.intercept and f.match(
|
|
|
|
self.state.intercept) and not f.request.is_replay:
|
2014-12-23 19:33:42 +00:00
|
|
|
f.intercept(self)
|
2012-02-07 03:39:37 +00:00
|
|
|
else:
|
2016-01-30 22:32:11 +00:00
|
|
|
#check if flow was intercepted within an inline script by flow.intercept()
|
2016-01-25 13:30:33 +00:00
|
|
|
if f.intercepted:
|
|
|
|
f.intercept(self)
|
|
|
|
else:
|
|
|
|
f.reply()
|
2015-04-07 03:59:38 +00:00
|
|
|
signals.flowlist_change.send(self)
|
2015-03-22 02:58:32 +00:00
|
|
|
signals.flow_change.send(self, flow = f)
|
2012-02-07 03:39:37 +00:00
|
|
|
|
|
|
|
def clear_events(self):
|
|
|
|
self.eventlist[:] = []
|
|
|
|
|
|
|
|
# Handlers
|
2014-09-03 14:57:56 +00:00
|
|
|
def handle_error(self, f):
|
|
|
|
f = flow.FlowMaster.handle_error(self, f)
|
2012-02-07 03:39:37 +00:00
|
|
|
if f:
|
2014-09-03 14:57:56 +00:00
|
|
|
self.process_flow(f)
|
2012-02-07 03:39:37 +00:00
|
|
|
return f
|
|
|
|
|
2014-09-03 14:57:56 +00:00
|
|
|
def handle_request(self, f):
|
|
|
|
f = flow.FlowMaster.handle_request(self, f)
|
2012-02-07 03:39:37 +00:00
|
|
|
if f:
|
2014-09-03 14:57:56 +00:00
|
|
|
self.process_flow(f)
|
2012-02-07 03:39:37 +00:00
|
|
|
return f
|
|
|
|
|
2014-09-03 14:57:56 +00:00
|
|
|
def handle_response(self, f):
|
|
|
|
f = flow.FlowMaster.handle_response(self, f)
|
2012-02-07 03:39:37 +00:00
|
|
|
if f:
|
2014-09-03 14:57:56 +00:00
|
|
|
self.process_flow(f)
|
2012-02-07 03:39:37 +00:00
|
|
|
return f
|
2015-11-13 13:08:39 +00:00
|
|
|
|
2015-11-14 04:57:02 +00:00
|
|
|
def handle_script_change(self, script):
|
|
|
|
if super(ConsoleMaster, self).handle_script_change(script):
|
|
|
|
signals.status_message.send(message='"{}" reloaded.'.format(script.filename))
|
|
|
|
else:
|
2016-01-27 09:12:18 +00:00
|
|
|
signals.status_message.send(message='Error reloading "{}".'.format(script.filename))
|