Skip to content

Commit 038e868

Browse files
BornToBeRootclaude
andcommitted
Refactor: NavigationManager with typed application requests
Replaces the EventSystem (Models), which passed a single string, with NavigationManager in the main project: - ApplicationRequest (target or profile) with derived requests for options: DNSLookupRequest (DNS server, query type), PortScannerRequest (ports), RDAPRequest (query type). - Host views implement IApplicationRequestHandler; MainWindow passes the request to the shown view instead of a switch per application. - Profile actions of the tools are public (e.g. ConnectProfile(profile)) so a request can run a profile. - Web Console accepts a URL (connect dialog), like the other remote tools. - All callers migrated (IP Scanner, Traceroute, Network Interface, RDAP, open settings). Traceroute > RDAP > AS number queries with type ASN. - The application search is cleared before a request, so a filtered application list doesn't make the request fail. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 8fe100a commit 038e868

39 files changed

Lines changed: 391 additions & 222 deletions

‎Source/NETworkManager.Models/EventSystem/EventSystem.cs‎

Lines changed: 0 additions & 22 deletions
This file was deleted.

‎Source/NETworkManager.Models/EventSystem/EventSystemRedirectArgs.cs‎

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using DnsClient;
2+
using NETworkManager.Models;
3+
using NETworkManager.Models.RDAP;
4+
using NETworkManager.Profiles;
5+
6+
namespace NETworkManager;
7+
8+
/// <summary>
9+
/// Request to open an application (tool), optionally with a target or a profile, e.g. from the run command or from
10+
/// another tool ("Open in ..."). Applications that support more options have a derived request.
11+
/// </summary>
12+
/// <param name="Application">Application to open.</param>
13+
public record ApplicationRequest(ApplicationName Application)
14+
{
15+
/// <summary>
16+
/// Target of the application, e.g. host(s), IP range, query or URL. If null (and no <see cref="Profile" /> is set),
17+
/// the application is only opened.
18+
/// </summary>
19+
public string Target { get; init; }
20+
21+
/// <summary>
22+
/// Profile to run in the application with its settings. <see cref="Target" /> is ignored if set.
23+
/// </summary>
24+
public ProfileInfo Profile { get; init; }
25+
}
26+
27+
/// <summary>
28+
/// Request to open the DNS Lookup.
29+
/// </summary>
30+
public sealed record DNSLookupRequest() : ApplicationRequest(ApplicationName.DNSLookup)
31+
{
32+
/// <summary>
33+
/// DNS server(s) to use (e.g. "1.1.1.1"). If null, the DNS server selected in the application is used.
34+
/// </summary>
35+
public string DNSServer { get; init; }
36+
37+
/// <summary>
38+
/// Query type. If null, the query type selected in the application is used.
39+
/// </summary>
40+
public QueryType? QueryType { get; init; }
41+
}
42+
43+
/// <summary>
44+
/// Request to open the Port Scanner.
45+
/// </summary>
46+
public sealed record PortScannerRequest() : ApplicationRequest(ApplicationName.PortScanner)
47+
{
48+
/// <summary>
49+
/// Ports to scan (e.g. "22;80;443" or "1-1024"). If null, the ports of the application are used.
50+
/// </summary>
51+
public string Ports { get; init; }
52+
}
53+
54+
/// <summary>
55+
/// Request to open RDAP.
56+
/// </summary>
57+
public sealed record RDAPRequest() : ApplicationRequest(ApplicationName.RDAP)
58+
{
59+
/// <summary>
60+
/// Query type. If null, the type is detected from the query (<see cref="RDAPQueryType.Auto" />).
61+
/// </summary>
62+
public RDAPQueryType? QueryType { get; init; }
63+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace NETworkManager;
2+
3+
/// <summary>
4+
/// Application view that can handle an <see cref="ApplicationRequest" /> with a target or a profile (e.g. open a new
5+
/// tab and start a scan). Views without this interface are only opened.
6+
/// </summary>
7+
public interface IApplicationRequestHandler
8+
{
9+
/// <summary>
10+
/// Handles the request. Called after the application view is shown.
11+
/// </summary>
12+
/// <param name="request">Request with a target or a profile.</param>
13+
void Open(ApplicationRequest request);
14+
}

‎Source/NETworkManager/MainWindow.xaml.cs‎

Lines changed: 21 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using NETworkManager.Localization;
77
using NETworkManager.Localization.Resources;
88
using NETworkManager.Models;
9-
using NETworkManager.Models.EventSystem;
109
using NETworkManager.Models.Network;
1110
using NETworkManager.Profiles;
1211
using NETworkManager.Settings;
@@ -438,11 +437,11 @@ public MainWindow()
438437
// Set window title
439438
Title = $"NETworkManager {AssemblyManager.Current.Version}";
440439

441-
// Register event system...
440+
// Register navigation requests (e.g. from the run command or other applications)
442441
SettingsManager.Current.PropertyChanged += SettingsManager_PropertyChanged;
443442

444-
EventSystem.OnRedirectDataToApplicationEvent += EventSystem_RedirectDataToApplicationEvent;
445-
EventSystem.OnRedirectToSettingsEvent += EventSystem_RedirectToSettingsEvent;
443+
NavigationManager.ApplicationRequested += NavigationManager_ApplicationRequested;
444+
NavigationManager.SettingsRequested += NavigationManager_SettingsRequested;
446445

447446
_isLoading = false;
448447
}
@@ -1048,92 +1047,38 @@ private void ClearSearchOnApplicationListMinimize()
10481047
ListViewApplication.ScrollIntoView(SelectedApplication);
10491048
}
10501049

