2012-02-23 02:52:01 +00:00
|
|
|
# Copyright (C) 2012 Aldo Cortesi
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2012-02-07 03:39:37 +00:00
|
|
|
import urwid
|
|
|
|
import common
|
|
|
|
|
2012-02-08 08:47:39 +00:00
|
|
|
def _mkhelp():
|
|
|
|
text = []
|
|
|
|
keys = [
|
2012-02-20 23:42:43 +00:00
|
|
|
("A", "accept all intercepted flows"),
|
2012-07-06 02:10:58 +00:00
|
|
|
("a", "accept this intercepted flow"),
|
2012-02-20 23:42:43 +00:00
|
|
|
("C", "clear flow list or eventlog"),
|
2012-02-09 04:00:37 +00:00
|
|
|
("d", "delete flow"),
|
2012-02-18 11:32:20 +00:00
|
|
|
("D", "duplicate flow"),
|
2012-02-18 01:45:22 +00:00
|
|
|
("e", "toggle eventlog"),
|
2012-02-08 09:55:48 +00:00
|
|
|
("l", "set limit filter pattern"),
|
|
|
|
("L", "load saved flows"),
|
|
|
|
("r", "replay request"),
|
2012-02-20 22:01:39 +00:00
|
|
|
("V", "revert changes to request"),
|
2012-07-06 02:41:10 +00:00
|
|
|
("w", "save flows "),
|
|
|
|
("W", "stream flows to file"),
|
2012-02-20 23:42:43 +00:00
|
|
|
("X", "kill and delete flow, even if it's mid-intercept"),
|
|
|
|
("tab", "tab between eventlog and flow list"),
|
|
|
|
("enter", "view flow"),
|
2012-02-08 09:28:15 +00:00
|
|
|
("|", "run script on this flow"),
|
2012-02-08 08:47:39 +00:00
|
|
|
]
|
|
|
|
text.extend(common.format_keyvals(keys, key="key", val="text", indent=4))
|
|
|
|
return text
|
|
|
|
help_context = _mkhelp()
|
|
|
|
|
2012-03-22 01:57:57 +00:00
|
|
|
footer = [
|
|
|
|
('heading_key', "?"), ":help ",
|
|
|
|
]
|
2012-02-08 08:47:39 +00:00
|
|
|
|
2012-02-08 01:07:17 +00:00
|
|
|
class EventListBox(urwid.ListBox):
|
|
|
|
def __init__(self, master):
|
|
|
|
self.master = master
|
|
|
|
urwid.ListBox.__init__(self, master.eventlist)
|
|
|
|
|
|
|
|
def keypress(self, size, key):
|
|
|
|
key = common.shortcuts(key)
|
|
|
|
if key == "C":
|
|
|
|
self.master.clear_events()
|
|
|
|
key = None
|
|
|
|
return urwid.ListBox.keypress(self, size, key)
|
|
|
|
|
|
|
|
|
|
|
|
class BodyPile(urwid.Pile):
|
|
|
|
def __init__(self, master):
|
|
|
|
h = urwid.Text("Event log")
|
|
|
|
h = urwid.Padding(h, align="left", width=("relative", 100))
|
|
|
|
|
2012-02-18 05:48:08 +00:00
|
|
|
self.inactive_header = urwid.AttrWrap(h, "heading_inactive")
|
2012-02-08 01:07:17 +00:00
|
|
|
self.active_header = urwid.AttrWrap(h, "heading")
|
|
|
|
|
|
|
|
urwid.Pile.__init__(
|
|
|
|
self,
|
|
|
|
[
|
2012-04-08 22:05:13 +00:00
|
|
|
FlowListBox(master),
|
2012-02-08 01:07:17 +00:00
|
|
|
urwid.Frame(EventListBox(master), header = self.inactive_header)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
self.master = master
|
|
|
|
self.focus = 0
|
|
|
|
|
|
|
|
def keypress(self, size, key):
|
|
|
|
if key == "tab":
|
|
|
|
self.focus = (self.focus + 1)%len(self.widget_list)
|
|
|
|
self.set_focus(self.focus)
|
|
|
|
if self.focus == 1:
|
|
|
|
self.widget_list[1].header = self.active_header
|
|
|
|
else:
|
|
|
|
self.widget_list[1].header = self.inactive_header
|
|
|
|
key = None
|
|
|
|
elif key == "v":
|
|
|
|
self.master.toggle_eventlog()
|
|
|
|
key = None
|
|
|
|
|
|
|
|
# This is essentially a copypasta from urwid.Pile's keypress handler.
|
|
|
|
# So much for "closed for modification, but open for extension".
|
|
|
|
item_rows = None
|
|
|
|
if len(size)==2:
|
|
|
|
item_rows = self.get_item_rows( size, focus=True )
|
|
|
|
i = self.widget_list.index(self.focus_item)
|
|
|
|
tsize = self.get_item_size(size,i,True,item_rows)
|
|
|
|
return self.focus_item.keypress( tsize, key )
|
|
|
|
|
|
|
|
|
2012-02-07 03:39:37 +00:00
|
|
|
class ConnectionItem(common.WWrap):
|
|
|
|
def __init__(self, master, state, flow, focus):
|
|
|
|
self.master, self.state, self.flow = master, state, flow
|
|
|
|
self.focus = focus
|
|
|
|
w = self.get_text()
|
|
|
|
common.WWrap.__init__(self, w)
|
|
|
|
|
|
|
|
def get_text(self):
|
2012-02-17 22:11:59 +00:00
|
|
|
return common.format_flow(self.flow, self.focus)
|
2012-02-07 03:39:37 +00:00
|
|
|
|
|
|
|
def selectable(self):
|
|
|
|
return True
|
|
|
|
|
2012-07-06 02:10:58 +00:00
|
|
|
def save_flows_prompt(self, k):
|
|
|
|
if k == "a":
|
|
|
|
self.master.path_prompt(
|
|
|
|
"Save all flows to: ",
|
|
|
|
self.state.last_saveload,
|
|
|
|
self.master.save_flows
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
self.master.path_prompt(
|
|
|
|
"Save this flow to: ",
|
|
|
|
self.state.last_saveload,
|
|
|
|
self.master.save_one_flow,
|
|
|
|
self.flow
|
|
|
|
)
|
|
|
|
|
2012-02-07 03:39:37 +00:00
|
|
|
def keypress(self, (maxcol,), key):
|
|
|
|
key = common.shortcuts(key)
|
|
|
|
if key == "a":
|
|
|
|
self.flow.accept_intercept()
|
|
|
|
self.master.sync_list_view()
|
|
|
|
elif key == "d":
|
|
|
|
self.flow.kill(self.master)
|
|
|
|
self.state.delete_flow(self.flow)
|
|
|
|
self.master.sync_list_view()
|
2012-02-18 11:32:20 +00:00
|
|
|
elif key == "D":
|
|
|
|
f = self.master.duplicate_flow(self.flow)
|
|
|
|
self.master.currentflow = f
|
|
|
|
self.master.focus_current()
|
2012-02-07 03:39:37 +00:00
|
|
|
elif key == "r":
|
2012-04-03 21:47:57 +00:00
|
|
|
self.flow.backup()
|
2012-02-07 03:39:37 +00:00
|
|
|
r = self.master.replay_request(self.flow)
|
|
|
|
if r:
|
|
|
|
self.master.statusbar.message(r)
|
|
|
|
self.master.sync_list_view()
|
2012-02-20 22:01:39 +00:00
|
|
|
elif key == "V":
|
2012-04-03 21:47:57 +00:00
|
|
|
if not self.flow.modified():
|
|
|
|
self.master.statusbar.message("Flow not modified.")
|
|
|
|
return
|
2012-02-07 03:39:37 +00:00
|
|
|
self.state.revert(self.flow)
|
|
|
|
self.master.sync_list_view()
|
2012-04-03 21:47:57 +00:00
|
|
|
self.master.statusbar.message("Reverted.")
|
2012-02-08 09:55:48 +00:00
|
|
|
elif key == "w":
|
2012-07-06 02:10:58 +00:00
|
|
|
self.master.prompt_onekey(
|
|
|
|
"Save",
|
|
|
|
(
|
|
|
|
("all flows", "a"),
|
|
|
|
("this flow", "t"),
|
|
|
|
),
|
|
|
|
self.save_flows_prompt,
|
2012-02-08 09:55:48 +00:00
|
|
|
)
|
2012-02-07 03:39:37 +00:00
|
|
|
elif key == "X":
|
|
|
|
self.flow.kill(self.master)
|
|
|
|
elif key == "enter":
|
|
|
|
if self.flow.request:
|
|
|
|
self.master.view_flow(self.flow)
|
|
|
|
elif key == "|":
|
|
|
|
self.master.path_prompt(
|
2012-02-08 09:55:48 +00:00
|
|
|
"Send flow to script: ",
|
2012-02-08 09:28:15 +00:00
|
|
|
self.state.last_script,
|
2012-02-08 09:55:48 +00:00
|
|
|
self.master.run_script_once,
|
2012-02-08 09:28:15 +00:00
|
|
|
self.flow
|
2012-02-07 03:39:37 +00:00
|
|
|
)
|
2012-02-08 09:28:15 +00:00
|
|
|
else:
|
|
|
|
return key
|
2012-02-07 03:39:37 +00:00
|
|
|
|
|
|
|
|
2012-04-08 22:05:13 +00:00
|
|
|
class FlowListWalker(urwid.ListWalker):
|
2012-02-07 03:39:37 +00:00
|
|
|
def __init__(self, master, state):
|
|
|
|
self.master, self.state = master, state
|
|
|
|
if self.state.flow_count():
|
|
|
|
self.set_focus(0)
|
|
|
|
|
|
|
|
def get_focus(self):
|
|
|
|
f, i = self.state.get_focus()
|
|
|
|
f = ConnectionItem(self.master, self.state, f, True) if f else None
|
|
|
|
return f, i
|
|
|
|
|
|
|
|
def set_focus(self, focus):
|
|
|
|
ret = self.state.set_focus(focus)
|
|
|
|
return ret
|
|
|
|
|
|
|
|
def get_next(self, pos):
|
|
|
|
f, i = self.state.get_next(pos)
|
|
|
|
f = ConnectionItem(self.master, self.state, f, False) if f else None
|
|
|
|
return f, i
|
|
|
|
|
|
|
|
def get_prev(self, pos):
|
|
|
|
f, i = self.state.get_prev(pos)
|
|
|
|
f = ConnectionItem(self.master, self.state, f, False) if f else None
|
|
|
|
return f, i
|
|
|
|
|
|
|
|
|
2012-04-08 22:05:13 +00:00
|
|
|
class FlowListBox(urwid.ListBox):
|
2012-02-07 03:39:37 +00:00
|
|
|
def __init__(self, master):
|
|
|
|
self.master = master
|
2012-04-08 22:05:13 +00:00
|
|
|
urwid.ListBox.__init__(self, master.flow_list_walker)
|
2012-02-07 03:39:37 +00:00
|
|
|
|
|
|
|
def keypress(self, size, key):
|
|
|
|
key = common.shortcuts(key)
|
|
|
|
if key == "A":
|
|
|
|
self.master.accept_all()
|
|
|
|
self.master.sync_list_view()
|
|
|
|
elif key == "C":
|
2012-02-20 23:42:43 +00:00
|
|
|
self.master.clear_flows()
|
2012-02-18 01:45:22 +00:00
|
|
|
elif key == "e":
|
2012-02-07 03:39:37 +00:00
|
|
|
self.master.toggle_eventlog()
|
2012-02-19 00:16:21 +00:00
|
|
|
elif key == "l":
|
|
|
|
self.master.prompt("Limit: ", self.master.state.limit_txt, self.master.set_limit)
|
|
|
|
elif key == "L":
|
|
|
|
self.master.path_prompt(
|
|
|
|
"Load flows: ",
|
|
|
|
self.master.state.last_saveload,
|
|
|
|
self.master.load_flows_callback
|
|
|
|
)
|
2012-07-06 02:41:10 +00:00
|
|
|
elif key == "W":
|
|
|
|
if self.master.stream:
|
|
|
|
self.master.stop_stream()
|
|
|
|
else:
|
|
|
|
self.master.path_prompt(
|
|
|
|
"Stream flows to: ",
|
|
|
|
self.master.state.last_saveload,
|
|
|
|
self.master.start_stream
|
|
|
|
)
|
2012-02-19 00:16:21 +00:00
|
|
|
else:
|
|
|
|
return urwid.ListBox.keypress(self, size, key)
|