diff options
author | Aldo Cortesi <aldo@nullcube.com> | 2016-10-20 11:27:05 +1300 |
---|---|---|
committer | Aldo Cortesi <aldo@nullcube.com> | 2016-10-20 11:27:05 +1300 |
commit | 01a449b5cb1106a867a6b73cd4877e9b2ec68171 (patch) | |
tree | 5a213fb4b8199d525079a55b64d6d232380be341 /mitmproxy | |
parent | 301d52d9d05f2c5f074fe68c73acc1c32e518020 (diff) | |
download | mitmproxy-01a449b5cb1106a867a6b73cd4877e9b2ec68171.tar.gz mitmproxy-01a449b5cb1106a867a6b73cd4877e9b2ec68171.tar.bz2 mitmproxy-01a449b5cb1106a867a6b73cd4877e9b2ec68171.zip |
netlib.exceptions.* -> mitmproxy.exceptions
Diffstat (limited to 'mitmproxy')
-rw-r--r-- | mitmproxy/addons/streambodies.py | 2 | ||||
-rw-r--r-- | mitmproxy/exceptions.py | 62 | ||||
-rw-r--r-- | mitmproxy/proxy/modes/socks_proxy.py | 3 | ||||
-rw-r--r-- | mitmproxy/proxy/protocol/base.py | 3 | ||||
-rw-r--r-- | mitmproxy/proxy/protocol/http.py | 19 | ||||
-rw-r--r-- | mitmproxy/proxy/protocol/http2.py | 1 | ||||
-rw-r--r-- | mitmproxy/proxy/protocol/http_replay.py | 3 | ||||
-rw-r--r-- | mitmproxy/proxy/protocol/rawtcp.py | 3 | ||||
-rw-r--r-- | mitmproxy/proxy/protocol/tls.py | 7 | ||||
-rw-r--r-- | mitmproxy/proxy/protocol/websockets.py | 3 | ||||
-rw-r--r-- | mitmproxy/proxy/root_context.py | 3 | ||||
-rw-r--r-- | mitmproxy/proxy/server.py | 3 |
12 files changed, 81 insertions, 31 deletions
diff --git a/mitmproxy/addons/streambodies.py b/mitmproxy/addons/streambodies.py index b3e5d6b2..bd8958b0 100644 --- a/mitmproxy/addons/streambodies.py +++ b/mitmproxy/addons/streambodies.py @@ -1,5 +1,5 @@ from netlib.http import http1 -from netlib import exceptions +from mitmproxy import exceptions from mitmproxy import ctx diff --git a/mitmproxy/exceptions.py b/mitmproxy/exceptions.py index 64cc457a..82022d31 100644 --- a/mitmproxy/exceptions.py +++ b/mitmproxy/exceptions.py @@ -1,7 +1,10 @@ """ We try to be very hygienic regarding the exceptions we throw: -Every Exception mitmproxy raises shall be a subclass of ProxyException. +- Every exception that might be externally visible to users shall be a subclass + of ProxyException.p +- Every exception in the base net module shall be a subclass + of NetlibException, and will not be propagated directly to users. See also: http://lucumr.pocoo.org/2014/10/16/on-error-handling/ """ @@ -100,3 +103,60 @@ class AddonError(Exception): class AddonHalt(Exception): pass + + +""" + Every net Exception raised shall be a subclass of NetlibException. +""" + + +class NetlibException(Exception): + """ + Base class for all exceptions thrown by netlib. + """ + def __init__(self, message=None): + super().__init__(message) + + +class Disconnect: + """Immediate EOF""" + + +class HttpException(NetlibException): + pass + + +class HttpReadDisconnect(HttpException, Disconnect): + pass + + +class HttpSyntaxException(HttpException): + pass + + +class TcpException(NetlibException): + pass + + +class TcpDisconnect(TcpException, Disconnect): + pass + + +class TcpReadIncomplete(TcpException): + pass + + +class TcpTimeout(TcpException): + pass + + +class TlsException(NetlibException): + pass + + +class InvalidCertificateException(TlsException): + pass + + +class Timeout(TcpException): + pass diff --git a/mitmproxy/proxy/modes/socks_proxy.py b/mitmproxy/proxy/modes/socks_proxy.py index 4f8473bd..175453b0 100644 --- a/mitmproxy/proxy/modes/socks_proxy.py +++ b/mitmproxy/proxy/modes/socks_proxy.py @@ -1,4 +1,3 @@ -import netlib.exceptions from mitmproxy import exceptions from mitmproxy.proxy import protocol from netlib import socks @@ -48,7 +47,7 @@ class Socks5Proxy(protocol.Layer, protocol.ServerConnectionMixin): connect_reply.to_file(self.client_conn.wfile) self.client_conn.wfile.flush() - except (socks.SocksError, netlib.exceptions.TcpException) as e: + except (socks.SocksError, exceptions.TcpException) as e: raise exceptions.Socks5ProtocolException("SOCKS5 mode failure: %s" % repr(e)) self.server_conn.address = connect_request.addr diff --git a/mitmproxy/proxy/protocol/base.py b/mitmproxy/proxy/protocol/base.py index 00d50721..97e90051 100644 --- a/mitmproxy/proxy/protocol/base.py +++ b/mitmproxy/proxy/protocol/base.py @@ -1,4 +1,3 @@ -import netlib.exceptions from mitmproxy import exceptions from mitmproxy import connections @@ -177,7 +176,7 @@ class ServerConnectionMixin: self.channel.ask("serverconnect", self.server_conn) try: self.server_conn.connect() - except netlib.exceptions.TcpException as e: + except exceptions.TcpException as e: raise exceptions.ProtocolException( "Server connection to {} failed: {}".format( repr(self.server_conn.address), str(e) diff --git a/mitmproxy/proxy/protocol/http.py b/mitmproxy/proxy/protocol/http.py index ec018f89..6bc06502 100644 --- a/mitmproxy/proxy/protocol/http.py +++ b/mitmproxy/proxy/protocol/http.py @@ -1,5 +1,4 @@ import h2.exceptions -import netlib.exceptions import time import traceback from mitmproxy import exceptions @@ -46,7 +45,7 @@ class _HttpTransmissionLayer(base.Layer): def send_response(self, response): if response.data.content is None: - raise netlib.exceptions.HttpException("Cannot assemble flow with missing content") + raise exceptions.HttpException("Cannot assemble flow with missing content") self.send_response_headers(response) self.send_response_body(response, [response.data.content]) @@ -146,10 +145,10 @@ class HttpLayer(base.Layer): request = self.get_request_from_client(f) # Make sure that the incoming request matches our expectations self.validate_request(request) - except netlib.exceptions.HttpReadDisconnect: + except exceptions.HttpReadDisconnect: # don't throw an error for disconnects that happen before/between requests. return - except netlib.exceptions.HttpException as e: + except exceptions.HttpException as e: # We optimistically guess there might be an HTTP client on the # other end self.send_error_response(400, repr(e)) @@ -173,7 +172,7 @@ class HttpLayer(base.Layer): if self.mode == "regular" and request.first_line_format == "authority": self.handle_regular_mode_connect(request) return - except (exceptions.ProtocolException, netlib.exceptions.NetlibException) as e: + except (exceptions.ProtocolException, exceptions.NetlibException) as e: # HTTPS tasting means that ordinary errors like resolution and # connection errors can happen here. self.send_error_response(502, repr(e)) @@ -224,7 +223,7 @@ class HttpLayer(base.Layer): self.handle_upstream_mode_connect(f.request.copy()) return - except (exceptions.ProtocolException, netlib.exceptions.NetlibException) as e: + except (exceptions.ProtocolException, exceptions.NetlibException) as e: self.send_error_response(502, repr(e)) if not f.response: f.error = flow.Error(str(e)) @@ -254,7 +253,7 @@ class HttpLayer(base.Layer): try: response = http.make_error_response(code, message, headers) self.send_response(response) - except (netlib.exceptions.NetlibException, h2.exceptions.H2Error, exceptions.Http2ProtocolException): + except (exceptions.NetlibException, h2.exceptions.H2Error, exceptions.Http2ProtocolException): self.log(traceback.format_exc(), "debug") def change_upstream_proxy_server(self, address): @@ -300,7 +299,7 @@ class HttpLayer(base.Layer): try: get_response() - except netlib.exceptions.NetlibException as e: + except exceptions.NetlibException as e: self.log( "server communication error: %s" % repr(e), level="debug" @@ -396,7 +395,7 @@ class HttpLayer(base.Layer): def validate_request(self, request): if request.first_line_format == "absolute" and request.scheme != "http": - raise netlib.exceptions.HttpException("Invalid request scheme: %s" % request.scheme) + raise exceptions.HttpException("Invalid request scheme: %s" % request.scheme) expected_request_forms = { "regular": ("authority", "absolute",), @@ -409,7 +408,7 @@ class HttpLayer(base.Layer): err_message = "Invalid HTTP request form (expected: %s, got: %s)" % ( " or ".join(allowed_request_forms), request.first_line_format ) - raise netlib.exceptions.HttpException(err_message) + raise exceptions.HttpException(err_message) if self.mode == "regular" and request.first_line_format == "absolute": request.first_line_format = "relative" diff --git a/mitmproxy/proxy/protocol/http2.py b/mitmproxy/proxy/protocol/http2.py index 93ac51bc..d0b0720d 100644 --- a/mitmproxy/proxy/protocol/http2.py +++ b/mitmproxy/proxy/protocol/http2.py @@ -8,7 +8,6 @@ from h2 import connection from h2 import events import queue -import netlib.exceptions from mitmproxy import exceptions from mitmproxy import http from mitmproxy.proxy.protocol import base diff --git a/mitmproxy/proxy/protocol/http_replay.py b/mitmproxy/proxy/protocol/http_replay.py index eef5a109..7efb0782 100644 --- a/mitmproxy/proxy/protocol/http_replay.py +++ b/mitmproxy/proxy/protocol/http_replay.py @@ -1,6 +1,5 @@ import traceback -import netlib.exceptions from mitmproxy import log from mitmproxy import controller from mitmproxy import exceptions @@ -97,7 +96,7 @@ class RequestReplayThread(basethread.BaseThread): response_reply = self.channel.ask("response", self.f) if response_reply == exceptions.Kill: raise exceptions.Kill() - except (exceptions.ReplayException, netlib.exceptions.NetlibException) as e: + except (exceptions.ReplayException, exceptions.NetlibException) as e: self.f.error = flow.Error(str(e)) if self.channel: self.channel.ask("error", self.f) diff --git a/mitmproxy/proxy/protocol/rawtcp.py b/mitmproxy/proxy/protocol/rawtcp.py index 513b90b3..d117fb41 100644 --- a/mitmproxy/proxy/protocol/rawtcp.py +++ b/mitmproxy/proxy/protocol/rawtcp.py @@ -2,7 +2,6 @@ import socket from OpenSSL import SSL -import netlib.exceptions import netlib.tcp from mitmproxy import tcp from mitmproxy import flow @@ -56,7 +55,7 @@ class RawTCPLayer(base.Layer): self.channel.ask("tcp_message", f) dst.sendall(tcp_message.content) - except (socket.error, netlib.exceptions.TcpException, SSL.Error) as e: + except (socket.error, exceptions.TcpException, SSL.Error) as e: if not self.ignore: f.error = flow.Error("TCP connection closed unexpectedly: {}".format(repr(e))) self.channel.tell("tcp_error", f) diff --git a/mitmproxy/proxy/protocol/tls.py b/mitmproxy/proxy/protocol/tls.py index 7b6b506c..b106aa0c 100644 --- a/mitmproxy/proxy/protocol/tls.py +++ b/mitmproxy/proxy/protocol/tls.py @@ -3,7 +3,6 @@ from typing import Optional # noqa from typing import Union import construct -import netlib.exceptions from mitmproxy import exceptions from mitmproxy.contrib.tls import _constructs from mitmproxy.proxy.protocol import base @@ -484,7 +483,7 @@ class TlsLayer(base.Layer): # The reason for this might be difficult to find, so we try to peek here to see if it # raises ann error. self.client_conn.rfile.peek(1) - except netlib.exceptions.TlsException as e: + except exceptions.TlsException as e: raise exceptions.ClientHandshakeException( "Cannot establish TLS with client (sni: {sni}): {e}".format( sni=self._client_hello.sni, e=repr(e) @@ -528,9 +527,9 @@ class TlsLayer(base.Layer): if tls_cert_err is not None: self.log(str(tls_cert_err), "warn") self.log("Ignoring server verification error, continuing with connection", "warn") - except netlib.exceptions.InvalidCertificateException as e: + except exceptions.InvalidCertificateException as e: raise exceptions.InvalidServerCertificate(str(e)) - except netlib.exceptions.TlsException as e: + except exceptions.TlsException as e: raise exceptions.TlsProtocolException( "Cannot establish TLS with {address} (sni: {sni}): {e}".format( address=repr(self.server_conn.address), diff --git a/mitmproxy/proxy/protocol/websockets.py b/mitmproxy/proxy/protocol/websockets.py index 636748a1..f84a1dc5 100644 --- a/mitmproxy/proxy/protocol/websockets.py +++ b/mitmproxy/proxy/protocol/websockets.py @@ -1,4 +1,3 @@ -import netlib.exceptions import socket import struct from OpenSSL import SSL @@ -105,7 +104,7 @@ class WebSocketsLayer(base.Layer): if not self._handle_frame(frame, source_conn, other_conn, is_server): return - except (socket.error, netlib.exceptions.TcpException, SSL.Error) as e: + except (socket.error, exceptions.TcpException, SSL.Error) as e: self.log("WebSockets connection closed unexpectedly by {}: {}".format( "server" if is_server else "client", repr(e)), "info") except Exception as e: # pragma: no cover diff --git a/mitmproxy/proxy/root_context.py b/mitmproxy/proxy/root_context.py index 6a99d9cf..eacf7e0b 100644 --- a/mitmproxy/proxy/root_context.py +++ b/mitmproxy/proxy/root_context.py @@ -1,4 +1,3 @@ -import netlib.exceptions from mitmproxy import log from mitmproxy import exceptions from mitmproxy.proxy import protocol @@ -43,7 +42,7 @@ class RootContext: def _next_layer(self, top_layer): try: d = top_layer.client_conn.rfile.peek(3) - except netlib.exceptions.TcpException as e: + except exceptions.TcpException as e: raise exceptions.ProtocolException(str(e)) client_tls = protocol.is_tls_record_magic(d) diff --git a/mitmproxy/proxy/server.py b/mitmproxy/proxy/server.py index b876f9ce..8472660a 100644 --- a/mitmproxy/proxy/server.py +++ b/mitmproxy/proxy/server.py @@ -2,7 +2,6 @@ import socket import sys import traceback -import netlib.exceptions from mitmproxy import exceptions from mitmproxy import connections from mitmproxy import http @@ -138,7 +137,7 @@ class ConnectionHandler: try: error_response = http.make_error_response(502, repr(e)) self.client_conn.send(http1.assemble_response(error_response)) - except netlib.exceptions.TcpException: + except exceptions.TcpException: pass except Exception: self.log(traceback.format_exc(), "error") |