Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_custom_error(self):
code = 0xFF
message = 'Custom error message'
self._check_error(socks.SocksError(message),
socks.SocksError, None, message)
self._check_error(socks.SocksError(message=message),
socks.SocksError, None, message)
self._check_error(socks.SocksError(code=code),
socks.SocksError, code, '')
self._check_error(socks.SocksError(message, code=code),
socks.SocksError, code, message)
self._check_error(socks.SocksError(message=message, code=code),
socks.SocksError, code, message)
create_connection=lambda a, p: the_proto,
)
sm.connection()
sm.feed_data(b'\x05')
sm.feed_data(b'\x00')
# reply with success, port 0x1234
sm.feed_data(b'\x05\x00\x00\x01\x00\x00\x00\x00\x12\x34')
# now some data that should get relayed
sm.feed_data(b'this is some relayed data')
# should *not* have disconnected
self.assertEqual(0, len(dis))
self.assertTrue(the_proto.data, b"this is some relayed data")
sm.disconnected(socks.SocksError("it's fine"))
self.assertEqual(1, len(Proto.lost))
self.assertTrue("it's fine" in str(Proto.lost[0]))
def test_custom_error(self):
code = 0xFF
message = 'Custom error message'
self._check_error(socks.SocksError(message),
socks.SocksError, None, message)
self._check_error(socks.SocksError(message=message),
socks.SocksError, None, message)
self._check_error(socks.SocksError(code=code),
socks.SocksError, code, '')
self._check_error(socks.SocksError(message, code=code),
socks.SocksError, code, message)
self._check_error(socks.SocksError(message=message, code=code),
socks.SocksError, code, message)
def __init__(self, message='', code=None):
super(SocksError, self).__init__(message or self.message)
self.message = message or self.message
self.code = code or self.code
def _parse_request_reply(self):
"waiting for a reply to our request"
# we need at least 6 bytes of data: 4 for the "header", such
# as it is, and 2 more if it's DOMAINNAME (for the size) or 4
# or 16 more if it's an IPv4/6 address reply. plus there's 2
# bytes on the end for the bound port.
if len(self._data) < 8:
return
msg = self._data[:4]
# not changing self._data yet, in case we've not got
# enough bytes so far.
(version, reply, _, typ) = struct.unpack('BBBB', msg)
if version != 5:
self.reply_error(SocksError(
"Expected version 5, got {}".format(version)))
return
if reply != self.SUCCEEDED:
self.reply_error(_create_socks_error(reply))
return
reply_dispatcher = {
self.REPLY_IPV4: self._parse_ipv4_reply,
self.REPLY_HOST: self._parse_domain_name_reply,
self.REPLY_IPV6: self._parse_ipv6_reply,
}
try:
method = reply_dispatcher[typ]
except KeyError:
self.reply_error(SocksError(
class ConnectionRefusedError(SocksError):
code = 0x05
message = 'Connection refused'
class TtlExpiredError(SocksError):
code = 0x06
message = 'TTL expired'
class CommandNotSupportedError(SocksError):
code = 0x07
message = 'Command not supported'
class AddressTypeNotSupportedError(SocksError):
code = 0x08
message = 'Address type not supported'
_socks_errors = {cls.code: cls for cls in SocksError.__subclasses__()}
def _create_socks_error(code):
try:
return _socks_errors[code]()
except KeyError:
return SocksError("Unknown SOCKS error-code {}".format(code),
code=code)
@inlineCallbacks
super(SocksError, self).__init__(message or self.message)
self.message = message or self.message
self.code = code or self.code
class GeneralServerFailureError(SocksError):
code = 0x01
message = 'general SOCKS server failure'
class ConnectionNotAllowedError(SocksError):
code = 0x02
message = 'connection not allowed by ruleset'
class NetworkUnreachableError(SocksError):
code = 0x03
message = 'Network unreachable'
class HostUnreachableError(SocksError):
code = 0x04
message = 'Host unreachable'
class ConnectionRefusedError(SocksError):
code = 0x05
message = 'Connection refused'
class TtlExpiredError(SocksError):
code = 0x06
class TtlExpiredError(SocksError):
code = 0x06
message = 'TTL expired'
class CommandNotSupportedError(SocksError):
code = 0x07
message = 'Command not supported'
class AddressTypeNotSupportedError(SocksError):
code = 0x08
message = 'Address type not supported'
_socks_errors = {cls.code: cls for cls in SocksError.__subclasses__()}
def _create_socks_error(code):
try:
return _socks_errors[code]()
except KeyError:
return SocksError("Unknown SOCKS error-code {}".format(code),
code=code)
@inlineCallbacks
def resolve(tor_endpoint, hostname):
"""
This is easier to use via :meth:`txtorcon.Tor.dns_resolve`
:param tor_endpoint: the Tor SOCKS endpoint to use.
class NetworkUnreachableError(SocksError):
code = 0x03
message = 'Network unreachable'
class HostUnreachableError(SocksError):
code = 0x04
message = 'Host unreachable'
class ConnectionRefusedError(SocksError):
code = 0x05
message = 'Connection refused'
class TtlExpiredError(SocksError):
code = 0x06
message = 'TTL expired'
class CommandNotSupportedError(SocksError):
code = 0x07
message = 'Command not supported'
class AddressTypeNotSupportedError(SocksError):
code = 0x08
message = 'Address type not supported'
_socks_errors = {cls.code: cls for cls in SocksError.__subclasses__()}
class HostUnreachableError(SocksError):
code = 0x04
message = 'Host unreachable'
class ConnectionRefusedError(SocksError):
code = 0x05
message = 'Connection refused'
class TtlExpiredError(SocksError):
code = 0x06
message = 'TTL expired'
class CommandNotSupportedError(SocksError):
code = 0x07
message = 'Command not supported'
class AddressTypeNotSupportedError(SocksError):
code = 0x08
message = 'Address type not supported'
_socks_errors = {cls.code: cls for cls in SocksError.__subclasses__()}
def _create_socks_error(code):
try:
return _socks_errors[code]()
except KeyError: