예제 #1
0
        public static void NotVerifiedRecieve(ClientInstance instance, byte[] bytes)
        {
            string messageFormatted = Common.ReturnEndOfStream(Encoding.ASCII.GetString(bytes));

            List <ConnectionMessageFormat> list = Serialization.DeserializeConnectionMessageFormat(messageFormatted);

            int clientID = NextAvailableClientID();

            // Add this client to NetStreams to keep track of connection.
            ServerHandler.activeClients.Add(new ClientList
            {
                ID            = clientID,
                ChannelID     = ServerConfigFormat.serverChosenDefaultChannelID,
                TCPClient     = instance.client,
                Username      = list[0].Username,
                RSAExponent   = list[0].RSAExponent,
                RSAModulus    = list[0].RSAModulus,
                UsernameColor = list[0].UserNameColor
            });

            if (ServerConfigFormat.serverChosenVersionCheck)
            {
                // Check if server and client versions are the same before continuing.
                if (!VersionHandler.VersionCheck(instance, list[0].ClientVersion))
                {
                    // Remove the item just added to active clients.
                    // The reason it is added before is to have a list to index when sending server message to.
                    ServerHandler.activeClients.RemoveAt(ServerHandler.activeClients.Count - 1);
                    return;
                }
            }

            string message = String.Format("({0}) {1} connected.", clientID, list[0].Username);

            ServerMessage.ServerGlobalMessage(ConsoleColor.Yellow, message);


            instance.clientVerified = true;

            List <WelcomeMessageFormat> welcomeMessage = new();

            welcomeMessage.Add(new WelcomeMessageFormat
            {
                MessageType      = MessageTypes.WELCOME,
                ConnectMessage   = ServerConfigFormat.serverChosenWelcomeMessage,
                ServerName       = ServerConfigFormat.serverChosenName,
                RSAExponent      = Encryption.RSAExponent,
                RSAModulus       = Encryption.RSAModulus,
                ServerVersion    = Assembly.GetExecutingAssembly().GetName().Version.ToString(),
                ClientID         = clientID,
                DefaultChannelID = ServerConfigFormat.serverChosenDefaultChannelID
            });


            Serialize(welcomeMessage, instance, false);
        }
예제 #2
0
        public static void ClientRequestsChannelSwitch(ClientInstance instance, string parameters)
        {
            bool parse = int.TryParse(parameters, out int outNum);
            int  index = ServerMessage.FindClientKeysIndex(instance.client);

            // If client has sent an invalid id.
            if (!parse)
            {
                // Simply send back the users current channel id, which has not changed.
                CommandHandler.ReplyToDataRequest(instance, ServerHandler.activeClients[index].ChannelID.ToString(), CommandDataTypes.CHANNELSWITCH);
                return;
            }

            // Check if the client's requested new channel id actually exists.
            bool foundID = false;

            for (int i = 0; i < serverChannels.Count; i++)
            {
                if (serverChannels[i].ID == outNum)
                {
                    foundID = true;
                }
            }
            if (!foundID)
            {
                // Simply send back the users current channel id, which has not changed.
                CommandHandler.ReplyToDataRequest(instance, ServerHandler.activeClients[index].ChannelID.ToString(), CommandDataTypes.CHANNELSWITCH);
                return;
            }
            // Check if client is trying to switch to the channel they are currently in.
            if (ServerHandler.activeClients[index].ChannelID == outNum)
            {
                // Simply send back the users current channel id, which has not changed.
                CommandHandler.ReplyToDataRequest(instance, ServerHandler.activeClients[index].ChannelID.ToString(), CommandDataTypes.CHANNELSWITCH);
                return;
            }

            ServerHandler.activeClients[index].ChannelID = outNum;

            CommandHandler.ReplyToDataRequest(instance, outNum.ToString(), CommandDataTypes.CHANNELSWITCH);

            string message = String.Format("({0}) {1} switched channel to: {2}", ServerHandler.activeClients[index].ID, ServerHandler.activeClients[index].Username, outNum.ToString());

            ServerMessage.ServerGlobalMessage(ConsoleColor.Yellow, message);
        }
예제 #3
0
 public static void Execute()
 {
     for (int i = 0; i < ServerHandler.activeClients.Count; i++)
     {
         bool parse = int.TryParse(Commands.CommandArguments[0], out int outNum);
         if (parse && ServerHandler.activeClients[i].ID == outNum)
         {
             string message = String.Format("({0}) {1} has been kicked.", ServerHandler.activeClients[i].ID, ServerHandler.activeClients[i].Username);
             ServerMessage.ServerGlobalMessage(ConsoleColor.Yellow, message);
             // Try / catch as an out of range solution.
             try
             {
                 ServerHandler.activeClients[i].TCPClient.Close();
             }
             catch (ArgumentOutOfRangeException) { }
             return;
         }
     }
 }
예제 #4
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;
            }
        }
예제 #5
0
 public static void Execute()
 {
     ServerMessage.ServerGlobalMessage(ConsoleColor.Yellow, Commands.CommandArguments[0]);
 }