2014-09-08 21:34:43 +00:00
|
|
|
from __future__ import print_function, absolute_import
|
|
|
|
import os
|
|
|
|
import signal
|
|
|
|
import sys
|
|
|
|
import netlib.version
|
|
|
|
from . import version, cmdline
|
|
|
|
from .proxy import process_proxy_options, ProxyServerError
|
|
|
|
from .proxy.server import DummyServer, ProxyServer
|
|
|
|
|
|
|
|
|
2014-11-15 04:47:39 +00:00
|
|
|
# This file is not included in coverage analysis or tests - anything that can be
|
|
|
|
# tested should live elsewhere.
|
|
|
|
|
2014-09-08 21:34:43 +00:00
|
|
|
def check_versions():
|
|
|
|
"""
|
2014-11-15 03:14:08 +00:00
|
|
|
Having installed a wrong version of pyOpenSSL or netlib is unfortunately a
|
|
|
|
very common source of error. Check before every start that both versions are
|
|
|
|
somewhat okay.
|
2014-09-08 21:34:43 +00:00
|
|
|
"""
|
2014-11-15 03:14:08 +00:00
|
|
|
# We don't introduce backward-incompatible changes in patch versions. Only
|
|
|
|
# consider major and minor version.
|
2014-09-08 21:34:43 +00:00
|
|
|
if netlib.version.IVERSION[:2] != version.IVERSION[:2]:
|
|
|
|
print(
|
|
|
|
"Warning: You are using mitmdump %s with netlib %s. "
|
2014-11-15 03:14:08 +00:00
|
|
|
"Most likely, that won't work - please upgrade!" % (
|
|
|
|
version.VERSION, netlib.version.VERSION
|
|
|
|
),
|
|
|
|
file=sys.stderr
|
|
|
|
)
|
|
|
|
import OpenSSL
|
|
|
|
import inspect
|
2014-09-08 21:45:39 +00:00
|
|
|
v = tuple([int(x) for x in OpenSSL.__version__.split(".")][:2])
|
2014-09-08 21:34:43 +00:00
|
|
|
if v < (0, 14):
|
2014-11-15 03:14:08 +00:00
|
|
|
print(
|
|
|
|
"You are using an outdated version of pyOpenSSL:"
|
|
|
|
" mitmproxy requires pyOpenSSL 0.14 or greater.",
|
|
|
|
file=sys.stderr
|
|
|
|
)
|
|
|
|
# Some users apparently have multiple versions of pyOpenSSL installed.
|
|
|
|
# Report which one we got.
|
2014-09-08 21:34:43 +00:00
|
|
|
pyopenssl_path = os.path.dirname(inspect.getfile(OpenSSL))
|
2014-11-15 03:14:08 +00:00
|
|
|
print(
|
|
|
|
"Your pyOpenSSL %s installation is located at %s" % (
|
|
|
|
OpenSSL.__version__, pyopenssl_path
|
|
|
|
),
|
|
|
|
file=sys.stderr
|
|
|
|
)
|
2014-09-08 21:34:43 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
def assert_utf8_env():
|
|
|
|
spec = ""
|
|
|
|
for i in ["LANG", "LC_CTYPE", "LC_ALL"]:
|
|
|
|
spec += os.environ.get(i, "").lower()
|
|
|
|
if "utf" not in spec:
|
2014-11-15 03:14:08 +00:00
|
|
|
print(
|
|
|
|
"Error: mitmproxy requires a UTF console environment.",
|
|
|
|
file=sys.stderr
|
|
|
|
)
|
|
|
|
print(
|
|
|
|
"Set your LANG enviroment variable to something like en_US.UTF-8",
|
|
|
|
file=sys.stderr
|
|
|
|
)
|
2014-09-08 21:34:43 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
def get_server(dummy_server, options):
|
|
|
|
if dummy_server:
|
|
|
|
return DummyServer(options)
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
return ProxyServer(options)
|
|
|
|
except ProxyServerError, v:
|
|
|
|
print(str(v), file=sys.stderr)
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
2015-04-07 22:20:32 +00:00
|
|
|
def mitmproxy(args=None): # pragma: nocover
|
2014-09-08 21:34:43 +00:00
|
|
|
from . import console
|
|
|
|
|
2014-11-15 03:29:38 +00:00
|
|
|
check_versions()
|
|
|
|
assert_utf8_env()
|
2014-09-08 21:34:43 +00:00
|
|
|
|
2014-11-15 03:29:38 +00:00
|
|
|
parser = cmdline.mitmproxy()
|
2015-04-07 22:20:32 +00:00
|
|
|
options = parser.parse_args(args)
|
2014-09-08 21:34:43 +00:00
|
|
|
if options.quiet:
|
|
|
|
options.verbose = 0
|
|
|
|
|
|
|
|
proxy_config = process_proxy_options(parser, options)
|
|
|
|
console_options = console.Options(**cmdline.get_common_options(options))
|
|
|
|
console_options.palette = options.palette
|
2015-04-06 22:01:18 +00:00
|
|
|
console_options.palette_transparent = options.palette_transparent
|
2014-09-08 21:34:43 +00:00
|
|
|
console_options.eventlog = options.eventlog
|
|
|
|
console_options.intercept = options.intercept
|
2015-04-15 00:56:43 +00:00
|
|
|
console_options.limit = options.limit
|
2014-09-08 21:34:43 +00:00
|
|
|
|
|
|
|
server = get_server(console_options.no_server, proxy_config)
|
|
|
|
|
|
|
|
m = console.ConsoleMaster(server, console_options)
|
|
|
|
try:
|
|
|
|
m.run()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2015-04-07 22:20:32 +00:00
|
|
|
def mitmdump(args=None): # pragma: nocover
|
2014-09-08 21:34:43 +00:00
|
|
|
from . import dump
|
|
|
|
|
2014-11-15 03:29:38 +00:00
|
|
|
check_versions()
|
2014-09-08 21:34:43 +00:00
|
|
|
|
2014-11-15 03:29:38 +00:00
|
|
|
parser = cmdline.mitmdump()
|
2015-04-07 22:20:32 +00:00
|
|
|
options = parser.parse_args(args)
|
2014-09-08 21:34:43 +00:00
|
|
|
if options.quiet:
|
|
|
|
options.verbose = 0
|
|
|
|
options.flow_detail = 0
|
|
|
|
|
|
|
|
proxy_config = process_proxy_options(parser, options)
|
|
|
|
dump_options = dump.Options(**cmdline.get_common_options(options))
|
|
|
|
dump_options.flow_detail = options.flow_detail
|
|
|
|
dump_options.keepserving = options.keepserving
|
|
|
|
dump_options.filtstr = " ".join(options.args) if options.args else None
|
|
|
|
|
|
|
|
server = get_server(dump_options.no_server, proxy_config)
|
|
|
|
|
|
|
|
try:
|
|
|
|
master = dump.DumpMaster(server, dump_options)
|
|
|
|
|
|
|
|
def cleankill(*args, **kwargs):
|
|
|
|
master.shutdown()
|
|
|
|
|
|
|
|
signal.signal(signal.SIGTERM, cleankill)
|
|
|
|
master.run()
|
|
|
|
except dump.DumpError as e:
|
|
|
|
print("mitmdump: %s" % e, file=sys.stderr)
|
|
|
|
sys.exit(1)
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
pass
|
|
|
|
|
2014-09-13 23:30:00 +00:00
|
|
|
|
2015-04-07 22:20:32 +00:00
|
|
|
def mitmweb(args=None): # pragma: nocover
|
2014-09-13 23:30:00 +00:00
|
|
|
from . import web
|
2014-09-14 00:22:28 +00:00
|
|
|
|
2014-11-15 03:29:38 +00:00
|
|
|
check_versions()
|
|
|
|
parser = cmdline.mitmweb()
|
2014-09-13 23:30:00 +00:00
|
|
|
|
2015-04-07 22:20:32 +00:00
|
|
|
options = parser.parse_args(args)
|
2014-09-13 23:30:00 +00:00
|
|
|
if options.quiet:
|
|
|
|
options.verbose = 0
|
|
|
|
|
|
|
|
proxy_config = process_proxy_options(parser, options)
|
|
|
|
web_options = web.Options(**cmdline.get_common_options(options))
|
2014-09-14 00:22:28 +00:00
|
|
|
web_options.intercept = options.intercept
|
|
|
|
web_options.wdebug = options.wdebug
|
|
|
|
web_options.wiface = options.wiface
|
|
|
|
web_options.wport = options.wport
|
2014-09-13 23:30:00 +00:00
|
|
|
|
|
|
|
server = get_server(web_options.no_server, proxy_config)
|
|
|
|
|
|
|
|
m = web.WebMaster(server, web_options)
|
|
|
|
try:
|
|
|
|
m.run()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
pass
|