Action = new ConfigurationPropertyAction(this) { Name = $"{nameof(PublicKeyProperty)}{nameof(ConfigurationProperty.Action)}", Action = (conf, prop) => { WaitCursor.SetOverrideCursor(Cursors.Wait); string output = new WireGuardExe().ExecuteCommand(new GeneratePublicKeyCommand(conf.PrivateKeyProperty.Value), out int exitCode); WaitCursor.SetOverrideCursor(null); if (exitCode == 0) { prop.Value = output; } else { new UnhandledErrorWindow { DataContext = new UnhandledErrorWindowModel { Title = Resources.Error, Text = $"{string.Format(Resources.PublicKeyError, PrivateKeyProperty.Value)}{Environment.NewLine}{Environment.NewLine}{output}", Exception = new Exception(output) } }.ShowDialog(); } }, DependentProperty = PrivateKeyProperty, DependencySatisfiedFunc = prop => string.IsNullOrEmpty(prop.Value) == false },
public override void Configure() { var serverConfiguration = new ServerConfiguration().Load <ServerConfiguration>(Configuration.LoadFromFile(ServerDataPath)); string originalServerIp = serverConfiguration.AddressProperty.Value; ServerConfigurationEditorWindow serverConfigurationEditor = new ServerConfigurationEditorWindow { DataContext = serverConfiguration }; WaitCursor.SetOverrideCursor(Cursors.Wait); if (serverConfigurationEditor.ShowDialog() == true) { WaitCursor.SetOverrideCursor(Cursors.Wait); // Save to Data SaveData(serverConfiguration); // Save to WG SaveWG(serverConfiguration); // Update clients var clientConfigurationsPrerequisite = new ClientConfigurationsPrerequisite(); clientConfigurationsPrerequisite.Update(); // Update Internet Sharing to use new server IP only if // - the new value passes validation // - the new value is not already in the registry if (string.IsNullOrEmpty(serverConfiguration.AddressProperty.Validation?.Validate?.Invoke(serverConfiguration.AddressProperty)) && !GetScopeAddressRegistryValue().Equals(serverConfiguration.IpAddress)) { SetScopeAddressRegistryValue(serverConfiguration.IpAddress); // If Internet Sharing is already enabled, and we just changed the server's network range, we should disable and re-enable ICS var ics = new InternetSharingPrerequisite(); if (ics.Fulfilled) { ics.Configure(); ics.Resolve(); } } // Update the tunnel service, if everyone is happy if (Fulfilled && (clientConfigurationsPrerequisite.Fulfilled || !ClientConfigurationsPrerequisite.AnyClients) && new TunnelServicePrerequisite().Fulfilled) { // Sync conf to tunnel string output = new WireGuardExe().ExecuteCommand(new SyncConfigurationCommand(WireGuardServerInterfaceName, ServerWGPath), out int exitCode); if (exitCode != 0) { // Notify the user that there was an error syncing the server conf. WaitCursor.SetOverrideCursor(null); new UnhandledErrorWindow { DataContext = new UnhandledErrorWindowModel { Title = Resources.Error, Text = $"{Resources.ServerSyncError}{Environment.NewLine}{Environment.NewLine}{output}", Exception = new Exception(output) } }.ShowDialog(); } } WaitCursor.SetOverrideCursor(null); } Refresh(); }
public override void Configure() { ClientConfigurationList clientConfigurations = new ClientConfigurationList(); List <ClientConfiguration> clientConfigurationsFromFile = new List <ClientConfiguration>(); // Load the clients from the conf files into a temporary list foreach (string clientConfigurationFile in Directory.GetFiles(ClientDataDirectory, "*.conf")) { clientConfigurationsFromFile.Add(new ClientConfiguration(clientConfigurations).Load <ClientConfiguration>(Configuration.LoadFromFile(clientConfigurationFile))); } // Now add them to the ObservableCollection, after sorting the temporary list foreach (ClientConfiguration clientConfiguration in clientConfigurationsFromFile.OrderBy(c => c.IndexProperty.Value)) { clientConfigurations.List.Add(clientConfiguration); } ClientConfigurationEditorWindow clientConfigurationEditorWindow = new ClientConfigurationEditorWindow { DataContext = clientConfigurations }; WaitCursor.SetOverrideCursor(Cursors.Wait); if (clientConfigurationEditorWindow.ShowDialog() == true) { WaitCursor.SetOverrideCursor(Cursors.Wait); // Delete the existing files (can't rely on updating them since the name of the client may have changed) foreach (string clientConfigurationFile in Directory.GetFiles(ClientDataDirectory, "*.conf")) { File.Delete(clientConfigurationFile); } foreach (string clientConfigurationFile in Directory.GetFiles(ClientWGDirectory, "*.conf")) { File.Delete(clientConfigurationFile); } // Check for duplicate names HashSet <string> discoveredDuplicateNames = new HashSet <string>(); foreach (ClientConfiguration clientConfiguration in clientConfigurations.List) { int i = 1; string originalName = clientConfiguration.NameProperty.Value; while (clientConfigurations.List.Any(c => c != clientConfiguration && c.NameProperty.Value == clientConfiguration.NameProperty.Value)) { if (discoveredDuplicateNames.Contains(originalName) == false) { // This is a duplicate name, but we haven't discovered it yet, meaning it's the first of its kind. // We want to rename the SECOND one, so we'll skip this one. discoveredDuplicateNames.Add(originalName); break; } clientConfiguration.NameProperty.Value = $"{originalName} ({i++})"; } } // Save to Data foreach (ClientConfiguration clientConfiguration in clientConfigurations.List) { clientConfiguration.IndexProperty.Value = clientConfigurations.List.IndexOf(clientConfiguration).ToString(); SaveData(clientConfiguration); } // Save to WG foreach (ClientConfiguration clientConfiguration in clientConfigurations.List) { SaveWG(clientConfiguration); } // Update server var serverConfigurationPrerequisite = new ServerConfigurationPrerequisite(); serverConfigurationPrerequisite.Update(); // Update the tunnel service, if everyone is happy if ((Fulfilled || !AnyClients) && serverConfigurationPrerequisite.Fulfilled && new TunnelServicePrerequisite().Fulfilled) { string output = new WireGuardExe().ExecuteCommand(new SyncConfigurationCommand(ServerConfigurationPrerequisite.WireGuardServerInterfaceName, ServerConfigurationPrerequisite.ServerWGPath), out int exitCode); if (exitCode != 0) { // Notify the user that there was an error syncing the server conf. WaitCursor.SetOverrideCursor(null); new UnhandledErrorWindow { DataContext = new UnhandledErrorWindowModel { Title = Resources.Error, Text = $"{Resources.ServerSyncError}{Environment.NewLine}{Environment.NewLine}{output}", Exception = new Exception(output) } }.ShowDialog(); } } WaitCursor.SetOverrideCursor(null); } Refresh(); }