def block_reply_packet(context, data): data = bytearray(data) o1, o2, o3, o4 = struct.unpack_from('BBBB', buffer(data), 0x14) ip_string = "%i.%i.%i.%i" % (o1, o2, o3, o4) struct.pack_into('BBBB', data, 0x14, int(i0), int(i1), int(i2), int(i3)) port = struct.unpack_from("H", buffer(data), 0x18)[0] if context.peer.transport.getHost().port > 12999: port += 1000 struct.pack_into("H", data, (0x14 + 0x04), port) if port not in blocks.blockList: blocks.blockList[port] = (ip_string, "PVP Arena", port) #not really sure if it's proper way if port in blocks.blockList and port not in blocks.listeningPorts: from ShipProxy import ProxyFactory if bindIp == "0.0.0.0": interface_ip = myIp else: interface_ip = bindIp block_endpoint = endpoints.TCP4ServerEndpoint(reactor, port, interface=interface_ip) block_endpoint.listen(ProxyFactory()) print("[ShipProxy] Opened listen socked on port %i for new ship." % port) blocks.listeningPorts.append(port) if verbose: print("[ShipProxy] rewriting block ip address in query response.") context.peer.changingBlocks = True return str(data)
def my_room_info_packet(context, data): data = bytearray(data) o1, o2, o3, o4 = struct.unpack_from('BBBB', buffer(data), 0x20) ip_string = "%i.%i.%i.%i" % (o1, o2, o3, o4) port = struct.unpack_from('H', buffer(data), 0x28)[0] if port not in blocks.blockList: if verbose: print("[BlockPacket] Discovered a 'My Room' block at %s:%i!" % (ip_string, port)) blocks.blockList[port] = (ip_string, "My Room", port) if port not in blocks.listeningPorts: from ShipProxy import ProxyFactory if bindIp == "0.0.0.0": interface_ip = myIp else: interface_ip = bindIp block_endpoint = endpoints.TCP4ServerEndpoint(reactor, port, interface=interface_ip) block_endpoint.listen(ProxyFactory()) print("[ShipProxy] Opened listen socked on port %i for new ship." % port) blocks.listeningPorts.append(port) struct.pack_into('BBBB', data, 0x20, int(i0), int(i1), int(i2), int(i3)) context.peer.changingBlocks = True return str(data)
def scrape_block_packet(ship_ip, ship_port, destination_ip): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) log.msg("[BlockQuery] Scraping %s:%i for a initial block..." % (ship_ip, ship_port)) s.settimeout(30) try: s.connect((ship_ip, ship_port)) except socket.error as e: log.msg("[BlockQuery] Scraping %s:%i connect return an error: %s" % (ship_ip, ship_port, e)) return None except Exception as e: log.msg("[BlockQuery] Scraping %s:%i connect return an error: %s" % (ship_ip, ship_port, sys.exc_info()[0])) return None data = io.BytesIO() try: data.write(s.recv(4)) except socket.error as e: log.msg("[BlockQuery] Scraping %s:%i write return an error: %s" % (ship_ip, ship_port, e)) return None except Exception as e: log.msg("[BlockQuery] Scraping %s:%i write return an error: %s" % (ship_ip, ship_port, sys.exc_info()[0])) return None actual_size = struct.unpack_from('i', data.getvalue(), 0x0)[0] try: data.write(s.recv(actual_size - 4)) except socket.error as e: log.msg("[BlockQuery] Scraping %s:%i recv return an error: %s" % (ship_ip, ship_port, e)) return None except Exception as e: log.msg("[BlockQuery] Scraping %s:%i recv return an error: %s" % (ship_ip, ship_port, sys.exc_info()[0])) return None s.close() data.flush() data = bytearray(data.getvalue()) name = data[0x24:0x64].decode('utf-16le') # namelog = name.encode('ascii', errors='ignore').rstrip('\0') o1, o2, o3, o4, port = struct.unpack_from('BBBBH', buffer(data), 0x68) ip_string = '%i.%i.%i.%i' % (o1, o2, o3, o4) if ship_ip == blockShipList[13000]: # Shared ship hack port += 1000 # Bump port up to 13000 struct.pack_into('H', data, (0x68 + 0x04), port) if port not in blocks.blockList: # log.msg("[BlockList] Discovered new block %s at addr %s:%i! Recording..." % (namelog, ip_string, port)) blocks.blockList[port] = (ip_string, name) if port not in blocks.listeningPorts: from ShipProxy import ProxyFactory if bindIp == "0.0.0.0": interface_ip = myIp else: interface_ip = bindIp block_endpoint = endpoints.TCP4ServerEndpoint(reactor, port, interface=interface_ip) # twisted.internet.error.CannotListenError: Couldn't listen on 0.0.0.0:12468: [Errno 98] Address already in use. block_endpoint.listen(ProxyFactory()) print("[ShipProxy] Opened listen socked on port %i for new ship." % port) blocks.listeningPorts.append(port) o1, o2, o3, o4 = destination_ip.split(".") struct.pack_into('BBBB', data, 0x68, int(o1), int(o2), int(o3), int(o4)) return str(data)
def block_reply_packet(context, data): data = bytearray(data) struct.pack_into('BBBB', data, 0x14, int(i0), int(i1), int(i2), int(i3)) port = struct.unpack_from("H", buffer(data), 0x18)[0] if port in blocks.blockList and port not in blocks.listeningPorts: from ShipProxy import ProxyFactory if bindIp == "0.0.0.0": interface_ip = myIp else: interface_ip = bindIp block_endpoint = TCP4ServerEndpoint(reactor, port, interface=interface_ip) block_endpoint.listen(ProxyFactory()) print("[ShipProxy] Opened listen socked on port %i for new ship." % port) blocks.listeningPorts.append(port) if verbose: print("[ShipProxy] rewriting block ip address in query response.") context.peer.changingBlocks = True return str(data)
o1, o2, o3, o4, port = struct.unpack_from('BBBBH', buffer(data), 0x64) ip_string = '%i.%i.%i.%i' % (o1, o2, o3, o4) if port not in blocks.blockList: #log.msg("[BlockList] Discovered new block %s at addr %s:%i! Recording..." % (namelog, ip_string, port)) blocks.blockList[port] = (ip_string, name) if port not in blocks.listeningPorts: from ShipProxy import ProxyFactory if bindIp == "0.0.0.0": interface_ip = myIp else: interface_ip = bindIp block_endpoint = TCP4ServerEndpoint(reactor, port, interface=interface_ip) #twisted.internet.error.CannotListenError: Couldn't listen on 0.0.0.0:12468: [Errno 98] Address already in use. block_endpoint.listen(ProxyFactory()) print("[ShipProxy] Opened listen socked on port %i for new ship." % port) blocks.listeningPorts.append(port) o1, o2, o3, o4 = destination_ip.split(".") struct.pack_into('BBBB', data, 0x64, int(o1), int(o2), int(o3), int(o4)) return str(data) def scrape_ship_packet(ship_ip, ship_port, destination_ip): o1, o2, o3, o4 = destination_ip.split(".") s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) log.msg("[ShipQuery] Scraping %s:%i for ship status..." % (ship_ip, ship_port)) s.settimeout(5) try: