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
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)
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)
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))
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))
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
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)
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)
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)
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)
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
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)
def read(cls, file: IO) -> State: """Reads the state from a file-like object.""" return cls(VarInt.read(file))
def slp_response(self) -> SLPResponse: """Returns an SLP response.""" return SLPResponse(VarInt(0), self.slp_content)
def read(cls, readfunc): """Reads the state from the respective connection.""" return cls(VarInt.read(readfunc))