def listen_to_server(self): """Thread to listen to the server""" size = 4096 while True: data, address = self.sock.recvfrom(size) if self.debug: print('received {} bytes from {}'.format(len(data), address)) if data: packet = packet_pb2.Packet() packet.ParseFromString(data) if self.debug: print(packet) if packet.type == packet_pb2.Packet.HEARTBEAT and not \ self.heartbeat_thread.is_alive(): print("Starting Heartbeat Thread") self.heartbeat_thread.start() if packet.type == packet_pb2.Packet.GAME: game_packet = server_pb2.ServerGameMessage() game_packet.ParseFromString( AESTool.decrypt(self.key, packet.message)) print(game_packet) if packet.type == packet_pb2.Packet.DATA: data_packet = server_pb2.ServerDataMessage() data_packet.ParseFromString( AESTool.decrypt(self.key, packet.message)) print(data_packet) else: print('Client disconnected')
def create_packet(packet_type, message, player_id=None): """Create a packet This function abstracts the creation process to add a checksum, timestamp, etc :param packet_type: the packet type, check protobuf for possible values """ global sequence packet = packet_pb2.Packet() packet.type = packet_type packet.sequence = sequence sequence += 1 if player_id is not None: packet.playerID = player_id packet.message = message timestamp = timestamp_pb2.Timestamp() timestamp.GetCurrentTime() packet.timestamp.CopyFrom(timestamp) packet.messageLength = len(packet.message) crc_data = packet.SerializePartialToString() packet.crc = (binascii.crc32(crc_data) & 0xFFFFFFFF) return packet
def main(): if len(sys.argv) != 3: print "Usage: ", sys.argv[0], " <protobuf input> <ASCII output>" exit(-1) # Open the file in read mode proto_in = protolib.openFileRd(sys.argv[1]) try: ascii_out = open(sys.argv[2], 'w') except IOError: print "Failed to open ", sys.argv[2], " for writing" exit(-1) # Read the magic number in 4-byte Little Endian magic_number = proto_in.read(4) if magic_number != "gem5": print "Unrecognized file", sys.argv[1] exit(-1) print "Parsing packet header" # Add the packet header header = packet_pb2.PacketHeader() protolib.decodeMessage(proto_in, header) print "Object id:", header.obj_id print "Tick frequency:", header.tick_freq print "Parsing packets" num_packets = 0 packet = packet_pb2.Packet() # Decode the packet messages until we hit the end of the file while protolib.decodeMessage(proto_in, packet): num_packets += 1 # ReadReq is 1 and WriteReq is 4 in src/mem/packet.hh Command enum cmd = 'r' if packet.cmd == 1 else ('w' if packet.cmd == 4 else 'u') if packet.HasField('pkt_id'): ascii_out.write('%s,' % (packet.pkt_id)) if packet.HasField('flags'): ascii_out.write( '%s,%s,%s,%s,%s' % (cmd, packet.addr, packet.size, packet.flags, packet.tick)) else: ascii_out.write('%s,%s,%s,%s' % (cmd, packet.addr, packet.size, packet.tick)) if packet.HasField('pc'): ascii_out.write(',%s\n' % (packet.pc)) else: ascii_out.write('\n') print "Parsed packets:", num_packets # We're done ascii_out.close() proto_in.close()
def main(): if len(sys.argv) != 3: print "Usage: ", sys.argv[0], " <protobuf input> <ASCII output>" exit(-1) try: proto_in = open(sys.argv[1], 'rb') except IOError: print "Failed to open ", sys.argv[1], " for reading" exit(-1) try: ascii_out = open(sys.argv[2], 'w') except IOError: print "Failed to open ", sys.argv[2], " for writing" exit(-1) # Read the magic number in 4-byte Little Endian magic_number = proto_in.read(4) if magic_number != "gem5": print "Unrecognized file" exit(-1) print "Parsing packet header" # Add the packet header header = packet_pb2.PacketHeader() decodeMessage(proto_in, header) print "Object id:", header.obj_id print "Tick frequency:", header.tick_freq print "Parsing packets" num_packets = 0 ignored_flags = False packet = packet_pb2.Packet() # Decode the packet messages until we hit the end of the file while decodeMessage(proto_in, packet): num_packets += 1 # ReadReq is 1 and WriteReq is 4 in src/mem/packet.hh Command enum cmd = 'r' if packet.cmd == 1 else ('w' if packet.cmd == 4 else 'u') if packet.HasField('flags'): # Currently not printing flags ignored_flags = True ascii_out.write('%s,%s,%s,%s\n' % (cmd, packet.addr, packet.size, packet.tick)) print "Parsed packets:", num_packets if ignored_flags: print "Encountered packet flags that were ignored" # We're done ascii_out.close() proto_in.close()
def work(pubsub, channel): pubsub.subscribe('queue:' + channel) while True: message = pubsub.get_message() if message is not None: data = redisConnection.rpop('queue:' + channel + ":list") if data is not None: packet = packet_pb2.Packet() packet.ParseFromString(data) aes_key = redisConnection.get(packet.playerID) if aes_key is not None: reply = None if channel is "gamemessages": game_packet = client_pb2.ClientGameMessage() game_packet.ParseFromString( AESTool.decrypt(aes_key, packet.message)) print("Worker got GAME: ", game_packet) reply_packet = server_pb2.ServerGameMessage() # Here's where you would do the heavy tasks to calculate that data reply_packet.xPos = 12 reply_packet.yPos = 1032 reply_packet.zPos = 1 message = AESTool.encrypt( aes_key, reply_packet.SerializeToString()) reply = create_packet(packet_pb2.Packet.GAME, message, packet.playerID) elif channel is "datamessages": data_packet = client_pb2.ClientDataMessage() data_packet.ParseFromString( AESTool.decrypt(aes_key, packet.message)) print("Worker got DATA: ", data_packet) # Here's where you would do the heavy tasks to calculate that data reply_packet = server_pb2.ServerDataMessage() reply_packet.health = 12 reply_packet.coins = 1032 reply_packet.mana = 1 message = AESTool.encrypt( aes_key, reply_packet.SerializeToString()) reply = create_packet(packet_pb2.Packet.DATA, message, packet.playerID) reply.address = packet.address reply.port = packet.port redisConnection.publish('queue:packetreplies', reply.SerializeToString())
def main(): if len(sys.argv) != 3: print "Usage: ", sys.argv[0], " <ASCII input> <protobuf output>" exit(-1) try: ascii_in = open(sys.argv[1], 'r') except IOError: print "Failed to open ", sys.argv[1], " for reading" exit(-1) try: proto_out = open(sys.argv[2], 'wb') except IOError: print "Failed to open ", sys.argv[2], " for writing" exit(-1) # Write the magic number in 4-byte Little Endian, similar to what # is done in src/proto/protoio.cc proto_out.write("gem5") # Add the packet header header = packet_pb2.PacketHeader() header.obj_id = "Converted ASCII trace " + sys.argv[1] # Assume the default tick rate header.tick_freq = 1000000000 encodeMessage(proto_out, header) # For each line in the ASCII trace, create a packet message and # write it to the encoded output for line in ascii_in: cmd, addr, size, tick = line.split(',') packet = packet_pb2.Packet() packet.tick = long(tick) # ReadReq is 1 and WriteReq is 4 in src/mem/packet.hh Command enum packet.cmd = 1 if cmd == 'r' else 4 packet.addr = long(addr) packet.size = int(size) encodeMessage(proto_out, packet) # We're done ascii_in.close() proto_out.close()
def send_responses(self): """Thread to send responses to the client We subscribe to a redis queue to get the packets the worker wants to send to the client """ pubsub = self.redis.pubsub(ignore_subscribe_messages=True) pubsub.subscribe('queue:packetreplies') while True: response = pubsub.get_message() if response is not None: packet = packet_pb2.Packet() data = response['data'] packet.ParseFromString(data) self.sock.sendto(packet.SerializeToString(), (packet.address, packet.port)) print('sent {} bytes to {}'.format( len(packet.SerializeToString()), packet.address))
def handle_packet(self, data, address): """Packer handler thread This is where the packet is checked for validity and put into the appropriate redis queue :param data: the data from the packet :address: a tuple with the (ip_address, port_number) """ packet = packet_pb2.Packet() packet.ParseFromString(data) if packet.type != packet_pb2.Packet.HEARTBEAT: print(packet) packet_crc = packet.crc packet.ClearField('crc') crc_data = packet.SerializePartialToString() crc = binascii.crc32(crc_data) & 0xFFFFFFFF if packet_crc != crc: print('Given CRC: {}'.format(packet.crc)) print('Calculated CRC: {}'.format( (binascii.crc32(crc_data) & 0xFFFFFFFF))) print("Dropped packet for bad checksum") return # put the crc back in there! packet.crc = packet_crc if packet.type == packet_pb2.Packet.HEARTBEAT: packet = create_packet(packet_pb2.Packet.HEARTBEAT, b"Pong") self.sock.sendto(packet.SerializeToString(), address) elif packet.type == packet_pb2.Packet.AUTH: decrypted_aes_key = self.rsa_key.decrypt(packet.message) print('DECRYPTED AES KEY:', [x for x in decrypted_aes_key]) print('PLAYER ID AUTH: ', packet.playerID) self.redis.set(packet.playerID, decrypted_aes_key) packet = create_packet(packet_pb2.Packet.HEARTBEAT, b"Pong") self.sock.sendto(packet.SerializeToString(), address) elif packet.type in PACKET_TYPE_TO_CHANNEL: packet.address = address[0] packet.port = address[1] self.forward_packet(packet, PACKET_TYPE_TO_CHANNEL[packet.type])
def to_log(message): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) host = server.loghost port = server.logport s.connect((host, int(port))) packet = packet_pb2.Packet() packet.command = 3 loginfo = packet_pb2.LogInfo() loginfo.server = bytes("smtp send server".encode('utf-8')) loginfo.reason = bytes(message.encode('utf-8')) packet.serialized = loginfo.SerializeToString() str_packet = packet.SerializeToString() len_packet = len(str_packet) s.send(len_packet.to_bytes(4, byteorder='big')) s.send(str_packet) s.close()
def create_trace(filename, max_addr, burst_size, itt): try: proto_out = gzip.open(filename, 'wb') except IOError: print "Failed to open ", filename, " for writing" exit(-1) # write the magic number in 4-byte Little Endian, similar to what # is done in src/proto/protoio.cc proto_out.write("gem5") # add the packet header header = packet_pb2.PacketHeader() header.obj_id = "lat_mem_rd for range 0:" + str(max_addr) # assume the default tick rate (1 ps) header.tick_freq = 1000000000000 protolib.encodeMessage(proto_out, header) # create a list of every single address to touch addrs = range(0, max_addr, burst_size) import random random.shuffle(addrs) tick = 0 # create a packet we can re-use for all the addresses packet = packet_pb2.Packet() # ReadReq is 1 in src/mem/packet.hh Command enum packet.cmd = 1 packet.size = int(burst_size) for addr in addrs: packet.tick = long(tick) packet.addr = long(addr) protolib.encodeMessage(proto_out, packet) tick = tick + itt proto_out.close()
def main(): if len(sys.argv) != 6: print "Usage: ", sys.argv[ 0], " <protobuf input> <ASCII output> <starting_tick> <ending_tick> <max_packets>" exit(-1) print "Command line arguments:", sys.argv # Open the file in read mode proto_in = protolib.openFileRd(sys.argv[1]) try: ascii_out = open(sys.argv[2], 'w') except IOError: print "Failed to open ", sys.argv[2], " for writing" exit(-1) # Read the magic number in 4-byte Little Endian magic_number = proto_in.read(4) if magic_number != "gem5": print "Unrecognized file", sys.argv[1] exit(-1) print "Parsing packet header" # Add the packet header header = packet_pb2.PacketHeader() protolib.decodeMessage(proto_in, header) print "Object id:", header.obj_id print "Tick frequency:", header.tick_freq starting_tick = long(sys.argv[3]) ending_tick = long(sys.argv[4]) max_packets = long(sys.argv[5]) print "Parsing packets from simulation tick: ", str( starting_tick), " to ", str( ending_tick), " max packets = ", max_packets num_packets = 0 packet = packet_pb2.Packet() currtick = 0 # Decode the packet messages until we hit the end of the file while protolib.decodeMessage(proto_in, packet) and currtick < starting_tick: currtick = long(packet.tick) if currtick >= starting_tick and currtick < ending_tick: print "Found first packet of interest at tick ", str(currtick) while protolib.decodeMessage( proto_in, packet ) and currtick < ending_tick and num_packets < max_packets: num_packets += 1 currtick = long(packet.tick) # ReadReq is 1 and WriteReq is 4 in src/mem/packet.hh Command enum #cmd = 'r' if packet.cmd == 1 else ('w' if packet.cmd == 4 else 'u') # MWG commented out cmd = packet.cmd # MWG if packet.HasField('pkt_id'): ascii_out.write('%s,' % (packet.pkt_id)) if packet.HasField('flags'): if packet.HasField('has_data'): addr = long(packet.addr) size = long(packet.size) flags = long(packet.flags) ascii_out.write('%s,%016X,%016X,%016X,%s,' % (cmd, addr, size, flags, packet.tick)) if packet.has_data == 1: ascii_out.write('1,') ascii_out.write( '%016X,%016X,%016X,%016X,%016X,%016X,%016X,%016X\n' % (packet.data0, packet.data1, packet.data2, packet.data3, packet.data4, packet.data5, packet.data6, packet.data7)) else: ascii_out.write('0,') ascii_out.write('0,0,0,0,0,0,0,0\n') else: display("uh oh, this should not have happened") exit(1) else: display("uh oh, this should not have happened") exit(1) if num_packets % 10000 == 0: print "Packet ", num_packets print "Found last packet of interest at tick ", str(currtick) print "Parsed packets:", num_packets else: print "No packets found in tick range of interest: [", starting_tick, ",", ending_tick, ")" # We're done ascii_out.close() proto_in.close()
# values taken as input assigned to variables SIP = configdict["ipaddr"] UDP_PORT = configdict["port"] base = configdict["base"] p = int(configdict["p"]) rdata = 0 addr = 0 r = 0 # lists for storing data from the pickle file temp = {} known_users = {} temp_known_users = {} # Message Packet for Authentication pkt1 = packet_pb2.Packet() # Password Secret def func(password): s = int(hashlib.sha256(password).hexdigest(), 16) return s # solving the challenge sent by the server def solvechallenge(c): return int(c, 0) & 0xfffffff # use serverkeyencryption(public_key_loading(pubkey_path), msg) to encrypt the msg with server's public key def public_key_loading(key):
def main(): if len(sys.argv) != 3: print "Usage: ", sys.argv[0], " <protobuf input> <ASCII output>" exit(-1) try: # First see if this file is gzipped try: # Opening the file works even if it is not a gzip file proto_in = gzip.open(sys.argv[1], 'rb') # Force a check of the magic number by seeking in the # file. If we do not do it here the error will occur when # reading the first message. proto_in.seek(1) proto_in.seek(0) except IOError: proto_in = open(sys.argv[1], 'rb') except IOError: print "Failed to open ", sys.argv[1], " for reading" exit(-1) try: ascii_out = open(sys.argv[2], 'w') except IOError: print "Failed to open ", sys.argv[2], " for writing" exit(-1) # Read the magic number in 4-byte Little Endian magic_number = proto_in.read(4) if magic_number != "gem5": print "Unrecognized file", sys.argv[1] exit(-1) print "Parsing packet header" # Add the packet header header = packet_pb2.PacketHeader() protolib.decodeMessage(proto_in, header) print "Object id:", header.obj_id print "Tick frequency:", header.tick_freq print "Parsing packets" num_packets = 0 packet = packet_pb2.Packet() # Decode the packet messages until we hit the end of the file while protolib.decodeMessage(proto_in, packet): num_packets += 1 # ReadReq is 1 and WriteReq is 4 in src/mem/packet.hh Command enum cmd = 'r' if packet.cmd == 1 else ('w' if packet.cmd == 4 else 'u') if packet.HasField('pkt_id'): ascii_out.write('%s,' % (packet.pkt_id)) if packet.HasField('flags'): ascii_out.write('%s,%s,%s,%s,%s\n' % (cmd, packet.addr, packet.size, packet.flags, packet.tick)) else: ascii_out.write('%s,%s,%s,%s\n' % (cmd, packet.addr, packet.size, packet.tick)) print "Parsed packets:", num_packets # We're done ascii_out.close() proto_in.close()