1051-
private async void EventSystem_RedirectDataToApplicationEvent(object sender, EventArgs e)
1050+
private async void NavigationManager_ApplicationRequested(object sender, ApplicationRequest request)
10521051
{
1053-
if (e is not EventSystemRedirectArgs data)
1054-
return;
1052+
// Clear the search, otherwise the application may be filtered out
1053+
ApplicationSearch = string.Empty;
10551054

1056-
// Try to find the application
1057-
var application = Applications.Cast<ApplicationInfo>().FirstOrDefault(x => x.Name == data.Application);
1055+
// Try to find the application (hidden applications can't be opened)
1056+
var application = Applications.Cast<ApplicationInfo>().FirstOrDefault(x => x.Name == request.Application);
10581057

10591058
// Show error message if the application was not found
10601059
if (application == null)
10611060
{
10621061
await DialogHelper.ShowMessageAsync(Application.Current.MainWindow, Strings.Error,
10631062
string.Format(Strings.CouldNotFindApplicationXXXMessage,
1064-
$"{data.Application}"), ChildWindowIcon.Error);
1063+
$"{request.Application}"), ChildWindowIcon.Error);
10651064

10661065
return;
10671066
}
10681067

1068+
if (SettingsViewIsOpen)
1069+
CloseSettings();
1070+
10691071
// Change application view
10701072
SelectedApplication = application;
10711073

1072-
if (string.IsNullOrEmpty(data.Args))
1074+
if (string.IsNullOrEmpty(request.Target) && request.Profile == null)
10731075
return;
10741076

