def sock_foward(dir, insocket, outsocket, outqueue, serverprops): buff = FowardingBuffer(insocket, outsocket) try: while True: #decode packet buff.packet_start() packetid = struct.unpack("!B", buff.read(1))[0] if packetid in mcpackets.decoders.keys(): packet = mcpackets.decode(dir, buff, packetid) else: print "unknown packet 0x%2X from" % packetid, {'c2s':'client', 's2c':'server'}[dir] buff.packet_end() continue packetbytes = buff.packet_end() modpacket = run_hooks(packetid, packet, serverprops) if modpacket == None: # if run_hooks returns none, the packet was not modified packet_info(packetid, packet, buff, serverprops) buff.write(packetbytes) else: packet_info(packetid, modpacket, buff, serverprops) buff.write(mcpackets.encode(dir,packetid,modpacket)) #print "changed packet" #packet_info(packetid, modpacket, buff, serverprops) #send all items in the outgoing queue while not outqueue.empty(): task = outqueue.get() buff.write(task) outqueue.task_done() except socket.error, e: print( dir, "connection quit unexpectedly:") print e return
def networkThread(serversocket, buff, clientprops): logprint("Started network thread\n") while 1: packetid = struct.unpack("!B", buff.read(1))[0] #if packetid not in [0x1F, 0x21, 0x1C, 0x04, 0x22, 0x18]: #logprint("Got %s\n" % mcpackets.decoders[packetid]["name"]) packet = mcpackets.decode("s2c", buff, packetid) if packetid == 0x33: logprint("test\n") if packetid in [0x33, 0x34, 0x35]: logprint("updated map with 0x%.2x\n" % packetid) minichunktracker.addPacketChanges(packetid, packet, None) drawMap() if packetid == 0x03: message = packet['message'].encode('utf-8') logprint(message+"\n")#,curses.color_pair(curses.COLOR_CYAN)) if packetid in [0x0B, 0x0C, 0x0D]: # spawn pos? clientprops.gotServLoc = 1 logprint("got player position: %f %f %f\n" % (clientprops.playerloc[0],clientprops.playerloc[1],clientprops.playerloc[2])) if packetid in [0x0B,0x0D]: clientprops.playerloc[0] = packet['x'] clientprops.playerloc[1] = packet['y'] clientprops.playerloc[2] = packet['z'] if packetid in [0x0C,0x0D]: clientprops.playerdir = packet['rotation'] clientprops.playerpitch = packet['pitch'] serversocket.sendall(mcpackets.encode("c2s",0x0D,{'x':clientprops.playerloc[0], 'y':clientprops.playerloc[1], 'z':clientprops.playerloc[2], 'stance':clientprops.playerloc[1] + 1.62, 'rotation':0, 'pitch':0, 'flying':0, })) if packetid == 0x06: logprint("got spawn pos") clientprops.playerloc[0] = packet['x'] clientprops.playerloc[1] = packet['y'] clientprops.playerloc[2] = packet['z'] serversocket.sendall(mcpackets.encode("c2s",0x0D,{'x':clientprops.playerloc[0], 'y':clientprops.playerloc[1], 'z':clientprops.playerloc[2], 'stance':clientprops.playerloc[1] + 1.62, 'rotation':0, 'pitch':0, 'flying':0, })) if packetid == 0x32: serversocket.sendall(mcpackets.encode("c2s",0x0D,{'x':clientprops.playerloc[0], 'y':clientprops.playerloc[1], 'z':clientprops.playerloc[2], 'stance':clientprops.playerloc[1] + 1.62, 'rotation':0, 'pitch':0, 'flying':0, }))
session = MCLogin() logprint("Connecting to server... ") serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) serversocket.connect(("stackunderflow.com",25555)) buff = serversocket.makefile('rb', 4096) logprint("Success\n") logprint("Sending handshake... ") serversocket.sendall(mcpackets.encode("c2s",0x02,{'username':session[0]})) logprint("Waiting for response... ") packetid = struct.unpack("!B", buff.read(1))[0] #logprint("Got %s\n" % mcpackets.decoders[packetid]["name"]) if packetid == 0x02: packet = mcpackets.decode("s2c", buff, packetid) connhash = packet['username'] logprint("Got conn hash: %s\n" % connhash) else: logprint("Error: expected 0x01, got 0x%x" % packetid) MCServerAuth(session,connhash) logprint("Sending login... ") serversocket.sendall(mcpackets.encode("c2s",0x01,{'protoversion':14, 'username':session[0], 'seed':0, 'dimension':0})) logprint(" successful\n") packetid = struct.unpack("!B", buff.read(1))[0] if packetid != 0x01: logprint("unexpected packet 0x%x\n" % packetid) packet = mcpackets.decode("s2c", buff, packetid)