Refactor command-line argument definition

- Argument definitions live in cmdline.py
- Parsing and initial processing lives in main.py
This commit is contained in:
Aldo Cortesi 2014-11-15 16:29:38 +13:00
parent 9c88622e25
commit 6c1dc4522d
2 changed files with 124 additions and 122 deletions

View File

@ -1,8 +1,9 @@
from __future__ import absolute_import from __future__ import absolute_import
import re import re
from configargparse import ArgumentTypeError import configargparse
import argparse
from netlib import http from netlib import http
from . import filt, utils from . import filt, utils, version
from .proxy import config from .proxy import config
APP_HOST = "mitm.it" APP_HOST = "mitm.it"
@ -103,7 +104,9 @@ def parse_server_spec(url):
p = http.parse_url(normalized_url) p = http.parse_url(normalized_url)
if not p or not p[1]: if not p or not p[1]:
raise ArgumentTypeError("Invalid server specification: %s" % url) raise argparse.ArgumentTypeError(
"Invalid server specification: %s" % url
)
if url.lower().startswith("https2http"): if url.lower().startswith("https2http"):
ssl = [True, False] ssl = [True, False]
@ -132,17 +135,19 @@ def get_common_options(options):
try: try:
p = parse_replace_hook(i) p = parse_replace_hook(i)
except ParseException, e: except ParseException, e:
raise ArgumentTypeError(e.message) raise argparse.ArgumentTypeError(e.message)
reps.append(p) reps.append(p)
for i in options.replace_file: for i in options.replace_file:
try: try:
patt, rex, path = parse_replace_hook(i) patt, rex, path = parse_replace_hook(i)
except ParseException, e: except ParseException, e:
raise ArgumentTypeError(e.message) raise argparse.ArgumentTypeError(e.message)
try: try:
v = open(path, "rb").read() v = open(path, "rb").read()
except IOError, e: except IOError, e:
raise ArgumentTypeError("Could not read replace file: %s" % path) raise argparse.ArgumentTypeError(
"Could not read replace file: %s" % path
)
reps.append((patt, rex, v)) reps.append((patt, rex, v))
setheaders = [] setheaders = []
@ -150,7 +155,7 @@ def get_common_options(options):
try: try:
p = parse_setheader(i) p = parse_setheader(i)
except ParseException, e: except ParseException, e:
raise ArgumentTypeError(e.message) raise argparse.ArgumentTypeError(e.message)
setheaders.append(p) setheaders.append(p)
return dict( return dict(
@ -508,3 +513,105 @@ def common_options(parser):
) )
config.ssl_option_group(parser) config.ssl_option_group(parser)
def mitmproxy():
# Don't import libmproxy.console for mitmdump, urwid is not available on all
# platforms.
from .console import palettes
parser = configargparse.ArgumentParser(usage="%(prog)s [options]")
parser.add_argument(
'--version',
action='version',
version=version.NAMEVERSION
)
common_options(parser)
parser.add_argument(
"--palette", type=str, default="dark",
action="store", dest="palette",
help="Select color palette: " + ", ".join(palettes.palettes.keys())
)
parser.add_argument(
"-e",
action="store_true", dest="eventlog",
help="Show event log."
)
group = parser.add_argument_group(
"Filters",
"See help in mitmproxy for filter expression syntax."
)
group.add_argument(
"-i", "--intercept", action="store",
type=str, dest="intercept", default=None,
help="Intercept filter expression."
)
return parser
def mitmdump():
parser = configargparse.ArgumentParser(usage="%(prog)s [options] [filter]")
parser.add_argument(
'--version',
action= 'version',
version= "mitmdump" + " " + version.VERSION
)
common_options(parser)
parser.add_argument(
"--keepserving",
action= "store_true", dest="keepserving", default=False,
help= """
Continue serving after client playback or file read. We exit by
default.
"""
)
parser.add_argument(
"-d",
action="count", dest="flow_detail", default=1,
help="Increase flow detail display level. Can be passed multiple times."
)
parser.add_argument('args', nargs=argparse.REMAINDER)
return parser
def mitmweb():
parser = configargparse.ArgumentParser(usage="%(prog)s [options]")
parser.add_argument(
'--version',
action='version',
version="mitmweb" + " " + version.VERSION
)
group = parser.add_argument_group("Mitmweb")
group.add_argument(
"--wport",
action="store", type=int, dest="wport", default=8081,
metavar="PORT",
help="Mitmweb port."
)
group.add_argument(
"--wiface",
action="store", dest="wiface", default="127.0.0.1",
metavar="IFACE",
help="Mitmweb interface."
)
group.add_argument(
"--wdebug",
action="store_true", dest="wdebug",
help="Turn on mitmweb debugging"
)
common_options(parser)
group = parser.add_argument_group(
"Filters",
"See help in mitmproxy for filter expression syntax."
)
group.add_argument(
"-i", "--intercept", action="store",
type=str, dest="intercept", default=None,
help="Intercept filter expression."
)
return parser

View File