1075-
// Crate a new tab / perform action
1076-
switch (data.Application)
1077-
{
1078-
case ApplicationName.Dashboard:
1079-
case ApplicationName.NetworkInterface:
1080-
case ApplicationName.WiFi:
1081-
break;
1082-
case ApplicationName.IPScanner:
1083-
_ipScannerHostView.AddTab(data.Args);
1084-
break;
1085-
case ApplicationName.PortScanner:
1086-
_portScannerHostView.AddTab(data.Args);
1087-
break;
1088-
case ApplicationName.PingMonitor:
1089-
_pingMonitorHostView.AddHost(data.Args);
1090-
break;
1091-
case ApplicationName.Traceroute:
1092-
_tracerouteHostView.AddTab(data.Args);
1093-
break;
1094-
case ApplicationName.DNSLookup:
1095-
_dnsLookupHostView.AddTab(data.Args);
1096-
break;
1097-
case ApplicationName.RemoteDesktop:
1098-
_remoteDesktopHostView.AddTab(data.Args);
1099-
break;
1100-
case ApplicationName.PowerShell:
1101-
_powerShellHostView.AddTab(data.Args);
1102-
break;
1103-
case ApplicationName.PuTTY:
1104-
_puttyHostView.AddTab(data.Args);
1105-
break;
1106-
case ApplicationName.TigerVNC:
1107-
_tigerVNCHostView.AddTab(data.Args);
1108-
break;
1109-
case ApplicationName.WebConsole:
1110-
break;
1111-
case ApplicationName.SNMP:
1112-
_snmpHostView.AddTab(data.Args);
1113-
break;
1114-
case ApplicationName.RDAP:
1115-
_rdapHostView.AddTab(data.Args);
1116-
break;
1117-
case ApplicationName.Whois:
1118-
_whoisHostView.AddTab(data.Args);
1119-
break;
1120-
case ApplicationName.SNTPLookup:
1121-
case ApplicationName.HostsFileEditor:
1122-
case ApplicationName.Firewall:
1123-
case ApplicationName.DiscoveryProtocol:
1124-
case ApplicationName.WakeOnLAN:
1125-
case ApplicationName.IPGeolocation:
1126-
case ApplicationName.SubnetCalculator:
1127-
case ApplicationName.BitCalculator:
1128-
case ApplicationName.Lookup:
1129-
case ApplicationName.Connections:
1130-
case ApplicationName.Listeners:
1131-
case ApplicationName.NeighborTable:
1132-
break;
1133-
default:
1134-
Log.Error($"Cannot redirect data to unknown application: {data.Application}");
1135-
break;
1136-
}
1077+
// Create a new tab / perform the action in the application
1078+
if (ContentControlApplication.Content is IApplicationRequestHandler handler)
1079+
handler.Open(request);
1080+
else
1081+
Log.Warn($"Application {request.Application} cannot handle a request with a target or profile.");
11371082
}
11381083

11391084
/// <summary>
@@ -1271,15 +1216,12 @@ private void RunCommandDo()
12711216
{
12721217
// Redirect to application
12731218
case RunCommandType.Application:
1274-
if (SettingsViewIsOpen)
1275-
CloseSettings();
1276-
12771219
var applicationName = (ApplicationName)Enum.Parse(typeof(ApplicationName), SelectedRunCommand.Name);
1278-
EventSystem.RedirectToApplication(applicationName, string.Empty);
1220+
NavigationManager.Open(new ApplicationRequest(applicationName));
12791221
break;
12801222
// Redirect to settings
12811223
case RunCommandType.Setting:
1282-
EventSystem.RedirectToSettings();
1224+
NavigationManager.OpenSettings();
12831225
break;
12841226
}
12851227

@@ -1348,7 +1290,7 @@ private void OpenSettings()
13481290
SettingsViewIsOpen = true;
13491291
}
13501292

