From 77e5d823e0e7cb2032e639de315d49bcad1945cc Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Mon, 26 Feb 2018 08:10:45 +1300 Subject: [PATCH 1/2] Shift option help styling admonition into the appropriate Loader docstring --- mitmproxy/addonmanager.py | 7 +++++++ mitmproxy/options.py | 8 +------- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/mitmproxy/addonmanager.py b/mitmproxy/addonmanager.py index 37c501ee2..bfaacf6df 100644 --- a/mitmproxy/addonmanager.py +++ b/mitmproxy/addonmanager.py @@ -92,6 +92,13 @@ class Loader: help: str, choices: typing.Optional[typing.Sequence[str]] = None ) -> None: + """ + Add an option to mitmproxy. + + Help should be a single paragraph with no linebreaks - it will be + reflowed by tools. Information on the data type should be omitted - + it will be generated and added by tools as needed. + """ if name in self.master.options: existing = self.master.options._options[name] same_signature = ( diff --git a/mitmproxy/options.py b/mitmproxy/options.py index 7a00bb879..e9e8a5639 100644 --- a/mitmproxy/options.py +++ b/mitmproxy/options.py @@ -14,12 +14,6 @@ log_verbosity = [ CA_DIR = "~/.mitmproxy" LISTEN_PORT = 8080 -# Some help text style guidelines: -# -# - Should be a single paragraph with no linebreaks. Help will be reflowed by -# tools. -# - Avoid adding information about the data type - we can generate that. - class Options(optmanager.OptManager): @@ -42,6 +36,7 @@ class Options(optmanager.OptManager): mode = None # type: str rawtcp = None # type: bool server = None # type: bool + showhost = None # type: bool spoof_source_address = None # type: bool ssl_insecure = None # type: bool ssl_verify_upstream_trusted_ca = None # type: Optional[str] @@ -56,7 +51,6 @@ class Options(optmanager.OptManager): # FIXME: Options that must be migrated to addons, but are complicated # because they're used by more than one addon, or because they're # embedded in the core code somehow. - showhost = None # type: bool verbosity = None # type: str view_filter = None # type: Optional[str] From 2fb85553cc57e9559ed6ea747b3043b6f87bf36e Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Mon, 26 Feb 2018 08:56:23 +1300 Subject: [PATCH 2/2] Revamp verbosity options Split verbosity into termlog_verbosity and console_eventlog_verbosity. This patch also removes printing to console if there are unknown options in the command-line. Options now live in separate addons, so having uknown options remaining is common and expected. We definitely shoould have some other way for users to see what was ignored so they can catch typos and the like, but that's a different patch. --- mitmproxy/addons/termlog.py | 9 ++++++++- mitmproxy/log.py | 9 +++++++++ mitmproxy/options.py | 13 ------------- mitmproxy/tools/console/consoleaddons.py | 6 ++++++ mitmproxy/tools/console/eventlog.py | 4 ++-- mitmproxy/tools/console/master.py | 2 +- mitmproxy/tools/main.py | 8 +++----- test/examples/test_xss_scanner.py | 3 +++ test/mitmproxy/addons/test_dumper.py | 2 +- test/mitmproxy/addons/test_termlog.py | 4 ++-- test/mitmproxy/tools/console/test_master.py | 2 -- test/mitmproxy/tools/test_dump.py | 2 +- 12 files changed, 36 insertions(+), 28 deletions(-) diff --git a/mitmproxy/addons/termlog.py b/mitmproxy/addons/termlog.py index 2a7e2d099..f09aa4b41 100644 --- a/mitmproxy/addons/termlog.py +++ b/mitmproxy/addons/termlog.py @@ -14,13 +14,20 @@ class TermLog: def __init__(self, outfile=None): self.outfile = outfile + def load(self, loader): + loader.add_option( + "termlog_verbosity", str, 'info', + "Log verbosity.", + choices=log.LogTierOrder + ) + def log(self, e): if log.log_tier(e.level) == log.log_tier("error"): outfile = self.outfile or realstderr else: outfile = self.outfile or realstdout - if log.log_tier(ctx.options.verbosity) >= log.log_tier(e.level): + if log.log_tier(ctx.options.termlog_verbosity) >= log.log_tier(e.level): click.secho( e.msg, file=outfile, diff --git a/mitmproxy/log.py b/mitmproxy/log.py index 3083a000f..d29880112 100644 --- a/mitmproxy/log.py +++ b/mitmproxy/log.py @@ -57,5 +57,14 @@ class Log: self.master.add_log(text, level) +LogTierOrder = [ + "error", + "warn", + "info", + "alert", + "debug", +] + + def log_tier(level): return dict(error=0, warn=1, info=2, alert=2, debug=3).get(level) diff --git a/mitmproxy/options.py b/mitmproxy/options.py index e9e8a5639..0ada5306b 100644 --- a/mitmproxy/options.py +++ b/mitmproxy/options.py @@ -3,13 +3,6 @@ from typing import Optional, Sequence from mitmproxy import optmanager from mitmproxy.net import tls -log_verbosity = [ - "error", - "warn", - "info", - "alert", - "debug", -] CA_DIR = "~/.mitmproxy" LISTEN_PORT = 8080 @@ -51,7 +44,6 @@ class Options(optmanager.OptManager): # FIXME: Options that must be migrated to addons, but are complicated # because they're used by more than one addon, or because they're # embedded in the core code somehow. - verbosity = None # type: str view_filter = None # type: Optional[str] def __init__(self, **kwargs) -> None: @@ -64,11 +56,6 @@ class Options(optmanager.OptManager): "showhost", bool, False, "Use the Host header to construct URLs for display." ) - self.add_option( - "verbosity", str, 'info', - "Log verbosity.", - choices=log_verbosity - ) # Proxy options self.add_option( diff --git a/mitmproxy/tools/console/consoleaddons.py b/mitmproxy/tools/console/consoleaddons.py index 3d76d20cb..2b9ff334c 100644 --- a/mitmproxy/tools/console/consoleaddons.py +++ b/mitmproxy/tools/console/consoleaddons.py @@ -7,6 +7,7 @@ from mitmproxy import command from mitmproxy import exceptions from mitmproxy import flow from mitmproxy import http +from mitmproxy import log from mitmproxy import contentviews from mitmproxy.utils import strutils import mitmproxy.types @@ -81,6 +82,11 @@ class ConsoleAddon: "The default content view mode.", choices = [i.name.lower() for i in contentviews.views] ) + loader.add_option( + "console_eventlog_verbosity", str, 'info', + "EventLog verbosity.", + choices=log.LogTierOrder + ) loader.add_option( "console_layout", str, "single", "Console layout.", diff --git a/mitmproxy/tools/console/eventlog.py b/mitmproxy/tools/console/eventlog.py index 8083180de..f3305469f 100644 --- a/mitmproxy/tools/console/eventlog.py +++ b/mitmproxy/tools/console/eventlog.py @@ -21,7 +21,7 @@ class EventLog(urwid.ListBox, layoutwidget.LayoutWidget): master.events.sig_add.connect(self.add_event) master.events.sig_refresh.connect(self.refresh_events) - self.master.options.subscribe(self.refresh_events, ["verbosity"]) + self.master.options.subscribe(self.refresh_events, ["console_eventlog_verbosity"]) self.refresh_events() super().__init__(self.walker) @@ -44,7 +44,7 @@ class EventLog(urwid.ListBox, layoutwidget.LayoutWidget): return super().keypress(size, key) def add_event(self, event_store, entry: log.LogEntry): - if log.log_tier(self.master.options.verbosity) < log.log_tier(entry.level): + if log.log_tier(self.master.options.console_eventlog_verbosity) < log.log_tier(entry.level): return txt = "%s: %s" % (entry.level, str(entry.msg)) if entry.level in ("error", "warn", "alert"): diff --git a/mitmproxy/tools/console/master.py b/mitmproxy/tools/console/master.py index 76d8724ae..5da6ef0bd 100644 --- a/mitmproxy/tools/console/master.py +++ b/mitmproxy/tools/console/master.py @@ -87,7 +87,7 @@ class ConsoleMaster(master.Master): ) def sig_add_log(self, event_store, entry: log.LogEntry): - if log.log_tier(self.options.verbosity) < log.log_tier(entry.level): + if log.log_tier(self.options.console_eventlog_verbosity) < log.log_tier(entry.level): return if entry.level in ("error", "warn", "alert"): if self.first_tick: diff --git a/mitmproxy/tools/main.py b/mitmproxy/tools/main.py index 5c2d9f2f7..330060f7d 100644 --- a/mitmproxy/tools/main.py +++ b/mitmproxy/tools/main.py @@ -46,10 +46,10 @@ def process_options(parser, opts, args): if args.quiet or args.options or args.commands: # also reduce log verbosity if --options or --commands is passed, # we don't want log messages from regular startup then. - args.verbosity = 'error' + args.termlog_verbosity = 'error' args.flow_detail = 0 if args.verbose: - args.verbosity = 'debug' + args.termlog_verbosity = 'debug' args.flow_detail = 2 adict = {} @@ -104,9 +104,7 @@ def run( master.server = server master.addons.trigger("configure", opts.keys()) master.addons.trigger("tick") - remaining = opts.update_known(**unknown) - if remaining and log.log_tier(opts.verbosity) > 1: - print("Ignored options: %s" % remaining) + opts.update_known(**unknown) if args.options: print(optmanager.dump_defaults(opts)) sys.exit(0) diff --git a/test/examples/test_xss_scanner.py b/test/examples/test_xss_scanner.py index 610bdd721..1d723d537 100644 --- a/test/examples/test_xss_scanner.py +++ b/test/examples/test_xss_scanner.py @@ -310,6 +310,9 @@ class TestXSSScanner(): def __init__(self): self.args = [] + def info(self, str): + self.args.append(str) + def error(self, str): self.args.append(str) return Logger() diff --git a/test/mitmproxy/addons/test_dumper.py b/test/mitmproxy/addons/test_dumper.py index ead6b7e72..7c54ab88a 100644 --- a/test/mitmproxy/addons/test_dumper.py +++ b/test/mitmproxy/addons/test_dumper.py @@ -147,7 +147,7 @@ class TestContentView: sio = io.StringIO() d = dumper.Dumper(sio) with taddons.context(d) as ctx: - ctx.configure(d, flow_detail=4, verbosity='debug') + ctx.configure(d, flow_detail=4) d.response(tflow.tflow()) assert ctx.master.has_log("content viewer failed") diff --git a/test/mitmproxy/addons/test_termlog.py b/test/mitmproxy/addons/test_termlog.py index 027bdfeb2..6c95df0c9 100644 --- a/test/mitmproxy/addons/test_termlog.py +++ b/test/mitmproxy/addons/test_termlog.py @@ -3,7 +3,6 @@ import pytest from mitmproxy.addons import termlog from mitmproxy import log -from mitmproxy.options import Options from mitmproxy.test import taddons @@ -16,7 +15,8 @@ class TestTermLog: ]) def test_output(self, outfile, expected_out, expected_err, capfd): t = termlog.TermLog(outfile=outfile) - with taddons.context(options=Options(verbosity='info')) as tctx: + with taddons.context(t) as tctx: + tctx.options.termlog_verbosity = "info" tctx.configure(t) t.log(log.LogEntry("one", "info")) t.log(log.LogEntry("two", "debug")) diff --git a/test/mitmproxy/tools/console/test_master.py b/test/mitmproxy/tools/console/test_master.py index 5be035e88..2879170de 100644 --- a/test/mitmproxy/tools/console/test_master.py +++ b/test/mitmproxy/tools/console/test_master.py @@ -7,8 +7,6 @@ from ... import tservers class TestMaster(tservers.MasterTest): def mkmaster(self, **opts): - if "verbosity" not in opts: - opts["verbosity"] = 'warn' o = options.Options(**opts) m = console.master.ConsoleMaster(o) m.addons.trigger("configure", o.keys()) diff --git a/test/mitmproxy/tools/test_dump.py b/test/mitmproxy/tools/test_dump.py index f303c8088..9e67f420f 100644 --- a/test/mitmproxy/tools/test_dump.py +++ b/test/mitmproxy/tools/test_dump.py @@ -11,7 +11,7 @@ from .. import tservers class TestDumpMaster(tservers.MasterTest): def mkmaster(self, flt, **opts): - o = options.Options(view_filter=flt, verbosity='error', **opts) + o = options.Options(view_filter=flt, **opts) m = dump.DumpMaster(o, with_termlog=False, with_dumper=False) return m