From 1daaa52c3649130dd9b2df81497e67156ce10c8a Mon Sep 17 00:00:00 2001 From: SCM6WE Date: Thu, 23 Sep 2021 16:03:12 +0200 Subject: [PATCH] Cleanup / Comments --- PolyChat/Controller.cs | 48 +++++++++++++++---- PolyChat/Models/FileManager.cs | 85 ++++++++++++++++++---------------- 2 files changed, 83 insertions(+), 50 deletions(-) diff --git a/PolyChat/Controller.cs b/PolyChat/Controller.cs index fa5e171..125a826 100644 --- a/PolyChat/Controller.cs +++ b/PolyChat/Controller.cs @@ -15,8 +15,7 @@ using Windows.Security.Cryptography; using Windows.Storage.Streams; namespace PolyChat -{ - +{ // 10.1.211.26 Marc // 10.1.218.90 Felix // 10.4.141.77 Pat @@ -37,7 +36,7 @@ namespace PolyChat public Controller(MainPage uiController) { UIController = uiController; - fileManager = new FileManager(uiController); + fileManager = new FileManager(this); fileManager.loadChats(); Serve(); @@ -65,6 +64,9 @@ namespace PolyChat } + /// + /// starts server for clients to connect to + /// private void Serve() { Debug.WriteLine("--- Controller.Serve ---"); @@ -107,6 +109,12 @@ namespace PolyChat } } + /// + /// Sends message to given ip + /// + /// + /// + /// public void SendMessage(string ip, string type, string content) { Debug.WriteLine("--- Controller.SendMessage ---"); @@ -121,7 +129,12 @@ namespace PolyChat // save to logs fileManager.saveChats(ip, json.ToString(), DateTime.Now); } - + + /// + /// if We recieve a message this method gets triggert + /// + /// ip from sender + /// String that is send private void OnMessage(string ip, JToken[] data) { Debug.WriteLine("--- Controller.OnMessage ---"); @@ -135,6 +148,12 @@ namespace PolyChat else Debug.WriteLine("Undefined: " + data); } + /// + /// Closes chat connection + /// + /// ip of user to be closed + /// + /// public void CloseChat(string IP, bool wasConnected = true, bool delete = false) { Debug.WriteLine($"Deleting connection with IP:{IP}"); @@ -149,6 +168,17 @@ namespace PolyChat fileManager.deleteChat(IP); } + /// + /// sends incoming message to ui + /// + /// ip of client that send the message + /// the json array that is ti be displayed in th gui + public void SendIncomingMessageUI(String ip, String jsonArr) + { + UIController.OnIncomingConnection(ip); + UIController.OnIncomingMessages(ip, jsonArr); + } + private void CloseChatUI(string IP, bool wasConnected = true, bool delete = false) { UIController.OnChatPartnerDeleted(IP); @@ -166,6 +196,10 @@ namespace PolyChat return Connections.ContainsKey(ip) && Connections[ip].IsConnected(); } + /// + /// returns your own ip that starts with 10. becuase that is our subnet + /// + /// public static string getIP() { IPHostEntry ipEntry = Dns.GetHostEntry(Dns.GetHostName()); @@ -180,11 +214,5 @@ namespace PolyChat return null; } - private void encode(string json) - { - String ecryptetText = FileManager.encrypt(json); - Debug.WriteLine(ecryptetText); - Debug.WriteLine(FileManager.decrypt(ecryptetText)); - } } } diff --git a/PolyChat/Models/FileManager.cs b/PolyChat/Models/FileManager.cs index 9a8045e..e841c29 100644 --- a/PolyChat/Models/FileManager.cs +++ b/PolyChat/Models/FileManager.cs @@ -14,13 +14,21 @@ namespace PolyChat.Models class FileManager { // Controller - private readonly MainPage UIController; + private readonly Controller controller; - public FileManager(MainPage uiController) + + //=============================================================================================================================================== + //Constructor + //=============================================================================================================================================== + public FileManager(Controller controller) { - UIController = uiController; + this.controller = controller; } + //=============================================================================================================================================== + // editing save files + //=============================================================================================================================================== + /// /// deletes chatlog of one speciffic user /// @@ -59,14 +67,13 @@ namespace PolyChat.Models if (File.Exists($"U:\\PolyChat\\Saves\\{ip}.txt")) { String jsonArr = decrypt(File.ReadAllText($"U:\\PolyChat\\Saves\\{ip}.txt")); - UIController.OnIncomingConnection(ip); - UIController.OnIncomingMessages(ip, jsonArr); + controller.SendIncomingMessageUI(ip, jsonArr); } } } /// - /// sends chatlogs as json array to uiController wit corrosponding ip + /// sends chatlogs as json array to uiController with corrosponding ip /// /// in ui when chat is clicked connection gets established /// @@ -97,22 +104,18 @@ namespace PolyChat.Models ip = ip.Substring(0, ip.Length - 4); Debug.WriteLine($"-{ip}"); Debug.WriteLine(jsonArr); - UIController.OnIncomingConnection(ip); - UIController.OnIncomingMessages(ip, jsonArr); + controller.SendIncomingMessageUI(ip, jsonArr); } } } /// - /// Saves incoming chat message to + /// Saves incoming chat message to U:\PolyChat\Saves\ip.txt /// /// /// public void saveChats(string ip, string json, DateTime timeStamp) { - //Vielleicht noch so machen dass die mit gleicher ip nacheinander gemacht - //werden damit es nicht zu überschreibungen kommt vielleicth auch ganz oben oder am ende ne - //writing flag setzen oder auch in der datei selbst ne flag setzen new Thread(() => { //breaking if namechange @@ -120,39 +123,39 @@ namespace PolyChat.Models if (!obj["type"].ToString().Equals("username")) { //adding timestamp - obj = JObject.Parse(json); obj.Add(new JProperty("timestamp", timeStamp)); json = obj.ToString(); + if (File.Exists($"U:\\PolyChat\\Saves\\{ip}.txt")) { Debug.WriteLine("--File allready exists--"); + //check for integraty of file string output = decrypt(File.ReadAllText($"U:\\PolyChat\\Saves\\{ip}.txt")); Debug.WriteLine($"---{output}---"); if (output.Substring(0, 1).Equals("[") && output.Substring(output.Length - 1, 1).Equals("]")) { - Debug.WriteLine("--adding new chatmessage--"); //structure intact //save new chat - String saved = output.Substring(0, output.Length - 1); - output = saved + ", " + json + " ]"; + Debug.WriteLine("--adding new chatmessage--"); + output = output.Substring(0, output.Length - 1) + ", " + json + " ]"; //rip appart and put file back together File.Delete($"U:\\PolyChat\\Saves\\{ip}.txt"); - File.WriteAllText($"U:\\PolyChat\\Saves\\{ip}.txt", encrypt(output)); + File.WriteAllText($"U:\\PolyChat\\Saves\\{ip}.txt", encrypt(output)); //encrypt and save to textfile } else { - Debug.WriteLine("--Structure not intact--"); - Debug.WriteLine("--redoing file--"); //structure not intact //redo file + Debug.WriteLine("--Structure not intact--"); + Debug.WriteLine("--redoing file--"); File.Delete($"U:\\PolyChat\\Saves\\{ip}.txt"); - File.WriteAllText($"U:\\PolyChat\\Saves\\{ip}.txt", encrypt($"[ {json} ]")); + File.WriteAllText($"U:\\PolyChat\\Saves\\{ip}.txt", encrypt($"[ {json} ]")); //encrypt and write to file } } else { - Debug.WriteLine("--Creating new File--"); //setup file + Debug.WriteLine("--Creating new File--"); File.WriteAllText($"U:\\PolyChat\\Saves\\{ip}.txt", encrypt($"[ {json} ]")); } } @@ -160,28 +163,35 @@ namespace PolyChat.Models } - //--------------------------------------------------------------------------------------------------- - //security - //--------------------------------------------------------------------------------------------------- - private void genKeys() + //=============================================================================================================================================== + // Encryption + //=============================================================================================================================================== + + /// + /// generates keypair [public, privte] (100% secure, trust me) + /// + /// + private String[] genKeys() { - + return new String[] {"sehrSichererKey", "nochVielSichererKey"}; } - + /// /// does exactly what it says. XD + /// + /// encrypts given string and returns unreadable string /// /// /// - public static String encrypt(String toEncrypt) + private String encrypt(String toEncrypt) { try { string textToEncrypt = toEncrypt; string ToReturn = ""; - string publickey = "santhosh"; - string secretkey = "engineer"; + string publickey = genKeys()[0]; + string secretkey = genKeys()[1]; byte[] secretkeyByte = { }; secretkeyByte = System.Text.Encoding.UTF8.GetBytes(secretkey); byte[] publickeybyte = { }; @@ -203,22 +213,23 @@ namespace PolyChat.Models { throw new Exception(ex.Message, ex.InnerException); } - } /// /// does exactly what it says. XD + /// + /// takes in unreadable string and returns infirmation /// /// /// - public static String decrypt(String toDecrypt) + private String decrypt(String toDecrypt) { try { string textToDecrypt = toDecrypt; string ToReturn = ""; - string publickey = "santhosh"; - string privatekey = "engineer"; + string publickey = genKeys()[0]; + string privatekey = genKeys()[1]; byte[] privatekeyByte = { }; privatekeyByte = System.Text.Encoding.UTF8.GetBytes(privatekey); byte[] publickeybyte = { }; @@ -244,10 +255,4 @@ namespace PolyChat.Models } } } - - - - - - }