Cleanup / Comments
This commit is contained in:
committed by
Felix Hartmann (PEA3-Fe-FI)
parent
ba4d0b619e
commit
1daaa52c36
@@ -16,7 +16,6 @@ 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
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// starts server for clients to connect to
|
||||
/// </summary>
|
||||
private void Serve()
|
||||
{
|
||||
Debug.WriteLine("--- Controller.Serve ---");
|
||||
@@ -107,6 +109,12 @@ namespace PolyChat
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sends message to given ip
|
||||
/// </summary>
|
||||
/// <param name="ip"></param>
|
||||
/// <param name="type"></param>
|
||||
/// <param name="content"></param>
|
||||
public void SendMessage(string ip, string type, string content)
|
||||
{
|
||||
Debug.WriteLine("--- Controller.SendMessage ---");
|
||||
@@ -122,6 +130,11 @@ namespace PolyChat
|
||||
fileManager.saveChats(ip, json.ToString(), DateTime.Now);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// if We recieve a message this method gets triggert
|
||||
/// </summary>
|
||||
/// <param name="ip">ip from sender</param>
|
||||
/// <param name="data">String that is send</param>
|
||||
private void OnMessage(string ip, JToken[] data)
|
||||
{
|
||||
Debug.WriteLine("--- Controller.OnMessage ---");
|
||||
@@ -135,6 +148,12 @@ namespace PolyChat
|
||||
else Debug.WriteLine("Undefined: " + data);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Closes chat connection
|
||||
/// </summary>
|
||||
/// <param name="IP">ip of user to be closed</param>
|
||||
/// <param name="wasConnected"></param>
|
||||
/// <param name="delete"></param>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// sends incoming message to ui
|
||||
/// </summary>
|
||||
/// <param name="ip">ip of client that send the message</param>
|
||||
/// <param name="jsonArr">the json array that is ti be displayed in th gui</param>
|
||||
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();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// returns your own ip that starts with 10. becuase that is our subnet
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
//===============================================================================================================================================
|
||||
|
||||
/// <summary>
|
||||
/// deletes chatlog of one speciffic user
|
||||
/// </summary>
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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
|
||||
/// </summary>
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves incoming chat message to
|
||||
/// Saves incoming chat message to U:\PolyChat\Saves\ip.txt
|
||||
/// </summary>
|
||||
/// <param name="ip"></param>
|
||||
/// <param name="json"></param>
|
||||
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
|
||||
//===============================================================================================================================================
|
||||
|
||||
/// <summary>
|
||||
/// generates keypair [public, privte] (100% secure, trust me)
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private String[] genKeys()
|
||||
{
|
||||
return new String[] {"sehrSichererKey", "nochVielSichererKey"};
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// does exactly what it says. XD
|
||||
///
|
||||
/// encrypts given string and returns unreadable string
|
||||
/// </summary>
|
||||
/// <param name="toEncrypt"></param>
|
||||
/// <returns></returns>
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// does exactly what it says. XD
|
||||
///
|
||||
/// takes in unreadable string and returns infirmation
|
||||
/// </summary>
|
||||
/// <param name="toEncrypt"></param>
|
||||
/// <returns></returns>
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user