@ -1,6 +1,4 @@
from __future__ import print_function, absolute_import from __future__ import print_function, absolute_import
import configargparse
import argparse
import os import os
import signal import signal
import sys import sys
@ -74,39 +72,13 @@ def get_server(dummy_server, options):
sys.exit(1) sys.exit(1)
def mitmproxy_cmdline(): def mitmproxy(): # pragma: nocover
# Don't import libmproxy.console for mitmdump, urwid is not available on all
# platforms.
from . import console from . import console
from .console import palettes
parser = configargparse.ArgumentParser(usage="%(prog)s [options]") check_versions()
parser.add_argument( assert_utf8_env()
'--version',
action='version',
version=version.NAMEVERSION
)
cmdline.common_options(parser)
parser.add_argument(
"--palette", type=str, default="dark",
action="store", dest="palette",
help="Select color palette: " + ", ".join(palettes.palettes.keys())
)
parser.add_argument(
"-e",
action="store_true", dest="eventlog",
help="Show event log."
)
group = parser.add_argument_group(
"Filters",
"See help in mitmproxy for filter expression syntax."
)
group.add_argument(
"-i", "--intercept", action="store",
type=str, dest="intercept", default=None,
help="Intercept filter expression."
)
parser = cmdline.mitmproxy()
options = parser.parse_args() options = parser.parse_args()
if options.quiet: if options.quiet:
options.verbose = 0 options.verbose = 0
@ -117,15 +89,6 @@ def mitmproxy_cmdline():
console_options.eventlog = options.eventlog console_options.eventlog = options.eventlog
console_options.intercept = options.intercept console_options.intercept = options.intercept
return console_options, proxy_config
def mitmproxy(): # pragma: nocover
from . import console
check_versions()
assert_utf8_env()
console_options, proxy_config = mitmproxy_cmdline()
server = get_server(console_options.no_server, proxy_config) server = get_server(console_options.no_server, proxy_config)
m = console.ConsoleMaster(server, console_options) m = console.ConsoleMaster(server, console_options)
@ -135,32 +98,12 @@ def mitmproxy(): # pragma: nocover
pass pass
def mitmdump_cmdline(): def mitmdump(): # pragma: nocover
from . import dump from . import dump
parser = configargparse.ArgumentParser(usage="%(prog)s [options] [filter]") check_versions()
parser.add_argument(
'--version',
action= 'version',
version= "mitmdump" + " " + version.VERSION
)
cmdline.common_options(parser)
parser.add_argument(
"--keepserving",
action= "store_true", dest="keepserving", default=False,
help= """
Continue serving after client playback or file read. We exit by
default.
"""
)
parser.add_argument(
"-d",
action="count", dest="flow_detail", default=1,
help="Increase flow detail display level. Can be passed multiple times."
)
parser.add_argument('args', nargs=argparse.REMAINDER)
parser = cmdline.mitmdump()
options = parser.parse_args() options = parser.parse_args()
if options.quiet: if options.quiet:
options.verbose = 0 options.verbose = 0
@ -172,14 +115,6 @@ def mitmdump_cmdline():
dump_options.keepserving = options.keepserving dump_options.keepserving = options.keepserving
dump_options.filtstr = " ".join(options.args) if options.args else None dump_options.filtstr = " ".join(options.args) if options.args else None
return dump_options, proxy_config
def mitmdump(): # pragma: nocover
from . import dump
check_versions()
dump_options, proxy_config = mitmdump_cmdline()
server = get_server(dump_options.no_server, proxy_config) server = get_server(dump_options.no_server, proxy_config)
try: try:
@ -197,44 +132,11 @@ def mitmdump(): # pragma: nocover
pass pass
def mitmweb_cmdline(): def mitmweb(): # pragma: nocover
from . import web from . import web
parser = configargparse.ArgumentParser(usage="%(prog)s [options]")
parser.add_argument(
'--version',
action='version',
version="mitmweb" + " " + version.VERSION
)
group = parser.add_argument_group("Mitmweb") check_versions()
group.add_argument( parser = cmdline.mitmweb()
"--wport",
action="store", type=int, dest="wport", default=8081,
metavar="PORT",
help="Mitmweb port."
)
group.add_argument(
"--wiface",
action="store", dest="wiface", default="127.0.0.1",
metavar="IFACE",
help="Mitmweb interface."
)
group.add_argument(
"--wdebug",
action="store_true", dest="wdebug",
help="Turn on mitmweb debugging"
)
cmdline.common_options(parser)
group = parser.add_argument_group(
"Filters",
"See help in mitmproxy for filter expression syntax."
)
group.add_argument(
"-i", "--intercept", action="store",
type=str, dest="intercept", default=None,
help="Intercept filter expression."
)
options = parser.parse_args() options = parser.parse_args()
if options.quiet: if options.quiet:
@ -246,14 +148,7 @@ def mitmweb_cmdline():
web_options.wdebug = options.wdebug web_options.wdebug = options.wdebug
web_options.wiface = options.wiface web_options.wiface = options.wiface
web_options.wport = options.wport web_options.wport = options.wport
return web_options, proxy_config
def mitmweb(): # pragma: nocover
from . import web
check_versions()
web_options, proxy_config = mitmweb_cmdline()
server = get_server(web_options.no_server, proxy_config) server = get_server(web_options.no_server, proxy_config)
m = web.WebMaster(server, web_options) m = web.WebMaster(server, web_options)