Merge branch 'ConnectionController' of https://sourcecode.socialcoding.bosch.com/scm/~hpl2fe/polychat into ConnectionController
This commit is contained in:
@@ -15,8 +15,7 @@ using Windows.Security.Cryptography;
|
|||||||
using Windows.Storage.Streams;
|
using Windows.Storage.Streams;
|
||||||
|
|
||||||
namespace PolyChat
|
namespace PolyChat
|
||||||
{
|
{
|
||||||
|
|
||||||
// 10.1.211.26 Marc
|
// 10.1.211.26 Marc
|
||||||
// 10.1.218.90 Felix
|
// 10.1.218.90 Felix
|
||||||
// 10.4.141.77 Pat
|
// 10.4.141.77 Pat
|
||||||
@@ -37,7 +36,8 @@ namespace PolyChat
|
|||||||
public Controller(MainPage uiController)
|
public Controller(MainPage uiController)
|
||||||
{
|
{
|
||||||
UIController = uiController;
|
UIController = uiController;
|
||||||
fileManager = new FileManager(uiController);
|
fileManager = new FileManager(this);
|
||||||
|
//OwnIP = getIP();
|
||||||
fileManager.loadChats();
|
fileManager.loadChats();
|
||||||
Serve();
|
Serve();
|
||||||
|
|
||||||
@@ -65,6 +65,9 @@ namespace PolyChat
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// starts server for clients to connect to
|
||||||
|
/// </summary>
|
||||||
private void Serve()
|
private void Serve()
|
||||||
{
|
{
|
||||||
Debug.WriteLine("--- Controller.Serve ---");
|
Debug.WriteLine("--- Controller.Serve ---");
|
||||||
@@ -107,6 +110,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)
|
public void SendMessage(string ip, string type, string content)
|
||||||
{
|
{
|
||||||
Debug.WriteLine("--- Controller.SendMessage ---");
|
Debug.WriteLine("--- Controller.SendMessage ---");
|
||||||
@@ -121,7 +130,12 @@ namespace PolyChat
|
|||||||
// save to logs
|
// save to logs
|
||||||
fileManager.saveChats(ip, json.ToString(), DateTime.Now);
|
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)
|
private void OnMessage(string ip, JToken[] data)
|
||||||
{
|
{
|
||||||
Debug.WriteLine("--- Controller.OnMessage ---");
|
Debug.WriteLine("--- Controller.OnMessage ---");
|
||||||
@@ -135,6 +149,12 @@ namespace PolyChat
|
|||||||
else Debug.WriteLine("Undefined: " + data);
|
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)
|
public void CloseChat(string IP, bool wasConnected = true, bool delete = false)
|
||||||
{
|
{
|
||||||
Debug.WriteLine($"Deleting connection with IP:{IP}");
|
Debug.WriteLine($"Deleting connection with IP:{IP}");
|
||||||
@@ -149,6 +169,17 @@ namespace PolyChat
|
|||||||
fileManager.deleteChat(IP);
|
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)
|
private void CloseChatUI(string IP, bool wasConnected = true, bool delete = false)
|
||||||
{
|
{
|
||||||
UIController.OnChatPartnerDeleted(IP);
|
UIController.OnChatPartnerDeleted(IP);
|
||||||
@@ -166,6 +197,10 @@ namespace PolyChat
|
|||||||
return Connections.ContainsKey(ip) && Connections[ip].IsConnected();
|
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()
|
public static string getIP()
|
||||||
{
|
{
|
||||||
IPHostEntry ipEntry = Dns.GetHostEntry(Dns.GetHostName());
|
IPHostEntry ipEntry = Dns.GetHostEntry(Dns.GetHostName());
|
||||||
@@ -180,11 +215,5 @@ namespace PolyChat
|
|||||||
return null;
|
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
|
class FileManager
|
||||||
{
|
{
|
||||||
// Controller
|
// 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>
|
/// <summary>
|
||||||
/// deletes chatlog of one speciffic user
|
/// deletes chatlog of one speciffic user
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -59,14 +67,13 @@ namespace PolyChat.Models
|
|||||||
if (File.Exists($"U:\\PolyChat\\Saves\\{ip}.txt"))
|
if (File.Exists($"U:\\PolyChat\\Saves\\{ip}.txt"))
|
||||||
{
|
{
|
||||||
String jsonArr = decrypt(File.ReadAllText($"U:\\PolyChat\\Saves\\{ip}.txt"));
|
String jsonArr = decrypt(File.ReadAllText($"U:\\PolyChat\\Saves\\{ip}.txt"));
|
||||||
UIController.OnIncomingConnection(ip);
|
controller.SendIncomingMessageUI(ip, jsonArr);
|
||||||
UIController.OnIncomingMessages(ip, jsonArr);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <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
|
/// in ui when chat is clicked connection gets established
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -97,22 +104,18 @@ namespace PolyChat.Models
|
|||||||
ip = ip.Substring(0, ip.Length - 4);
|
ip = ip.Substring(0, ip.Length - 4);
|
||||||
Debug.WriteLine($"-{ip}");
|
Debug.WriteLine($"-{ip}");
|
||||||
Debug.WriteLine(jsonArr);
|
Debug.WriteLine(jsonArr);
|
||||||
UIController.OnIncomingConnection(ip);
|
controller.SendIncomingMessageUI(ip, jsonArr);
|
||||||
UIController.OnIncomingMessages(ip, jsonArr);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Saves incoming chat message to
|
/// Saves incoming chat message to U:\PolyChat\Saves\ip.txt
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="ip"></param>
|
/// <param name="ip"></param>
|
||||||
/// <param name="json"></param>
|
/// <param name="json"></param>
|
||||||
public void saveChats(string ip, string json, DateTime timeStamp)
|
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(() =>
|
new Thread(() =>
|
||||||
{
|
{
|
||||||
//breaking if namechange
|
//breaking if namechange
|
||||||
@@ -120,39 +123,39 @@ namespace PolyChat.Models
|
|||||||
if (!obj["type"].ToString().Equals("username"))
|
if (!obj["type"].ToString().Equals("username"))
|
||||||
{
|
{
|
||||||
//adding timestamp
|
//adding timestamp
|
||||||
obj = JObject.Parse(json);
|
|
||||||
obj.Add(new JProperty("timestamp", timeStamp));
|
obj.Add(new JProperty("timestamp", timeStamp));
|
||||||
json = obj.ToString();
|
json = obj.ToString();
|
||||||
|
|
||||||
if (File.Exists($"U:\\PolyChat\\Saves\\{ip}.txt"))
|
if (File.Exists($"U:\\PolyChat\\Saves\\{ip}.txt"))
|
||||||
{
|
{
|
||||||
Debug.WriteLine("--File allready exists--");
|
Debug.WriteLine("--File allready exists--");
|
||||||
|
|
||||||
//check for integraty of file
|
//check for integraty of file
|
||||||
string output = decrypt(File.ReadAllText($"U:\\PolyChat\\Saves\\{ip}.txt"));
|
string output = decrypt(File.ReadAllText($"U:\\PolyChat\\Saves\\{ip}.txt"));
|
||||||
Debug.WriteLine($"---{output}---");
|
Debug.WriteLine($"---{output}---");
|
||||||
if (output.Substring(0, 1).Equals("[") && output.Substring(output.Length - 1, 1).Equals("]"))
|
if (output.Substring(0, 1).Equals("[") && output.Substring(output.Length - 1, 1).Equals("]"))
|
||||||
{
|
{
|
||||||
Debug.WriteLine("--adding new chatmessage--");
|
|
||||||
//structure intact
|
//structure intact
|
||||||
//save new chat
|
//save new chat
|
||||||
String saved = output.Substring(0, output.Length - 1);
|
Debug.WriteLine("--adding new chatmessage--");
|
||||||
output = saved + ", " + json + " ]";
|
output = output.Substring(0, output.Length - 1) + ", " + json + " ]"; //rip appart and put file back together
|
||||||
File.Delete($"U:\\PolyChat\\Saves\\{ip}.txt");
|
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
|
else
|
||||||
{
|
{
|
||||||
Debug.WriteLine("--Structure not intact--");
|
|
||||||
Debug.WriteLine("--redoing file--");
|
|
||||||
//structure not intact
|
//structure not intact
|
||||||
//redo file
|
//redo file
|
||||||
|
Debug.WriteLine("--Structure not intact--");
|
||||||
|
Debug.WriteLine("--redoing file--");
|
||||||
File.Delete($"U:\\PolyChat\\Saves\\{ip}.txt");
|
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
|
else
|
||||||
{
|
{
|
||||||
Debug.WriteLine("--Creating new File--");
|
|
||||||
//setup file
|
//setup file
|
||||||
|
Debug.WriteLine("--Creating new File--");
|
||||||
File.WriteAllText($"U:\\PolyChat\\Saves\\{ip}.txt", encrypt($"[ {json} ]"));
|
File.WriteAllText($"U:\\PolyChat\\Saves\\{ip}.txt", encrypt($"[ {json} ]"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -160,28 +163,35 @@ namespace PolyChat.Models
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//---------------------------------------------------------------------------------------------------
|
//===============================================================================================================================================
|
||||||
//security
|
// Encryption
|
||||||
//---------------------------------------------------------------------------------------------------
|
//===============================================================================================================================================
|
||||||
private void genKeys()
|
|
||||||
|
/// <summary>
|
||||||
|
/// generates keypair [public, privte] (100% secure, trust me)
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
private String[] genKeys()
|
||||||
{
|
{
|
||||||
|
return new String[] {"12345678", "12345678" };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// does exactly what it says. XD
|
/// does exactly what it says. XD
|
||||||
|
///
|
||||||
|
/// encrypts given string and returns unreadable string
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="toEncrypt"></param>
|
/// <param name="toEncrypt"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static String encrypt(String toEncrypt)
|
private String encrypt(String toEncrypt)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
string textToEncrypt = toEncrypt;
|
string textToEncrypt = toEncrypt;
|
||||||
string ToReturn = "";
|
string ToReturn = "";
|
||||||
string publickey = "santhosh";
|
string publickey = genKeys()[0];
|
||||||
string secretkey = "engineer";
|
string secretkey = genKeys()[1];
|
||||||
byte[] secretkeyByte = { };
|
byte[] secretkeyByte = { };
|
||||||
secretkeyByte = System.Text.Encoding.UTF8.GetBytes(secretkey);
|
secretkeyByte = System.Text.Encoding.UTF8.GetBytes(secretkey);
|
||||||
byte[] publickeybyte = { };
|
byte[] publickeybyte = { };
|
||||||
@@ -203,22 +213,23 @@ namespace PolyChat.Models
|
|||||||
{
|
{
|
||||||
throw new Exception(ex.Message, ex.InnerException);
|
throw new Exception(ex.Message, ex.InnerException);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// does exactly what it says. XD
|
/// does exactly what it says. XD
|
||||||
|
///
|
||||||
|
/// takes in unreadable string and returns infirmation
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="toEncrypt"></param>
|
/// <param name="toEncrypt"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static String decrypt(String toDecrypt)
|
private String decrypt(String toDecrypt)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
string textToDecrypt = toDecrypt;
|
string textToDecrypt = toDecrypt;
|
||||||
string ToReturn = "";
|
string ToReturn = "";
|
||||||
string publickey = "santhosh";
|
string publickey = genKeys()[0];
|
||||||
string privatekey = "engineer";
|
string privatekey = genKeys()[1];
|
||||||
byte[] privatekeyByte = { };
|
byte[] privatekeyByte = { };
|
||||||
privatekeyByte = System.Text.Encoding.UTF8.GetBytes(privatekey);
|
privatekeyByte = System.Text.Encoding.UTF8.GetBytes(privatekey);
|
||||||
byte[] publickeybyte = { };
|
byte[] publickeybyte = { };
|
||||||
@@ -244,10 +255,4 @@ namespace PolyChat.Models
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user