Automate slurping up command-line options

Now that options are completely regular, we can automate reading them out of
arguments, rather than listing them out by hand like savages.
This commit is contained in:
Aldo Cortesi 2017-03-07 08:49:19 +13:00
parent edfd62e42a
commit 2312cf6fb0
6 changed files with 23 additions and 124 deletions

View File

@ -10,6 +10,13 @@ from mitmproxy.utils import human
class Core: class Core:
def configure(self, opts, updated): def configure(self, opts, updated):
if opts.add_upstream_certs_to_client_chain and not opts.upstream_cert:
raise exceptions.OptionsError(
"The no-upstream-cert and add-upstream-certs-to-client-chain "
"options are mutually exclusive. If no-upstream-cert is enabled "
"then the upstream certificate is not retrieved before generating "
"the client certificate chain."
)
if "body_size_limit" in updated and opts.body_size_limit: if "body_size_limit" in updated and opts.body_size_limit:
try: try:
opts._processed["body_size_limit"] = human.parse_size( opts._processed["body_size_limit"] = human.parse_size(

View File

@ -162,6 +162,9 @@ class OptManager:
def keys(self): def keys(self):
return set(self._options.keys()) return set(self._options.keys())
def __contains__(self, k):
return k in self._options
def reset(self): def reset(self):
""" """
Restore defaults for all options. Restore defaults for all options.

View File

@ -1,7 +1,6 @@
import argparse import argparse
import os import os
from mitmproxy import exceptions
from mitmproxy import options from mitmproxy import options
from mitmproxy import version from mitmproxy import version
@ -13,82 +12,6 @@ class ParseException(Exception):
pass pass
def get_common_options(args):
if args.add_upstream_certs_to_client_chain and not args.upstream_cert:
raise exceptions.OptionsError(
"The no-upstream-cert and add-upstream-certs-to-client-chain "
"options are mutually exclusive. If no-upstream-cert is enabled "
"then the upstream certificate is not retrieved before generating "
"the client certificate chain."
)
if args.quiet:
args.verbose = 0
return dict(
onboarding=args.onboarding,
onboarding_host=args.onboarding_host,
onboarding_port=args.onboarding_port,
anticache=args.anticache,
anticomp=args.anticomp,
client_replay=args.client_replay,
replay_kill_extra=args.replay_kill_extra,
no_server=args.no_server,
refresh_server_playback=args.refresh_server_playback,
server_replay_use_headers=args.server_replay_use_headers,
rfile=args.rfile,
replacements=args.replacements,
replacement_files=args.replacement_files,
setheaders=args.setheaders,
keep_host_header=args.keep_host_header,
server_replay=args.server_replay,
scripts=args.scripts,
stickycookie=args.stickycookie,
stickyauth=args.stickyauth,
stream_large_bodies=args.stream_large_bodies,
showhost=args.showhost,
streamfile=args.streamfile,
verbosity=args.verbose,
server_replay_nopop=args.server_replay_nopop,
server_replay_ignore_content=args.server_replay_ignore_content,
server_replay_ignore_params=args.server_replay_ignore_params,
server_replay_ignore_payload_params=args.server_replay_ignore_payload_params,
server_replay_ignore_host=args.server_replay_ignore_host,
auth_nonanonymous = args.auth_nonanonymous,
auth_singleuser = args.auth_singleuser,
auth_htpasswd = args.auth_htpasswd,
add_upstream_certs_to_client_chain = args.add_upstream_certs_to_client_chain,
body_size_limit = args.body_size_limit,
cadir = args.cadir,
certs = args.certs,
ciphers_client = args.ciphers_client,
ciphers_server = args.ciphers_server,
client_certs = args.client_certs,
ignore_hosts = args.ignore_hosts,
listen_host = args.listen_host,
listen_port = args.listen_port,
upstream_bind_address = args.upstream_bind_address,
mode = args.mode,
upstream_cert = args.upstream_cert,
spoof_source_address = args.spoof_source_address,
http2 = args.http2,
http2_priority = args.http2_priority,
websocket = args.websocket,
rawtcp = args.rawtcp,
upstream_auth = args.upstream_auth,
ssl_version_client = args.ssl_version_client,
ssl_version_server = args.ssl_version_server,
ssl_insecure = args.ssl_insecure,
ssl_verify_upstream_trusted_cadir = args.ssl_verify_upstream_trusted_cadir,
ssl_verify_upstream_trusted_ca = args.ssl_verify_upstream_trusted_ca,
tcp_hosts = args.tcp_hosts,
)
def basic_options(parser, opts): def basic_options(parser, opts):
parser.add_argument( parser.add_argument(
'--version', '--version',
@ -272,7 +195,7 @@ def mitmdump(opts):
opts.make_parser(parser, "keepserving") opts.make_parser(parser, "keepserving")
opts.make_parser(parser, "flow_detail", metavar = "LEVEL") opts.make_parser(parser, "flow_detail", metavar = "LEVEL")
parser.add_argument( parser.add_argument(
'filter', 'filter_args',
nargs="...", nargs="...",
help=""" help="""
Filter view expression, used to only show flows that match a certain filter. Filter view expression, used to only show flows that match a certain filter.

View File

@ -38,6 +38,14 @@ def process_options(parser, options, args):
if args.version: if args.version:
print(debug.dump_system_info()) print(debug.dump_system_info())
sys.exit(0) sys.exit(0)
if args.quiet:
args.flow_detail = 0
adict = {}
for n in dir(args):
if n in options:
adict[n] = getattr(args, n)
options.merge(adict)
debug.register_info_dumpers() debug.register_info_dumpers()
pconf = config.ProxyConfig(options) pconf = config.ProxyConfig(options)
@ -67,21 +75,6 @@ def mitmproxy(args=None): # pragma: no cover
try: try:
console_options.load_paths(args.conf) console_options.load_paths(args.conf)
console_options.merge(cmdline.get_common_options(args))
console_options.merge(
dict(
console_palette = args.console_palette,
console_palette_transparent = args.console_palette_transparent,
console_eventlog = args.console_eventlog,
console_focus_follow = args.console_focus_follow,
console_mouse = args.console_mouse,
console_order = args.console_order,
filter = args.filter,
intercept = args.intercept,
)
)
server = process_options(parser, console_options, args) server = process_options(parser, console_options, args)
m = console.master.ConsoleMaster(console_options, server) m = console.master.ConsoleMaster(console_options, server)
except exceptions.OptionsError as e: except exceptions.OptionsError as e:
@ -101,21 +94,9 @@ def mitmdump(args=None): # pragma: no cover
dump_options = options.Options() dump_options = options.Options()
parser = cmdline.mitmdump(dump_options) parser = cmdline.mitmdump(dump_options)
args = parser.parse_args(args) args = parser.parse_args(args)
if args.quiet:
args.flow_detail = 0
master = None master = None
try: try:
dump_options.load_paths(args.conf) dump_options.load_paths(args.conf)
dump_options.merge(cmdline.get_common_options(args))
dump_options.merge(
dict(
flow_detail = args.flow_detail,
keepserving = args.keepserving,
filtstr = " ".join(args.filter) if args.filter else None,
)
)
server = process_options(parser, dump_options, args) server = process_options(parser, dump_options, args)
master = dump.DumpMaster(dump_options, server) master = dump.DumpMaster(dump_options, server)
@ -145,16 +126,6 @@ def mitmweb(args=None): # pragma: no cover
try: try:
web_options.load_paths(args.conf) web_options.load_paths(args.conf)
web_options.merge(cmdline.get_common_options(args))
web_options.merge(
dict(
intercept = args.intercept,
web_open_browser = args.web_open_browser,
web_debug = args.web_debug,
web_iface = args.web_iface,
web_port = args.web_port,
)
)
server = process_options(parser, web_options, args) server = process_options(parser, web_options, args)
m = web.master.WebMaster(web_options, server) m = web.master.WebMaster(web_options, server)
except exceptions.OptionsError as e: except exceptions.OptionsError as e:

View File

@ -6,6 +6,7 @@ import pytest
from mitmproxy.tools import cmdline from mitmproxy.tools import cmdline
from mitmproxy.tools import main
from mitmproxy import options from mitmproxy import options
from mitmproxy.proxy import ProxyConfig from mitmproxy.proxy import ProxyConfig
from mitmproxy.proxy.server import DummyServer, ProxyServer, ConnectionHandler from mitmproxy.proxy.server import DummyServer, ProxyServer, ConnectionHandler
@ -33,7 +34,7 @@ class TestProcessProxyOptions:
opts = options.Options() opts = options.Options()
cmdline.common_options(parser, opts) cmdline.common_options(parser, opts)
args = parser.parse_args(args=args) args = parser.parse_args(args=args)
opts.merge(cmdline.get_common_options(args)) main.process_options(parser, opts, args)
pconf = config.ProxyConfig(opts) pconf = config.ProxyConfig(opts)
return parser, pconf return parser, pconf

View File

@ -1,5 +1,6 @@
import argparse import argparse
from mitmproxy.tools import cmdline from mitmproxy.tools import cmdline
from mitmproxy.tools import main
from mitmproxy import options from mitmproxy import options
@ -8,14 +9,7 @@ def test_common():
opts = options.Options() opts = options.Options()
cmdline.common_options(parser, opts) cmdline.common_options(parser, opts)
args = parser.parse_args(args=[]) args = parser.parse_args(args=[])
assert main.process_options(parser, opts, args)
assert cmdline.get_common_options(args)
args.stickycookie = "foo"
args.stickyauth = "foo"
v = cmdline.get_common_options(args)
assert v["stickycookie"] == "foo"
assert v["stickyauth"] == "foo"
def test_mitmproxy(): def test_mitmproxy():