mitmproxy: fix most flake8 offenses

This commit is contained in:
Thomas Kriechbaumer 2016-05-28 22:39:30 +02:00
parent e4045dc7f8
commit 7971dce223
12 changed files with 36 additions and 19 deletions

View File

@ -6,6 +6,7 @@ from .exceptions import Kill
class Master(object): class Master(object):
""" """
The master handles mitmproxy's main event loop. The master handles mitmproxy's main event loop.
""" """
@ -48,6 +49,7 @@ class Master(object):
class ServerMaster(Master): class ServerMaster(Master):
""" """
The ServerMaster adds server thread support to the master. The ServerMaster adds server thread support to the master.
""" """
@ -74,6 +76,7 @@ class ServerMaster(Master):
class ServerThread(threading.Thread): class ServerThread(threading.Thread):
def __init__(self, server): def __init__(self, server):
self.server = server self.server = server
super(ServerThread, self).__init__() super(ServerThread, self).__init__()
@ -85,6 +88,7 @@ class ServerThread(threading.Thread):
class Channel(object): class Channel(object):
""" """
The only way for the proxy server to communicate with the master The only way for the proxy server to communicate with the master
is to use the channel it has been given. is to use the channel it has been given.
@ -126,6 +130,7 @@ class Channel(object):
class DummyReply(object): class DummyReply(object):
""" """
A reply object that does nothing. Useful when we need an object to seem A reply object that does nothing. Useful when we need an object to seem
like it has a channel, and during testing. like it has a channel, and during testing.
@ -143,6 +148,7 @@ NO_REPLY = object()
class Reply(object): class Reply(object):
""" """
Messages sent through a channel are decorated with a "reply" attribute. Messages sent through a channel are decorated with a "reply" attribute.
This object is used to respond to the message through the return This object is used to respond to the message through the return

View File

@ -74,8 +74,8 @@ class DumpMaster(flow.FlowMaster):
if self.server and self.server.config.http2 and not tcp.HAS_ALPN: # pragma: no cover if self.server and self.server.config.http2 and not tcp.HAS_ALPN: # pragma: no cover
print("ALPN support missing (OpenSSL 1.0.2+ required)!\n" print("ALPN support missing (OpenSSL 1.0.2+ required)!\n"
"HTTP/2 is disabled. Use --no-http2 to silence this warning.", "HTTP/2 is disabled. Use --no-http2 to silence this warning.",
file=sys.stderr) file=sys.stderr)
if options.filtstr: if options.filtstr:
self.filt = filt.parse(options.filtstr) self.filt = filt.parse(options.filtstr)

View File

@ -13,6 +13,7 @@ import sys
class ProxyException(Exception): class ProxyException(Exception):
""" """
Base class for all exceptions thrown by mitmproxy. Base class for all exceptions thrown by mitmproxy.
""" """
@ -22,6 +23,7 @@ class ProxyException(Exception):
class Kill(ProxyException): class Kill(ProxyException):
""" """
Signal that both client and server connection(s) should be killed immediately. Signal that both client and server connection(s) should be killed immediately.
""" """
@ -37,6 +39,7 @@ class TlsProtocolException(ProtocolException):
class ClientHandshakeException(TlsProtocolException): class ClientHandshakeException(TlsProtocolException):
def __init__(self, message, server): def __init__(self, message, server):
super(ClientHandshakeException, self).__init__(message) super(ClientHandshakeException, self).__init__(message)
self.server = server self.server = server
@ -67,6 +70,7 @@ class ReplayException(ProxyException):
class ScriptException(ProxyException): class ScriptException(ProxyException):
@classmethod @classmethod
def from_exception_context(cls, cut_tb=1): def from_exception_context(cls, cut_tb=1):
""" """

View File

