private void ManuallyConnectCommandExecuted(object sender, ExecutedRoutedEventArgs e) { Dialogs.ManuallyConnectDialog _dialog = new Dialogs.ManuallyConnectDialog(); _dialog.ShowInTaskbar = false; _dialog.Owner = this; if (_dialog.ShowDialog() == false) { return; } else { string _address = _dialog.IP; string _encodedPassword = _dialog.password; if (_encodedPassword != null) { byte[] hash = ((System.Security.Cryptography.HashAlgorithm)System.Security.Cryptography.CryptoConfig.CreateFromName("MD5")).ComputeHash(new UTF8Encoding().GetBytes(_encodedPassword)); _encodedPassword = System.BitConverter.ToString(hash).Replace("-", string.Empty).ToLower(); } Thread _thread = new Thread(() => ConnectToPeerByIP(_address, _encodedPassword)); _thread.Name = _address + " handler"; _thread.IsBackground = true; _thread.Start(); } }
private void AcceptGreetAndProcess(bool serverOrClient, Socket socket) { // Checks greeting message and processes if correct, // Extracts the nick and machine_name if true // serverOrClient is true for server and false in case of client //server recieves hello server, client receives hello client string _prefix; int _size; if (serverOrClient) { _prefix = "Hello server, this is "; } else { _prefix = "Hello client, this is "; } string _message; if (!Network.NetworkCommunicationManagers.ReceiveIntOverSocket(socket, out _size)) { Network.NetworkCommunicationManagers.Disconnect(socket); return; } if (!Network.NetworkCommunicationManagers.ReceiveStringOverSocket(socket, out _message, _size)) { Network.NetworkCommunicationManagers.Disconnect(socket); return; } //didn't start with the given greeting if (_message.Substring(0, _prefix.Length) != _prefix) { Network.NetworkCommunicationManagers.Disconnect(socket); return; } //no nickname sent _message = _message.Remove(0, _prefix.Length); if (_message.Length == 0) { Network.NetworkCommunicationManagers.Disconnect(socket); return; } string _clientSocketRemoteEndPointString; string _socketRemoteEndPointString = socket.RemoteEndPoint.ToString(); //Check if already connected to the client foreach (ConnectedPeerDataContainer _client in connectedPeersList) { try { _clientSocketRemoteEndPointString = _client.socket.RemoteEndPoint.ToString(); if (_clientSocketRemoteEndPointString.Remove(_clientSocketRemoteEndPointString.LastIndexOf(':')) == _socketRemoteEndPointString.Remove(_socketRemoteEndPointString.LastIndexOf(':'))) { WriteToLogbox("Already Connected to Client " + _socketRemoteEndPointString.Remove(_socketRemoteEndPointString.LastIndexOf(':'))); MessageBox.Show("Already Connected to Client"); Network.NetworkCommunicationManagers.Disconnect(socket); return; } } catch (ObjectDisposedException) { } } ConnectedPeerDataContainer obj = new ConnectedPeerDataContainer(); int _indexOfDelimiter = _message.IndexOf(':'); if (_indexOfDelimiter == -1) { Network.NetworkCommunicationManagers.Disconnect(socket); return; } obj.nick = _message.Substring(0, _indexOfDelimiter); string _remainingMessage = _message.Substring(_indexOfDelimiter + 1); int _temp; if (serverOrClient) { if (password != null) { _indexOfDelimiter = _remainingMessage.IndexOf(':'); if (_indexOfDelimiter == -1) { Network.NetworkCommunicationManagers.SendIntOverSocket(socket, (int)PrimaryCommands.Error); Network.NetworkCommunicationManagers.SendIntOverSocket(socket, (int)ErrorCommands.PasswordRequired); Network.NetworkCommunicationManagers.ReceiveIntOverSocket(socket, out _temp); Network.NetworkCommunicationManagers.Disconnect(socket); return; } string _providedPassword = _remainingMessage.Substring(_indexOfDelimiter + 1); _remainingMessage = _remainingMessage.Substring(0, _indexOfDelimiter); if (_providedPassword != password) { Network.NetworkCommunicationManagers.SendIntOverSocket(socket, (int)PrimaryCommands.Error); Network.NetworkCommunicationManagers.SendIntOverSocket(socket, (int)ErrorCommands.PasswordIncorrect); Network.NetworkCommunicationManagers.ReceiveIntOverSocket(socket, out _temp); Network.NetworkCommunicationManagers.Disconnect(socket); return; } } } obj.encodedMachineName = _remainingMessage; obj.socket = socket; Dispatcher.Invoke(DispatcherPriority.Normal, (Action)(() => { AddNewTab(obj.nick, _socketRemoteEndPointString.Remove(_socketRemoteEndPointString.LastIndexOf(':'))); })); Dispatcher.Invoke(DispatcherPriority.Normal, (Action)(() => { WriteToTab(_socketRemoteEndPointString.Remove(_socketRemoteEndPointString.LastIndexOf(':')), "Connected", obj.nick, 0); })); if (serverOrClient) { if (!Network.NetworkCommunicationManagers.SendIntOverSocket(socket, (int)PrimaryCommands.OK)) { Network.NetworkCommunicationManagers.Disconnect(socket); return; } ; if (!Network.NetworkCommunicationManagers.ReceiveIntOverSocket(socket, out _temp)) { Network.NetworkCommunicationManagers.Disconnect(socket); return; } ; } else { if (!Network.NetworkCommunicationManagers.ReceiveIntOverSocket(socket, out _temp)) { Network.NetworkCommunicationManagers.Disconnect(socket); return; } ; if (!Network.NetworkCommunicationManagers.SendIntOverSocket(socket, (int)PrimaryCommands.OK)) { Network.NetworkCommunicationManagers.Disconnect(socket); return; } ; } _clientSocketRemoteEndPointString = socket.RemoteEndPoint.ToString(); string _ip = _clientSocketRemoteEndPointString.Remove(_clientSocketRemoteEndPointString.LastIndexOf(':')); string _nick = obj.nick; try { switch ((PrimaryCommands)_temp) { case PrimaryCommands.OK: obj.key = Encryption.PerformAssymetricKeyExchangeUsingECDiffieHellmanOnSocket(socket); break; case PrimaryCommands.Error: Network.NetworkCommunicationManagers.ReceiveIntOverSocket(socket, out _temp); Network.NetworkCommunicationManagers.SendIntOverSocket(socket, (int)PrimaryCommands.OK); switch ((ErrorCommands)_temp) { case ErrorCommands.PasswordRequired: WriteToLogbox("Password Required for " + _ip); WriteToLogbox("Disconnected- " + _ip); Dispatcher.Invoke(DispatcherPriority.Normal, (Action)(() => { WriteToTab(_ip, "Password Required", nick, 0); })); Dispatcher.Invoke(DispatcherPriority.Normal, (Action)(() => { WriteToTab(_ip, "Disconnected", nick, 0); })); try { socket.Shutdown(SocketShutdown.Both); socket.Close(); } catch (Exception) { } Dispatcher.Invoke(DispatcherPriority.Normal, (Action)(() => { Dialogs.ManuallyConnectDialog _dialog; while (true) { _dialog = new Dialogs.ManuallyConnectDialog(_ip, _nick, "Password Required"); _dialog.ShowInTaskbar = false; _dialog.Owner = this; if (_dialog.ShowDialog() == false) { return; } else { string _address = _dialog.IP; if (_dialog.password == null) { continue; } byte[] hash = ((System.Security.Cryptography.HashAlgorithm)System.Security.Cryptography.CryptoConfig.CreateFromName("MD5")).ComputeHash(new UTF8Encoding().GetBytes(_dialog.password)); string _encodedPassword = BitConverter.ToString(hash).Replace("-", string.Empty).ToLower(); Thread _thread = new Thread(() => ConnectToPeerByIP(_address, _encodedPassword)); _thread.Name = _address + " handler"; _thread.IsBackground = true; _thread.Start(); break; } } })); return; case ErrorCommands.PasswordIncorrect: WriteToLogbox("Incorrect password provided for " + _ip); WriteToLogbox("Disconnected- " + _ip); Dispatcher.Invoke(DispatcherPriority.Normal, (Action)(() => { WriteToTab(_ip, "Incorrect Password", nick, 0); })); Dispatcher.Invoke(DispatcherPriority.Normal, (Action)(() => { WriteToTab(_ip, "Disconnected", nick, 0); })); try { socket.Shutdown(SocketShutdown.Both); socket.Close(); } catch (Exception) { } Dispatcher.Invoke(DispatcherPriority.Normal, (Action)(() => { Dialogs.ManuallyConnectDialog _dialog; while (true) { _dialog = new Dialogs.ManuallyConnectDialog(_ip, _nick, "Incorrect Password"); _dialog.ShowInTaskbar = false; _dialog.Owner = this; if (_dialog.ShowDialog() == false) { return; } else { if (_dialog.password == null) { continue; } string _address = _dialog.IP; byte[] hash = ((System.Security.Cryptography.HashAlgorithm)System.Security.Cryptography.CryptoConfig.CreateFromName("MD5")).ComputeHash(new UTF8Encoding().GetBytes(_dialog.password)); string _encodedPassword = BitConverter.ToString(hash).Replace("-", string.Empty).ToLower(); Thread _thread = new Thread(() => ConnectToPeerByIP(_address, _encodedPassword)); _thread.Name = _address + " handler"; _thread.IsBackground = true; _thread.Start(); break; } } })); return; } return; default: _message = "Invalid MessageCode Received, The other client is most probably running a newer version of the application with a new Feature.. !!"; Dispatcher.Invoke(DispatcherPriority.Normal, (Action)(() => { WriteToTab(_ip, _message, _nick, 0); })); WriteToLogbox("Invalid MessageCode- " + _nick + " (" + _clientSocketRemoteEndPointString + ") : " + _temp); break; } } catch (Exception) { } if (obj.key == null) { Network.NetworkCommunicationManagers.Disconnect(socket); return; } connectedPeersList.Add(obj); ProcessClient(obj); }