2014-03-10 21:36:47 +00:00
|
|
|
from __future__ import absolute_import
|
2014-03-09 20:51:24 +00:00
|
|
|
import os
|
2014-08-09 01:03:21 +00:00
|
|
|
import re
|
2014-03-09 20:51:24 +00:00
|
|
|
from netlib import http_auth, certutils
|
2014-09-05 13:16:20 +00:00
|
|
|
from .. import utils, platform
|
2014-09-08 12:32:42 +00:00
|
|
|
from .primitives import RegularProxyMode, TransparentProxyMode, UpstreamProxyMode, ReverseProxyMode
|
2014-03-09 20:51:24 +00:00
|
|
|
|
|
|
|
TRANSPARENT_SSL_PORTS = [443, 8443]
|
|
|
|
CONF_BASENAME = "mitmproxy"
|
|
|
|
CONF_DIR = "~/.mitmproxy"
|
|
|
|
|
|
|
|
|
2014-09-08 10:20:40 +00:00
|
|
|
def parse_host_pattern(patterns):
|
|
|
|
return [re.compile(p, re.IGNORECASE) for p in patterns]
|
|
|
|
|
|
|
|
|
2014-03-09 20:51:24 +00:00
|
|
|
class ProxyConfig:
|
2014-09-05 13:16:20 +00:00
|
|
|
def __init__(self, confdir=CONF_DIR, ca_file=None, clientcerts=None,
|
2014-08-08 02:43:44 +00:00
|
|
|
no_upstream_cert=False, body_size_limit=None,
|
|
|
|
mode=None, upstream_server=None, http_form_in=None, http_form_out=None,
|
2014-08-09 01:03:21 +00:00
|
|
|
authenticator=None, ignore=[],
|
2014-08-30 18:15:19 +00:00
|
|
|
ciphers=None, certs=[], certforward=False, ssl_ports=TRANSPARENT_SSL_PORTS):
|
2014-03-09 20:51:24 +00:00
|
|
|
self.ciphers = ciphers
|
|
|
|
self.clientcerts = clientcerts
|
|
|
|
self.no_upstream_cert = no_upstream_cert
|
|
|
|
self.body_size_limit = body_size_limit
|
2014-08-08 02:43:44 +00:00
|
|
|
|
|
|
|
if mode == "transparent":
|
2014-09-08 12:32:42 +00:00
|
|
|
self.mode = TransparentProxyMode(platform.resolver(), TRANSPARENT_SSL_PORTS)
|
2014-08-08 02:43:44 +00:00
|
|
|
elif mode == "reverse":
|
2014-09-08 12:32:42 +00:00
|
|
|
self.mode = ReverseProxyMode(upstream_server)
|
2014-08-08 02:43:44 +00:00
|
|
|
elif mode == "upstream":
|
2014-09-08 12:32:42 +00:00
|
|
|
self.mode = UpstreamProxyMode(upstream_server)
|
2014-08-08 02:43:44 +00:00
|
|
|
else:
|
2014-09-08 12:32:42 +00:00
|
|
|
self.mode = RegularProxyMode()
|
|
|
|
|
|
|
|
self.mode.http_form_in = http_form_in or self.mode.http_form_in
|
|
|
|
self.mode.http_form_out = http_form_out or self.mode.http_form_out
|
2014-08-08 02:43:44 +00:00
|
|
|
|
2014-09-08 10:20:40 +00:00
|
|
|
self.ignore = parse_host_pattern(ignore)
|
2014-03-09 20:51:24 +00:00
|
|
|
self.authenticator = authenticator
|
|
|
|
self.confdir = os.path.expanduser(confdir)
|
2014-09-05 13:16:20 +00:00
|
|
|
self.ca_file = ca_file or os.path.join(self.confdir, CONF_BASENAME + "-ca.pem")
|
2014-03-09 20:51:24 +00:00
|
|
|
self.certstore = certutils.CertStore.from_store(self.confdir, CONF_BASENAME)
|
2014-04-29 12:54:11 +00:00
|
|
|
for spec, cert in certs:
|
|
|
|
self.certstore.add_cert_file(spec, cert)
|
2014-03-11 00:02:10 +00:00
|
|
|
self.certforward = certforward
|
2014-08-30 18:15:19 +00:00
|
|
|
self.ssl_ports = ssl_ports
|
2014-03-09 20:51:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
def process_proxy_options(parser, options):
|
|
|
|
body_size_limit = utils.parse_size(options.body_size_limit)
|
|
|
|
|
2014-03-10 04:11:51 +00:00
|
|
|
c = 0
|
2014-08-08 02:43:44 +00:00
|
|
|
mode, upstream_server = None, None
|
2014-03-09 20:51:24 +00:00
|
|
|
if options.transparent_proxy:
|
2014-03-10 04:11:51 +00:00
|
|
|
c += 1
|
2014-03-09 20:51:24 +00:00
|
|
|
if not platform.resolver:
|
|
|
|
return parser.error("Transparent mode not supported on this platform.")
|
2014-08-08 02:43:44 +00:00
|
|
|
mode = "transparent"
|
2014-03-09 20:51:24 +00:00
|
|
|
if options.reverse_proxy:
|
2014-03-10 04:11:51 +00:00
|
|
|
c += 1
|
2014-08-08 02:43:44 +00:00
|
|
|
mode = "reverse"
|
|
|
|
upstream_server = options.reverse_proxy
|
2014-03-13 23:02:00 +00:00
|
|
|
if options.upstream_proxy:
|
2014-03-10 04:11:51 +00:00
|
|
|
c += 1
|
2014-08-08 02:43:44 +00:00
|
|
|
mode = "upstream"
|
|
|
|
upstream_server = options.upstream_proxy
|
2014-03-10 04:11:51 +00:00
|
|
|
if c > 1:
|
2014-09-08 12:32:42 +00:00
|
|
|
return parser.error("Transparent mode, reverse mode and upstream proxy mode "
|
|
|
|
"are mutually exclusive.")
|
2014-03-09 20:51:24 +00:00
|
|
|
|
|
|
|
if options.clientcerts:
|
|
|
|
options.clientcerts = os.path.expanduser(options.clientcerts)
|
|
|
|
if not os.path.exists(options.clientcerts) or not os.path.isdir(options.clientcerts):
|
|
|
|
return parser.error(
|
|
|
|
"Client certificate directory does not exist or is not a directory: %s" % options.clientcerts
|
|
|
|
)
|
|
|
|
|
|
|
|
if (options.auth_nonanonymous or options.auth_singleuser or options.auth_htpasswd):
|
|
|
|
if options.auth_singleuser:
|
|
|
|
if len(options.auth_singleuser.split(':')) != 2:
|
|
|
|
return parser.error("Invalid single-user specification. Please use the format username:password")
|
|
|
|
username, password = options.auth_singleuser.split(':')
|
|
|
|
password_manager = http_auth.PassManSingleUser(username, password)
|
|
|
|
elif options.auth_nonanonymous:
|
|
|
|
password_manager = http_auth.PassManNonAnon()
|
|
|
|
elif options.auth_htpasswd:
|
|
|
|
try:
|
|
|
|
password_manager = http_auth.PassManHtpasswd(options.auth_htpasswd)
|
|
|
|
except ValueError, v:
|
|
|
|
return parser.error(v.message)
|
|
|
|
authenticator = http_auth.BasicProxyAuth(password_manager, "mitmproxy")
|
|
|
|
else:
|
|
|
|
authenticator = http_auth.NullProxyAuth(None)
|
|
|
|
|
|
|
|
certs = []
|
|
|
|
for i in options.certs:
|
|
|
|
parts = i.split("=", 1)
|
|
|
|
if len(parts) == 1:
|
|
|
|
parts = ["*", parts[0]]
|
|
|
|
parts[1] = os.path.expanduser(parts[1])
|
|
|
|
if not os.path.exists(parts[1]):
|
2014-08-08 02:43:44 +00:00
|
|
|
parser.error("Certificate file does not exist: %s" % parts[1])
|
2014-03-09 20:51:24 +00:00
|
|
|
certs.append(parts)
|
|
|
|
|
|
|
|
return ProxyConfig(
|
2014-08-08 02:43:44 +00:00
|
|
|
confdir=options.confdir,
|
|
|
|
clientcerts=options.clientcerts,
|
|
|
|
no_upstream_cert=options.no_upstream_cert,
|
|
|
|
body_size_limit=body_size_limit,
|
|
|
|
mode=mode,
|
|
|
|
upstream_server=upstream_server,
|
|
|
|
http_form_in=options.http_form_in,
|
|
|
|
http_form_out=options.http_form_out,
|
2014-08-09 01:03:21 +00:00
|
|
|
ignore=options.ignore,
|
2014-08-08 02:43:44 +00:00
|
|
|
authenticator=authenticator,
|
|
|
|
ciphers=options.ciphers,
|
|
|
|
certs=certs,
|
|
|
|
certforward=options.certforward,
|
2014-03-09 20:51:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def ssl_option_group(parser):
|
|
|
|
group = parser.add_argument_group("SSL")
|
|
|
|
group.add_argument(
|
|
|
|
"--cert", dest='certs', default=[], type=str,
|
2014-08-08 02:43:44 +00:00
|
|
|
metavar="SPEC", action="append",
|
|
|
|
help='Add an SSL certificate. SPEC is of the form "[domain=]path". ' \
|
|
|
|
'The domain may include a wildcard, and is equal to "*" if not specified. ' \
|
|
|
|
'The file at path is a certificate in PEM format. If a private key is included in the PEM, ' \
|
2014-03-09 20:51:24 +00:00
|
|
|
'it is used, else the default key in the conf dir is used. Can be passed multiple times.'
|
|
|
|
)
|
|
|
|
group.add_argument(
|
|
|
|
"--client-certs", action="store",
|
|
|
|
type=str, dest="clientcerts", default=None,
|
|
|
|
help="Client certificate directory."
|
|
|
|
)
|
|
|
|
group.add_argument(
|
|
|
|
"--ciphers", action="store",
|
|
|
|
type=str, dest="ciphers", default=None,
|
|
|
|
help="SSL cipher specification."
|
2014-03-11 00:02:10 +00:00
|
|
|
)
|
|
|
|
group.add_argument(
|
|
|
|
"--cert-forward", action="store_true",
|
|
|
|
dest="certforward", default=False,
|
|
|
|
help="Simply forward SSL certificates from upstream."
|
|
|
|
)
|
2014-03-13 22:43:26 +00:00
|
|
|
group.add_argument(
|
|
|
|
"--no-upstream-cert", default=False,
|
|
|
|
action="store_true", dest="no_upstream_cert",
|
|
|
|
help="Don't connect to upstream server to look up certificate details."
|
2014-08-30 18:15:19 +00:00
|
|
|
)
|
|
|
|
group.add_argument(
|
|
|
|
"--ssl-port", action="append", type=int, dest="ssl_ports", default=TRANSPARENT_SSL_PORTS,
|
|
|
|
metavar="PORT",
|
|
|
|
help="Can be passed multiple times. Specify destination ports which are assumed to be SSL. "
|
|
|
|
"Defaults to %s." % str(TRANSPARENT_SSL_PORTS)
|
2014-03-13 22:43:26 +00:00
|
|
|
)
|