codekingpro/portable-devtools
115k
1import urwid2 3from mitmproxy import flowfilter4from mitmproxy.tools.console import common5from mitmproxy.tools.console import layoutwidget6from mitmproxy.tools.console import tabs7 8 9class CListBox(urwid.ListBox):10 def __init__(self, contents):11 self.length = len(contents)12 contents = contents[:] + [urwid.Text(["\n"])] * 513 super().__init__(contents)14 15 def keypress(self, size, key):16 if key == "m_end":17 self.set_focus(self.length - 1)18 elif key == "m_start":19 self.set_focus(0)20 else:21 return super().keypress(size, key)22 23 24class HelpView(tabs.Tabs, layoutwidget.LayoutWidget):25 title = "Help"26 keyctx = "help"27 28 def __init__(self, master):29 self.master = master30 self.helpctx = ""31 super().__init__(32 [33 [self.keybindings_title, self.keybindings],34 [self.filtexp_title, self.filtexp],35 ]36 )37 38 def keybindings_title(self):39 return "Key Bindings"40 41 def format_keys(self, binds):42 kvs = []43 for b in binds:44 k = b.key45 if b.key == " ":46 k = "space"47 kvs.append((k, b.help or b.command))48 return common.format_keyvals(kvs)49 50 def keybindings(self):51 text = [urwid.Text([("title", "Common Keybindings")])]52 53 text.extend(self.format_keys(self.master.keymap.list("commonkey")))54 55 text.append(urwid.Text(["\n", ("title", "Keybindings for this view")]))56 if self.helpctx:57 text.extend(self.format_keys(self.master.keymap.list(self.helpctx)))58 59 text.append(60 urwid.Text(61 [62 "\n",63 ("title", "Global Keybindings"),64 ]65 )66 )67 68 text.extend(self.format_keys(self.master.keymap.list("global")))69 70 return CListBox(text)71 72 def filtexp_title(self):73 return "Filter Expressions"74 75 def filtexp(self):76 text = []77 text.extend(common.format_keyvals(flowfilter.help, indent=4))78 text.append(79 urwid.Text(80 [81 "\n",82 ("text", " Regexes are Python-style.\n"),83 ("text", " Regexes can be specified as quoted strings.\n"),84 (85 "text",86 ' Header matching (~h, ~hq, ~hs) is against a string of the form "name: value".\n',87 ),88 (89 "text",90 " Expressions with no operators are regex matches against URL.\n",91 ),92 ("text", " Default binary operator is &.\n"),93 ("head", "\n Examples:\n"),94 ]95 )96 )97 examples = [98 (r"google\.com", r"Url containing \"google.com"),99 ("~q ~b test", r"Requests where body contains \"test\""),100 (101 r"!(~q & ~t \"text/html\")",102 "Anything but requests with a text/html content type.",103 ),104 ]105 text.extend(common.format_keyvals(examples, indent=4))106 return CListBox(text)107 108 def layout_pushed(self, prev):109 """110 We are just about to push a window onto the stack.111 """112 self.helpctx = prev.keyctx113 self.show()114 