Merge pull request #2454 from mhils/minor-improvements

Minor improvements
This commit is contained in:
Maximilian Hils 2017-07-20 17:45:40 +02:00 committed by GitHub
commit 8526ca9e17
6 changed files with 73 additions and 63 deletions

View File

@ -112,7 +112,7 @@ class Core:
val = sval # type: typing.Union[int, str] val = sval # type: typing.Union[int, str]
if spec == "status_code": if spec == "status_code":
try: try:
val = int(val) val = int(val) # type: ignore
except ValueError as v: except ValueError as v:
raise exceptions.CommandError( raise exceptions.CommandError(
"Status code is not an integer: %s" % val "Status code is not an integer: %s" % val
@ -145,7 +145,7 @@ class Core:
if spec == "status_code": if spec == "status_code":
resp.status_code = val resp.status_code = val
if val in status_codes.RESPONSES: if val in status_codes.RESPONSES:
resp.reason = status_codes.RESPONSES[int(val)] resp.reason = status_codes.RESPONSES[val] # type: ignore
elif spec == "reason": elif spec == "reason":
resp.reason = val resp.reason = val
else: else:

View File

@ -1,4 +1,7 @@
import typing
from mitmproxy import controller from mitmproxy import controller
from mitmproxy import flow
from mitmproxy import http from mitmproxy import http
from mitmproxy import tcp from mitmproxy import tcp
from mitmproxy import websocket from mitmproxy import websocket
@ -8,27 +11,26 @@ Events = frozenset([
"clientdisconnect", "clientdisconnect",
"serverconnect", "serverconnect",
"serverdisconnect", "serverdisconnect",
# TCP
"tcp_start", "tcp_start",
"tcp_message", "tcp_message",
"tcp_error", "tcp_error",
"tcp_end", "tcp_end",
# HTTP
"http_connect", "http_connect",
"request", "request",
"requestheaders", "requestheaders",
"response", "response",
"responseheaders", "responseheaders",
"error", "error",
# WebSocket
"websocket_handshake", "websocket_handshake",
"websocket_start", "websocket_start",
"websocket_message", "websocket_message",
"websocket_error", "websocket_error",
"websocket_end", "websocket_end",
# misc
"next_layer", "next_layer",
"configure", "configure",
"done", "done",
"log", "log",
@ -38,9 +40,10 @@ Events = frozenset([
"update", "update",
]) ])
TEventGenerator = typing.Iterator[typing.Tuple[str, typing.Any]]
def iterate(f):
if isinstance(f, http.HTTPFlow): def _iterate_http(f: http.HTTPFlow) -> TEventGenerator:
if f.request: if f.request:
yield "requestheaders", f yield "requestheaders", f
yield "request", f yield "request", f
@ -49,7 +52,9 @@ def iterate(f):
yield "response", f yield "response", f
if f.error: if f.error:
yield "error", f yield "error", f
elif isinstance(f, websocket.WebSocketFlow):
def _iterate_websocket(f: websocket.WebSocketFlow) -> TEventGenerator:
messages = f.messages messages = f.messages
f.messages = [] f.messages = []
f.reply = controller.DummyReply() f.reply = controller.DummyReply()
@ -60,7 +65,9 @@ def iterate(f):
if f.error: if f.error:
yield "websocket_error", f yield "websocket_error", f
yield "websocket_end", f yield "websocket_end", f
elif isinstance(f, tcp.TCPFlow):
def _iterate_tcp(f: tcp.TCPFlow) -> TEventGenerator:
messages = f.messages messages = f.messages
f.messages = [] f.messages = []
f.reply = controller.DummyReply() f.reply = controller.DummyReply()
@ -71,5 +78,19 @@ def iterate(f):
if f.error: if f.error:
yield "tcp_error", f yield "tcp_error", f
yield "tcp_end", f yield "tcp_end", f
_iterate_map = {
http.HTTPFlow: _iterate_http,
websocket.WebSocketFlow: _iterate_websocket,
tcp.TCPFlow: _iterate_tcp,
} # type: typing.Dict[typing.Type[flow.Flow], typing.Callable[[typing.Any], TEventGenerator]]
def iterate(f: flow.Flow) -> TEventGenerator:
try:
e = _iterate_map[type(f)]
except KeyError as err:
raise TypeError("Unknown flow type: {}".format(f)) from err
else: else:
raise TypeError() yield from e(f)

View File

@ -209,15 +209,11 @@ class Options(optmanager.OptManager):
self.add_option( self.add_option(
"proxyauth", Optional[str], None, "proxyauth", Optional[str], None,
""" """
Require proxy authentication. Value may be "any" to require Require proxy authentication. Format:
authenticaiton but accept any credentials, start with "@" to specify "username:pass",
a path to an Apache htpasswd file, be of the form "any" to accept any user/pass combination,
"username:password", or be of the form "@path" to use an Apache htpasswd file,
"ldap[s]:url_server_ldap:dn_auth:password:dn_subtree", or "ldap[s]:url_server_ldap:dn_auth:password:dn_subtree" for LDAP authentication.
the dn_auth & password is the dn/pass used to authenticate
the dn subtree is the subtree that we will search to find the username
an example would be
"ldap:localhost:cn=default,dc=example,dc=com:password:ou=application,dc=example,dc=com".
""" """
) )
self.add_option( self.add_option(
@ -288,7 +284,7 @@ class Options(optmanager.OptManager):
""" """
Mode can be "regular", "transparent", "socks5", "reverse:SPEC", Mode can be "regular", "transparent", "socks5", "reverse:SPEC",
or "upstream:SPEC". For reverse and upstream proxy modes, SPEC or "upstream:SPEC". For reverse and upstream proxy modes, SPEC
is proxy specification in the form of "http[s]://host[:port]". is host specification in the form of "http[s]://host[:port]".
""" """
) )
self.add_option( self.add_option(
@ -311,9 +307,8 @@ class Options(optmanager.OptManager):
self.add_option( self.add_option(
"http2_priority", bool, False, "http2_priority", bool, False,
""" """
PRIORITY forwarding for HTTP/2 connections. PRIORITY forwarding is PRIORITY forwarding for HTTP/2 connections. Disabled by default to ensure compatibility
disabled by default, because some webservers fail to implement the with misbehaving servers.
RFC properly.
""" """
) )
self.add_option( self.add_option(
@ -337,7 +332,7 @@ class Options(optmanager.OptManager):
self.add_option( self.add_option(
"upstream_auth", Optional[str], None, "upstream_auth", Optional[str], None,
""" """
Add HTTP Basic authentcation to upstream proxy and reverse proxy Add HTTP Basic authentication to upstream proxy and reverse proxy
requests. Format: username:password. requests. Format: username:password.
""" """
) )

View File

@ -2,7 +2,6 @@ import argparse
import os import os
from mitmproxy import options from mitmproxy import options
from mitmproxy import version
CONFIG_PATH = os.path.join(options.CA_DIR, "config.yaml") CONFIG_PATH = os.path.join(options.CA_DIR, "config.yaml")
@ -12,14 +11,9 @@ def common_options(parser, opts):
parser.add_argument( parser.add_argument(
'--version', '--version',
action='store_true', action='store_true',
help="show version number and exit",
dest='version', dest='version',
) )
parser.add_argument(
'--shortversion',
action='version',
help="show program's short version number and exit",
version=version.VERSION
)
parser.add_argument( parser.add_argument(
'--options', '--options',
action='store_true', action='store_true',

View File

@ -71,7 +71,7 @@ def format_address(address: tuple) -> str:
""" """
try: try:
host = ipaddress.ip_address(address[0]) host = ipaddress.ip_address(address[0])
if host.version == 4: if isinstance(host, ipaddress.IPv4Address):
return "{}:{}".format(str(host), address[1]) return "{}:{}".format(str(host), address[1])
# If IPv6 is mapped to IPv4 # If IPv6 is mapped to IPv4
elif host.ipv4_mapped: elif host.ipv4_mapped:

View File

@ -90,7 +90,7 @@ setup(
'dev': [ 'dev': [
"flake8>=3.2.1, <3.4", "flake8>=3.2.1, <3.4",
"Flask>=0.10.1, <0.13", "Flask>=0.10.1, <0.13",
"mypy>=0.501, <0.512", "mypy>=0.501, <0.521",
"pytest-cov>=2.2.1, <3", "pytest-cov>=2.2.1, <3",
"pytest-faulthandler>=1.3.0, <2", "pytest-faulthandler>=1.3.0, <2",
"pytest-timeout>=1.0.0, <2", "pytest-timeout>=1.0.0, <2",