Handle command-line argument errors properly

At some point we stopped handling exceptions from get_common_options properly.
This commit is contained in:
Aldo Cortesi 2016-07-18 17:09:55 +12:00
parent d2268ddb1e
commit bd733e1232
2 changed files with 37 additions and 29 deletions

View File

@ -6,6 +6,7 @@ import re
import configargparse import configargparse
from mitmproxy import exceptions
from mitmproxy import filt from mitmproxy import filt
from mitmproxy.proxy import config from mitmproxy.proxy import config
from netlib import human from netlib import human
@ -149,17 +150,17 @@ def get_common_options(args):
try: try:
p = parse_replace_hook(i) p = parse_replace_hook(i)
except ParseException as e: except ParseException as e:
raise configargparse.ArgumentTypeError(e) raise exceptions.OptionsError(e)
reps.append(p) reps.append(p)
for i in args.replace_file: for i in args.replace_file:
try: try:
patt, rex, path = parse_replace_hook(i) patt, rex, path = parse_replace_hook(i)
except ParseException as e: except ParseException as e:
raise configargparse.ArgumentTypeError(e) raise exceptions.OptionsError(e)
try: try:
v = open(path, "rb").read() v = open(path, "rb").read()
except IOError as e: except IOError as e:
raise configargparse.ArgumentTypeError( raise exceptions.OptionsError(
"Could not read replace file: %s" % path "Could not read replace file: %s" % path
) )
reps.append((patt, rex, v)) reps.append((patt, rex, v))
@ -169,17 +170,17 @@ def get_common_options(args):
try: try:
p = parse_setheader(i) p = parse_setheader(i)
except ParseException as e: except ParseException as e:
raise configargparse.ArgumentTypeError(e) raise exceptions.OptionsError(e)
setheaders.append(p) setheaders.append(p)
if args.outfile and args.outfile[0] == args.rfile: if args.outfile and args.outfile[0] == args.rfile:
if args.outfile[1] == "wb": if args.outfile[1] == "wb":
raise configargparse.ArgumentTypeError( raise exceptions.OptionsError(
"Cannot use '{}' for both reading and writing flows. " "Cannot use '{}' for both reading and writing flows. "
"Are you looking for --afile?".format(args.rfile) "Are you looking for --afile?".format(args.rfile)
) )
else: else:
raise configargparse.ArgumentTypeError( raise exceptions.OptionsError(
"Cannot use '{}' for both reading and appending flows. " "Cannot use '{}' for both reading and appending flows. "
"That would trigger an infinite loop." "That would trigger an infinite loop."
) )
@ -194,7 +195,12 @@ def get_common_options(args):
body_size_limit = args.body_size_limit body_size_limit = args.body_size_limit
if body_size_limit: if body_size_limit:
body_size_limit = human.parse_size(body_size_limit) try:
body_size_limit = human.parse_size(body_size_limit)
except ValueError, e:
raise exceptions.OptionsError(
"Invalid body size limit specification: %s" % body_size_limit
)
return dict( return dict(
app=args.app, app=args.app,

View File

@ -64,16 +64,18 @@ def mitmproxy(args=None): # pragma: no cover
parser = cmdline.mitmproxy() parser = cmdline.mitmproxy()
args = parser.parse_args(args) args = parser.parse_args(args)
console_options = console.master.Options(**cmdline.get_common_options(args))
console_options.palette = args.palette
console_options.palette_transparent = args.palette_transparent
console_options.eventlog = args.eventlog
console_options.follow = args.follow
console_options.intercept = args.intercept
console_options.limit = args.limit
console_options.no_mouse = args.no_mouse
try: try:
console_options = console.master.Options(
**cmdline.get_common_options(args)
)
console_options.palette = args.palette
console_options.palette_transparent = args.palette_transparent
console_options.eventlog = args.eventlog
console_options.follow = args.follow
console_options.intercept = args.intercept
console_options.limit = args.limit
console_options.no_mouse = args.no_mouse
proxy_config = process_options(parser, console_options, args) proxy_config = process_options(parser, console_options, args)
server = get_server(console_options.no_server, proxy_config) server = get_server(console_options.no_server, proxy_config)
m = console.master.ConsoleMaster(server, console_options) m = console.master.ConsoleMaster(server, console_options)
@ -96,12 +98,12 @@ def mitmdump(args=None): # pragma: no cover
if args.quiet: if args.quiet:
args.flow_detail = 0 args.flow_detail = 0
dump_options = dump.Options(**cmdline.get_common_options(args))
dump_options.flow_detail = args.flow_detail
dump_options.keepserving = args.keepserving
dump_options.filtstr = " ".join(args.args) if args.args else None
try: try:
dump_options = dump.Options(**cmdline.get_common_options(args))
dump_options.flow_detail = args.flow_detail
dump_options.keepserving = args.keepserving
dump_options.filtstr = " ".join(args.args) if args.args else None
proxy_config = process_options(parser, dump_options, args) proxy_config = process_options(parser, dump_options, args)
server = get_server(dump_options.no_server, proxy_config) server = get_server(dump_options.no_server, proxy_config)
master = dump.DumpMaster(server, dump_options) master = dump.DumpMaster(server, dump_options)
@ -130,16 +132,16 @@ def mitmweb(args=None): # pragma: no cover
args = parser.parse_args(args) args = parser.parse_args(args)
web_options = web.master.Options(**cmdline.get_common_options(args))
web_options.intercept = args.intercept
web_options.wdebug = args.wdebug
web_options.wiface = args.wiface
web_options.wport = args.wport
web_options.wsingleuser = args.wsingleuser
web_options.whtpasswd = args.whtpasswd
web_options.process_web_options(parser)
try: try:
web_options = web.master.Options(**cmdline.get_common_options(args))
web_options.intercept = args.intercept
web_options.wdebug = args.wdebug
web_options.wiface = args.wiface
web_options.wport = args.wport
web_options.wsingleuser = args.wsingleuser
web_options.whtpasswd = args.whtpasswd
web_options.process_web_options(parser)
proxy_config = process_options(parser, web_options, args) proxy_config = process_options(parser, web_options, args)
server = get_server(web_options.no_server, proxy_config) server = get_server(web_options.no_server, proxy_config)
m = web.master.WebMaster(server, web_options) m = web.master.WebMaster(server, web_options)