@ -38,6 +38,7 @@ import pyparsing as pp
class _Token(object): class _Token(object):
def dump(self, indent=0, fp=sys.stdout): def dump(self, indent=0, fp=sys.stdout):
print("{spacing}{name}{expr}".format( print("{spacing}{name}{expr}".format(
spacing="\t" * indent, spacing="\t" * indent,

View File

@ -8,6 +8,7 @@ import re
from six.moves.urllib.parse import quote from six.moves.urllib.parse import quote
from six.moves.urllib.parse import quote_plus from six.moves.urllib.parse import quote_plus
def curl_command(flow): def curl_command(flow):
data = "curl " data = "curl "
@ -124,13 +125,12 @@ def locust_code(flow):
max_wait = 3000 max_wait = 3000
""").strip() """).strip()
components = map(lambda x: quote(x, safe=""), flow.request.path_components) components = map(lambda x: quote(x, safe=""), flow.request.path_components)
file_name = "_".join(components) file_name = "_".join(components)
name = re.sub('\W|^(?=\d)', '_', file_name) name = re.sub('\W|^(?=\d)', '_', file_name)
url = flow.request.scheme + "://" + flow.request.host + "/" + "/".join(components) url = flow.request.scheme + "://" + flow.request.host + "/" + "/".join(components)
if name == "" or name is None: if name == "" or name is None:
new_name = "_".join([str(flow.request.host) , str(flow.request.timestamp_start)]) new_name = "_".join([str(flow.request.host), str(flow.request.timestamp_start)])
name = re.sub('\W|^(?=\d)', '_', new_name) name = re.sub('\W|^(?=\d)', '_', new_name)
args = "" args = ""
headers = "" headers = ""

View File

@ -10,6 +10,7 @@ from .. import stateobject, utils
class ClientConnection(tcp.BaseHandler, stateobject.StateObject): class ClientConnection(tcp.BaseHandler, stateobject.StateObject):
""" """
A client connection A client connection
@ -21,6 +22,7 @@ class ClientConnection(tcp.BaseHandler, stateobject.StateObject):
timestamp_ssl_setup: TLS established timestamp timestamp_ssl_setup: TLS established timestamp
timestamp_end: Connection end timestamp timestamp_end: Connection end timestamp
""" """
def __init__(self, client_connection, address, server): def __init__(self, client_connection, address, server):
# Eventually, this object is restored from state. We don't have a # Eventually, this object is restored from state. We don't have a
# connection then. # connection then.
@ -101,6 +103,7 @@ class ClientConnection(tcp.BaseHandler, stateobject.StateObject):
class ServerConnection(tcp.TCPClient, stateobject.StateObject): class ServerConnection(tcp.TCPClient, stateobject.StateObject):
""" """
A server connection A server connection
@ -117,6 +120,7 @@ class ServerConnection(tcp.TCPClient, stateobject.StateObject):
timestamp_ssl_setup: TLS established timestamp timestamp_ssl_setup: TLS established timestamp
timestamp_end: Connection end timestamp timestamp_end: Connection end timestamp
""" """
def __init__(self, address, source_address=None): def __init__(self, address, source_address=None):
tcp.TCPClient.__init__(self, address, source_address) tcp.TCPClient.__init__(self, address, source_address)
@ -182,7 +186,7 @@ class ServerConnection(tcp.TCPClient, stateobject.StateObject):
timestamp_ssl_setup=None, timestamp_ssl_setup=None,
timestamp_end=None, timestamp_end=None,
via=None via=None
)) ))
def copy(self): def copy(self):
return copy.copy(self) return copy.copy(self)

View File

@ -72,9 +72,9 @@ class HTTPRequest(MessageMixin, Request):
def get_state(self): def get_state(self):
state = super(HTTPRequest, self).get_state() state = super(HTTPRequest, self).get_state()
state.update( state.update(
stickycookie = self.stickycookie, stickycookie=self.stickycookie,
stickyauth = self.stickyauth, stickyauth=self.stickyauth,
is_replay = self.is_replay, is_replay=self.is_replay,
) )
return state return state
@ -109,6 +109,7 @@ class HTTPRequest(MessageMixin, Request):
class HTTPResponse(MessageMixin, Response): class HTTPResponse(MessageMixin, Response):
""" """
A mitmproxy HTTP response. A mitmproxy HTTP response.
This is a very thin wrapper on top of :py:class:`netlib.http.Response` and This is a very thin wrapper on top of :py:class:`netlib.http.Response` and
@ -124,7 +125,7 @@ class HTTPResponse(MessageMixin, Response):
content, content,
timestamp_start=None, timestamp_start=None,
timestamp_end=None, timestamp_end=None,
is_replay = False is_replay=False
): ):
Response.__init__( Response.__init__(
self, self,

View File

@ -6,6 +6,7 @@ from .flow import Flow
class TCPMessage(Serializable): class TCPMessage(Serializable):
def __init__(self, from_client, content, timestamp=None): def __init__(self, from_client, content, timestamp=None):
self.content = content self.content = content
self.from_client = from_client self.from_client = from_client
@ -33,6 +34,7 @@ class TCPMessage(Serializable):
class TCPFlow(Flow): class TCPFlow(Flow):
""" """
A TCPFlow is a simplified representation of a TCP session. A TCPFlow is a simplified representation of a TCP session.
""" """

View File

@ -62,7 +62,7 @@ class RequestReplayThread(threading.Thread):
) )
r.first_line_format = "relative" r.first_line_format = "relative"
else: else:
r.first_line_format= "absolute" r.first_line_format = "absolute"
else: else:
server_address = (r.host, r.port) server_address = (r.host, r.port)
server = ServerConnection(server_address, (self.config.host, 0)) server = ServerConnection(server_address, (self.config.host, 0))

