2014-03-10 21:36:47 +00:00
|
|
|
from __future__ import absolute_import
|
2015-03-13 11:07:35 +00:00
|
|
|
|
|
|
|
import glob
|
|
|
|
import mailcap
|
|
|
|
import mimetypes
|
|
|
|
import tempfile
|
|
|
|
import os
|
|
|
|
import os.path
|
|
|
|
import shlex
|
|
|
|
import stat
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
import traceback
|
2012-02-07 03:39:37 +00:00
|
|
|
import urwid
|
2015-03-13 11:07:35 +00:00
|
|
|
import weakref
|
|
|
|
|
|
|
|
from .. import controller, utils, flow, script
|
|
|
|
from . import flowlist, flowview, help, common
|
|
|
|
from . import grideditor, palettes, contentview, flowdetailview
|
2012-02-07 03:39:37 +00:00
|
|
|
|
|
|
|
EVENTLOG_SIZE = 500
|
|
|
|
|
|
|
|
|
|
|
|
class _PathCompleter:
|
|
|
|
def __init__(self, _testing=False):
|
|
|
|
"""
|
|
|
|
_testing: disables reloading of the lookup table to make testing possible.
|
|
|
|
"""
|
|
|
|
self.lookup, self.offset = None, None
|
|
|
|
self.final = None
|
|
|
|
self._testing = _testing
|
|
|
|
|
|
|
|
def reset(self):
|
|
|
|
self.lookup = None
|
|
|
|
self.offset = -1
|
|
|
|
|
|
|
|
def complete(self, txt):
|
|
|
|
"""
|
|
|
|
Returns the next completion for txt, or None if there is no completion.
|
|
|
|
"""
|
|
|
|
path = os.path.expanduser(txt)
|
|
|
|
if not self.lookup:
|
|
|
|
if not self._testing:
|
|
|
|
# Lookup is a set of (display value, actual value) tuples.
|
|
|
|
self.lookup = []
|
|
|
|
if os.path.isdir(path):
|
|
|
|
files = glob.glob(os.path.join(path, "*"))
|
|
|
|
prefix = txt
|
|
|
|
else:
|
|
|
|
files = glob.glob(path+"*")
|
|
|
|
prefix = os.path.dirname(txt)
|
|
|
|
prefix = prefix or "./"
|
|
|
|
for f in files:
|
|
|
|
display = os.path.join(prefix, os.path.basename(f))
|
|
|
|
if os.path.isdir(f):
|
|
|
|
display += "/"
|
|
|
|
self.lookup.append((display, f))
|
|
|
|
if not self.lookup:
|
|
|
|
self.final = path
|
|
|
|
return path
|
|
|
|
self.lookup.sort()
|
|
|
|
self.offset = -1
|
|
|
|
self.lookup.append((txt, txt))
|
|
|
|
self.offset += 1
|
|
|
|
if self.offset >= len(self.lookup):
|
|
|
|
self.offset = 0
|
|
|
|
ret = self.lookup[self.offset]
|
|
|
|
self.final = ret[1]
|
|
|
|
return ret[0]
|
|
|
|
|
2015-03-13 10:58:22 +00:00
|
|
|
|
2012-02-07 03:39:37 +00:00
|
|
|
class PathEdit(urwid.Edit, _PathCompleter):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
urwid.Edit.__init__(self, *args, **kwargs)
|
|
|
|
_PathCompleter.__init__(self)
|
|
|
|
|
|
|
|
def keypress(self, size, key):
|
|
|
|
if key == "tab":
|
|
|
|
comp = self.complete(self.get_edit_text())
|
|
|
|
self.set_edit_text(comp)
|
|
|
|
self.set_edit_pos(len(comp))
|
|
|
|
else:
|
|
|
|
self.reset()
|
|
|
|
return urwid.Edit.keypress(self, size, key)
|
|
|
|
|
|
|
|
|
2015-03-13 12:03:46 +00:00
|
|
|
class ActionBar(urwid.WidgetWrap):
|
2012-02-07 03:39:37 +00:00
|
|
|
def __init__(self):
|
|
|
|
self.message("")
|
|
|
|
|
|
|
|
def selectable(self):
|
|
|
|
return True
|
|
|
|
|
|
|
|
def path_prompt(self, prompt, text):
|
2012-08-31 01:41:08 +00:00
|
|
|
self.expire = None
|
2015-03-13 12:03:46 +00:00
|
|
|
self._w = PathEdit(prompt, text)
|
2012-02-07 03:39:37 +00:00
|
|
|
|
|
|
|
def prompt(self, prompt, text = ""):
|
2012-06-03 01:41:44 +00:00
|
|
|
self.expire = None
|
2012-04-04 22:21:56 +00:00
|
|
|
# A (partial) workaround for this Urwid issue:
|
|
|
|
# https://github.com/Nic0/tyrs/issues/115
|
|
|
|
# We can remove it once veryone is beyond 1.0.1
|
|
|
|
if isinstance(prompt, basestring):
|
|
|
|
prompt = unicode(prompt)
|
2015-03-13 12:03:46 +00:00
|
|
|
self._w = urwid.Edit(prompt, text or "")
|
2012-02-07 03:39:37 +00:00
|
|
|
|
2012-06-03 01:41:44 +00:00
|
|
|
def message(self, message, expire=None):
|
|
|
|
self.expire = expire
|
2015-03-13 12:03:46 +00:00
|
|
|
self._w = urwid.Text(message)
|
2012-02-07 03:39:37 +00:00
|
|
|
|
|
|
|
|
2015-03-13 12:03:46 +00:00
|
|
|
class StatusBar(urwid.WidgetWrap):
|
2012-02-07 03:39:37 +00:00
|
|
|
def __init__(self, master, helptext):
|
|
|
|
self.master, self.helptext = master, helptext
|
|
|
|
self.ab = ActionBar()
|
2015-03-13 12:03:46 +00:00
|
|
|
self.ib = urwid.WidgetWrap(urwid.Text(""))
|
|
|
|
self._w = urwid.Pile([self.ib, self.ab])
|
2012-02-07 03:39:37 +00:00
|
|
|
|
|
|
|
def get_status(self):
|
|
|
|
r = []
|
|
|
|
|
2012-08-18 11:39:52 +00:00
|
|
|
if self.master.setheaders.count():
|
|
|
|
r.append("[")
|
|
|
|
r.append(("heading_key", "H"))
|
|
|
|
r.append("eaders]")
|
2012-03-17 21:33:11 +00:00
|
|
|
if self.master.replacehooks.count():
|
|
|
|
r.append("[")
|
|
|
|
r.append(("heading_key", "R"))
|
2012-03-18 21:12:06 +00:00
|
|
|
r.append("eplacing]")
|
2012-02-07 03:39:37 +00:00
|
|
|
if self.master.client_playback:
|
|
|
|
r.append("[")
|
2012-02-18 05:48:08 +00:00
|
|
|
r.append(("heading_key", "cplayback"))
|
2012-02-07 03:39:37 +00:00
|
|
|
r.append(":%s to go]"%self.master.client_playback.count())
|
|
|
|
if self.master.server_playback:
|
|
|
|
r.append("[")
|
2012-02-18 05:48:08 +00:00
|
|
|
r.append(("heading_key", "splayback"))
|
2012-03-05 09:05:11 +00:00
|
|
|
if self.master.nopop:
|
|
|
|
r.append(":%s in file]"%self.master.server_playback.count())
|
|
|
|
else:
|
|
|
|
r.append(":%s to go]"%self.master.server_playback.count())
|
2014-10-18 16:29:35 +00:00
|
|
|
if self.master.get_ignore_filter():
|
2014-09-08 10:20:40 +00:00
|
|
|
r.append("[")
|
|
|
|
r.append(("heading_key", "I"))
|
2014-10-18 16:29:35 +00:00
|
|
|
r.append("gnore:%d]" % len(self.master.get_ignore_filter()))
|
|
|
|
if self.master.get_tcp_filter():
|
|
|
|
r.append("[")
|
|
|
|
r.append(("heading_key", "T"))
|
|
|
|
r.append("CP:%d]" % len(self.master.get_tcp_filter()))
|
2012-02-07 03:39:37 +00:00
|
|
|
if self.master.state.intercept_txt:
|
|
|
|
r.append("[")
|
2012-02-18 05:48:08 +00:00
|
|
|
r.append(("heading_key", "i"))
|
2012-02-07 03:39:37 +00:00
|
|
|
r.append(":%s]"%self.master.state.intercept_txt)
|
|
|
|
if self.master.state.limit_txt:
|
|
|
|
r.append("[")
|
2012-02-18 05:48:08 +00:00
|
|
|
r.append(("heading_key", "l"))
|
2012-02-07 03:39:37 +00:00
|
|
|
r.append(":%s]"%self.master.state.limit_txt)
|
|
|
|
if self.master.stickycookie_txt:
|
|
|
|
r.append("[")
|
2012-02-18 05:48:08 +00:00
|
|
|
r.append(("heading_key", "t"))
|
2012-02-07 03:39:37 +00:00
|
|
|
r.append(":%s]"%self.master.stickycookie_txt)
|
|
|
|
if self.master.stickyauth_txt:
|
|
|
|
r.append("[")
|
2012-02-18 05:48:08 +00:00
|
|
|
r.append(("heading_key", "u"))
|
2012-02-07 03:39:37 +00:00
|
|
|
r.append(":%s]"%self.master.stickyauth_txt)
|
2012-08-18 05:08:17 +00:00
|
|
|
if self.master.state.default_body_view.name != "Auto":
|
2012-04-01 22:49:57 +00:00
|
|
|
r.append("[")
|
|
|
|
r.append(("heading_key", "M"))
|
2012-08-18 05:08:17 +00:00
|
|
|
r.append(":%s]"%self.master.state.default_body_view.name)
|
2012-02-07 03:39:37 +00:00
|
|
|
|
|
|
|
opts = []
|
|
|
|
if self.master.anticache:
|
|
|
|
opts.append("anticache")
|
|
|
|
if self.master.anticomp:
|
|
|
|
opts.append("anticomp")
|
2013-03-17 04:31:35 +00:00
|
|
|
if self.master.showhost:
|
|
|
|
opts.append("showhost")
|
2012-02-07 03:39:37 +00:00
|
|
|
if not self.master.refresh_server_playback:
|
|
|
|
opts.append("norefresh")
|
|
|
|
if self.master.killextra:
|
|
|
|
opts.append("killextra")
|
2012-07-03 10:56:25 +00:00
|
|
|
if self.master.server.config.no_upstream_cert:
|
|
|
|
opts.append("no-upstream-cert")
|
2012-08-02 06:57:56 +00:00
|
|
|
if self.master.state.follow_focus:
|
|
|
|
opts.append("following")
|
2014-07-25 16:55:01 +00:00
|
|
|
if self.master.stream_large_bodies:
|
|
|
|
opts.append("stream:%s" % utils.pretty_size(self.master.stream_large_bodies.max_size))
|
2012-02-07 03:39:37 +00:00
|
|
|
|
|
|
|
if opts:
|
|
|
|
r.append("[%s]"%(":".join(opts)))
|
|
|
|
|
2014-09-08 12:32:42 +00:00
|
|
|
if self.master.server.config.mode in ["reverse", "upstream"]:
|
|
|
|
dst = self.master.server.config.mode.dst
|
2014-03-18 18:38:02 +00:00
|
|
|
scheme = "https" if dst[0] else "http"
|
|
|
|
if dst[1] != dst[0]:
|
|
|
|
scheme += "2https" if dst[1] else "http"
|
2014-09-08 12:32:42 +00:00
|
|
|
r.append("[dest:%s]"%utils.unparse_url(scheme, *dst[2:]))
|
2013-12-09 09:34:47 +00:00
|
|
|
if self.master.scripts:
|
2014-09-07 13:57:36 +00:00
|
|
|
r.append("[")
|
|
|
|
r.append(("heading_key", "s"))
|
|
|
|
r.append("cripts:%s]"%len(self.master.scripts))
|
2014-03-13 22:47:02 +00:00
|
|
|
# r.append("[lt:%0.3f]"%self.master.looptime)
|
2012-02-07 03:39:37 +00:00
|
|
|
|
2012-07-06 02:41:10 +00:00
|
|
|
if self.master.stream:
|
|
|
|
r.append("[W:%s]"%self.master.stream_path)
|
|
|
|
|
2012-02-07 03:39:37 +00:00
|
|
|
return r
|
|
|
|
|
|
|
|
def redraw(self):
|
2012-06-03 01:41:44 +00:00
|
|
|
if self.ab.expire and time.time() > self.ab.expire:
|
2012-02-07 03:39:37 +00:00
|
|
|
self.message("")
|
|
|
|
|
2012-04-08 21:48:39 +00:00
|
|
|
fc = self.master.state.flow_count()
|
2013-07-28 10:40:51 +00:00
|
|
|
if self.master.state.focus is None:
|
|
|
|
offset = 0
|
2012-04-08 21:48:39 +00:00
|
|
|
else:
|
2013-07-28 10:40:51 +00:00
|
|
|
offset = min(self.master.state.focus + 1, fc)
|
2013-07-28 06:00:49 +00:00
|
|
|
t = [
|
|
|
|
('heading', ("[%s/%s]"%(offset, fc)).ljust(9))
|
|
|
|
]
|
2012-04-08 21:48:39 +00:00
|
|
|
|
2012-04-02 01:24:51 +00:00
|
|
|
if self.master.server.bound:
|
2014-09-03 10:24:14 +00:00
|
|
|
host = self.master.server.address.host
|
|
|
|
if host == "0.0.0.0":
|
|
|
|
host = "*"
|
|
|
|
boundaddr = "[%s:%s]"%(host, self.master.server.address.port)
|
2012-02-07 03:39:37 +00:00
|
|
|
else:
|
|
|
|
boundaddr = ""
|
2012-02-19 21:15:58 +00:00
|
|
|
t.extend(self.get_status())
|
2012-02-07 03:39:37 +00:00
|
|
|
status = urwid.AttrWrap(urwid.Columns([
|
|
|
|
urwid.Text(t),
|
|
|
|
urwid.Text(
|
|
|
|
[
|
|
|
|
self.helptext,
|
2012-02-19 21:15:58 +00:00
|
|
|
boundaddr
|
2012-02-07 03:39:37 +00:00
|
|
|
],
|
|
|
|
align="right"
|
|
|
|
),
|
2012-02-18 05:48:08 +00:00
|
|
|
]), "heading")
|
2015-03-13 12:03:46 +00:00
|
|
|
self.ib._w = status
|
2012-02-07 03:39:37 +00:00
|
|
|
|
|
|
|
def update(self, text):
|
|
|
|
self.helptext = text
|
|
|
|
self.redraw()
|
2015-03-13 10:58:22 +00:00
|
|
|
self.master.loop.draw_screen()
|
2012-02-07 03:39:37 +00:00
|
|
|
|
|
|
|
def selectable(self):
|
|
|
|
return True
|
|
|
|
|
|
|
|
def get_edit_text(self):
|
|
|
|
return self.ab.w.get_edit_text()
|
|
|
|
|
|
|
|
def path_prompt(self, prompt, text):
|
|
|
|
return self.ab.path_prompt(prompt, text)
|
|
|
|
|
|
|
|
def prompt(self, prompt, text = ""):
|
|
|
|
self.ab.prompt(prompt, text)
|
|
|
|
|
|
|
|
def message(self, msg, expire=None):
|
|
|
|
if expire:
|
2012-06-03 01:41:44 +00:00
|
|
|
expire = time.time() + float(expire)/1000
|
|
|
|
self.ab.message(msg, expire)
|
2015-03-13 10:58:22 +00:00
|
|
|
self.master.loop.draw_screen()
|
2012-02-07 03:39:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ConsoleState(flow.State):
|
|
|
|
def __init__(self):
|
|
|
|
flow.State.__init__(self)
|
|
|
|
self.focus = None
|
2012-08-02 06:57:56 +00:00
|
|
|
self.follow_focus = None
|
2012-08-18 05:29:29 +00:00
|
|
|
self.default_body_view = contentview.get("Auto")
|
2013-07-28 06:00:49 +00:00
|
|
|
|
|
|
|
self.view_mode = common.VIEW_LIST
|
2012-02-07 03:39:37 +00:00
|
|
|
self.view_flow_mode = common.VIEW_FLOW_REQUEST
|
2013-07-28 06:00:49 +00:00
|
|
|
|
2012-02-07 03:39:37 +00:00
|
|
|
self.last_script = ""
|
|
|
|
self.last_saveload = ""
|
2012-04-01 21:30:38 +00:00
|
|
|
self.flowsettings = weakref.WeakKeyDictionary()
|
|
|
|
|
|
|
|
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)
|
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
|
|
|
|
|
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):
|
|
|
|
return self.get_from_pos(pos+1)
|
|
|
|
|
|
|
|
def get_prev(self, pos):
|
|
|
|
return self.get_from_pos(pos-1)
|
|
|
|
|
|
|
|
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):
|
|
|
|
self.focus = None
|
|
|
|
super(ConsoleState, self).clear()
|
|
|
|
|
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",
|
|
|
|
"keepserving",
|
|
|
|
"kill",
|
|
|
|
"intercept",
|
|
|
|
"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",
|
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())
|
|
|
|
self.looptime = 0
|
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-04-08 22:05:13 +00:00
|
|
|
self.flow_list_walker = None
|
2012-06-30 05:37:38 +00:00
|
|
|
self.set_palette(options.palette)
|
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)
|
|
|
|
|
|
|
|
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
|
2012-02-07 03:39:37 +00:00
|
|
|
|
|
|
|
self.eventlog = options.eventlog
|
|
|
|
self.eventlist = urwid.SimpleListWalker([])
|
|
|
|
|
2015-02-05 13:15:26 +00:00
|
|
|
self.statusbar = None
|
|
|
|
|
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)
|
|
|
|
|
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)
|
2013-07-23 22:32:56 +00:00
|
|
|
|
2014-12-29 13:40:34 +00:00
|
|
|
def start_stream_to_path(self, path, mode="wb"):
|
2012-07-06 02:41:10 +00:00
|
|
|
path = os.path.expanduser(path)
|
|
|
|
try:
|
2014-12-29 13:40:34 +00:00
|
|
|
f = file(path, mode)
|
|
|
|
self.start_stream(f, None)
|
2012-07-06 02:41:10 +00:00
|
|
|
except IOError, v:
|
|
|
|
return str(v)
|
|
|
|
self.stream_path = path
|
|
|
|
|
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:
|
2014-03-13 00:04:45 +00:00
|
|
|
self.add_event("Method %s return: %s"%(method, val), "debug")
|
2012-10-10 22:12:06 +00:00
|
|
|
else:
|
2014-03-13 00:04:45 +00:00
|
|
|
self.add_event("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
|
2014-03-13 00:04:45 +00:00
|
|
|
self.add_event("Running script on flow: %s"%command, "debug")
|
2014-01-12 00:59:32 +00:00
|
|
|
|
|
|
|
try:
|
2014-01-12 10:01:59 +00:00
|
|
|
s = script.Script(command, self)
|
2014-01-12 00:59:32 +00:00
|
|
|
except script.ScriptError, v:
|
2012-10-10 22:12:06 +00:00
|
|
|
self.statusbar.message("Error loading script.")
|
2014-03-13 00:04:45 +00:00
|
|
|
self.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()
|
2012-02-20 23:42:43 +00:00
|
|
|
self.refresh_flow(f)
|
2014-01-12 10:01:59 +00:00
|
|
|
self.state.last_script = command
|
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:
|
|
|
|
self.statusbar.message(ret)
|
2014-01-12 10:01:59 +00:00
|
|
|
self.state.last_script = command
|
2012-02-07 03:39:37 +00:00
|
|
|
|
|
|
|
def toggle_eventlog(self):
|
|
|
|
self.eventlog = not self.eventlog
|
2012-02-20 23:42:43 +00:00
|
|
|
self.view_flowlist()
|
2012-02-07 03:39:37 +00:00
|
|
|
|
2015-01-06 18:49:49 +00:00
|
|
|
def _readflow(self, paths):
|
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-02-05 13:44:45 +00:00
|
|
|
return flow.read_flows_from_paths(paths)
|
|
|
|
except flow.FlowReadError as e:
|
2015-01-08 18:56:19 +00:00
|
|
|
if not self.statusbar:
|
2015-02-05 13:44:45 +00:00
|
|
|
print >> sys.stderr, e.strerror
|
2015-01-08 18:56:19 +00:00
|
|
|
sys.exit(1)
|
|
|
|
else:
|
2015-02-05 13:44:45 +00:00
|
|
|
self.statusbar.message(e.strerror)
|
|
|
|
return None
|
|
|
|
|
|
|
|
def client_playback_path(self, path):
|
|
|
|
flows = self._readflow(path)
|
|
|
|
if flows:
|
|
|
|
self.start_client_playback(flows, False)
|
2012-02-07 03:39:37 +00:00
|
|
|
|
|
|
|
def server_playback_path(self, path):
|
2015-02-05 13:44:45 +00:00
|
|
|
flows = self._readflow(path)
|
|
|
|
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:
|
2012-12-23 00:26:15 +00:00
|
|
|
self.statusbar.message("Can't start editor: %s" % " ".join(c))
|
|
|
|
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-13 11:07:35 +00:00
|
|
|
self.statusbar.message(
|
|
|
|
"Can't start external viewer: %s" % " ".join(c)
|
|
|
|
)
|
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):
|
|
|
|
self.palette = palettes.palettes[name]
|
2012-02-07 03:39:37 +00:00
|
|
|
|
2015-03-13 10:58:22 +00:00
|
|
|
def input_filter(self, keys, raw):
|
|
|
|
for k in keys:
|
|
|
|
if self.prompting:
|
|
|
|
if k == "esc":
|
|
|
|
self.prompt_cancel()
|
|
|
|
elif self.onekey:
|
|
|
|
if k == "enter":
|
|
|
|
self.prompt_cancel()
|
|
|
|
elif k in self.onekey:
|
|
|
|
self.prompt_execute(k)
|
|
|
|
elif k == "enter":
|
|
|
|
self.prompt_execute()
|
|
|
|
else:
|
|
|
|
self.view.keypress(self.loop.screen_size, k)
|
|
|
|
else:
|
|
|
|
k = self.view.keypress(self.loop.screen_size, k)
|
|
|
|
if k:
|
|
|
|
self.statusbar.message("")
|
|
|
|
if k == "?":
|
|
|
|
self.view_help()
|
|
|
|
elif k == "c":
|
|
|
|
if not self.client_playback:
|
|
|
|
self.path_prompt(
|
|
|
|
"Client replay: ",
|
|
|
|
self.state.last_saveload,
|
|
|
|
self.client_playback_path
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
self.prompt_onekey(
|
|
|
|
"Stop current client replay?",
|
|
|
|
(
|
|
|
|
("yes", "y"),
|
|
|
|
("no", "n"),
|
|
|
|
),
|
|
|
|
self.stop_client_playback_prompt,
|
|
|
|
)
|
|
|
|
elif k == "H":
|
|
|
|
self.view_grideditor(
|
|
|
|
grideditor.SetHeadersEditor(
|
|
|
|
self,
|
|
|
|
self.setheaders.get_specs(),
|
|
|
|
self.setheaders.set
|
|
|
|
)
|
|
|
|
)
|
|
|
|
elif k == "I":
|
|
|
|
self.view_grideditor(
|
|
|
|
grideditor.HostPatternEditor(
|
|
|
|
self,
|
|
|
|
[[x] for x in self.get_ignore_filter()],
|
|
|
|
self.edit_ignore_filter
|
|
|
|
)
|
|
|
|
)
|
|
|
|
elif k == "T":
|
|
|
|
self.view_grideditor(
|
|
|
|
grideditor.HostPatternEditor(
|
|
|
|
self,
|
|
|
|
[[x] for x in self.get_tcp_filter()],
|
|
|
|
self.edit_tcp_filter
|
|
|
|
)
|
|
|
|
)
|
|
|
|
elif k == "i":
|
|
|
|
self.prompt(
|
|
|
|
"Intercept filter: ",
|
|
|
|
self.state.intercept_txt,
|
|
|
|
self.set_intercept
|
|
|
|
)
|
|
|
|
elif k == "Q":
|
|
|
|
raise urwid.ExitMainLoop
|
|
|
|
elif k == "q":
|
|
|
|
self.prompt_onekey(
|
|
|
|
"Quit",
|
|
|
|
(
|
|
|
|
("yes", "y"),
|
|
|
|
("no", "n"),
|
|
|
|
),
|
|
|
|
self.quit,
|
|
|
|
)
|
|
|
|
elif k == "M":
|
|
|
|
self.prompt_onekey(
|
|
|
|
"Global default display mode",
|
|
|
|
contentview.view_prompts,
|
|
|
|
self.change_default_display_mode
|
|
|
|
)
|
|
|
|
elif k == "R":
|
|
|
|
self.view_grideditor(
|
|
|
|
grideditor.ReplaceEditor(
|
|
|
|
self,
|
|
|
|
self.replacehooks.get_specs(),
|
|
|
|
self.replacehooks.set
|
|
|
|
)
|
|
|
|
)
|
|
|
|
elif k == "s":
|
|
|
|
self.view_grideditor(
|
|
|
|
grideditor.ScriptEditor(
|
|
|
|
self,
|
|
|
|
[[i.command] for i in self.scripts],
|
|
|
|
self.edit_scripts
|
|
|
|
)
|
|
|
|
)
|
|
|
|
#if self.scripts:
|
|
|
|
# self.load_script(None)
|
|
|
|
#else:
|
|
|
|
# self.path_prompt(
|
|
|
|
# "Set script: ",
|
|
|
|
# self.state.last_script,
|
|
|
|
# self.set_script
|
|
|
|
# )
|
|
|
|
elif k == "S":
|
|
|
|
if not self.server_playback:
|
|
|
|
self.path_prompt(
|
|
|
|
"Server replay path: ",
|
|
|
|
self.state.last_saveload,
|
|
|
|
self.server_playback_path
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
self.prompt_onekey(
|
|
|
|
"Stop current server replay?",
|
|
|
|
(
|
|
|
|
("yes", "y"),
|
|
|
|
("no", "n"),
|
|
|
|
),
|
|
|
|
self.stop_server_playback_prompt,
|
|
|
|
)
|
|
|
|
elif k == "o":
|
|
|
|
self.prompt_onekey(
|
|
|
|
"Options",
|
|
|
|
(
|
|
|
|
("anticache", "a"),
|
|
|
|
("anticomp", "c"),
|
|
|
|
("showhost", "h"),
|
|
|
|
("killextra", "k"),
|
|
|
|
("norefresh", "n"),
|
|
|
|
("no-upstream-certs", "u"),
|
|
|
|
),
|
|
|
|
self._change_options
|
|
|
|
)
|
|
|
|
elif k == "t":
|
|
|
|
self.prompt(
|
|
|
|
"Sticky cookie filter: ",
|
|
|
|
self.stickycookie_txt,
|
|
|
|
self.set_stickycookie
|
|
|
|
)
|
|
|
|
elif k == "u":
|
|
|
|
self.prompt(
|
|
|
|
"Sticky auth filter: ",
|
|
|
|
self.stickyauth_txt,
|
|
|
|
self.set_stickyauth
|
|
|
|
)
|
|
|
|
self.statusbar.redraw()
|
|
|
|
|
|
|
|
def ticker(self, *userdata):
|
|
|
|
changed = self.tick(self.masterq, timeout=0)
|
|
|
|
if changed:
|
|
|
|
self.loop.draw_screen()
|
|
|
|
self.statusbar.redraw()
|
|
|
|
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)
|
2012-02-07 03:39:37 +00:00
|
|
|
self.ui.register_palette(self.palette)
|
2012-04-08 22:05:13 +00:00
|
|
|
self.flow_list_walker = flowlist.FlowListWalker(self, self.state)
|
2012-02-07 03:39:37 +00:00
|
|
|
self.view = None
|
|
|
|
self.statusbar = None
|
|
|
|
self.header = None
|
|
|
|
self.body = None
|
2012-02-08 08:47:39 +00:00
|
|
|
self.help_context = None
|
2012-02-07 03:39:37 +00:00
|
|
|
self.prompting = False
|
|
|
|
self.onekey = False
|
2015-03-13 10:58:22 +00:00
|
|
|
self.loop = urwid.MainLoop(
|
|
|
|
self.view,
|
|
|
|
screen = self.ui,
|
2015-03-13 11:29:21 +00:00
|
|
|
input_filter = self.input_filter
|
2015-03-13 10:58:22 +00:00
|
|
|
)
|
2012-02-20 23:42:43 +00:00
|
|
|
self.view_flowlist()
|
2015-03-13 10:58:22 +00:00
|
|
|
self.statusbar.redraw()
|
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-01-02 00:26:22 +00:00
|
|
|
self.add_event(
|
|
|
|
"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)
|
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()
|
|
|
|
|
|
|
|
def make_view(self):
|
|
|
|
self.view = urwid.Frame(
|
2015-03-13 10:58:22 +00:00
|
|
|
self.body,
|
|
|
|
header = self.header,
|
|
|
|
footer = self.statusbar
|
|
|
|
)
|
2012-02-07 03:39:37 +00:00
|
|
|
self.view.set_focus("body")
|
2015-03-13 10:58:22 +00:00
|
|
|
return self.view
|
2012-02-07 03:39:37 +00:00
|
|
|
|
|
|
|
def view_help(self):
|
2015-03-13 10:58:22 +00:00
|
|
|
h = help.HelpView(
|
|
|
|
self,
|
|
|
|
self.help_context,
|
|
|
|
(self.statusbar, self.body, self.header)
|
|
|
|
)
|
2012-03-22 01:57:57 +00:00
|
|
|
self.statusbar = StatusBar(self, help.footer)
|
2012-02-08 08:47:39 +00:00
|
|
|
self.body = h
|
2012-02-07 03:39:37 +00:00
|
|
|
self.header = None
|
2015-03-13 10:58:22 +00:00
|
|
|
self.loop.widget = self.make_view()
|
2012-04-02 23:10:25 +00:00
|
|
|
|
|
|
|
def view_flowdetails(self, flow):
|
2015-03-13 10:58:22 +00:00
|
|
|
h = flowdetailview.FlowDetailsView(
|
|
|
|
self,
|
|
|
|
flow,
|
|
|
|
(self.statusbar, self.body, self.header)
|
|
|
|
)
|
2012-04-02 23:10:25 +00:00
|
|
|
self.statusbar = StatusBar(self, flowdetailview.footer)
|
|
|
|
self.body = h
|
|
|
|
self.header = None
|
2015-03-13 10:58:22 +00:00
|
|
|
self.loop.widget = self.make_view()
|
2012-02-07 03:39:37 +00:00
|
|
|
|
2012-03-18 21:12:06 +00:00
|
|
|
def view_grideditor(self, ge):
|
|
|
|
self.body = ge
|
2012-02-07 03:39:37 +00:00
|
|
|
self.header = None
|
2012-08-31 01:28:04 +00:00
|
|
|
self.help_context = ge.make_help()
|
2012-03-22 01:57:57 +00:00
|
|
|
self.statusbar = StatusBar(self, grideditor.footer)
|
2015-03-13 10:58:22 +00:00
|
|
|
self.loop.widget = self.make_view()
|
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:
|
2012-02-20 23:42:43 +00:00
|
|
|
self.body = flowlist.BodyPile(self)
|
2012-02-07 03:39:37 +00:00
|
|
|
else:
|
2012-04-08 22:05:13 +00:00
|
|
|
self.body = flowlist.FlowListBox(self)
|
2012-03-22 01:57:57 +00:00
|
|
|
self.statusbar = StatusBar(self, flowlist.footer)
|
2012-02-07 03:39:37 +00:00
|
|
|
self.header = None
|
2013-07-28 06:00:49 +00:00
|
|
|
self.state.view_mode = common.VIEW_LIST
|
2012-02-08 08:47:39 +00:00
|
|
|
|
2015-03-13 10:58:22 +00:00
|
|
|
self.loop.widget = self.make_view()
|
2012-02-20 23:42:43 +00:00
|
|
|
self.help_context = flowlist.help_context
|
2012-02-07 03:39:37 +00:00
|
|
|
|
|
|
|
def view_flow(self, flow):
|
2012-03-19 22:11:53 +00:00
|
|
|
self.body = flowview.FlowView(self, self.state, flow)
|
|
|
|
self.header = flowview.FlowViewHeader(self, flow)
|
2012-03-22 01:57:57 +00:00
|
|
|
self.statusbar = StatusBar(self, flowview.footer)
|
2013-07-28 06:00:49 +00:00
|
|
|
self.state.set_focus_flow(flow)
|
|
|
|
self.state.view_mode = common.VIEW_FLOW
|
2015-03-13 10:58:22 +00:00
|
|
|
self.loop.widget = self.make_view()
|
2012-02-20 23:42:43 +00:00
|
|
|
self.help_context = flowview.help_context
|
2012-02-07 03:39:37 +00:00
|
|
|
|
|
|
|
def _write_flows(self, path, flows):
|
|
|
|
self.state.last_saveload = path
|
|
|
|
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()
|
|
|
|
except IOError, v:
|
|
|
|
self.statusbar.message(v.strerror)
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
def load_flows_callback(self, path):
|
|
|
|
if not path:
|
|
|
|
return
|
2015-01-02 00:26:22 +00:00
|
|
|
ret = self.load_flows_path(path)
|
2012-02-07 03:39:37 +00:00
|
|
|
return ret or "Flows loaded from %s"%path
|
|
|
|
|
2015-01-02 00:26:22 +00:00
|
|
|
def load_flows_path(self, path):
|
2012-02-07 03:39:37 +00:00
|
|
|
self.state.last_saveload = 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)
|
2012-02-08 23:09:40 +00:00
|
|
|
except flow.FlowReadError, v:
|
2015-01-02 00:26:22 +00:00
|
|
|
reterr = str(v)
|
2012-04-08 22:05:13 +00:00
|
|
|
if self.flow_list_walker:
|
2012-02-07 03:39:37 +00:00
|
|
|
self.sync_list_view()
|
2012-07-24 03:15:41 +00:00
|
|
|
return reterr
|
2012-02-07 03:39:37 +00:00
|
|
|
|
|
|
|
def path_prompt(self, prompt, text, callback, *args):
|
|
|
|
self.statusbar.path_prompt(prompt, text)
|
|
|
|
self.view.set_focus("footer")
|
|
|
|
self.prompting = (callback, args)
|
|
|
|
|
|
|
|
def prompt(self, prompt, text, callback, *args):
|
|
|
|
self.statusbar.prompt(prompt, text)
|
|
|
|
self.view.set_focus("footer")
|
|
|
|
self.prompting = (callback, args)
|
|
|
|
|
|
|
|
def prompt_edit(self, prompt, text, callback):
|
|
|
|
self.statusbar.prompt(prompt + ": ", text)
|
|
|
|
self.view.set_focus("footer")
|
|
|
|
self.prompting = (callback, [])
|
|
|
|
|
|
|
|
def prompt_onekey(self, prompt, keys, callback, *args):
|
|
|
|
"""
|
|
|
|
Keys are a set of (word, key) tuples. The appropriate key in the
|
|
|
|
word is highlighted.
|
|
|
|
"""
|
|
|
|
prompt = [prompt, " ("]
|
|
|
|
mkup = []
|
|
|
|
for i, e in enumerate(keys):
|
|
|
|
mkup.extend(common.highlight_key(e[0], e[1]))
|
|
|
|
if i < len(keys)-1:
|
|
|
|
mkup.append(",")
|
|
|
|
prompt.extend(mkup)
|
|
|
|
prompt.append(")? ")
|
|
|
|
self.onekey = "".join(i[1] for i in keys)
|
|
|
|
self.prompt(prompt, "", callback, *args)
|
|
|
|
|
|
|
|
def prompt_done(self):
|
|
|
|
self.prompting = False
|
|
|
|
self.onekey = False
|
|
|
|
self.view.set_focus("body")
|
|
|
|
self.statusbar.message("")
|
|
|
|
|
|
|
|
def prompt_execute(self, txt=None):
|
|
|
|
if not txt:
|
|
|
|
txt = self.statusbar.get_edit_text()
|
|
|
|
p, args = self.prompting
|
|
|
|
self.prompt_done()
|
|
|
|
msg = p(txt, *args)
|
|
|
|
if msg:
|
|
|
|
self.statusbar.message(msg, 1000)
|
|
|
|
|
|
|
|
def prompt_cancel(self):
|
|
|
|
self.prompt_done()
|
|
|
|
|
|
|
|
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)
|
|
|
|
self.sync_list_view()
|
|
|
|
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):
|
2012-08-18 05:08:17 +00:00
|
|
|
v = contentview.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
|
|
|
|
2012-02-07 03:39:37 +00:00
|
|
|
def pop_view(self):
|
2013-07-28 06:00:49 +00:00
|
|
|
if self.state.view_mode == common.VIEW_FLOW:
|
|
|
|
self.view_flow(self.state.view[self.state.focus])
|
2012-02-07 03:39:37 +00:00
|
|
|
else:
|
2012-02-20 23:42:43 +00:00
|
|
|
self.view_flowlist()
|
2012-02-07 03:39:37 +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)
|
2014-01-11 23:49:19 +00:00
|
|
|
|
2014-10-18 16:29:35 +00:00
|
|
|
def edit_ignore_filter(self, ignore):
|
2014-09-08 10:20:40 +00:00
|
|
|
patterns = (x[0] for x in ignore)
|
2014-10-18 16:29:35 +00:00
|
|
|
self.set_ignore_filter(patterns)
|
|
|
|
|
|
|
|
def edit_tcp_filter(self, tcp):
|
|
|
|
patterns = (x[0] for x in tcp)
|
|
|
|
self.set_tcp_filter(patterns)
|
2014-09-08 10:20:40 +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 _change_options(self, a):
|
|
|
|
if a == "a":
|
|
|
|
self.anticache = not self.anticache
|
|
|
|
if a == "c":
|
|
|
|
self.anticomp = not self.anticomp
|
2013-03-17 04:31:35 +00:00
|
|
|
if a == "h":
|
|
|
|
self.showhost = not self.showhost
|
|
|
|
self.sync_list_view()
|
2013-07-28 06:00:49 +00:00
|
|
|
self.refresh_focus()
|
2012-02-07 03:39:37 +00:00
|
|
|
elif a == "k":
|
|
|
|
self.killextra = not self.killextra
|
|
|
|
elif a == "n":
|
|
|
|
self.refresh_server_playback = not self.refresh_server_playback
|
2012-04-03 23:24:58 +00:00
|
|
|
elif a == "u":
|
2015-03-13 11:07:35 +00:00
|
|
|
self.server.config.no_upstream_cert =\
|
|
|
|
not self.server.config.no_upstream_cert
|
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
|
|
|
|
|
|
|
def sync_list_view(self):
|
2012-04-08 22:05:13 +00:00
|
|
|
self.flow_list_walker._modified()
|
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()
|
|
|
|
self.sync_list_view()
|
|
|
|
|
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())
|
|
|
|
self.sync_list_view()
|
|
|
|
|
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)
|
|
|
|
self.sync_list_view()
|
|
|
|
|
2013-07-28 06:00:49 +00:00
|
|
|
def refresh_focus(self):
|
|
|
|
if self.state.view:
|
|
|
|
self.refresh_flow(self.state.view[self.state.focus])
|
|
|
|
|
2012-02-20 23:42:43 +00:00
|
|
|
def refresh_flow(self, c):
|
|
|
|
if hasattr(self.header, "refresh_flow"):
|
|
|
|
self.header.refresh_flow(c)
|
|
|
|
if hasattr(self.body, "refresh_flow"):
|
|
|
|
self.body.refresh_flow(c)
|
|
|
|
if hasattr(self.statusbar, "refresh_flow"):
|
|
|
|
self.statusbar.refresh_flow(c)
|
2012-02-07 03:39:37 +00:00
|
|
|
|
2014-09-03 14:57:56 +00:00
|
|
|
def process_flow(self, f):
|
2014-02-05 13:33:17 +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:
|
2014-09-03 14:57:56 +00:00
|
|
|
f.reply()
|
2012-02-07 03:39:37 +00:00
|
|
|
self.sync_list_view()
|
2012-02-20 23:42:43 +00:00
|
|
|
self.refresh_flow(f)
|
2012-02-07 03:39:37 +00:00
|
|
|
|
|
|
|
def clear_events(self):
|
|
|
|
self.eventlist[:] = []
|
|
|
|
|
|
|
|
def add_event(self, e, level="info"):
|
2014-11-14 23:52:26 +00:00
|
|
|
needed = dict(error=0, info=1, debug=2).get(level, 1)
|
2014-03-13 22:25:13 +00:00
|
|
|
if self.options.verbosity < needed:
|
2014-03-13 00:04:45 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
if level == "error":
|
2012-07-26 22:08:10 +00:00
|
|
|
e = urwid.Text(("error", str(e)))
|
2014-03-13 00:04:45 +00:00
|
|
|
else:
|
|
|
|
e = urwid.Text(str(e))
|
2012-02-07 03:39:37 +00:00
|
|
|
self.eventlist.append(e)
|
|
|
|
if len(self.eventlist) > EVENTLOG_SIZE:
|
|
|
|
self.eventlist.pop(0)
|
2012-10-28 20:30:59 +00:00
|
|
|
self.eventlist.set_focus(len(self.eventlist)-1)
|
2012-02-07 03:39:37 +00:00
|
|
|
|
|
|
|
# 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
|