better error messages, remove error cause

This commit is contained in:
Maximilian Hils 2015-09-11 02:17:04 +02:00
parent 40ce06e780
commit dd414e4852
8 changed files with 38 additions and 19 deletions

View File

@ -16,13 +16,11 @@ class ProxyException(Exception):
message: the error message message: the error message
cause: (optional) an error object that caused this exception, e.g. an IOError. cause: (optional) an error object that caused this exception, e.g. an IOError.
""" """
def __init__(self, message, cause=None): def __init__(self, message):
""" """
:param message: Error Message :param message: Error Message
:param cause: Exception object that caused this exception to be thrown.
""" """
super(ProxyException, self).__init__(message) super(ProxyException, self).__init__(message)
self.cause = cause
class ProtocolException(ProxyException): class ProtocolException(ProxyException):
@ -34,8 +32,9 @@ class TlsException(ProtocolException):
class ClientHandshakeException(TlsException): class ClientHandshakeException(TlsException):
# This subclass is quite useful to give hints about cert errors. def __init__(self, message, server):
pass super(ClientHandshakeException, self).__init__(message)
self.server = server
class Socks5Exception(ProtocolException): class Socks5Exception(ProtocolException):

View File

@ -1,6 +1,8 @@
from __future__ import (absolute_import, print_function, division) from __future__ import (absolute_import, print_function, division)
import six
import sys import sys
import six
from netlib import tcp from netlib import tcp
from ..models import ServerConnection from ..models import ServerConnection
from ..exceptions import ProtocolException from ..exceptions import ProtocolException
@ -176,8 +178,11 @@ class ServerConnectionMixin(object):
except tcp.NetLibError as e: except tcp.NetLibError as e:
six.reraise( six.reraise(
ProtocolException, ProtocolException,
ProtocolException("Server connection to %s failed: %s" % ProtocolException(
(repr(self.server_conn.address), e), e), "Server connection to {} failed: {}".format(
repr(self.server_conn.address), str(e)
)
),
sys.exc_info()[2] sys.exc_info()[2]
) )

View File

@ -397,7 +397,7 @@ class HttpLayer(Layer):
if isinstance(e, ProtocolException): if isinstance(e, ProtocolException):
six.reraise(ProtocolException, e, sys.exc_info()[2]) six.reraise(ProtocolException, e, sys.exc_info()[2])
else: else:
six.reraise(ProtocolException, ProtocolException("Error in HTTP connection: %s" % repr(e), e), sys.exc_info()[2]) six.reraise(ProtocolException, ProtocolException("Error in HTTP connection: %s" % repr(e)), sys.exc_info()[2])
finally: finally:
flow.live = False flow.live = False

View File

@ -67,6 +67,6 @@ class RawTCPLayer(Layer):
except (socket.error, NetLibError, SSL.Error) as e: except (socket.error, NetLibError, SSL.Error) as e:
six.reraise( six.reraise(
ProtocolException, ProtocolException,
ProtocolException("TCP connection closed unexpectedly: {}".format(repr(e)), e), ProtocolException("TCP connection closed unexpectedly: {}".format(repr(e))),
sys.exc_info()[2] sys.exc_info()[2]
) )

View File

@ -416,7 +416,12 @@ class TlsLayer(Layer):
except NetLibError as e: except NetLibError as e:
six.reraise( six.reraise(
ClientHandshakeException, ClientHandshakeException,
ClientHandshakeException("Cannot establish TLS with client: %s" % repr(e), e), ClientHandshakeException(
"Cannot establish TLS with client (sni: {sni}): {e}".format(
sni=self.client_sni, e=repr(e)
),
self.client_sni or repr(self.server_conn.address)
),
sys.exc_info()[2] sys.exc_info()[2]
) )
@ -473,7 +478,7 @@ class TlsLayer(Layer):
address=repr(self.server_conn.address), address=repr(self.server_conn.address),
sni=self.sni_for_server_connection, sni=self.sni_for_server_connection,
e=repr(e), e=repr(e),
), e), )),
sys.exc_info()[2] sys.exc_info()[2]
) )
except NetLibError as e: except NetLibError as e:
@ -483,7 +488,7 @@ class TlsLayer(Layer):
address=repr(self.server_conn.address), address=repr(self.server_conn.address),
sni=self.sni_for_server_connection, sni=self.sni_for_server_connection,
e=repr(e), e=repr(e),
), e), )),
sys.exc_info()[2] sys.exc_info()[2]
) )

View File

@ -48,7 +48,7 @@ class Socks5Proxy(Layer, ServerConnectionMixin):
self.client_conn.wfile.flush() self.client_conn.wfile.flush()
except (socks.SocksError, NetLibError) as e: except (socks.SocksError, NetLibError) as e:
raise Socks5Exception("SOCKS5 mode failure: %s" % repr(e), e) raise Socks5Exception("SOCKS5 mode failure: %s" % repr(e))
self.server_conn.address = connect_request.addr self.server_conn.address = connect_request.addr

View File

@ -14,7 +14,7 @@ class TransparentProxy(Layer, ServerConnectionMixin):
try: try:
self.server_conn.address = self.resolver.original_addr(self.client_conn.connection) self.server_conn.address = self.resolver.original_addr(self.client_conn.connection)
except Exception as e: except Exception as e:
raise ProtocolException("Transparent mode failure: %s" % repr(e), e) raise ProtocolException("Transparent mode failure: %s" % repr(e))
layer = self.ctx.next_layer(self) layer = self.ctx.next_layer(self)
try: try:

View File

@ -8,7 +8,7 @@ import six
from netlib import tcp from netlib import tcp
from netlib.http.http1 import HTTP1Protocol from netlib.http.http1 import HTTP1Protocol
from netlib.tcp import NetLibError from netlib.tcp import NetLibError
from ..exceptions import ProtocolException, ServerException from ..exceptions import ProtocolException, ServerException, ClientHandshakeException
from ..protocol import Kill from ..protocol import Kill
from ..models import ClientConnection, make_error_response from ..models import ClientConnection, make_error_response
from .modes import HttpUpstreamProxy, HttpProxy, ReverseProxy, TransparentProxy, Socks5Proxy from .modes import HttpUpstreamProxy, HttpProxy, ReverseProxy, TransparentProxy, Socks5Proxy
@ -42,7 +42,7 @@ class ProxyServer(tcp.TCPServer):
except socket.error as e: except socket.error as e:
six.reraise( six.reraise(
ServerException, ServerException,
ServerException('Error starting proxy server: ' + repr(e), e), ServerException('Error starting proxy server: ' + repr(e)),
sys.exc_info()[2] sys.exc_info()[2]
) )
self.channel = None self.channel = None
@ -121,8 +121,18 @@ class ConnectionHandler(object):
except Kill: except Kill:
self.log("Connection killed", "info") self.log("Connection killed", "info")
except ProtocolException as e: except ProtocolException as e:
self.log(repr(e), "info")
self.log(traceback.format_exc(), "debug") if isinstance(e, ClientHandshakeException):
self.log(
"Client Handshake failed. "
"The client may not trust the proxy's certificate for {}.".format(e.server),
"error"
)
self.log(repr(e), "debug")
else:
self.log(repr(e), "error")
self.log(traceback.format_exc(), "debug")
# If an error propagates to the topmost level, # If an error propagates to the topmost level,
# we send an HTTP error response, which is both # we send an HTTP error response, which is both
# understandable by HTTP clients and humans. # understandable by HTTP clients and humans.