Пример #1
0
    def _handleRequest(self, data):
        '''Process an incoming connection request and assign the request
        to an oppy.stream.stream.Stream.

        Send a SUCCESS reply to the client and advance to the FORWARDING
        state if we get a good request.

        :param str data: incoming request data to process
        '''
        try:
            addr_type = _parseSOCKSRequestHeader(data)
        except (MalformedSOCKSRequest, UnsupportedCommand,
                UnsupportedVersion) as e:
            logging.error(e)
            if isinstance(e, UnsupportedVersion):
                self._sendReply(SOCKS_FAILURE)
            elif isinstance(e, UnsupportedCommand):
                self._sendReply(COMMAND_NOT_SUPPORTED)
            else:
                self._sendReply(SOCKS_FAILURE)
            self.transport.loseConnection()
            return

        data = data[REQUEST_HEADER_LEN:]

        try:
            self.request = _parseRequest(data, addr_type)
        except (MalformedSOCKSRequest, UnsupportedAddressType) as e:
            logging.error(e)
            if isinstance(e, MalformedSOCKSRequest):
                self._sendReply(SOCKS_FAILURE)
            else:
                self._sendReply(ADDRESS_TYPE_NOT_SUPPORTED)
            self.transport.loseConnection()
            return

        self.stream = Stream(self._circuit_manager, self.request, self)
        self.state = State.FORWARDING
        self._sendReply(SUCCEEDED)
Пример #2
0
    def _handleRequest(self, data):
        '''Process an incoming connection request and assign the request
        to an oppy.stream.stream.Stream.

        Send a SUCCESS reply to the client and advance to the FORWARDING
        state if we get a good request.

        :param str data: incoming request data to process
        '''
        VER_LEN = 1
        CMD_LEN = 1
        RSV_LEN = 1
        ADDR_TYPE_LEN = 1
        offset = 0

        ver = data[: VER_LEN]
        offset += VER_LEN
        cmd = data[offset : offset + CMD_LEN]
        offset += CMD_LEN
        rsv = data[offset : offset + RSV_LEN]
        offset += RSV_LEN
        addr_type = data[offset : offset + ADDR_TYPE_LEN]
        offset += ADDR_TYPE_LEN

        if ver != VER:
            logging.error("Unsupported SOCKS version: {}.".format(ver))
            self._sendReply(SOCKS_FAILURE)
            self.transport.loseConnection()
            return

        if cmd != CONNECT:
            msg = "SOCKS client tried an unsupported request: {}."
            logging.error(msg.format(cmd))
            self._sendReply(COMMAND_NOT_SUPPORTED)
            self.transport.loseConnection()
            return

        if rsv != RSV:
            msg = "Reserved byte was non-zero in SOCKS client request."
            logging.error(msg)
            self._sendReply(SOCKS_FAILURE)
            self.transport.loseConnection()
            return

        IPv4_LEN = 4
        IPv6_LEN = 16
        PORT_LEN = 2

        if addr_type == IPv4:
            addr = data[offset : offset + IPv4_LEN]
            offset += IPv4_LEN
            port = port = data[offset : offset + PORT_LEN]
            self.request = ExitRequest(port, addr=addr)
        elif addr_type == DOMAIN_NAME:
            length = struct.unpack("!B", data[offset])[0]
            # hostname length is 1 byte
            offset += 1
            host = data[offset : offset + length]
            offset += length
            port = data[offset : offset + PORT_LEN]
            self.request = ExitRequest(port, host=host)
        elif addr_type == IPv6:
            addr = data[offset : offset + IPv6_LEN]
            offset += IPv6_LEN
            port = data[offset : offset + PORT_LEN]
            self.request = ExitRequest(port, addr=addr)
        else:
            msg = "SOCKS client made a request with unsupported address "
            msg += "type: {}.".format(addr_type)
            self._sendReply(ADDRESS_TYPE_NOT_SUPPORTED)
            self.transport.loseConnection()
            return

        self.stream = Stream(self.request, self)
        self.state = State.FORWARDING
        self._sendReply(SUCCEEDED)