1351-
private void EventSystem_RedirectToSettingsEvent(object sender, EventArgs e)
1293+
private void NavigationManager_SettingsRequested(object sender, EventArgs e)
13521294
{
13531295
OpenSettings();
13541296
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
3+
namespace NETworkManager;
4+
5+
/// <summary>
6+
/// Navigation between the applications (tools) and the settings. The main window handles the requests.
7+
/// </summary>
8+
public static class NavigationManager
9+
{
10+
/// <summary>
11+
/// Occurs when an application should be opened, see <see cref="Open" />.
12+
/// </summary>
13+
public static event EventHandler<ApplicationRequest> ApplicationRequested;
14+
15+
/// <summary>
16+
/// Occurs when the settings should be opened, see <see cref="OpenSettings" />.
17+
/// </summary>
18+
public static event EventHandler SettingsRequested;
19+
20+
/// <summary>
21+
/// Opens an application and passes the request (target, profile, options) to it.
22+
/// </summary>
23+
/// <param name="request">Request with the application to open.</param>
24+
public static void Open(ApplicationRequest request)
25+
{
26+
ApplicationRequested?.Invoke(null, request);
27+
}
28+
29+
/// <summary>
30+
/// Opens the settings of the current application.
31+
/// </summary>
32+
public static void OpenSettings()
33+
{
34+
SettingsRequested?.Invoke(null, EventArgs.Empty);
35+
}
36+
}

‎Source/NETworkManager/ViewModels/DNSLookupHostViewModel.cs‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using DnsClient;
12
using Dragablz;
23
using NETworkManager.Controls;
34
using NETworkManager.Localization.Resources;
@@ -128,7 +129,7 @@ private bool LookupProfile_CanExecute(object obj)
128129
/// </summary>
129130
private void LookupProfileAction()
130131
{
131-
AddTab(SelectedProfile.DNSLookup_Host);
132+
AddTab(SelectedProfile);
132133
}
133134

134135
/// <summary>
@@ -152,15 +153,26 @@ private static void CloseItemAction(ItemActionCallbackArgs<TabablzControl> args)
152153
/// Adds a new tab for the specified host.
153154
/// </summary>
154155
/// <param name="host">The host to lookup.</param>
155-
public void AddTab(string host = null)
156+
/// <param name="dnsServer">DNS server(s) to use. If null, the selected DNS server is used.</param>
157+
/// <param name="queryType">Query type. If null, the selected query type is used.</param>
158+
public void AddTab(string host = null, string dnsServer = null, QueryType? queryType = null)
156159
{
157160
var tabId = Guid.NewGuid();
158161

159-
TabItems.Add(new DragablzTabItem(host ?? Strings.NewTab, new DNSLookupView(tabId, host),
162+
TabItems.Add(new DragablzTabItem(host ?? Strings.NewTab, new DNSLookupView(tabId, host, dnsServer, queryType),
160163
tabId));
161164

162165
SelectedTabIndex = TabItems.Count - 1;
163166
}
164167

168+
/// <summary>
169+
/// Adds a new tab for the host of the specified profile.
170+
/// </summary>
171+
/// <param name="profile">The profile to lookup.</param>
172+
public void AddTab(ProfileInfo profile)
173+
{
174+
AddTab(profile.DNSLookup_Host);
175+
}
176+
165177
#endregion
166178
}

‎Source/NETworkManager/ViewModels/DNSLookupViewModel.cs‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,9 @@ private set
248248
/// </summary>
249249
/// <param name="tabId">The ID of the tab.</param>
250250
/// <param name="host">The host to lookup.</param>
251-
public DNSLookupViewModel(Guid tabId, string host)
251+
/// <param name="dnsServer">DNS server(s) to use (not saved as default). If null, the last selected one is used.</param>
252+
/// <param name="queryType">Query type (not saved as default). If null, the last selected one is used.</param>
253+
public DNSLookupViewModel(Guid tabId, string host, string dnsServer = null, QueryType? queryType = null)
252254
{
253255
_isLoading = true;
254256

@@ -276,6 +278,13 @@ public DNSLookupViewModel(Guid tabId, string host)
276278

277279
LoadSettings();
278280

281+
// Options of a request (e.g. run command), which are not saved as default.
282+
if (!string.IsNullOrEmpty(dnsServer))
283+
DNSServer = dnsServer;
284+
285+
if (queryType != null && QueryTypes.Contains(queryType.Value))
286+
QueryType = queryType.Value;
287+
279288
_isLoading = false;
280289
}
281290

‎Source/NETworkManager/ViewModels/IPScannerHostViewModel.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public void AddTab(string hostOrIPRange = null)
165165
/// Adds a new tab for the specified profile.
166166
/// </summary>
167167
/// <param name="profile">The profile to scan.</param>
168-
private void AddTab(ProfileInfo profile)
168+
public void AddTab(ProfileInfo profile)
169169
{
170170
AddTab(profile.IPScanner_HostOrIPRange);
171171
}

0 commit comments

Comments
 (0)