Example #1
0
        private void ParseData(byte[] data)
        {
            byte[] x = null;
            if (Key != null)
            {
                try
                {
                    byte[] buffer = Convert.FromBase64String(data.GetString());
                    x = buffer.Decrypt(Key);
                }
                catch { x = data; }
            }
            else
            {
                x = data;
            }

            // Parse any commands from our received data.
            string packet = x.GetString();

            try
            {
                if (packet.Contains(ECPPacketType.HANDSHAKE.GetString()))
                {
                    // Generate a new handshake request for the client.
                    if (packet.Substring(0, ECPPacketType.HANDSHAKE.GetString().Length) == ECPPacketType.HANDSHAKE.GetString())
                    {
                        try
                        {
                            // Log that we've received a handshake request.
                            Server.LogOutput(ClientID + " has requested a handshake.", EntryType.Notice);

                            // Jit our diffie-hellman before sending the actual request packet.
                            ECPDiffieHellman Jitter = new ECPDiffieHellman(32).GenerateRequest();

                            // Create a new request packet.
                            Exchange = new ECPDiffieHellman(256).GenerateRequest();

                            // Send our packet to the client and log it.
                            string message = ECPPacketType.HANDSHAKE.GetString() + Exchange.ToString();
                            Server.Broadcast(message, ClientID);
                            Server.LogOutput("A handshake has been sent to " + ClientID + ".", EntryType.Notice);
                        }
                        catch //catch(Exception ex)
                        {
                            //Server.LogOutput(ex.StackTrace, EntryType.Error);
                            Server.LogOutput("A handshake could not be sent to " + ClientID + ".", EntryType.Error);
                        }
                    }
                }
                else if (packet.Contains(ECPPacketType.HREPLY.GetString()))
                {
                    // Generate a new encryption key using our handshake response.
                    if (packet.Substring(0, ECPPacketType.HREPLY.GetString().Length) == ECPPacketType.HREPLY.GetString())
                    {
                        try
                        {
                            // Parse our response to get our handshake reply.
                            string response = packet.Replace(ECPPacketType.HREPLY.GetString(), null);

                            // Generate a new session key from our response.
                            Exchange.HandleResponse(response);
                            Key = Convert.ToBase64String(Exchange.Key);
                            Server.LogOutput("A new session key has been generated!", EntryType.Success);
                        }
                        catch { Server.LogOutput("The handshake response could not be processed.", EntryType.Error); }

                        if (Key != null)
                        {
                            Server.Broadcast(ECPPacketType.HSUCCESS.GetString().Encrypt(Key), ClientID);
                            Server.LogOutput(Key, EntryType.General);
                        }
                    }
                }
                else
                {
                    // Pass our data to our event.
                    Server.DataReceived(ClientID, x);
                }
            }
            catch { }
        }
Example #2
0
        private void ParseData(byte[] data)
        {
            byte[] x = null;
            if (Key != null)
            {
                try
                {
                    byte[] buffer = Convert.FromBase64String(data.GetString());
                    x = buffer.Decrypt(Key);
                }
                catch { x = data; }
            }
            else
            {
                x = data;
            }

            // Parse any commands from our received data.
            string command = x.GetString();

            try
            {
                if (command.Contains(commands[0]))
                {
                    // Generate a new handshake request for the server.
                    if (command.Substring(0, commands[0].Length) == commands[0])
                    {
                        try
                        {
                            // Create a new response packet.
                            string response = command.Replace(commands[0], null);
                            Exchange = new ECPDiffieHellman(256).GenerateResponse(response);

                            // Generate a new session key from our response.
                            Key = Convert.ToBase64String(Exchange.Key);

                            // Send our reponse packet to the server and log it.
                            string message = "{HREPLY}" + Exchange.ToString();
                            Send(message);
                            Handshake = false;
                            LogOutput("A new session key has been generated!", EntryType.Success);
                        }
                        catch { LogOutput("A handshake could not be sent to the server.", EntryType.Error); }

                        if (Key != null)
                        {
                            LogOutput(Key, EntryType.General);
                        }
                    }
                }
                else if (command.Contains(commands[2]))
                {
                    if (command.Substring(0, commands[2].Length) == commands[2])
                    {
                        Stream.Close();
                        Client.Close();
                        ClientDisconnected();
                        LogOutput("The connection has been closed by the server.", EntryType.Notice, true);
                    }
                }
                else
                {
                    // Pass our data to our event.
                    DataReceived(x);
                }
            }
            catch { }
        }