From 09c503563ad2e42812bf8043aedd9ecf980babf6 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sat, 15 Nov 2014 17:25:05 +1300 Subject: [PATCH] Enable config file parsing We support 4 different config files: ~/.mitmproxy/common.conf: Options that are common to all tools ~/.mitmproxy/mitmproxy.conf: Options for mitmproxy ~/.mitmproxy/mitmdump.conf: Options for mitmdump ~/.mitmproxy/mitmweb.conf: Options for mitmweb Options in the tool-specific config files over-ride options in common.conf. If a non-common option is put in common.conf, an error will be raised if a non-supporting tool is used. --- libmproxy/cmdline.py | 36 ++++++++++++++++++++++++++++++++---- test/test_cmdline.py | 18 +++++++++++++++++- 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/libmproxy/cmdline.py b/libmproxy/cmdline.py index 99e977d45..20dd0b6ad 100644 --- a/libmproxy/cmdline.py +++ b/libmproxy/cmdline.py @@ -1,4 +1,5 @@ from __future__ import absolute_import +import os import re import configargparse import argparse @@ -311,7 +312,7 @@ def common_options(parser): help="Don't start a proxy server." ) group.add_argument( - "-p", + "-p", "--port", action="store", type=int, dest="port", default=8080, help="Proxy service port." ) @@ -520,7 +521,16 @@ def mitmproxy(): # platforms. from .console import palettes - parser = configargparse.ArgumentParser(usage="%(prog)s [options]") + parser = configargparse.ArgumentParser( + usage="%(prog)s [options]", + args_for_setting_config_path = ["--conf"], + default_config_files = [ + os.path.join(config.CA_DIR, "common.conf"), + os.path.join(config.CA_DIR, "mitmproxy.conf") + ], + add_config_file_help = True, + add_env_var_help = True + ) parser.add_argument( '--version', action='version', @@ -551,7 +561,16 @@ def mitmproxy(): def mitmdump(): - parser = configargparse.ArgumentParser(usage="%(prog)s [options] [filter]") + parser = configargparse.ArgumentParser( + usage="%(prog)s [options] [filter]", + args_for_setting_config_path = ["--conf"], + default_config_files = [ + os.path.join(config.CA_DIR, "common.conf"), + os.path.join(config.CA_DIR, "mitmdump.conf") + ], + add_config_file_help = True, + add_env_var_help = True + ) parser.add_argument( '--version', @@ -577,7 +596,16 @@ def mitmdump(): def mitmweb(): - parser = configargparse.ArgumentParser(usage="%(prog)s [options]") + parser = configargparse.ArgumentParser( + usage="%(prog)s [options]", + args_for_setting_config_path = ["--conf"], + default_config_files = [ + os.path.join(config.CA_DIR, "common.conf"), + os.path.join(config.CA_DIR, "mitmweb.conf") + ], + add_config_file_help = True, + add_env_var_help = True + ) parser.add_argument( '--version', action='version', diff --git a/test/test_cmdline.py b/test/test_cmdline.py index 12e8aa897..476fc6206 100644 --- a/test/test_cmdline.py +++ b/test/test_cmdline.py @@ -1,7 +1,6 @@ import argparse from libmproxy import cmdline import tutils -import os.path def test_parse_replace_hook(): @@ -51,6 +50,7 @@ def test_parse_setheaders(): x = cmdline.parse_setheader("/foo/bar/voing") assert x == ("foo", "bar", "voing") + def test_common(): parser = argparse.ArgumentParser() cmdline.common_options(parser) @@ -108,3 +108,19 @@ def test_common(): assert len(v) == 1 assert v[0][2].strip() == "replacecontents" + +def test_mitmproxy(): + ap = cmdline.mitmproxy() + assert ap + + +def test_mitmdump(): + ap = cmdline.mitmdump() + assert ap + + +def test_mitmweb(): + ap = cmdline.mitmweb() + assert ap + +