def encode_extra(packet): bbuff = BoundBuffer() if packet.data['nbt'] is None: packet.data['nbt'] = nbt._TagEnd() nbt.TagByte(packet.data['nbt'].id)._render_buffer(bbuff) if packet.data['nbt'].id == nbt.TAG_COMPOUND: nbt.TagString(packet.data['nbt'].name)._render_buffer(bbuff) packet.data['nbt']._render_buffer(bbuff) return bbuff.flush()
def __init__(self, sock, event): self.sock = sock self.event = event self.host = None self.port = None self.connected = False self.encrypted = False self.proto_state = proto.HANDSHAKE_STATE self.comp_state = proto.PROTO_COMP_OFF self.comp_threshold = -1 self.sbuff = b'' self.rbuff = BoundBuffer()
def pack_slot(slot): o = pack(MC_SHORT, slot['id']) if slot['id'] != -1: o += pack(MC_BYTE, slot['amount']) o += pack(MC_SHORT, slot['damage']) if 'enchants' in slot: ench = slot['enchants'] bbuff = BoundBuffer() nbt.TagByte(ench.id)._render_buffer(bbuff) nbt.TagString(ench.name)._render_buffer(bbuff) ench._render_buffer(bbuff) o += bbuff.flush() else: o += pack(MC_BYTE, 0) return o
def pack_slot(slot): o = pack(MC_SHORT, slot["id"]) if slot["id"] != -1: o += pack(MC_BYTE, slot["amount"]) o += pack(MC_SHORT, slot["damage"]) if "enchants" in slot: ench = slot["enchants"] bbuff = BoundBuffer() nbt.TagByte(ench.id)._render_buffer(bbuff) nbt.TagString(ench.name)._render_buffer(bbuff) ench._render_buffer(bbuff) o += bbuff.flush() else: o += pack(MC_BYTE, 0) return o
def unpack_bulk(self, data): bbuff = BoundBuffer(data['data']) skylight = data['sky_light'] for meta in data['metadata']: key = meta['chunk_x'], meta['chunk_z'] if key not in self.columns: self.columns[key] = ChunkColumn() self.columns[key].unpack(bbuff, meta['primary_bitmap'], skylight)
def unpack_column(self, data): bbuff = BoundBuffer(data['data']) skylight = True if self.dimension == DIMENSION_OVERWOLD else False key = data['chunk_x'], data['chunk_z'] if key not in self.columns: self.columns[key] = ChunkColumn() self.columns[key].unpack(bbuff, data['primary_bitmap'], skylight, data['continuous'])
def decode(self, bbuff, proto_comp_state): self.data = {} packet_length = datautils.unpack(MC_VARINT, bbuff) packet_data = bbuff.recv(packet_length) pbuff = BoundBuffer(packet_data) if proto_comp_state == proto.PROTO_COMP_ON: body_length = datautils.unpack(MC_VARINT, pbuff) if body_length: body_data = zlib.decompress(pbuff.flush(), zlib.MAX_WBITS) pbuff.write(body_data) pbuff.save() try: # Ident self.__ident[2] = datautils.unpack(MC_VARINT, pbuff) self.ident = tuple(self.__ident) self.str_ident = proto.packet_ident2str[self.ident] # Payload for dtype, name in proto.hashed_structs[self.ident]: self.data[name] = datautils.unpack(dtype, pbuff) # Extension if self.ident in hashed_extensions: hashed_extensions[self.ident].decode_extra(self, pbuff) if pbuff: raise PacketDecodeFailure(self, pbuff) except BufferUnderflowException: raise PacketDecodeFailure(self, pbuff, True) return self
def decode(self, bbuff, proto_comp_state): self.data = {} packet_length = datautils.unpack(MC_VARINT, bbuff) packet_data = bbuff.recv(packet_length) pbuff = BoundBuffer(packet_data) if proto_comp_state == mcdata.PROTO_COMP_ON: body_length = datautils.unpack(MC_VARINT, pbuff) if body_length > 0: body_data = zlib.decompress(pbuff.flush(), zlib.MAX_WBITS) pbuff.write(body_data) pbuff.save() try: # Ident self.__ident[2] = datautils.unpack(MC_VARINT, pbuff) self.ident = tuple(self.__ident) self.str_ident = mcdata.packet_ident2str[self.ident] # Payload for dtype, name in mcdata.hashed_structs[self.ident]: self.data[name] = datautils.unpack(dtype, pbuff) # Extension if self.ident in hashed_extensions: hashed_extensions[self.ident].decode_extra(self, pbuff) if len(pbuff) > 0: raise PacketDecodeFailure(self, pbuff) except BufferUnderflowException: raise PacketDecodeFailure(self, pbuff, True) return self
def handle_unpack_chunk(self, data): chunk_x = data.chunk_x chunk_z = data.chunk_z mask = data.primary_bitmap continuous = data.continuous bbuff = BoundBuffer(data.data) if self.dimension == DIMENSION_OVERWORLD: skylight = True else: skylight = False key = (chunk_x, chunk_z) if key not in self.columns: self.columns[key] = smpmap.ChunkColumn() self.columns[key].unpack(bbuff, mask, skylight, continuous)
def handle_unpack_bulk(self, data): #print "unpacking bulk" skylight = data.sky_light bbuff = BoundBuffer(data.data) #print "light: %s: buffer:"%skylight #print bbuff for meta in data.metadata: chunk_x = meta.chunk_x chunk_z = meta.chunk_z mask = meta.primary_bitmap #print "unpacking chunk meta x: %d, z: %d, mask: %d"%(chunk_x, chunk_z, mask) key = (chunk_x, chunk_z) if key not in self.columns: self.columns[key] = smpmap.ChunkColumn() self.columns[key].unpack(bbuff, mask, skylight)
class NetCore(object): def __init__(self, sock, event, select): self.sock = sock self.event = event self.select = select self.host = None self.port = None self.connected = False self.encrypted = False self.proto_state = proto.HANDSHAKE_STATE self.comp_state = proto.PROTO_COMP_OFF self.comp_threshold = -1 self.sbuff = b'' self.rbuff = BoundBuffer() def reset(self, sock): self.__init__(sock, self.event, self.select) def connect(self, host='localhost', port=25565): self.host = host self.port = port try: logger.debug('NETCORE: Attempting to connect to host: %s port: %s', host, port) # Set the connect to be a blocking operation self.sock.setblocking(True) self.sock.connect((host, port)) self.sock.setblocking(False) self.connected = True self.event.emit('net_connect', (host, port)) logger.debug('NETCORE: Connected to host: %s port: %s', host, port) except socket.error as error: logger.error('NETCORE: Error on Connect') self.event.emit('SOCKET_ERR', error) def set_proto_state(self, state): self.proto_state = state self.event.emit(proto.state_lookup[state] + '_STATE') def set_comp_state(self, threshold): self.comp_threshold = threshold if threshold >= 0: self.comp_state = proto.PROTO_COMP_ON def push(self, packet): data = packet.encode(self.comp_state, self.comp_threshold) self.sbuff += (self.cipher.encrypt(data) if self.encrypted else data) self.event.emit(packet.ident, packet) self.event.emit(packet.str_ident, packet) self.select.schedule_sending(self.sock) def push_packet(self, ident, data): self.push(mcpacket.Packet(ident, data)) def read_packet(self, data=b''): self.rbuff.append( self.cipher.decrypt(data) if self.encrypted else data) while self.rbuff: self.rbuff.save() try: packet = mcpacket.Packet(ident=( self.proto_state, proto.SERVER_TO_CLIENT )).decode(self.rbuff, self.comp_state) except BufferUnderflowException: self.rbuff.revert() break except mcpacket.PacketDecodeFailure as err: logger.warning('NETCORE: Packet decode failed') logger.warning( 'NETCORE: Failed packet ident is probably: %s', err.packet.str_ident ) self.event.emit('PACKET_ERR', err) break self.event.emit(packet.ident, packet) self.event.emit(packet.str_ident, packet) def enable_crypto(self, secret_key): self.cipher = AESCipher(secret_key) self.encrypted = True def disable_crypto(self): self.cipher = None self.encrypted = False
class NetCore(object): def __init__(self, sock, event): self.sock = sock self.event = event self.host = None self.port = None self.connected = False self.encrypted = False self.proto_state = proto.HANDSHAKE_STATE self.comp_state = proto.PROTO_COMP_OFF self.comp_threshold = -1 self.sbuff = b'' self.rbuff = BoundBuffer() def connect(self, host='localhost', port=25565): self.host = host self.port = port try: logger.debug("NETCORE: Attempting to connect to host: %s port: %s", host, port) # Set the connect to be a blocking operation self.sock.setblocking(True) self.sock.connect((self.host, self.port)) self.sock.setblocking(False) self.connected = True self.event.emit('net_connect', (self.host, self.port)) logger.debug("NETCORE: Connected to host: %s port: %s", host, port) except socket.error as error: logger.error("NETCORE: Error on Connect") self.event.emit('SOCKET_ERR', error) def set_proto_state(self, state): self.proto_state = state self.event.emit(proto.state_lookup[state] + '_STATE') def set_comp_state(self, threshold): self.comp_threshold = threshold if threshold >= 0: self.comp_state = proto.PROTO_COMP_ON def push(self, packet): data = packet.encode(self.comp_state, self.comp_threshold) self.sbuff += (self.cipher.encrypt(data) if self.encrypted else data) self.event.emit(packet.ident, packet) self.event.emit(packet.str_ident, packet) self.sock.sending = True def push_packet(self, ident, data): self.push(mcpacket.Packet(ident, data)) def read_packet(self, data=b''): self.rbuff.append( self.cipher.decrypt(data) if self.encrypted else data) while self.rbuff: self.rbuff.save() try: packet = mcpacket.Packet( ident=(self.proto_state, proto.SERVER_TO_CLIENT)).decode( self.rbuff, self.comp_state) except BufferUnderflowException: self.rbuff.revert() break except mcpacket.PacketDecodeFailure as err: logger.warning('NETCORE: Packet decode failed') logger.warning('NETCORE: Failed packet ident is probably: %s', err.packet.str_ident) self.event.emit('PACKET_ERR', err) break self.event.emit(packet.ident, packet) self.event.emit(packet.str_ident, packet) def enable_crypto(self, secret_key): self.cipher = AESCipher(secret_key) self.encrypted = True def disable_crypto(self): self.cipher = None self.encrypted = False def reset(self, sock): self.__init__(sock, self.event)