Exemplo n.º 1
0
        public IPacket ReadPacket(out byte[] rawPacket, out byte[] decryptedPacket)
        {
            /* Receive data from the socket, saves it a buffer,
             * then reads packet from the buffer.
             */

            var timeout = DateTime.Now.AddMilliseconds(500); // 500ms

            while (DataAvailable && DateTime.Now < timeout)
            {
                CoCStream.ReadToBuffer(); // reads data saves it a buffer

                //var packetBuffer = new PacketBuffer(CoCStream.ReadBuffer.ToArray());
                var enPacketReader = new PacketReader(CoCStream.ReadBuffer);

                // read header
                var packetID      = enPacketReader.ReadUInt16();
                var packetLength  = enPacketReader.ReadInt24();
                var packetVersion = enPacketReader.ReadUInt16();

                // read body
                if (packetLength > enPacketReader.BaseStream.Length) // check if data is enough data is avaliable in the buffer
                {
                    continue;
                }

                var encryptedData = GetPacketBody(packetLength);
                var decryptedData = (byte[])encryptedData.Clone(); // cloning just cause we want the encrypted data

                CoCCrypto.Decrypt(decryptedData);

                var dePacketReader = new PacketReader(new MemoryStream(decryptedData));

                var packet = CreatePacketInstance(packetID);
                if (packet is UnknownPacket)
                {
                    packet = new UnknownPacket
                    {
                        ID      = packetID,
                        Length  = packetLength,
                        Version = packetVersion
                    };
                    ((UnknownPacket)packet).EncryptedData = encryptedData;
                }

                decryptedPacket = decryptedData;
                rawPacket       = ExtractRawPacket(packetLength);
                //CoCStream.ReadBuffer = new MemoryStream(4096);
                //CoCStream.Write(packetBuffer.Buffer, 0, packetBuffer.Buffer.Length);
                try
                {
                    packet.ReadPacket(dePacketReader);
                }
                catch { }
                return(packet);
            }
            decryptedPacket = null;
            rawPacket       = null;
            return(null);
        }
Exemplo n.º 2
0
        public ProxyNetworkManager(Socket connection)
        {
            if (PacketDictionary == null)
                InitializePacketDictionary();

            Connection = connection;
            CoCStream = new CoCStream(connection);
            CoCCrypto = new CoCCrypto();
        }
Exemplo n.º 3
0
        public ProxyNetworkManager(Socket connection)
        {
            if (PacketDictionary == null)
            {
                InitializePacketDictionary();
            }

            Connection = connection;
            CoCStream  = new CoCStream(connection);
            CoCCrypto  = new CoCCrypto();
        }
        public void WritePacket(IPacket packet)
        {
            /* Writes packet to a buffer,
             * then sends the buffer to the socket
             */
            MemoryStream tempStream = new MemoryStream();

            packet.WritePacket(new PacketWriter(tempStream));
            byte[] buffer = new byte[tempStream.Length];
            tempStream.Read(buffer, 0, buffer.Length);
            buffer = (buffer.Skip(HeaderSize).ToArray());
            CoCCrypto.Encrypt(buffer);
            CoCStream.Write(buffer, 0, buffer.Length);
        }