From ebabd8036dd13f0557b1f6e44b6ddff596b59f56 Mon Sep 17 00:00:00 2001 From: SCM6WE Date: Thu, 23 Sep 2021 14:07:07 +0200 Subject: [PATCH 1/3] Propper file manager with encryption of files --- PolyChat/Controller.cs | 106 ++------------------------- PolyChat/Models/FileManager.cs | 127 ++++++++++++++++++++++++++++++++- 2 files changed, 131 insertions(+), 102 deletions(-) diff --git a/PolyChat/Controller.cs b/PolyChat/Controller.cs index 8d35d15..f9c8131 100644 --- a/PolyChat/Controller.cs +++ b/PolyChat/Controller.cs @@ -26,6 +26,7 @@ namespace PolyChat private const ushort PORT = 8050; // Controller private readonly MainPage UIController; + private readonly FileManager fileManager; // Props private Dictionary Connections = new Dictionary(); private string OwnName = ""; @@ -38,9 +39,9 @@ namespace PolyChat public Controller(MainPage uiController) { UIController = uiController; + fileManager = new FileManager(uiController); OwnIP = getIP(); - loadChats(); - encode("test"); + fileManager.loadChats(); Serve(); // test @@ -120,7 +121,7 @@ namespace PolyChat // send as json Connections[ip].SendMessage(json.ToString()); // save to logs - SaveChats(ip, json.ToString(), DateTime.Now); + fileManager.saveChats(ip, json.ToString(), DateTime.Now); } private void OnMessage(string ip, JToken[] data) @@ -131,7 +132,7 @@ namespace PolyChat DateTime now = DateTime.Now; Debug.WriteLine("RAW: " + data[0]); UIController.OnIncomingMessage(ip, data[0].ToString(), now); - SaveChats(ip, data[0].ToString(), now); + fileManager.saveChats(ip, data[0].ToString(), now); } else Debug.WriteLine("Undefined: " + data); } @@ -177,106 +178,11 @@ namespace PolyChat return null; } - /// - /// sends chatlogs as json array to uiController wit corrosponding ip - /// - /// in ui when chat is clicked connection gets established - /// - /// - public void loadChats() - { - //TODO: also load chatlogs when user tries to connect - //load dir and create if non existant - if (Directory.Exists("U:\\PolyChat\\Saves")) - { - Debug.WriteLine("--Path exists.--"); - } - else - { - Directory.CreateDirectory("U:\\PolyChat\\Saves"); - Debug.WriteLine("--Path Created--."); - } - - //go through all files and send ip and json array to ui - String[] filepaths = Directory.GetFiles("U:\\PolyChat\\Saves"); - if (filepaths.Length > 0) - { - Debug.WriteLine("---Loading Saves"); - foreach (String path in filepaths) - { - Debug.WriteLine($"--{path}"); - String jsonArr = File.ReadAllText(path); - String ip = Path.GetFileName(path); - ip = ip.Substring(0, ip.Length - 4); - Debug.WriteLine($"-{ip}"); - Debug.WriteLine(jsonArr); - UIController.OnIncomingConnection(ip); - UIController.OnIncomingMessages(ip, jsonArr); - } - } - } - - /// - /// Saves incoming chat message to - /// - /// - /// - 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 - JObject obj = JObject.Parse(json); - 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 = 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 + " ]"; - File.Delete($"U:\\PolyChat\\Saves\\{ip}.txt"); - File.WriteAllText($"U:\\PolyChat\\Saves\\{ip}.txt", output); - } - else - { - Debug.WriteLine("--Structure not intact--"); - Debug.WriteLine("--redoing file--"); - //structure not intact - //redo file - File.Delete($"U:\\PolyChat\\Saves\\{ip}.txt"); - File.WriteAllText($"U:\\PolyChat\\Saves\\{ip}.txt", $"[ {json} ]"); - } - } - else - { - Debug.WriteLine("--Creating new File--"); - //setup file - File.WriteAllText($"U:\\PolyChat\\Saves\\{ip}.txt", $"[ {json} ]"); - } - } - }).Start(); - } - private void encode(string json) { String ecryptetText = FileManager.encrypt(json); Debug.WriteLine(ecryptetText); - //Debug.WriteLine(FileManager.decrypt(ecryptetText)); + Debug.WriteLine(FileManager.decrypt(ecryptetText)); } } } diff --git a/PolyChat/Models/FileManager.cs b/PolyChat/Models/FileManager.cs index 815873c..3702fd5 100644 --- a/PolyChat/Models/FileManager.cs +++ b/PolyChat/Models/FileManager.cs @@ -5,12 +5,131 @@ using System.Text; using System.Threading.Tasks; using System.IO; using System.Security.Cryptography; - +using System.Diagnostics; +using System.Threading; +using Newtonsoft.Json.Linq; namespace PolyChat.Models { class FileManager { + // Controller + private readonly MainPage UIController; + + public FileManager(MainPage uiController) + { + UIController = uiController; + } + + /// + /// sends chatlogs as json array to uiController wit corrosponding ip + /// + /// in ui when chat is clicked connection gets established + /// + /// + public void loadChats() + { + //load dir and create if non existant + if (Directory.Exists("U:\\PolyChat\\Saves")) + { + Debug.WriteLine("--Path exists.--"); + } + else + { + Directory.CreateDirectory("U:\\PolyChat\\Saves"); + Debug.WriteLine("--Path Created--."); + } + + //go through all files and send ip and json array to ui + String[] filepaths = Directory.GetFiles("U:\\PolyChat\\Saves"); + if (filepaths.Length > 0) + { + Debug.WriteLine("---Loading Saves"); + foreach (String path in filepaths) + { + Debug.WriteLine($"--{path}"); + String jsonArr = decrypt(File.ReadAllText(path)); + String ip = Path.GetFileName(path); + ip = ip.Substring(0, ip.Length - 4); + Debug.WriteLine($"-{ip}"); + Debug.WriteLine(jsonArr); + UIController.OnIncomingConnection(ip); + UIController.OnIncomingMessages(ip, jsonArr); + } + } + } + + /// + /// Saves incoming chat message to + /// + /// + /// + 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 + JObject obj = JObject.Parse(json); + 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 + " ]"; + File.Delete($"U:\\PolyChat\\Saves\\{ip}.txt"); + File.WriteAllText($"U:\\PolyChat\\Saves\\{ip}.txt", encrypt(output)); + } + else + { + Debug.WriteLine("--Structure not intact--"); + Debug.WriteLine("--redoing file--"); + //structure not intact + //redo file + File.Delete($"U:\\PolyChat\\Saves\\{ip}.txt"); + File.WriteAllText($"U:\\PolyChat\\Saves\\{ip}.txt", encrypt($"[ {json} ]")); + } + } + else + { + Debug.WriteLine("--Creating new File--"); + //setup file + File.WriteAllText($"U:\\PolyChat\\Saves\\{ip}.txt", encrypt($"[ {json} ]")); + } + } + }).Start(); + } + + + //--------------------------------------------------------------------------------------------------- + //security + //--------------------------------------------------------------------------------------------------- + private void genKeys() + { + + } + + + /// + /// does exactly what it says. XD + /// + /// + /// public static String encrypt(String toEncrypt) { try { @@ -41,7 +160,11 @@ namespace PolyChat.Models } - + /// + /// does exactly what it says. XD + /// + /// + /// public static String decrypt(String toDecrypt) { try From 2ec9ffb30f88fd667c116e0e22e0c229cbaaf5e9 Mon Sep 17 00:00:00 2001 From: SCM6WE Date: Thu, 23 Sep 2021 14:26:35 +0200 Subject: [PATCH 2/3] Added method to delete userspeciffic logs. --- PolyChat/Controller.cs | 2 ++ PolyChat/Models/FileManager.cs | 61 ++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/PolyChat/Controller.cs b/PolyChat/Controller.cs index f9c8131..d7a2d6f 100644 --- a/PolyChat/Controller.cs +++ b/PolyChat/Controller.cs @@ -93,8 +93,10 @@ namespace PolyChat } else { + Debug.WriteLine("---- Added new Connection ----");//Todo show error! Connections.Add(ForeignIp, new Connection(socket, Data => OnMessage(ForeignIp, Data), CloseChat)); UIController.OnIncomingConnection(ForeignIp); + fileManager.loadChats(ForeignIp); } }); }); diff --git a/PolyChat/Models/FileManager.cs b/PolyChat/Models/FileManager.cs index 3702fd5..ec1485f 100644 --- a/PolyChat/Models/FileManager.cs +++ b/PolyChat/Models/FileManager.cs @@ -21,6 +21,67 @@ namespace PolyChat.Models UIController = uiController; } + /// + /// deletes chatlog of one speciffic user + /// + /// + public void deleteChat(String ip) + { + if (Directory.Exists("U:\\PolyChat\\Saves")) + { + Debug.WriteLine("--Path exists.--"); + //go through all files and send ip and json array to ui + String[] filepaths = Directory.GetFiles("U:\\PolyChat\\Saves"); + if (filepaths.Length > 0) + { + foreach (String path in filepaths) + { + if(Path.GetFileName(path).Equals(ip)) + { + File.Delete(path); + return; + } + } + } + } + } + + /// + /// loads one chatlog probably when someone tries to connect + /// + /// + public void loadChats() + { + //load dir and create if non existant + if (Directory.Exists("U:\\PolyChat\\Saves")) + { + Debug.WriteLine("--Path exists.--"); + } + else + { + Directory.CreateDirectory("U:\\PolyChat\\Saves"); + Debug.WriteLine("--Path Created--."); + } + + //go through all files and send ip and json array to ui + String[] filepaths = Directory.GetFiles("U:\\PolyChat\\Saves"); + if (filepaths.Length > 0) + { + Debug.WriteLine("---Loading Saves"); + foreach (String path in filepaths) + { + Debug.WriteLine($"--{path}"); + String jsonArr = decrypt(File.ReadAllText(path)); + String ip = Path.GetFileName(path); + ip = ip.Substring(0, ip.Length - 4); + Debug.WriteLine($"-{ip}"); + Debug.WriteLine(jsonArr); + UIController.OnIncomingConnection(ip); + UIController.OnIncomingMessages(ip, jsonArr); + } + } + } + /// /// sends chatlogs as json array to uiController wit corrosponding ip /// From 154f79667c891750d5d1012682f48619fa328cc7 Mon Sep 17 00:00:00 2001 From: SCM6WE Date: Thu, 23 Sep 2021 14:32:22 +0200 Subject: [PATCH 3/3] load chatlogs on newly created chat --- PolyChat/Controller.cs | 4 ++-- PolyChat/Models/FileManager.cs | 37 ++++++++++------------------------ 2 files changed, 13 insertions(+), 28 deletions(-) diff --git a/PolyChat/Controller.cs b/PolyChat/Controller.cs index d7a2d6f..1ec8a3e 100644 --- a/PolyChat/Controller.cs +++ b/PolyChat/Controller.cs @@ -93,10 +93,10 @@ namespace PolyChat } else { - Debug.WriteLine("---- Added new Connection ----");//Todo show error! + Debug.WriteLine("---- Added new Connection ----"); Connections.Add(ForeignIp, new Connection(socket, Data => OnMessage(ForeignIp, Data), CloseChat)); UIController.OnIncomingConnection(ForeignIp); - fileManager.loadChats(ForeignIp); + fileManager.loadChat(ForeignIp); } }); }); diff --git a/PolyChat/Models/FileManager.cs b/PolyChat/Models/FileManager.cs index ec1485f..474aee2 100644 --- a/PolyChat/Models/FileManager.cs +++ b/PolyChat/Models/FileManager.cs @@ -36,7 +36,7 @@ namespace PolyChat.Models { foreach (String path in filepaths) { - if(Path.GetFileName(path).Equals(ip)) + if (Path.GetFileName(path).Equals(ip)) { File.Delete(path); return; @@ -50,32 +50,15 @@ namespace PolyChat.Models /// loads one chatlog probably when someone tries to connect /// /// - public void loadChats() + public void loadChat(String ip) { //load dir and create if non existant if (Directory.Exists("U:\\PolyChat\\Saves")) { Debug.WriteLine("--Path exists.--"); - } - else - { - Directory.CreateDirectory("U:\\PolyChat\\Saves"); - Debug.WriteLine("--Path Created--."); - } - - //go through all files and send ip and json array to ui - String[] filepaths = Directory.GetFiles("U:\\PolyChat\\Saves"); - if (filepaths.Length > 0) - { - Debug.WriteLine("---Loading Saves"); - foreach (String path in filepaths) + if (File.Exists($"U:\\PolyChat\\Saves\\{ip}.txt")) { - Debug.WriteLine($"--{path}"); - String jsonArr = decrypt(File.ReadAllText(path)); - String ip = Path.GetFileName(path); - ip = ip.Substring(0, ip.Length - 4); - Debug.WriteLine($"-{ip}"); - Debug.WriteLine(jsonArr); + String jsonArr = decrypt(File.ReadAllText($"U:\\PolyChat\\Saves\\{ip}.txt")); UIController.OnIncomingConnection(ip); UIController.OnIncomingMessages(ip, jsonArr); } @@ -180,9 +163,9 @@ namespace PolyChat.Models //--------------------------------------------------------------------------------------------------- //security //--------------------------------------------------------------------------------------------------- - private void genKeys() - { - + private void genKeys() + { + } @@ -193,7 +176,8 @@ namespace PolyChat.Models /// public static String encrypt(String toEncrypt) { - try { + try + { string textToEncrypt = toEncrypt; string ToReturn = ""; string publickey = "santhosh"; @@ -214,7 +198,8 @@ namespace PolyChat.Models ToReturn = Convert.ToBase64String(ms.ToArray()); } return ToReturn; - } catch (Exception ex) + } + catch (Exception ex) { throw new Exception(ex.Message, ex.InnerException); }