From fbed1eff31ee2324401ded0823ac0e53fda5cb01 Mon Sep 17 00:00:00 2001 From: Peter Surda Date: Fri, 10 Jun 2016 12:43:37 +0200 Subject: [PATCH] Socks error handling - Socks errors are now handled in a smoother manner --- src/class_outgoingSynSender.py | 7 +++++-- src/socks/__init__.py | 17 ++++++++++++++--- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/class_outgoingSynSender.py b/src/class_outgoingSynSender.py index d68d5149..f01cbd5f 100644 --- a/src/class_outgoingSynSender.py +++ b/src/class_outgoingSynSender.py @@ -222,8 +222,11 @@ class outgoingSynSender(threading.Thread, StoppableThread): 'updateStatusBar', tr._translate( "MainWindow", "SOCKS5 Authentication problem: %1").arg(str(err)))) except socks.Socks5Error as err: - pass - logger.error('SOCKS5 error. (It is possible that the server wants authentication).) ' + str(err)) + if err[0] in [3, 4, 5, 6]: + # this is a more bening "error": host unreachable, network unreachable, connection refused, TTL expired + logger.debug('SOCKS5 error. ' + str(err)) + else: + logger.error('SOCKS5 error. ' + str(err)) except socks.Socks4Error as err: logger.error('Socks4Error: ' + str(err)) except socket.error as err: diff --git a/src/socks/__init__.py b/src/socks/__init__.py index ffcf2c10..2bd03f48 100644 --- a/src/socks/__init__.py +++ b/src/socks/__init__.py @@ -63,7 +63,8 @@ _generalerrors = ("success", "not connected", "not available", "bad proxy type", - "bad input") + "bad input", + "timed out") _socks5errors = ("succeeded", "general SOCKS server failure", @@ -129,7 +130,10 @@ class socksocket(socket.socket): Receive EXACTLY the number of bytes requested from the socket. Blocks until the required number of bytes have been received. """ - data = self.recv(count) + try: + data = self.recv(count) + except socket.timeout: + raise GeneralProxyError((6, "timed out")) while len(data) < count: d = self.recv(count-len(data)) if not d: raise GeneralProxyError((0, "connection closed unexpectedly")) @@ -396,7 +400,14 @@ class socksocket(socket.socket): portnum = self.__proxy[2] else: portnum = 1080 - _orgsocket.connect(self, (self.__proxy[1], portnum)) + try: + _orgsocket.connect(self, (self.__proxy[1], portnum)) + except socket.error as e: + if e[0] == 101: + raise Socks5Error((3, _socks5errors[3])) + if e[0] == 111: + raise Socks5Error((5, _socks5errors[5])) + raise self.__negotiatesocks5() self.__connectsocks5(destpair[0], destpair[1]) elif self.__proxy[0] == PROXY_TYPE_SOCKS4: