aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--mitmproxy/cmdline.py71
-rw-r--r--mitmproxy/flow/options.py8
-rw-r--r--mitmproxy/protocol/base.py4
-rw-r--r--mitmproxy/protocol/http_replay.py4
-rw-r--r--mitmproxy/proxy/config.py6
-rw-r--r--mitmproxy/proxy/server.py4
-rw-r--r--test/mitmproxy/test_protocol_http2.py9
-rw-r--r--test/mitmproxy/test_proxy.py6
-rw-r--r--test/mitmproxy/test_server.py4
-rw-r--r--test/mitmproxy/tservers.py40
10 files changed, 82 insertions, 74 deletions
diff --git a/mitmproxy/cmdline.py b/mitmproxy/cmdline.py
index 507ddfc7..1e5064f7 100644
--- a/mitmproxy/cmdline.py
+++ b/mitmproxy/cmdline.py
@@ -131,26 +131,26 @@ def parse_upstream_auth(auth):
return b"Basic" + b" " + base64.b64encode(strutils.always_bytes(auth))
-def get_common_options(options):
+def get_common_options(args):
stickycookie, stickyauth = None, None
- if options.stickycookie_filt:
- stickycookie = options.stickycookie_filt
+ if args.stickycookie_filt:
+ stickycookie = args.stickycookie_filt
- if options.stickyauth_filt:
- stickyauth = options.stickyauth_filt
+ if args.stickyauth_filt:
+ stickyauth = args.stickyauth_filt
- stream_large_bodies = options.stream_large_bodies
+ stream_large_bodies = args.stream_large_bodies
if stream_large_bodies:
stream_large_bodies = human.parse_size(stream_large_bodies)
reps = []
- for i in options.replace:
+ for i in args.replace:
try:
p = parse_replace_hook(i)
except ParseException as e:
raise configargparse.ArgumentTypeError(e)
reps.append(p)
- for i in options.replace_file:
+ for i in args.replace_file:
try:
patt, rex, path = parse_replace_hook(i)
except ParseException as e:
@@ -164,18 +164,18 @@ def get_common_options(options):
reps.append((patt, rex, v))
setheaders = []
- for i in options.setheader:
+ for i in args.setheader:
try:
p = parse_setheader(i)
except ParseException as e:
raise configargparse.ArgumentTypeError(e)
setheaders.append(p)
- if options.outfile and options.outfile[0] == options.rfile:
- if options.outfile[1] == "wb":
+ if args.outfile and args.outfile[0] == args.rfile:
+ if args.outfile[1] == "wb":
raise configargparse.ArgumentTypeError(
"Cannot use '{}' for both reading and writing flows. "
- "Are you looking for --afile?".format(options.rfile)
+ "Are you looking for --afile?".format(args.rfile)
)
else:
raise configargparse.ArgumentTypeError(
@@ -184,33 +184,36 @@ def get_common_options(options):
)
return dict(
- app=options.app,
- app_host=options.app_host,
- app_port=options.app_port,
-
- anticache=options.anticache,
- anticomp=options.anticomp,
- client_replay=options.client_replay,
- kill=options.kill,
- no_server=options.no_server,
- refresh_server_playback=not options.norefresh,
- rheaders=options.rheaders,
- rfile=options.rfile,
+ app=args.app,
+ app_host=args.app_host,
+ app_port=args.app_port,
+
+ anticache=args.anticache,
+ anticomp=args.anticomp,
+ client_replay=args.client_replay,
+ kill=args.kill,
+ no_server=args.no_server,
+ refresh_server_playback=not args.norefresh,
+ rheaders=args.rheaders,
+ rfile=args.rfile,
replacements=reps,
setheaders=setheaders,
- server_replay=options.server_replay,
- scripts=options.scripts,
+ server_replay=args.server_replay,
+ scripts=args.scripts,
stickycookie=stickycookie,
stickyauth=stickyauth,
stream_large_bodies=stream_large_bodies,
- showhost=options.showhost,
- outfile=options.outfile,
- verbosity=options.verbose,
- nopop=options.nopop,
- replay_ignore_content=options.replay_ignore_content,
- replay_ignore_params=options.replay_ignore_params,
- replay_ignore_payload_params=options.replay_ignore_payload_params,
- replay_ignore_host=options.replay_ignore_host
+ showhost=args.showhost,
+ outfile=args.outfile,
+ verbosity=args.verbose,
+ nopop=args.nopop,
+ replay_ignore_content=args.replay_ignore_content,
+ replay_ignore_params=args.replay_ignore_params,
+ replay_ignore_payload_params=args.replay_ignore_payload_params,
+ replay_ignore_host=args.replay_ignore_host,
+
+ listen_host = args.addr,
+ listen_port = args.port,
)
diff --git a/mitmproxy/flow/options.py b/mitmproxy/flow/options.py
index 6c2e3933..8879b01a 100644
--- a/mitmproxy/flow/options.py
+++ b/mitmproxy/flow/options.py
@@ -36,6 +36,10 @@ class Options(options.Options):
replay_ignore_params=(), # type: Sequence[str]
replay_ignore_payload_params=(), # type: Sequence[str]
replay_ignore_host=False, # type: bool
+
+ # Proxy options
+ listen_host = "", # type: str
+ listen_port = 8080, # type: int
):
# We could replace all assignments with clever metaprogramming,
# but type hints are a much more valueable asset.
@@ -66,4 +70,8 @@ class Options(options.Options):
self.replay_ignore_params = replay_ignore_params
self.replay_ignore_payload_params = replay_ignore_payload_params
self.replay_ignore_host = replay_ignore_host
+
+ self.listen_host = listen_host
+ self.listen_port = listen_port
+
super(Options, self).__init__()
diff --git a/mitmproxy/protocol/base.py b/mitmproxy/protocol/base.py
index 11773385..bf0cbbae 100644
--- a/mitmproxy/protocol/base.py
+++ b/mitmproxy/protocol/base.py
@@ -114,7 +114,7 @@ class ServerConnectionMixin(object):
def __init__(self, server_address=None):
super(ServerConnectionMixin, self).__init__()
- self.server_conn = models.ServerConnection(server_address, (self.config.host, 0))
+ self.server_conn = models.ServerConnection(server_address, (self.config.options.listen_host, 0))
self.__check_self_connect()
def __check_self_connect(self):
@@ -125,7 +125,7 @@ class ServerConnectionMixin(object):
address = self.server_conn.address
if address:
self_connect = (
- address.port == self.config.port and
+ address.port == self.config.options.listen_port and
address.host in ("localhost", "127.0.0.1", "::1")
)
if self_connect:
diff --git a/mitmproxy/protocol/http_replay.py b/mitmproxy/protocol/http_replay.py
index 986de845..27dfb741 100644
--- a/mitmproxy/protocol/http_replay.py
+++ b/mitmproxy/protocol/http_replay.py
@@ -46,7 +46,7 @@ class RequestReplayThread(basethread.BaseThread):
# In all modes, we directly connect to the server displayed
if self.config.mode == "upstream":
server_address = self.config.upstream_server.address
- server = models.ServerConnection(server_address, (self.config.host, 0))
+ server = models.ServerConnection(server_address, (self.config.options.listen_host, 0))
server.connect()
if r.scheme == "https":
connect_request = models.make_connect_request((r.data.host, r.port))
@@ -68,7 +68,7 @@ class RequestReplayThread(basethread.BaseThread):
r.first_line_format = "absolute"
else:
server_address = (r.host, r.port)
- server = models.ServerConnection(server_address, (self.config.host, 0))
+ server = models.ServerConnection(server_address, (self.config.options.listen_host, 0))
server.connect()
if r.scheme == "https":
server.establish_ssl(
diff --git a/mitmproxy/proxy/config.py b/mitmproxy/proxy/config.py
index 3aa16174..2f76e23c 100644
--- a/mitmproxy/proxy/config.py
+++ b/mitmproxy/proxy/config.py
@@ -60,8 +60,6 @@ class ProxyConfig:
def __init__(
self,
options,
- host='',
- port=8080,
cadir=CA_DIR,
clientcerts=None,
no_upstream_cert=False,
@@ -85,8 +83,6 @@ class ProxyConfig:
add_upstream_certs_to_client_chain=False,
):
self.options = options
- self.host = host
- self.port = port
self.ciphers_client = ciphers_client
self.ciphers_server = ciphers_server
self.clientcerts = clientcerts
@@ -218,8 +214,6 @@ def process_proxy_options(parser, options, args):
return ProxyConfig(
options,
- host=args.addr,
- port=args.port,
cadir=args.cadir,
clientcerts=args.clientcerts,
no_upstream_cert=args.no_upstream_cert,
diff --git a/mitmproxy/proxy/server.py b/mitmproxy/proxy/server.py
index 7e96911a..e5c4c3a1 100644
--- a/mitmproxy/proxy/server.py
+++ b/mitmproxy/proxy/server.py
@@ -41,7 +41,9 @@ class ProxyServer(tcp.TCPServer):
"""
self.config = config
try:
- super(ProxyServer, self).__init__((config.host, config.port))
+ super(ProxyServer, self).__init__(
+ (config.options.listen_host, config.options.listen_port)
+ )
except socket.error as e:
six.reraise(
exceptions.ServerException,
diff --git a/test/mitmproxy/test_protocol_http2.py b/test/mitmproxy/test_protocol_http2.py
index 04ca94bf..dd81ba23 100644
--- a/test/mitmproxy/test_protocol_http2.py
+++ b/test/mitmproxy/test_protocol_http2.py
@@ -90,9 +90,10 @@ class _Http2TestBase(object):
@classmethod
def setup_class(cls):
cls.masteroptions = options.Options()
- cls.config = ProxyConfig(cls.masteroptions, **cls.get_proxy_config())
+ cnf, opts = cls.get_proxy_config()
+ cls.config = ProxyConfig(opts, **cnf)
- tmaster = tservers.TestMaster(cls.masteroptions, cls.config)
+ tmaster = tservers.TestMaster(opts, cls.config)
tmaster.start_app(APP_HOST, APP_PORT)
cls.proxy = tservers.ProxyThread(tmaster)
cls.proxy.start()
@@ -103,12 +104,14 @@ class _Http2TestBase(object):
@classmethod
def get_proxy_config(cls):
+ opts = options.Options(listen_port=0)
cls.cadir = os.path.join(tempfile.gettempdir(), "mitmproxy")
- return dict(
+ d = dict(
no_upstream_cert=False,
cadir=cls.cadir,
authenticator=None,
)
+ return d, opts
@property
def master(self):
diff --git a/test/mitmproxy/test_proxy.py b/test/mitmproxy/test_proxy.py
index f4f73cf5..c87e1ad4 100644
--- a/test/mitmproxy/test_proxy.py
+++ b/test/mitmproxy/test_proxy.py
@@ -160,15 +160,13 @@ class TestProxyServer:
@tutils.skip_windows
def test_err(self):
conf = ProxyConfig(
- options.Options(),
- port=1
+ options.Options(listen_port=1),
)
tutils.raises("error starting proxy server", ProxyServer, conf)
def test_err_2(self):
conf = ProxyConfig(
- options.Options(),
- host="invalidhost"
+ options.Options(listen_host="invalidhost"),
)
tutils.raises("error starting proxy server", ProxyServer, conf)
diff --git a/test/mitmproxy/test_server.py b/test/mitmproxy/test_server.py
index 2e580d47..20372c92 100644
--- a/test/mitmproxy/test_server.py
+++ b/test/mitmproxy/test_server.py
@@ -484,9 +484,9 @@ class TestHttps2Http(tservers.ReverseProxyTest):
@classmethod
def get_proxy_config(cls):
- d = super(TestHttps2Http, cls).get_proxy_config()
+ d, opts = super(TestHttps2Http, cls).get_proxy_config()
d["upstream_server"] = ("http", d["upstream_server"][1])
- return d
+ return d, opts
def pathoc(self, ssl, sni=None):
"""
diff --git a/test/mitmproxy/tservers.py b/test/mitmproxy/tservers.py
index e236656e..f9e1925f 100644
--- a/test/mitmproxy/tservers.py
+++ b/test/mitmproxy/tservers.py
@@ -33,7 +33,6 @@ def errapp(environ, start_response):
class TestMaster(flow.FlowMaster):
def __init__(self, opts, config):
- config.port = 0
s = ProxyServer(config)
state = flow.State()
flow.FlowMaster.__init__(self, opts, s, state)
@@ -55,7 +54,8 @@ class ProxyThread(threading.Thread):
threading.Thread.__init__(self)
self.tmaster = tmaster
self.name = "ProxyThread (%s:%s)" % (
- tmaster.server.address.host, tmaster.server.address.port)
+ tmaster.server.address.host, tmaster.server.address.port
+ )
controller.should_exit = False
@property
@@ -80,7 +80,6 @@ class ProxyTestBase(object):
no_upstream_cert = False
authenticator = None
masterclass = TestMaster
- masteroptions = options.Options()
add_upstream_certs_to_client_chain = False
@classmethod
@@ -92,8 +91,9 @@ class ProxyTestBase(object):
ssl=cls.ssl,
ssloptions=cls.ssloptions)
- cls.config = ProxyConfig(cls.masteroptions, **cls.get_proxy_config())
- tmaster = cls.masterclass(cls.masteroptions, cls.config)
+ cnf, opts = cls.get_proxy_config()
+ cls.config = ProxyConfig(opts, **cnf)
+ tmaster = cls.masterclass(opts, cls.config)
tmaster.start_app(APP_HOST, APP_PORT)
cls.proxy = ProxyThread(tmaster)
cls.proxy.start()
@@ -120,12 +120,13 @@ class ProxyTestBase(object):
@classmethod
def get_proxy_config(cls):
cls.cadir = os.path.join(tempfile.gettempdir(), "mitmproxy")
- return dict(
+ cnf = dict(
no_upstream_cert = cls.no_upstream_cert,
cadir = cls.cadir,
authenticator = cls.authenticator,
add_upstream_certs_to_client_chain = cls.add_upstream_certs_to_client_chain,
)
+ return cnf, options.Options(listen_port=0)
class HTTPProxyTest(ProxyTestBase):
@@ -199,9 +200,9 @@ class TransparentProxyTest(ProxyTestBase):
@classmethod
def get_proxy_config(cls):
- d = ProxyTestBase.get_proxy_config()
+ d, opts = ProxyTestBase.get_proxy_config()
d["mode"] = "transparent"
- return d
+ return d, opts
def pathod(self, spec, sni=None):
"""
@@ -231,13 +232,13 @@ class ReverseProxyTest(ProxyTestBase):
@classmethod
def get_proxy_config(cls):
- d = ProxyTestBase.get_proxy_config()
+ d, opts = ProxyTestBase.get_proxy_config()
d["upstream_server"] = (
"https" if cls.ssl else "http",
("127.0.0.1", cls.server.port)
)
d["mode"] = "reverse"
- return d
+ return d, opts
def pathoc(self, sni=None):
"""
@@ -266,9 +267,9 @@ class SocksModeTest(HTTPProxyTest):
@classmethod
def get_proxy_config(cls):
- d = ProxyTestBase.get_proxy_config()
+ d, opts = ProxyTestBase.get_proxy_config()
d["mode"] = "socks5"
- return d
+ return d, opts
class ChainProxyTest(ProxyTestBase):
@@ -284,20 +285,19 @@ class ChainProxyTest(ProxyTestBase):
@classmethod
def setup_class(cls):
- cls.masteroptions = options.Options()
cls.chain = []
super(ChainProxyTest, cls).setup_class()
for _ in range(cls.n):
- config = ProxyConfig(cls.masteroptions, **cls.get_proxy_config())
- tmaster = cls.masterclass(cls.masteroptions, config)
+ cnf, opts = cls.get_proxy_config()
+ config = ProxyConfig(opts, **cnf)
+ tmaster = cls.masterclass(opts, config)
proxy = ProxyThread(tmaster)
proxy.start()
cls.chain.insert(0, proxy)
# Patch the orginal proxy to upstream mode
- cls.config = cls.proxy.tmaster.config = cls.proxy.tmaster.server.config = ProxyConfig(
- cls.masteroptions,
- **cls.get_proxy_config())
+ cnf, opts = cls.get_proxy_config()
+ cls.config = cls.proxy.tmaster.config = cls.proxy.tmaster.server.config = ProxyConfig(opts, **cnf)
@classmethod
def teardown_class(cls):
@@ -313,13 +313,13 @@ class ChainProxyTest(ProxyTestBase):
@classmethod
def get_proxy_config(cls):
- d = super(ChainProxyTest, cls).get_proxy_config()
+ d, opts = super(ChainProxyTest, cls).get_proxy_config()
if cls.chain: # First proxy is in normal mode.
d.update(
mode="upstream",
upstream_server=("http", ("127.0.0.1", cls.chain[0].port))
)
- return d
+ return d, opts
class HTTPUpstreamProxyTest(ChainProxyTest, HTTPProxyTest):