public static void RepeatToAllClientsInChannel <T>(List <T> list, ClientInstance instance) { // Get the index of the client initially sending the message. // This is to get the channel ID from the user, and to only transmit to others with that channel ID. int index = ServerMessage.FindClientKeysIndex(instance.client); string json = Serialization.Serialize(list, false); byte[] data = Serialization.AddEndCharToMessage(json); for (int i = 0; i < ServerHandler.activeClients.Count; i++) { // Same channel check. if (ServerHandler.activeClients[i].ChannelID == ServerHandler.activeClients[index].ChannelID) { byte[] encrypted = EncryptMessage(data, ServerHandler.activeClients[i].RSAModulus, ServerHandler.activeClients[i].RSAExponent); // Try to send to [i] client, if the client does not exist anymore, remove from activeClients. try { StreamHandler.WriteToStream(ServerHandler.activeClients[i].TCPClient.GetStream(), encrypted); } catch (ObjectDisposedException) { ServerHandler.activeClients.RemoveAt(i); } catch (InvalidOperationException) { ServerHandler.activeClients.RemoveAt(i); } } } }
public static void ClientPrivateMessage(ClientInstance instance, string parameters) { string[] args = parameters.Split(','); for (int i = 0; i < ServerHandler.activeClients.Count; i++) { if (args[0] == ServerHandler.activeClients[i].ID.ToString() && !String.IsNullOrEmpty(args[1])) { // Get the index of the client sending the message. int index = ServerMessage.FindClientKeysIndex(instance.client); // Send the message to the reciever. ServerMessage.ClientPrivateMessage( ServerHandler.activeClients[i].TCPClient, ConsoleColor.Yellow, args[1], ServerHandler.activeClients[index].ID, ServerHandler.activeClients[index].Username ); // Send a message to the sender to confirm the message has been sent. ServerMessage.ServerClientMessage(instance.client, ConsoleColor.Yellow, "Message has been sent."); } } }
public static void Serialize <T>(List <T> message, ClientInstance instance, bool encrypt) { string json = Serialization.Serialize(message, false); byte[] data = Serialization.AddEndCharToMessage(json); if (encrypt) { int index = ServerMessage.FindClientKeysIndex(instance.client); data = EncryptMessage(data, ServerHandler.activeClients[index].RSAModulus, ServerHandler.activeClients[index].RSAExponent); } StreamHandler.WriteToStream(instance.stream, data); }
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); }
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); }
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; } }