Пример #1
0
def get_response(text: str) -> bytes:
    """Returns the response text message."""

    payload = dumps({'text': text}).encode('latin-1')
    payload_length = bytes(VarInt(len(payload)))
    payload = bytes(VarInt(0)) + payload_length + payload
    payload_length = bytes(VarInt(len(payload)))
    return payload_length + payload
Пример #2
0
 def _handle_login(self, rfile: IO, wfile: IO):
     """Performs a login."""
     size = VarInt.read(rfile)
     packet_id = VarInt.read(rfile)
     packet_id_length = len(bytes(packet_id))
     payload = rfile.read(size - packet_id_length)
     LOGGER.debug('Got packet ID: %s', packet_id)
     user_name = payload[2:].decode('latin-1')
     LOGGER.debug('User "%s" logged in.', user_name)
     self._perform_login(wfile)
Пример #3
0
 def _handle_login(self, connection):
     """Performs a login."""
     size = VarInt.read(connection.recv)
     packet_id = VarInt.read(connection.recv)
     packet_id_length = len(bytes(packet_id))
     payload = connection.recv(size - packet_id_length)
     LOGGER.debug('Got packet ID: %s', packet_id)
     user_name = payload[2:].decode('latin-1')
     LOGGER.debug('User "%s" logged in.', user_name)
     self._perform_login(connection)
Пример #4
0
class State(VarInt, Enum):
    """Protocol state."""

    HANDSHAKE = VarInt(0)
    STATUS = VarInt(1)
    LOGIN = VarInt(2)

    @classmethod
    def read(cls, file: IO) -> State:
        """Reads the state from a file-like object."""
        return cls(VarInt.read(file))
Пример #5
0
class State(Enum):
    """Protocol state."""

    HANDSHAKE = VarInt(0)
    STATUS = VarInt(1)
    LOGIN = VarInt(2)

    @classmethod
    def read(cls, readfunc):
        """Reads the state from the respective connection."""
        return cls(VarInt.read(readfunc))
Пример #6
0
 def __bytes__(self):
     """Returns the respective bytes."""
     json = dumps(self).encode('latin-1')
     json_size = len(json)
     json_size = VarInt(json_size)
     json_size = bytes(json_size)
     payload = json_size + json
     payload = bytes(self.packet_id) + payload
     payload_size = len(payload)
     payload_size = VarInt(payload_size)
     payload_size = bytes(payload_size)
     return payload_size + payload
Пример #7
0
 def read(cls, file: IO) -> SLPResponse:
     """Read an SLP response from a file-like object."""
     total_size = VarInt.read(file)
     LOGGER.debug('Read total size: %s', total_size)
     packet_id = VarInt.read(file)
     LOGGER.debug('Read packet ID: %s', packet_id)
     json_size = VarInt.read(file)
     LOGGER.debug('Read JSON size: %s', json_size)
     json_bytes = file.read(json_size)
     LOGGER.debug('Read JSON bytes: %s', json_bytes)
     string = json_bytes.decode('latin-1')
     json = loads(string)
     return cls(packet_id, json)
Пример #8
0
 def read(cls, readfunc):
     """Creates the SLP response from the respective payload."""
     total_size = VarInt.read(readfunc)
     LOGGER.debug('Read total size: %s', total_size)
     packet_id = VarInt.read(readfunc)
     LOGGER.debug('Read packet ID: %s', packet_id)
     json_size = VarInt.read(readfunc)
     LOGGER.debug('Read JSON size: %s', json_size)
     json_bytes = readfunc(json_size)
     LOGGER.debug('Read JSON bytes: %s', json_bytes)
     string = json_bytes.decode('latin-1')
     json = loads(string)
     return cls(packet_id, json)
Пример #9
0
 def read(cls, file: IO) -> Handshake:
     """Reads a handshake object from a file-like object."""
     size = VarInt.read(file)
     LOGGER.debug('Read size: %s', size)
     version = VarInt.read(file)
     LOGGER.debug('Read version: %s', version)
     version_length = len(bytes(version))
     payload_size = size - version_length - 1  # Do not read next state.
     payload = file.read(payload_size)
     LOGGER.debug('Read payload: %s', payload)
     address = payload[4:-2].decode()
     port = int.from_bytes(payload[-2:], 'little')
     next_state = State.read(file)
     return cls(version, address, port, next_state)
Пример #10
0
 def read(cls, readfunc):
     """Creates a handshake object from the respective bytes."""
     size = VarInt.read(readfunc)
     LOGGER.debug('Read size: %s', size)
     version = VarInt.read(readfunc)
     LOGGER.debug('Read version: %s', version)
     version_length = len(bytes(version))
     payload_size = size - version_length - 1  # Do not read next state.
     payload = readfunc(payload_size)
     LOGGER.debug('Read payload: %s', payload)
     address = payload[4:-2].decode()
     port = int.from_bytes(payload[-2:], 'little')
     next_state = State.read(readfunc)
     return cls(version, address, port, next_state)
Пример #11
0
def get_response(text):
    """Returns the response text message."""

    json = {'text': text}
    string = dumps(json)
    payload = string.encode('latin-1')
    payload_length = len(payload)
    payload_length = VarInt(payload_length)
    payload_length = bytes(payload_length)
    payload = payload_length + payload
    payload = bytes(VarInt(0)) + payload
    payload_length = len(payload)
    payload_length = VarInt(payload_length)
    payload_length = bytes(payload_length)
    return payload_length + payload
Пример #12
0
 def slp_response(self):
     """Returns an SLP response."""
     json = {
         'version': {
             'name': '1.14.2',
             'protocol': self.protocol
         },
         'players': {
             'max': self.max_players,
             'online': 0
         },
         'description': {
             'text': self.description
         }
     }
     return SLPResponse(VarInt(0), json)
Пример #13
0
 def read(cls, file: IO) -> State:
     """Reads the state from a file-like object."""
     return cls(VarInt.read(file))
Пример #14
0
 def slp_response(self) -> SLPResponse:
     """Returns an SLP response."""
     return SLPResponse(VarInt(0), self.slp_content)
Пример #15
0
 def read(cls, readfunc):
     """Reads the state from the respective connection."""
     return cls(VarInt.read(readfunc))