Skip to content

Commit

Permalink
feat(log): also report negotiations error in WARN level
Browse files Browse the repository at this point in the history
- Like #70, but also for negiotiation errors (e.g. user/pswd denied
messages).
- Increase log-level from DEBUG-->WARNING because if dest-address
resolves to multiple IPs, practically that is the only place where the
real failure-cause it is reported.
- Correct way should have been to log from within Python's
`socket.create_connection()`.
  • Loading branch information
ankostis committed Mar 8, 2017
1 parent d0eb45b commit 0e22bb9
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions socks.py
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,18 @@ def _negotiate_HTTP(self, dest_addr, dest_port):
HTTP: _negotiate_HTTP
}

def _prepare_error_msg(self, proxy_addr, action, error):
proxy_type = self.proxy[0]
proxy_addr, proxy_port = proxy_addr
proxy_server = "{0}:{1}".format(proxy_addr, proxy_port)
printable_type = PRINTABLE_PROXY_TYPES[proxy_type]

msg = "Failed %s to %s-proxy(%s) due to: %s: %s" % (
action, printable_type, proxy_server,
type(error).__name__, error)

return msg

@set_self_blocking
def connect(self, dest_pair):
"""
Expand Down Expand Up @@ -794,9 +806,8 @@ def connect(self, dest_pair):
proxy_server = "{0}:{1}".format(proxy_addr, proxy_port)
printable_type = PRINTABLE_PROXY_TYPES[proxy_type]

msg = "Error connecting to {0} proxy {1}".format(printable_type,
proxy_server)
log.debug("%s due to: %s", msg, error)
msg = self._prepare_error_msg(proxy_addr, 'connecting', error)
log.warning(msg)
raise ProxyConnectionError(msg, error)

else:
Expand All @@ -808,10 +819,14 @@ def connect(self, dest_pair):
except socket.error as error:
# Wrap socket errors
self.close()
raise GeneralProxyError("Socket error", error)
except ProxyError:
msg = self._prepare_error_msg(proxy_addr, 'negotiating', error)
log.warning(msg)
raise GeneralProxyError(msg, error)
except ProxyError as error:
# Protocol error while negotiating with proxy
self.close()
msg = self._prepare_error_msg(proxy_addr, 'negotiating', error)
log.warning(msg)
raise

def _proxy_addr(self):
Expand Down

0 comments on commit 0e22bb9

Please sign in to comment.