def testNToP(self): from twisted.python.compat import inet_ntop f = lambda a: inet_ntop(socket.AF_INET6, a) g = lambda a: inet_ntop(socket.AF_INET, a) self.assertEquals('::', f('\x00' * 16)) self.assertEquals('::1', f('\x00' * 15 + '\x01')) self.assertEquals( 'aef:b01:506:1001:ffff:9997:55:170', f('\x0a\xef\x0b\x01\x05\x06\x10\x01\xff\xff\x99\x97\x00\x55\x01\x70')) self.assertEquals('1.0.1.0', g('\x01\x00\x01\x00')) self.assertEquals('170.85.170.85', g('\xaa\x55\xaa\x55')) self.assertEquals('255.255.255.255', g('\xff\xff\xff\xff')) self.assertEquals('100::', f('\x01' + '\x00' * 15)) self.assertEquals('100::1', f('\x01' + '\x00' * 14 + '\x01'))
def _request(self): buf = self._buf if len(buf) < 4: return ver, cmd, _, addr_type = buf.unpackN("!4B", 4) if ver != EnumSOCKS.VERSION: self.reply(EnumReply.GENERAL_SOCKS_SERVER_FAILURE, msg="Version mismatch") return if cmd not in EnumSOCKS.Cmds: self.reply(EnumReply.COMMAND_NOT_SUPPORTED, msg="Command not support") return if addr_type == EnumSOCKS.ATYP_IPV4: server = socket.inet_ntoa(buf.getN(4)) deferred = defer.succeed(server) elif addr_type == EnumSOCKS.ATYP_IPV6: server = compat.inet_ntop(socket.AF_INET6, buf.getN(16)) deferred = defer.succeed(server) elif addr_type == EnumSOCKS.ATYP_DOMAIN: host_len = buf.getb() host = buf.getN(host_len) deferred = self._reactor.resolve(host) else: self.reply(EnumReply.ADDRESS_TYPE_ERROR, msg="Address Type not support") return port = buf.unpackN("!H", 2)[0] deferred.addCallback(self._request2, port, cmd) self._state = EnumState.REQUEST2 self.buf = buf
def testNToP(self): from twisted.python.compat import inet_ntop f = lambda a: inet_ntop(socket.AF_INET6, a) g = lambda a: inet_ntop(socket.AF_INET, a) self.assertEqual("::", f("\x00" * 16)) self.assertEqual("::1", f("\x00" * 15 + "\x01")) self.assertEqual( "aef:b01:506:1001:ffff:9997:55:170", f("\x0a\xef\x0b\x01\x05\x06\x10\x01\xff\xff\x99\x97\x00\x55\x01\x70") ) self.assertEqual("1.0.1.0", g("\x01\x00\x01\x00")) self.assertEqual("170.85.170.85", g("\xaa\x55\xaa\x55")) self.assertEqual("255.255.255.255", g("\xff\xff\xff\xff")) self.assertEqual("100::", f("\x01" + "\x00" * 15)) self.assertEqual("100::1", f("\x01" + "\x00" * 14 + "\x01"))
def processRequest(self): """ Parse the client request, and setup the TCP/IP connection """ msg = self.buf.peek() if len(msg) < 4: return # Parse VER, CMD, RSV, ATYP ver = msg.get_uint8() cmd = msg.get_uint8() rsv = msg.get_uint8() atyp = msg.get_uint8() if ver != _SOCKS_VERSION: log.error("Invalid SOCKS version: '%d'" % ver) self.sendReply(SOCKSv5Reply.GeneralFailure) return if cmd not in self.ACCEPTABLE_CMDS: log.error("Invalid SOCKS command: '%d'" % cmd) self.sendReply(SOCKSv5Reply.CommandNotSupported) return if rsv != _SOCKS_RSV: log.error("Invalid SOCKS RSV: '%d'" % rsv) self.sendReply(SOCKSv5Reply.GeneralFailure) return # Deal with the address addr = None if atyp == _SOCKS_ATYP_IP_V4: if len(msg) < 4: return addr = socket.inet_ntoa(msg.get(4)) elif atyp == _SOCKS_ATYP_IP_V6: if len(msg) < 16: return addr = compat.inet_ntop(socket.AF_INET6,msg.get(16)) elif atyp == _SOCKS_ATYP_DOMAINNAME: if len(msg) < 1: return alen = msg.get_uint8() if alen == 0: log.error("Domain name length is 0") self.sendReply(SOCKSv5Reply.GeneralFailure) return if len(msg) < alen: return addr = msg.get(alen) else: log.error("Invalid SOCKS address type: '%d'" % atyp) self.sendReply(SOCKSv5Reply.AddressTypeNotSupported) return # Deal with the port if len(msg) < 2: return port = msg.get_uint16(True) # Ensure there is no trailing garbage if len(msg) > 0: log.error("Peer sent trailing garbage after request") self.transport.loseConnection() return self.buf.clear() if cmd == _SOCKS_CMD_CONNECT: self.processCmdConnect(addr, port) elif cmd == _SOCKS_CMD_BIND: self.processCmdBind(addr, port) elif cmd == _SOCKS_CMD_UDP_ASSOCIATE: self.processCmdUdpAssociate(addr, port) else: # Should *NEVER* happen log.error("Unimplemented command received") self.transport.loseConnection()
def processRequest(self): """ Parse the client request, and setup the TCP/IP connection """ msg = self.buf.peek() if len(msg) < 4: return # Parse VER, CMD, RSV, ATYP ver = msg.get_uint8() cmd = msg.get_uint8() rsv = msg.get_uint8() atyp = msg.get_uint8() if ver != _SOCKS_VERSION: log.error("Invalid SOCKS version: '%d'" % ver) self.sendReply(SOCKSv5Reply.GeneralFailure) return if cmd not in self.ACCEPTABLE_CMDS: log.error("Invalid SOCKS command: '%d'" % cmd) self.sendReply(SOCKSv5Reply.CommandNotSupported) return if rsv != _SOCKS_RSV: log.error("Invalid SOCKS RSV: '%d'" % rsv) self.sendReply(SOCKSv5Reply.GeneralFailure) return # Deal with the address addr = None if atyp == _SOCKS_ATYP_IP_V4: if len(msg) < 4: return addr = socket.inet_ntoa(msg.get(4)) elif atyp == _SOCKS_ATYP_IP_V6: if len(msg) < 16: return addr = compat.inet_ntop(socket.AF_INET6, msg.get(16)) elif atyp == _SOCKS_ATYP_DOMAINNAME: if len(msg) < 1: return alen = msg.get_uint8() if alen == 0: log.error("Domain name length is 0") self.sendReply(SOCKSv5Reply.GeneralFailure) return if len(msg) < alen: return addr = msg.get(alen) else: log.error("Invalid SOCKS address type: '%d'" % atyp) self.sendReply(SOCKSv5Reply.AddressTypeNotSupported) return # Deal with the port if len(msg) < 2: return port = msg.get_uint16(True) # Ensure there is no trailing garbage if len(msg) > 0: log.error("Peer sent trailing garbage after request") self.transport.loseConnection() return self.buf.clear() if cmd == _SOCKS_CMD_CONNECT: self.processCmdConnect(addr, port) elif cmd == _SOCKS_CMD_BIND: self.processCmdBind(addr, port) elif cmd == _SOCKS_CMD_UDP_ASSOCIATE: self.processCmdUdpAssociate(addr, port) else: # Should *NEVER* happen log.error("Unimplemented command received") self.transport.loseConnection()