Ejemplo n.º 1
0
        public static void DecryptPacket(ServerState state, byte[] packet)
        {
            using (var reader = new Reader(packet))
            {
                var ID = reader.ReadUInt16();
                reader.Seek(3, SeekOrigin.Current);
                var Version = reader.ReadUInt16();

                byte[] cipherText = reader.ReadAllBytes, plainText;

                var Name = Packet_Names.GetName(ID);

                switch (ID)
                {
                case 10100:
                {
                    plainText = cipherText;

                    break;
                }

                case 10101:
                {
                    state.ClientKey = cipherText.Take(32).ToArray();

                    var nonce = GenericHash.Hash(state.ClientKey.Concat(state.ServerKey.PublicKey).ToArray(), null,
                                                 24);

                    cipherText = cipherText.Skip(32).ToArray();

                    plainText = PublicKeyBox.Open(cipherText, nonce, state.ServerKey.PrivateKey, state.ClientKey);

                    state.SessionKey        = plainText.Take(24).ToArray();
                    state.ClientState.Nonce = plainText.Skip(24).Take(24).ToArray();

                    plainText = plainText.Skip(24).Skip(24).ToArray();

                    break;
                }

                default:
                {
                    state.ClientState.Nonce = Utilities.Increment(Utilities.Increment(state.ClientState.Nonce));

                    plainText = SecretBox.Open(new byte[16].Concat(cipherText).ToArray(), state.ClientState.Nonce,
                                               state.SharedKey);

                    break;
                }
                }

                ClientCrypto.EncryptPacket(state.ClientState, ID, Version, plainText);

                Console.WriteLine(
                    $"[{DateTime.Now.ToLongTimeString()}, CLIENT, {ID}] {Resources.Definition.Decode(new Reader(plainText), ID)}");

                Logger.Write(BitConverter.ToString(plainText).Replace("-", string.Empty), $"{ID}_{Name}",
                             LogType.PACKET);
            }
        }
Ejemplo n.º 2
0
        public static void ReceiveCallback(IAsyncResult Result)
        {
            try
            {
                var state  = Result.AsyncState as State;
                var socket = state.Socket;

                int bytesAvailable, bytesNeeded, bytesRead = 0, bytesReceived = socket.EndReceive(Result);

                while (bytesRead < bytesReceived)
                {
                    bytesAvailable = bytesReceived - bytesRead;

                    if (bytesReceived > 0 && state.Packet.Length >= 7)
                    {
                        var payloadLength =
                            BitConverter.ToInt32(new byte[1].Concat(state.Packet.Skip(2).Take(3)).Reverse().ToArray(),
                                                 0);

                        bytesNeeded = payloadLength - (state.Packet.Length - 7);
                        if (bytesAvailable >= bytesNeeded)
                        {
                            state.Packet = state.Packet.Concat(state.Buffer.Skip(bytesRead).Take(bytesNeeded))
                                           .ToArray();
                            bytesRead      += bytesNeeded;
                            bytesAvailable -= bytesNeeded;

                            if (state.GetType() == typeof(ClientState))
                            {
                                ClientCrypto.DecryptPacket(state as ClientState, state.Packet);
                            }
                            else if (state.GetType() == typeof(ServerState))
                            {
                                ServerCrypto.DecryptPacket(state as ServerState, state.Packet);
                            }

                            state.Packet = new byte[0];
                        }
                        else
                        {
                            state.Packet = state.Packet.Concat(state.Buffer.Skip(bytesRead).Take(bytesAvailable))
                                           .ToArray();
                            bytesRead      = bytesReceived;
                            bytesAvailable = 0;
                        }
                    }
                    else if (bytesAvailable >= 7)
                    {
                        state.Packet    = state.Packet.Concat(state.Buffer.Skip(bytesRead).Take(7)).ToArray();
                        bytesRead      += 7;
                        bytesAvailable -= 7;
                    }
                    else
                    {
                        state.Packet   = state.Packet.Concat(state.Buffer.Skip(bytesRead).Take(bytesAvailable)).ToArray();
                        bytesRead      = bytesReceived;
                        bytesAvailable = 0;
                    }
                }

                socket.BeginReceive(state.Buffer, 0, 2048, 0, ReceiveCallback, state);
            }
            catch (Exception)
            {
            }
        }