View File

@ -312,6 +312,7 @@ class TlsClientHello(object):
class TlsLayer(Layer): class TlsLayer(Layer):
""" """
The TLS layer implements transparent TLS connections. The TLS layer implements transparent TLS connections.
@ -469,9 +470,9 @@ class TlsLayer(Layer):
cert, key, chain_file = self._find_cert() cert, key, chain_file = self._find_cert()
if self.config.add_upstream_certs_to_client_chain: if self.config.add_upstream_certs_to_client_chain:
extra_certs = self.server_conn.server_certs extra_certs = self.server_conn.server_certs
else: else:
extra_certs = None extra_certs = None
try: try:
self.client_conn.convert_to_ssl( self.client_conn.convert_to_ssl(
@ -482,7 +483,7 @@ class TlsLayer(Layer):
dhparams=self.config.certstore.dhparams, dhparams=self.config.certstore.dhparams,
chain_file=chain_file, chain_file=chain_file,
alpn_select_callback=self.__alpn_select_callback, alpn_select_callback=self.__alpn_select_callback,
extra_chain_certs = extra_certs, extra_chain_certs=extra_certs,
) )
# Some TLS clients will not fail the handshake, # Some TLS clients will not fail the handshake,
# but will immediately throw an "unexpected eof" error on the first read. # but will immediately throw an "unexpected eof" error on the first read.

View File

@ -58,7 +58,7 @@ class ProxyConfig:
body_size_limit=None, body_size_limit=None,
mode="regular", mode="regular",
upstream_server=None, upstream_server=None,
upstream_auth = None, upstream_auth=None,
authenticator=None, authenticator=None,
ignore_hosts=tuple(), ignore_hosts=tuple(),
tcp_hosts=tuple(), tcp_hosts=tuple(),
@ -120,7 +120,7 @@ def process_proxy_options(parser, options):
body_size_limit = utils.parse_size(options.body_size_limit) body_size_limit = utils.parse_size(options.body_size_limit)
c = 0 c = 0
mode, upstream_server, upstream_auth = "regular", None, None mode, upstream_server, upstream_auth = "regular", None, None
if options.transparent_proxy: if options.transparent_proxy:
c += 1 c += 1
if not platform.resolver: if not platform.resolver:
@ -161,7 +161,7 @@ def process_proxy_options(parser, options):
options.clientcerts = os.path.expanduser(options.clientcerts) options.clientcerts = os.path.expanduser(options.clientcerts)
if not os.path.exists(options.clientcerts): if not os.path.exists(options.clientcerts):
return parser.error( return parser.error(
"Client certificate path does not exist: %s" % options.clientcerts "Client certificate path does not exist: %s" % options.clientcerts
) )
if options.auth_nonanonymous or options.auth_singleuser or options.auth_htpasswd: if options.auth_nonanonymous or options.auth_singleuser or options.auth_htpasswd:

View File

@ -68,6 +68,7 @@ like so::
""" """
import six import six
from collections import deque
__ver_major__ = 0 __ver_major__ = 0
__ver_minor__ = 2 __ver_minor__ = 2
@ -77,9 +78,6 @@ __version__ = "%d.%d.%d%s" % (
__ver_major__, __ver_minor__, __ver_patch__, __ver_sub__) __ver_major__, __ver_minor__, __ver_patch__, __ver_sub__)
from collections import deque
def dumps(value, encoding=None): def dumps(value, encoding=None):
"""dumps(object,encoding=None) -> string """dumps(object,encoding=None) -> string