Пример #1
0
        private void DeviceListView_EncryptionButton_Click(object sender, RoutedEventArgs e)
        {
            PhysicalAddressSerializable clickMac = PhysicalAddressSerializable.Parse(((Button)sender).Tag.ToString());
            Device device = Settings.SettingsData.GetDevice(clickMac);

            if (device.EncryptionEnabled)
            {
                Settings.ConfirmationDialog dialog = new Settings.ConfirmationDialog("Remove encryption?", $"Do you want to remove encryption for communication with '{device.Name}'?", "Yes", "Cancel");
                dialog.Owner = Application.Current.MainWindow;
                if (dialog.ShowDialog() ?? false)
                {
                    Settings.SettingsData.SetDeviceEncryptionPassword(clickMac, null);
                }
            }
            else
            {
                EncryptionPasswordDialog dialog = new EncryptionPasswordDialog(device.Name);
                dialog.Owner = Application.Current.MainWindow;

                if (dialog.ShowDialog() ?? false)
                {
                    Settings.SettingsData.SetDeviceEncryptionPassword(clickMac, dialog.Password);
                }
            }
        }
Пример #2
0
        private async Task EstablishConnection(string ipPort)
        {
            await Task.Run(() =>
            {
                if (ipPort == null || ipPort == "")
                {
                    throw new FailedConnectingException("Wrong IP.");
                }

                string[] ipParts = ipPort.Split(':');
                if (ipParts.Length != 2)
                {
                    throw new FailedConnectingException("Wrong IP.");
                }

                IPAddress deviceIP;
                if (!IPAddress.TryParse(ipParts[0], out deviceIP))
                {
                    throw new FailedConnectingException("Unknown IP.");
                }

                Device remoteDevice = Settings.SettingsData.GetDevice(deviceIP);
                if (remoteDevice == null)
                {
                    throw new FailedConnectingException("Unknown IP.");
                }


                CommunicationResult res = null;
                byte[] buffer           = null;


                // Check busy
                if (IsBusy)
                {
                    TrySend(ipPort, new byte[] { (byte)TTInstruction.Connection_RefuseBusy });
                    throw new FailedConnectingException($"Connection from {remoteDevice.Name} refused because busy.");
                }


                // Check permission
                if (remoteDevice.ReceiveMode == DeviceReceiveMode.Deny)
                {
                    TrySend(ipPort, new byte[] { (byte)TTInstruction.Connection_RefuseDeny });
                    throw new FailedConnectingException($"Connection from {remoteDevice.Name} refused because it's not allowed.");
                }
                if (remoteDevice.ReceiveMode == DeviceReceiveMode.AskEachTime)
                {
                    timer          = new Timer(Settings.SettingsData.MaxPermissionAskWaitMs);
                    timer.Elapsed += Events_TimerPermissionAskTimeout;
                    timer.Start();

                    bool allowed = false;
                    Application.Current.Dispatcher.Invoke(() =>
                    {
                        confirmationDialog       = new Settings.ConfirmationDialog("Accept connection?", $"Do you want to accept a connection from '{remoteDevice.Name}'?", "Yes", "No");
                        confirmationDialog.Owner = Application.Current.MainWindow;
                        allowed = confirmationDialog.ShowDialog() ?? false;
                    });
                    timer.Stop();
                    timer.Dispose();
                    if (!allowed)
                    {
                        timer.Stop();
                        timer.Dispose();


                        TrySend(ipPort, new byte[] { (byte)TTInstruction.Connection_RefuseDeny });
                        throw new FailedConnectingException($"Connection from {remoteDevice.Name} refused because you refused it.");
                    }
                }


                // Encryption
                DataEncryptor encryptor = null;
                if (remoteDevice.EncryptionEnabled)
                {
                    encryptor = new DataEncryptor(remoteDevice.EncryptionPassword);


                    // Ask for password
                    TrySend(ipPort, new byte[] { (byte)TTInstruction.Connection_AskPass });


                    // Receive password
                    res = TryRead(ipPort, 1 + TTNet.TimePasswordLength, false);
                    if (res.Instruction != TTInstruction.Connection_SendPass)
                    {
                        if (res.Instruction == TTInstruction.Connection_RefuseDeny)
                        {
                            throw new FailedConnectingException($"Connection from {remoteDevice.Name} refused refused because it did not have a password.");
                        }
                    }


                    // Check password
                    byte[] receivedTimePassword = encryptor.AESDecryptBytes(res.Data);
                    if (!TTNet.CheckTimePasswordValid(receivedTimePassword, remoteDevice, maxPingMs))
                    {
                        TrySend(ipPort, new byte[] { (byte)TTInstruction.Connection_RefusePass });
                        throw new FailedConnectingException($"Connection from {remoteDevice.Name} refused because it did not have the correct password.");
                    }
                    TrySend(ipPort, new byte[] { (byte)TTInstruction.Connection_AcceptPass });


                    // Send password
                    byte[] timePassword = TTNet.GenerateTimePasswordBytes();
                    buffer = new byte[1] {
                        (byte)TTInstruction.Connection_SendPass
                    }.ToList().Concat(encryptor.AESEncryptBytes(timePassword)).ToArray();
                    TrySend(ipPort, buffer);
                }
                else
                {
                    // Send connection accept
                    TrySend(ipPort, new byte[] { (byte)TTInstruction.Connection_Accept });
                }


                // Wait if client accepted connection
                res = TryRead(ipPort, 1, false);
                if (res.Instruction != TTInstruction.Connection_Accept)
                {
                    if (res.Instruction == TTInstruction.Connection_RefusePass)
                    {
                        throw new FailedConnectingException($"Connection from {remoteDevice.Name} refused because it requires a password and none is set.");
                    }
                }


                // Check busy again
                if (IsBusy)
                {
                    TrySend(ipPort, new byte[] { (byte)TTInstruction.Connection_RefuseBusy });
                    throw new FailedConnectingException($"Refused connection from {remoteDevice.Name} because busy.");
                }


                clientDevice    = remoteDevice;
                clientIpPort    = ipPort;
                clientEncryptor = encryptor;
                OnRecordableEvent($"Established { (clientDevice.EncryptionEnabled ? "secure " : "")}connection with {remoteDevice.Name}.", Console.ConsoleMessageType.Common);
            });
        }