From 7971dce2231bc32c25b962d425d8ad935568a699 Mon Sep 17 00:00:00 2001 From: Thomas Kriechbaumer Date: Sat, 28 May 2016 22:39:30 +0200 Subject: [PATCH] mitmproxy: fix most flake8 offenses --- mitmproxy/controller.py | 6 ++++++ mitmproxy/dump.py | 4 ++-- mitmproxy/exceptions.py | 4 ++++ mitmproxy/filt.py | 1 + mitmproxy/flow_export.py | 4 ++-- mitmproxy/models/connections.py | 6 +++++- mitmproxy/models/http.py | 9 +++++---- mitmproxy/models/tcp.py | 2 ++ mitmproxy/protocol/http_replay.py | 2 +- mitmproxy/protocol/tls.py | 7 ++++--- mitmproxy/proxy/config.py | 6 +++--- mitmproxy/tnetstring.py | 4 +--- 12 files changed, 36 insertions(+), 19 deletions(-) diff --git a/mitmproxy/controller.py b/mitmproxy/controller.py index 81978a098..af8a77bd3 100644 --- a/mitmproxy/controller.py +++ b/mitmproxy/controller.py @@ -6,6 +6,7 @@ from .exceptions import Kill class Master(object): + """ The master handles mitmproxy's main event loop. """ @@ -48,6 +49,7 @@ class Master(object): class ServerMaster(Master): + """ The ServerMaster adds server thread support to the master. """ @@ -74,6 +76,7 @@ class ServerMaster(Master): class ServerThread(threading.Thread): + def __init__(self, server): self.server = server super(ServerThread, self).__init__() @@ -85,6 +88,7 @@ class ServerThread(threading.Thread): class Channel(object): + """ The only way for the proxy server to communicate with the master is to use the channel it has been given. @@ -126,6 +130,7 @@ class Channel(object): class DummyReply(object): + """ A reply object that does nothing. Useful when we need an object to seem like it has a channel, and during testing. @@ -143,6 +148,7 @@ NO_REPLY = object() class Reply(object): + """ Messages sent through a channel are decorated with a "reply" attribute. This object is used to respond to the message through the return diff --git a/mitmproxy/dump.py b/mitmproxy/dump.py index f1eabdb8c..8f9488bea 100644 --- a/mitmproxy/dump.py +++ b/mitmproxy/dump.py @@ -74,8 +74,8 @@ class DumpMaster(flow.FlowMaster): 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" - "HTTP/2 is disabled. Use --no-http2 to silence this warning.", - file=sys.stderr) + "HTTP/2 is disabled. Use --no-http2 to silence this warning.", + file=sys.stderr) if options.filtstr: self.filt = filt.parse(options.filtstr) diff --git a/mitmproxy/exceptions.py b/mitmproxy/exceptions.py index 8f989063f..7e3e6d869 100644 --- a/mitmproxy/exceptions.py +++ b/mitmproxy/exceptions.py @@ -13,6 +13,7 @@ import sys class ProxyException(Exception): + """ Base class for all exceptions thrown by mitmproxy. """ @@ -22,6 +23,7 @@ class ProxyException(Exception): class Kill(ProxyException): + """ Signal that both client and server connection(s) should be killed immediately. """ @@ -37,6 +39,7 @@ class TlsProtocolException(ProtocolException): class ClientHandshakeException(TlsProtocolException): + def __init__(self, message, server): super(ClientHandshakeException, self).__init__(message) self.server = server @@ -67,6 +70,7 @@ class ReplayException(ProxyException): class ScriptException(ProxyException): + @classmethod def from_exception_context(cls, cut_tb=1): """ diff --git a/mitmproxy/filt.py b/mitmproxy/filt.py index f34969dde..6228d3646 100644 --- a/mitmproxy/filt.py +++ b/mitmproxy/filt.py @@ -38,6 +38,7 @@ import pyparsing as pp class _Token(object): + def dump(self, indent=0, fp=sys.stdout): print("{spacing}{name}{expr}".format( spacing="\t" * indent, diff --git a/mitmproxy/flow_export.py b/mitmproxy/flow_export.py index 2ac33e152..0f3847f1c 100644 --- a/mitmproxy/flow_export.py +++ b/mitmproxy/flow_export.py @@ -8,6 +8,7 @@ import re from six.moves.urllib.parse import quote from six.moves.urllib.parse import quote_plus + def curl_command(flow): data = "curl " @@ -124,13 +125,12 @@ def locust_code(flow): max_wait = 3000 """).strip() - components = map(lambda x: quote(x, safe=""), flow.request.path_components) file_name = "_".join(components) name = re.sub('\W|^(?=\d)', '_', file_name) url = flow.request.scheme + "://" + flow.request.host + "/" + "/".join(components) 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) args = "" headers = "" diff --git a/mitmproxy/models/connections.py b/mitmproxy/models/connections.py index 91590bcae..24cdff747 100644 --- a/mitmproxy/models/connections.py +++ b/mitmproxy/models/connections.py @@ -10,6 +10,7 @@ from .. import stateobject, utils class ClientConnection(tcp.BaseHandler, stateobject.StateObject): + """ A client connection @@ -21,6 +22,7 @@ class ClientConnection(tcp.BaseHandler, stateobject.StateObject): timestamp_ssl_setup: TLS established timestamp timestamp_end: Connection end timestamp """ + def __init__(self, client_connection, address, server): # Eventually, this object is restored from state. We don't have a # connection then. @@ -101,6 +103,7 @@ class ClientConnection(tcp.BaseHandler, stateobject.StateObject): class ServerConnection(tcp.TCPClient, stateobject.StateObject): + """ A server connection @@ -117,6 +120,7 @@ class ServerConnection(tcp.TCPClient, stateobject.StateObject): timestamp_ssl_setup: TLS established timestamp timestamp_end: Connection end timestamp """ + def __init__(self, address, source_address=None): tcp.TCPClient.__init__(self, address, source_address) @@ -182,7 +186,7 @@ class ServerConnection(tcp.TCPClient, stateobject.StateObject): timestamp_ssl_setup=None, timestamp_end=None, via=None - )) + )) def copy(self): return copy.copy(self) diff --git a/mitmproxy/models/http.py b/mitmproxy/models/http.py index 75ffbfd0a..43618ef80 100644 --- a/mitmproxy/models/http.py +++ b/mitmproxy/models/http.py @@ -72,9 +72,9 @@ class HTTPRequest(MessageMixin, Request): def get_state(self): state = super(HTTPRequest, self).get_state() state.update( - stickycookie = self.stickycookie, - stickyauth = self.stickyauth, - is_replay = self.is_replay, + stickycookie=self.stickycookie, + stickyauth=self.stickyauth, + is_replay=self.is_replay, ) return state @@ -109,6 +109,7 @@ class HTTPRequest(MessageMixin, Request): class HTTPResponse(MessageMixin, Response): + """ A mitmproxy HTTP response. This is a very thin wrapper on top of :py:class:`netlib.http.Response` and @@ -124,7 +125,7 @@ class HTTPResponse(MessageMixin, Response): content, timestamp_start=None, timestamp_end=None, - is_replay = False + is_replay=False ): Response.__init__( self, diff --git a/mitmproxy/models/tcp.py b/mitmproxy/models/tcp.py index 7e966b95c..b87a74acf 100644 --- a/mitmproxy/models/tcp.py +++ b/mitmproxy/models/tcp.py @@ -6,6 +6,7 @@ from .flow import Flow class TCPMessage(Serializable): + def __init__(self, from_client, content, timestamp=None): self.content = content self.from_client = from_client @@ -33,6 +34,7 @@ class TCPMessage(Serializable): class TCPFlow(Flow): + """ A TCPFlow is a simplified representation of a TCP session. """ diff --git a/mitmproxy/protocol/http_replay.py b/mitmproxy/protocol/http_replay.py index e78af074d..f7f271482 100644 --- a/mitmproxy/protocol/http_replay.py +++ b/mitmproxy/protocol/http_replay.py @@ -62,7 +62,7 @@ class RequestReplayThread(threading.Thread): ) r.first_line_format = "relative" else: - r.first_line_format= "absolute" + r.first_line_format = "absolute" else: server_address = (r.host, r.port) server = ServerConnection(server_address, (self.config.host, 0)) diff --git a/mitmproxy/protocol/tls.py b/mitmproxy/protocol/tls.py index 74c55ab47..5facff731 100644 --- a/mitmproxy/protocol/tls.py +++ b/mitmproxy/protocol/tls.py @@ -312,6 +312,7 @@ class TlsClientHello(object): class TlsLayer(Layer): + """ The TLS layer implements transparent TLS connections. @@ -469,9 +470,9 @@ class TlsLayer(Layer): cert, key, chain_file = self._find_cert() if self.config.add_upstream_certs_to_client_chain: - extra_certs = self.server_conn.server_certs + extra_certs = self.server_conn.server_certs else: - extra_certs = None + extra_certs = None try: self.client_conn.convert_to_ssl( @@ -482,7 +483,7 @@ class TlsLayer(Layer): dhparams=self.config.certstore.dhparams, chain_file=chain_file, 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, # but will immediately throw an "unexpected eof" error on the first read. diff --git a/mitmproxy/proxy/config.py b/mitmproxy/proxy/config.py index 58b568ea8..5587e111b 100644 --- a/mitmproxy/proxy/config.py +++ b/mitmproxy/proxy/config.py @@ -58,7 +58,7 @@ class ProxyConfig: body_size_limit=None, mode="regular", upstream_server=None, - upstream_auth = None, + upstream_auth=None, authenticator=None, ignore_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) c = 0 - mode, upstream_server, upstream_auth = "regular", None, None + mode, upstream_server, upstream_auth = "regular", None, None if options.transparent_proxy: c += 1 if not platform.resolver: @@ -161,7 +161,7 @@ def process_proxy_options(parser, options): options.clientcerts = os.path.expanduser(options.clientcerts) if not os.path.exists(options.clientcerts): 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: diff --git a/mitmproxy/tnetstring.py b/mitmproxy/tnetstring.py index d9d61258b..acf563ac0 100644 --- a/mitmproxy/tnetstring.py +++ b/mitmproxy/tnetstring.py @@ -68,6 +68,7 @@ like so:: """ import six +from collections import deque __ver_major__ = 0 __ver_minor__ = 2 @@ -77,9 +78,6 @@ __version__ = "%d.%d.%d%s" % ( __ver_major__, __ver_minor__, __ver_patch__, __ver_sub__) -from collections import deque - - def dumps(value, encoding=None): """dumps(object,encoding=None) -> string