Exemplo n.º 1
0
    def data_received(self, data: bytes):
        if self.connection_manager:
            if not self.peer_address:
                addr_info = self.transport.get_extra_info('peername')
                self.peer_address, self.peer_port = addr_info
            # assert self.peer_address is not None
            self.connection_manager.received_data(
                f"{self.peer_address}:{self.peer_port}", len(data))
        if not self.transport or self.transport.is_closing():
            log.warning("transport closing, but got more bytes from %s:%i\n%s",
                        self.peer_address, self.peer_port,
                        binascii.hexlify(data))
            if self._response_fut and not self._response_fut.done():
                self._response_fut.cancel()
            return
        if not self._response_fut:
            log.warning(
                "Protocol received data before expected, probable race on keep alive. Closing transport."
            )
            return self.close()
        if self._blob_bytes_received and not self.writer.closed():
            return self._write(data)

        response = BlobResponse.deserialize(self.buf + data)
        if not response.responses and not self._response_fut.done():
            self.buf += data
            return
        else:
            self.buf = b''

        if response.responses and self.blob:
            blob_response = response.get_blob_response()
            if blob_response and not blob_response.error and blob_response.blob_hash == self.blob.blob_hash:
                # set the expected length for the incoming blob if we didn't know it
                self.blob.set_length(blob_response.length)
            elif blob_response and not blob_response.error and self.blob.blob_hash != blob_response.blob_hash:
                # the server started sending a blob we didn't request
                log.warning(
                    "%s started sending blob we didn't request %s instead of %s",
                    self.peer_address, blob_response.blob_hash,
                    self.blob.blob_hash)
                return
        if response.responses:
            log.debug("got response from %s:%i <- %s", self.peer_address,
                      self.peer_port, response.to_dict())
            # fire the Future with the response to our request
            self._response_fut.set_result(response)
        if response.blob_data and self.writer and not self.writer.closed():
            # log.debug("got %i blob bytes from %s:%i", len(response.blob_data), self.peer_address, self.peer_port)
            # write blob bytes if we're writing a blob and have blob bytes to write
            self._write(response.blob_data)
Exemplo n.º 2
0
async def check_p2p(ip, port):
    writer = None
    try:
        reader, writer = await asyncio.open_connection(ip, port)
        writer.write(
            BlobRequest.make_request_for_blob_hash('0' * 96).serialize())
        return BlobResponse.deserialize(
            await
            reader.readuntil(b'}')).get_address_response().lbrycrd_address
    except OSError:
        return None
    finally:
        if writer:
            writer.close()
            await writer.wait_closed()