1212using System . Collections . Generic ;
1313using System . Globalization ;
1414using System . Linq ;
15- using System . Net ;
1615using System . Windows . Controls ;
1716
1817namespace NETworkManager ;
@@ -35,8 +34,6 @@ public static class RunCommandManager
3534 [ "entity" ] = RDAPQueryType . Entity
3635 } ;
3736
38- private static readonly IReadOnlyList < RunCommandDefinition > Commands = CreateCommands ( ) ;
39-
4037 /// <summary>
4138 /// Gets the commands of the visible applications and the settings.
4239 /// </summary>
@@ -46,7 +43,17 @@ public static IReadOnlyList<RunCommandDefinition> GetCommands()
4643 var visibleApplications = SettingsManager . Current . General_ApplicationList . Where ( x => x . IsVisible )
4744 . Select ( x => x . Name ) . ToHashSet ( ) ;
4845
49- return [ .. Commands . Where ( x => x . Type == RunCommandType . Setting || visibleApplications . Contains ( x . Application ) ) ] ;
46+ // Created on each call, since the syntax is translated.
47+ return [ .. CreateCommands ( ) . Where ( x => x . Type == RunCommandType . Setting || visibleApplications . Contains ( x . Application ) ) ] ;
48+ }
49+
50+ /// <summary>
51+ /// Gets the names of the DNS servers configured in the DNS lookup (e.g. "Cloudflare").
52+ /// </summary>
53+ /// <returns>Names of the DNS servers.</returns>
54+ public static IEnumerable < string > GetDNSServers ( )
55+ {
56+ return SettingsManager . Current . DNSLookup_DNSServers . Select ( x => x . Name ) ;
5057 }
5158
5259 /// <summary>
@@ -217,13 +224,11 @@ private static ApplicationRequest CreateRequest(RunCommandParseResult result)
217224 ApplicationName . DNSLookup => new DNSLookupRequest
218225 {
219226 Target = result . Target ,
220- DNSServer = result . Argument ,
221- QueryType = result . Type != null
222- ? Enum . Parse < QueryType > ( result . Type , true )
223- // Only IP addresses: reverse lookup
224- : result . Target . Split ( ';' ) . All ( x => IPAddress . TryParse ( x , out _ ) )
225- ? QueryType . PTR
226- : null
227+ // A configured DNS server is matched by its name (case-insensitive), otherwise it's a custom server.
228+ DNSServer = GetDNSServers ( ) . FirstOrDefault ( x =>
229+ string . Equals ( x , result . Argument , StringComparison . OrdinalIgnoreCase ) ) ?? result . Argument ,
230+ // Without a type, the DNS lookup uses PTR for IP addresses and its selected type for hosts.
231+ QueryType = result . Type != null ? Enum . Parse < QueryType > ( result . Type , true ) : null
227232 } ,
228233 ApplicationName . PortScanner => new PortScannerRequest { Target = result . Target , Ports = result . Argument } ,
229234 ApplicationName . RDAP => new RDAPRequest { Target = result . Target , QueryType = GetRDAPQueryType ( result . Type ) } ,
@@ -242,24 +247,31 @@ private static RDAPQueryType GetRDAPQueryType(string type)
242247 /// </summary>
243248 private static List < RunCommandDefinition > CreateCommands ( )
244249 {
250+ // Syntax of the arguments, e.g. "<Hosts> [Ports]"
251+ static string Required ( string name ) => $ "<{ name } >";
252+ static string Optional ( string name ) => $ "[{ name } ]";
253+
245254 var commands = ApplicationManager . GetNames ( ) . Where ( x => x != ApplicationName . None ) . Select ( name => name switch
246255 {
247- ApplicationName . IPScanner => Create ( name , [ "scan" ] , "<range>" , true ) ,
248- ApplicationName . PortScanner => Create ( name , [ "portscan" ] , "<hosts> [ports]" , true ,
249- RunCommandArgument . Ports ) ,
250- ApplicationName . PingMonitor => Create ( name , [ "ping" ] , "<hosts>" , true ) ,
251- ApplicationName . Traceroute => Create ( name , [ "trace" , "tracert" ] , "<host>" , true ) ,
252- ApplicationName . DNSLookup => Create ( name , [ "dns" , "nslookup" ] , "<hosts> [server] [type]" , true ,
256+ ApplicationName . IPScanner => Create ( name , [ "scan" ] , Required ( Strings . IPRange ) , true ) ,
257+ ApplicationName . PortScanner => Create ( name , [ "portscan" ] ,
258+ $ "{ Required ( Strings . Hosts ) } { Optional ( Strings . Ports ) } ", true , RunCommandArgument . Ports ) ,
259+ ApplicationName . PingMonitor => Create ( name , [ "ping" ] , Required ( Strings . Hosts ) , true ) ,
260+ ApplicationName . Traceroute => Create ( name , [ "trace" , "tracert" ] , Required ( Strings . Host ) , true ) ,
261+ ApplicationName . DNSLookup => Create ( name , [ "dns" , "nslookup" ] ,
262+ $ "{ Required ( Strings . Hosts ) } { Optional ( Strings . DNSServer ) } { Optional ( Strings . Type ) } ", true ,
253263 RunCommandArgument . DNSServer ,
254264 [ .. DNSLookup . QueryTypes . Select ( x => x . ToString ( ) . ToLowerInvariant ( ) ) . Order ( ) ] ) ,
255- ApplicationName . RemoteDesktop => Create ( name , [ "rdp" , "mstsc" ] , "<host[:port]>" , true ) ,
256- ApplicationName . PowerShell => Create ( name , [ "ps" , "pwsh" ] , "<host>" , true ) ,
257- ApplicationName . PuTTY => Create ( name , [ "ssh" ] , "<host>" , true ) ,
258- ApplicationName . TigerVNC => Create ( name , [ "vnc" ] , "<host>" , true ) ,
259- ApplicationName . WebConsole => Create ( name , [ "web" ] , "<url>" , true ) ,
260- ApplicationName . SNMP => Create ( name , [ ] , "<host>" , true ) ,
261- ApplicationName . RDAP => Create ( name , [ ] , "<query> [type]" , true , types : [ .. RDAPTypes . Keys ] ) ,
262- ApplicationName . Whois => Create ( name , [ ] , "<domain>" , true ) ,
265+ ApplicationName . RemoteDesktop => Create ( name , [ "rdp" , "mstsc" ] ,
266+ $ "{ Required ( Strings . Host ) } [:{ Required ( Strings . Port ) } ]", true ) ,
267+ ApplicationName . PowerShell => Create ( name , [ "ps" , "pwsh" ] , Required ( Strings . Host ) , true ) ,
268+ ApplicationName . PuTTY => Create ( name , [ "ssh" ] , Required ( Strings . Host ) , true ) ,
269+ ApplicationName . TigerVNC => Create ( name , [ "vnc" ] , Required ( Strings . Host ) , true ) ,
270+ ApplicationName . WebConsole => Create ( name , [ "web" ] , Required ( Strings . URL ) , true ) ,
271+ ApplicationName . SNMP => Create ( name , [ ] , Required ( Strings . Host ) , true ) ,
272+ ApplicationName . RDAP => Create ( name , [ ] , $ "{ Required ( Strings . Query ) } { Optional ( Strings . Type ) } ", true ,
273+ types : [ .. RDAPTypes . Keys ] ) ,
274+ ApplicationName . Whois => Create ( name , [ ] , Required ( Strings . Domain ) , true ) ,
263275 _ => Create ( name , [ ] , string . Empty , false )
264276 } ) . ToList ( ) ;
265277
@@ -287,7 +299,7 @@ private static RunCommandDefinition Create(ApplicationName application, string[]
287299 SupportsProfile = supportsTarget ,
288300 Argument = argument ,
289301 Types = types ?? [ ] ,
290- Syntax = supportsTarget ? $ "{ syntax } | @<profile >" : string . Empty
302+ Syntax = supportsTarget ? $ "{ syntax } | @<{ Strings . Profile } >" : string . Empty
291303 } ;
292304 }
293305}
0 commit comments