fix: make DNS changes reversible with a snapshot-based Undo#825
Merged
Conversation
DNS preset changes were only reversible via 'Reset to DHCP', which silently discarded any manually-configured static DNS the user had before. Applying a preset now snapshots the servers in effect first, and a new 'Undo' button on the DNS & Hosts tab restores that exact prior configuration (re-applying the previous static servers, or resetting to DHCP if that was the prior state). - DnsService.CaptureCurrentServersAsync reads and validates the current IPv4 servers; RestoreServersAsync re-applies them (empty snapshot => DHCP reset), validating every address before interpolating it into the Set script. - DnsHostsViewModel snapshots before SetDnsAsync, exposes RestorePreviousDns (admin-gated, Confirm-gated) and CanRestorePreviousDns for the button. - DnsHostsView: Undo button (SecondaryButton, AutomationProperties.Name), visible only after a change, disabled while applying. - Tests cover capture (parse/filter/DHCP-empty) and restore (re-apply/DHCP/ null-throw/invalid-address-throw) via the IPowerShellRunner seam. Closes the reversibility half of audit test finding #3 (the Confirm gate was already added in 1.20.0). README + CHANGELOG updated.
Comment on lines
+131
to
+135
| foreach (var server in servers) | ||
| { | ||
| if (!IPAddress.TryParse(server, out _)) | ||
| throw new ArgumentException($"Invalid DNS address in snapshot: '{server}'", nameof(servers)); | ||
| } |
| await RefreshDnsAsync(); | ||
| Log.Information("DNS restored to previous setting ({Label})", label); | ||
| } | ||
| catch (OperationCanceledException) { } |
Comment on lines
+250
to
+255
| catch (Exception ex) | ||
| { | ||
| Application.Current?.Dispatcher?.Invoke(() => | ||
| StatusMessage = $"Failed to restore DNS: {ex.Message}"); | ||
| Log.Error(ex, "Failed to restore previous DNS"); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Audit Round 4 / PR4 — closes the reversibility half of test finding #3. (The Confirm gate it also called for was already added in 1.20.0.)
DNS preset changes were only reversible via Reset to DHCP, which silently discarded any manually-configured static DNS the user had before. Now:
Changes
DnsService.CaptureCurrentServersAsync— reads + validates current IPv4 servers (filters blank/non-IP noise; empty => the adapter was on DHCP).DnsService.RestoreServersAsync— re-applies a snapshot (empty => DHCP reset), validating every address before interpolating it into theSet-DnsClientServerAddressscript.DnsHostsViewModel— snapshots beforeSetDnsAsync;RestorePreviousDnscommand is admin-gated andConfirm-gated;CanRestorePreviousDnsdrives the button.DnsHostsView— Undo button (SecondaryButton,AutomationProperties.Name, BoolToVis visibility), shown only after a change, disabled while applying.Reversibility (CODE QUALITY merge-blocker)
This makes the DNS mutation genuinely snapshot-reversible, satisfying the "every system modification must be reversible" contract.
Tests
DnsServiceTestscover capture (parse / non-IP filtering / DHCP-empty) and restore (re-apply exact servers / empty=>DHCP / null-throw / invalid-address-throw) — all via theIPowerShellRunnerseam, no live DNS touched. All literals synthetic/public (8.8.8.8,9.9.9.9).Verification
MainWindowViewModelbuilds the VM via the unchanged ctor; new service methods are additive.fix:— version bumped to 1.20.5, CHANGELOG + README updated in this PR.