Example #1
0
        public static void ReplyToDataRequest(ClientInstance instance, string message, CommandDataTypes dataType)
        {
            List <DataReplyFormat> serverMessage = new();

            serverMessage.Add(new DataReplyFormat
            {
                MessageType = MessageTypes.DATAREPLY,
                DataType    = dataType,
                Data        = message,
            });

            string json = Serialization.Serialize(serverMessage, false);

            byte[] data = Serialization.AddEndCharToMessage(json);

            int index = ServerMessage.FindClientKeysIndex(instance.client);

            byte[] encrypted = MessageHandler.EncryptMessage(data, ServerHandler.activeClients[index].RSAModulus, ServerHandler.activeClients[index].RSAExponent);

            StreamHandler.WriteToStream(instance.stream, encrypted);
        }
Example #2
0
        // The loop to recieve incoming packets.
        public static void RecieveMessage(ClientInstance instance)
        {
            long lastMessage = 0;

            byte[] bytes = new byte[8192];

            while ((instance.stream.Read(bytes, 0, bytes.Length)) > 0)
            {
                // Checks if the client has sent messages too fast.
                if (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() - lastMessage >= ServerConfigFormat.serverChosenClientTime)
                {
                    lastMessage = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();

                    // I had some issues with trailing zero bytes, and this solves that.
                    int i = bytes.Length - 1;
                    while (bytes[i] == 0)
                    {
                        --i;
                    }

                    byte[] bytesResized = new byte[i + 1];
                    Array.Copy(bytes, bytesResized, i + 1);

                    Array.Clear(bytes, 0, bytes.Length); // Seems to solve an issue where bytes from the last message stick around.


                    // Is client verified, meaning client has established initial connection and communication.
                    if (instance.clientVerified)
                    {
                        VerifiedRecieve(instance, bytesResized);
                    }
                    else
                    {
                        NotVerifiedRecieve(instance, bytesResized);
                    }
                }
            }
        }
Example #3
0
        public static void VerifiedRecieve(ClientInstance instance, byte[] bytes)
        {
            // Random CryptographicException that i am not able to replicate
            // TODO Investigate reason for exception.
            string message = "";

            try
            {
                message = Encryption.DecryptMessageData(bytes);
            }
            catch (CryptographicException)
            {
                int    index         = ServerMessage.FindClientKeysIndex(instance.client);
                string serverMessage = String.Format("{0} Was kicked due to a cryptography error.", ServerHandler.activeClients[index].Username);
                ServerMessage.ServerGlobalMessage(ConsoleColor.Yellow, serverMessage);
                instance.client.Close();
            }

            string messageFormatted = Common.ReturnEndOfStream(message);

            byte[] messageBytes = Encoding.ASCII.GetBytes(messageFormatted);

            List <MessageFormat> messageList;

            switch (Common.ReturnMessageType(messageBytes))
            {
            case MessageTypes.MESSAGE:
                int index = ServerMessage.FindClientKeysIndex(instance.client);

                // Error when deserializing message.
                // Could mean corrupt or icorrect data has been transmitted.
                try
                {
                    messageList = Serialization.DeserializeMessageFormat(messageFormatted);

                    List <MessageReplyFormat> replyFormat = new();
                    replyFormat.Add(new MessageReplyFormat
                    {
                        MessageType   = MessageTypes.MESSAGEREPLY,
                        Message       = messageList[0].Message,
                        ID            = ServerHandler.activeClients[index].ID,
                        Username      = ServerHandler.activeClients[index].Username,
                        UsernameColor = ServerHandler.activeClients[index].UsernameColor,
                    });

                    ConsoleOutput.RecievedMessageReplyFormat(replyFormat, ServerHandler.activeClients[index].ChannelID);


                    // Encrypts the message and sends it to all clients.
                    RepeatToAllClientsInChannel(replyFormat, instance);
                }
                catch (JsonException)
                {
                    string serverMessage = String.Format("({0}) {1} Was kicked due to an invalid message.", ServerHandler.activeClients[index].ID, ServerHandler.activeClients[index].Username);
                    ServerMessage.ServerGlobalMessage(ConsoleColor.Yellow, serverMessage);
                    instance.client.Close();
                }
                break;

            case MessageTypes.DATAREQUEST:
                List <DataRequestFormat> dataList = Serialization.DeserializeDataRequestFormat(messageFormatted);
                CommandHandler.RecievedDataRequest(instance, dataList);
                break;
            }
        }
Example #4
0
 public static void ClientChannelSwitch(ClientInstance instance, string parameters)
 {
     ChannelHandler.ClientRequestsChannelSwitch(instance, parameters);
 }