Пример #1
0
    def dataReceived(self, data):
        if verbose:
            print("[ShipProxy] [%i] Received data from %s!" % (
                self.packetCount,
                self.transport.getPeer().host,
            ))

        encryption_enabled = (self.c4crypto is not None)
        if encryption_enabled:
            data = self.c4crypto.decrypt(data)
        self.readBuffer += data

        while len(self.readBuffer) >= 8:
            packet_size = struct.unpack_from('i', self.readBuffer)[0]
            packet_type = struct.unpack_from('BB', self.readBuffer, 4)
            #If the packets reported size is less than a normal packet header, wrap it up to 8 as it should NEVER be that way.
            #This prevents an infinite loop.
            if packet_size < 8:
                print(
                    "[ShipProxy] Warning! Got invalid packet size %i. Resetting to 8 to prevent infinite loop..."
                    % packet_size)
                packet_size = 8

            if verbose:
                print(
                    "[ShipProxy] [%i] Received packet with size %i, id %x:%x" %
                    (self.packetCount, packet_size, packet_type[0],
                     packet_type[1]))

            if len(self.readBuffer) < packet_size:
                if verbose:
                    print(
                        "[ShipProxy] [%i] Buffer only contains %i, waiting for more data."
                        % (self.packetCount, len(self.readBuffer)))
                break

            packet = self.readBuffer[:packet_size]
            self.readBuffer = self.readBuffer[packet_size:]

            if packet is not None:
                for f in plugin_manager.rawPacketFunctions:
                    packet = f(self, packet, packet_type[0], packet_type[1])

            try:
                packet_handler = packets.packetList[packet_type]
                packet = packet_handler(self, packet)
            except KeyError:
                if verbose:
                    print(
                        "[ShipProxy] No packet function for id %x:%x, using default functionality..."
                        % (packet_type[0], packet_type[1]))

            if (packet_type[0],
                    packet_type[1]) in plugin_manager.packetFunctions:
                for f in plugin_manager.packetFunctions[(packet_type[0],
                                                         packet_type[1])]:
                    if packet is not None:
                        packet = f(self, packet)

            if packet is None:
                return

            if self.playerId is not None:
                if self.playerId not in clients.connectedClients:  # Inital add
                    clients.add_client(self)
                    self.loaded = True
                    for f in plugin_manager.onInitialConnection:
                        f(self)
                elif not self.loaded:
                    clients.populate_data(self)
                    for f in plugin_manager.onConnection:
                        f(self)

            if encryption_enabled:
                packet = self.c4crypto.encrypt(packet)
            else:
                # check if encryption was newly enabled while parsing this packet
                # if it was, then decrypt any packets that may be waiting in the buffer
                if self.c4crypto is not None:
                    encryption_enabled = True
                    self.readBuffer = self.c4crypto.decrypt(self.readBuffer)

            self.peer.transport.write(packet)

            self.packetCount += 1
            self.peer.packetCount = self.packetCount
Пример #2
0
    def dataReceived(self, data):
        if verbose:
            print("[ShipProxy] [%i] Received data from %s!" % (self.packetCount, self.transport.getPeer().host,))

        encryption_enabled = (self.c4crypto is not None)
        if encryption_enabled:
            data = self.c4crypto.decrypt(data)
        self.readBuffer += data

        while len(self.readBuffer) >= 8:
            packet_size = struct.unpack_from('i', self.readBuffer)[0]
            packet_type = struct.unpack_from('BB', self.readBuffer, 4)
            #If the packets reported size is less than a normal packet header, wrap it up to 8 as it should NEVER be that way.
            #This prevents an infinite loop.
            if packet_size < 8:
                print("[ShipProxy] Warning! Got invalid packet size %i. Resetting to 8 to prevent infinite loop..." % packet_size)
                packet_size = 8

            if verbose:
                print("[ShipProxy] [%i] Received packet with size %i, id %x:%x" % (
                    self.packetCount, packet_size, packet_type[0], packet_type[1]))

            if len(self.readBuffer) < packet_size:
                if verbose:
                    print("[ShipProxy] [%i] Buffer only contains %i, waiting for more data." % (
                        self.packetCount, len(self.readBuffer)))
                break

            packet = self.readBuffer[:packet_size]
            self.readBuffer = self.readBuffer[packet_size:]

            if packet is not None:
                for f in plugin_manager.rawPacketFunctions:
                    packet = f(self, packet, packet_type[0], packet_type[1])

            try:
                packet_handler = packets.packetList[packet_type]
                packet = packet_handler(self, packet)
            except KeyError:
                if verbose:
                    print("[ShipProxy] No packet function for id %x:%x, using default functionality..." % (
                        packet_type[0], packet_type[1]))

            if (packet_type[0], packet_type[1]) in plugin_manager.packetFunctions:
                for f in plugin_manager.packetFunctions[(packet_type[0], packet_type[1])]:
                    if packet is not None:
                        packet = f(self, packet)

            if packet is None:
                return

            if self.playerId is not None:
                if self.playerId not in clients.connectedClients:  # Inital add
                    clients.add_client(self)
                    self.loaded = True
                    for f in plugin_manager.onInitialConnection:
                        f(self)
                elif not self.loaded:
                    clients.populate_data(self)
                    for f in plugin_manager.onConnection:
                        f(self)

            if encryption_enabled:
                packet = self.c4crypto.encrypt(packet)
            else:
                # check if encryption was newly enabled while parsing this packet
                # if it was, then decrypt any packets that may be waiting in the buffer
                if self.c4crypto is not None:
                    encryption_enabled = True
                    self.readBuffer = self.c4crypto.decrypt(self.readBuffer)

            self.peer.transport.write(packet)

            self.packetCount += 1
            self.peer.packetCount = self.packetCount