@@ -81,77 +81,50 @@ public string Host
8181 /// <summary>
8282 /// Backing field for <see cref="DNSServer"/>.
8383 /// </summary>
84- private DNSServerConnectionInfoProfile _dnsServer = new ( ) ;
84+ private string _dnsServer ;
8585
8686 /// <summary>
8787 /// Gets or sets the selected DNS server.
88+ /// This can either be an ip/host:port or a profile name.
8889 /// </summary>
89- public DNSServerConnectionInfoProfile DNSServer
90+ public string DNSServer
9091 {
9192 get => _dnsServer ;
92- private set
93+ set
9394 {
9495 if ( _dnsServer == value )
9596 return ;
96-
97- _dnsServer = value ?? new DNSServerConnectionInfoProfile ( ) ;
9897
99- if ( ! _isLoading )
100- SettingsManager . Current . DNSLookup_SelectedDNSServer = _dnsServer ;
101-
102- OnPropertyChanged ( ) ;
103- }
104- }
98+ // Try finding matching dns server profile by name, otherwise set to null (de-select)
99+ SelectedDNSServer = SettingsManager . Current . DNSLookup_DNSServers
100+ . FirstOrDefault ( x => x . Name == value ) ;
105101
106- private DNSServerConnectionInfoProfile _selectedListProfile ;
107- public DNSServerConnectionInfoProfile SelectedListProfile
108- {
109- get => _selectedListProfile ;
110- set
111- {
112- if ( _selectedListProfile == value )
113- return ;
114-
115- if ( value != null )
116- {
117- DNSServer = value ;
118- DNSServerQuickInput = value . ToString ( ) ; // uses your override
119- }
102+ if ( ! _isLoading )
103+ SettingsManager . Current . DNSLookup_SelectedDNSServer_v2 = value ;
120104
121- _selectedListProfile = value ;
105+ _dnsServer = value ;
122106 OnPropertyChanged ( ) ;
123107 }
124108 }
125109
126- // Text box content
127- private string _dnsServerQuickInput = string . Empty ;
128- public string DNSServerQuickInput
110+ private DNSServerConnectionInfoProfile _selectedDNSServer ;
111+ public DNSServerConnectionInfoProfile SelectedDNSServer
129112 {
130- get => _dnsServerQuickInput ;
113+ get => _selectedDNSServer ;
131114 set
132115 {
133- if ( _dnsServerQuickInput == value )
116+ if ( _selectedDNSServer == value )
134117 return ;
135118
136- _dnsServerQuickInput = value ? . Trim ( ) ?? string . Empty ;
119+ _selectedDNSServer = value ;
137120 OnPropertyChanged ( ) ;
138-
139- // As soon as user types → deselect any list item
140- SelectedListProfile = null ;
141-
142- // Create custom profile from raw IP
143- if ( IPAddress . TryParse ( _dnsServerQuickInput , out IPAddress x ) )
144- {
145- // Temporarily switch to this custom profile
146- DNSServer = new DNSServerConnectionInfoProfile ( "CUSTOM" , [ new ServerConnectionInfo ( x . ToString ( ) , 53 ) ] ) ;
147- }
148121 }
149122 }
150123
151124 /// <summary>
152125 /// Backing field for <see cref="QueryTypes"/>.
153126 /// </summary>
154- private List < QueryType > _queryTypes = new ( ) ;
127+ private List < QueryType > _queryTypes = [ ] ;
155128
156129 /// <summary>
157130 /// Gets the list of available query types.
@@ -217,7 +190,7 @@ public bool IsRunning
217190 /// <summary>
218191 /// Backing field for <see cref="Results"/>.
219192 /// </summary>
220- private ObservableCollection < DNSLookupRecordInfo > _results = new ( ) ;
193+ private ObservableCollection < DNSLookupRecordInfo > _results = [ ] ;
221194
222195 /// <summary>
223196 /// Gets or sets the collection of lookup results.
@@ -351,13 +324,10 @@ public DNSLookupViewModel(IDialogCoordinator instance, Guid tabId, string host)
351324 ListSortDirection . Descending ) ) ;
352325 DNSServers . SortDescriptions . Add ( new SortDescription ( nameof ( DNSServerConnectionInfoProfile . Name ) ,
353326 ListSortDirection . Ascending ) ) ;
354- var initialDNSServer = DNSServers . SourceCollection . Cast < DNSServerConnectionInfoProfile > ( )
355- . FirstOrDefault ( x => x . Name == SettingsManager . Current . DNSLookup_SelectedDNSServer . Name ) ??
356- DNSServers . SourceCollection . Cast < DNSServerConnectionInfoProfile > ( ) . First ( ) ;
357-
358- DNSServer = initialDNSServer ;
359- SelectedListProfile = initialDNSServer ;
360- DNSServerQuickInput = initialDNSServer . ToString ( ) ;
327+
328+ DNSServer = string . IsNullOrEmpty ( SettingsManager . Current . DNSLookup_SelectedDNSServer_v2 )
329+ ? SettingsManager . Current . DNSLookup_DNSServers . FirstOrDefault ( ) ? . Name
330+ : SettingsManager . Current . DNSLookup_SelectedDNSServer_v2 ;
361331
362332 ResultsView = CollectionViewSource . GetDefaultView ( Results ) ;
363333 ResultsView . GroupDescriptions . Add ( new PropertyGroupDescription ( nameof ( DNSLookupRecordInfo . NameServerAsString ) ) ) ;
@@ -381,7 +351,7 @@ public void OnLoaded()
381351 return ;
382352
383353 if ( ! string . IsNullOrEmpty ( Host ) )
384- Query ( ) ;
354+ QueryAsync ( ) ;
385355
386356 _firstLoad = false ;
387357 }
@@ -441,7 +411,7 @@ private bool Query_CanExecute(object parameter)
441411 private void QueryAction ( )
442412 {
443413 if ( ! IsRunning )
444- Query ( ) ;
414+ QueryAsync ( ) ;
445415 }
446416
447417 /// <summary>
@@ -464,7 +434,7 @@ private void ExportAction()
464434 /// <summary>
465435 /// Performs the DNS query.
466436 /// </summary>
467- private void Query ( )
437+ private async Task QueryAsync ( )
468438 {
469439 IsStatusMessageDisplayed = false ;
470440 StatusMessage = string . Empty ;
@@ -496,9 +466,67 @@ private void Query()
496466 dnsSettings . CustomDNSSuffix = SettingsManager . Current . DNSLookup_CustomDNSSuffix ? . TrimStart ( '.' ) ;
497467 }
498468
499- var dnsLookup = DNSServer . UseWindowsDNSServer
500- ? new DNSLookup ( dnsSettings )
501- : new DNSLookup ( dnsSettings , DNSServer . Servers ) ;
469+
470+ DNSLookup dnsLookup ;
471+
472+ // Try find existing dns server profile
473+ var dnsServerProfile = SettingsManager . Current . DNSLookup_DNSServers
474+ . FirstOrDefault ( x => x . Name == DNSServer ) ;
475+
476+ // Use profile if found
477+ if ( dnsServerProfile != null )
478+ {
479+ dnsLookup = dnsServerProfile . UseWindowsDNSServer
480+ ? new DNSLookup ( dnsSettings )
481+ : new DNSLookup ( dnsSettings , dnsServerProfile . Servers ) ;
482+ }
483+ // Otherwise try to parse custom server string
484+ else
485+ {
486+ List < ServerConnectionInfo > customDNSServers = [ ] ;
487+
488+ foreach ( var customDNSServer in DNSServer . Split ( ';' ) . Select ( x => x . Trim ( ) ) )
489+ {
490+ var customDNSServerArgs = customDNSServer . Split ( ':' ) ;
491+
492+ var server = customDNSServerArgs [ 0 ] ;
493+ var port = customDNSServerArgs . Length == 2 && int . TryParse ( customDNSServerArgs [ 1 ] , out var p ) ? p : 53 ;
494+
495+ // Resolve hostname to IP address
496+ if ( ! IPAddress . TryParse ( server , out _ ) )
497+ {
498+ var dnsResult = await DNSClientHelper . ResolveAorAaaaAsync ( server ,
499+ SettingsManager . Current . Network_ResolveHostnamePreferIPv4 ) ;
500+
501+ if ( dnsResult . HasError )
502+ {
503+ var dnsErrorMessage = DNSClientHelper . FormatDNSClientResultError ( server , dnsResult ) ;
504+
505+ if ( ! string . IsNullOrEmpty ( StatusMessage ) )
506+ StatusMessage += Environment . NewLine ;
507+
508+ StatusMessage += $ "{ Strings . DNSServer } : { dnsErrorMessage } ";
509+ IsStatusMessageDisplayed = true ;
510+
511+ continue ; // Skip this server, try next one
512+ }
513+
514+ server = dnsResult . Value . ToString ( ) ;
515+ }
516+
517+ customDNSServers . Add ( new ServerConnectionInfo ( server , port , TransportProtocol . Udp ) ) ;
518+ }
519+
520+ // Check if we have any valid custom dns servers
521+ if ( customDNSServers . Count == 0 )
522+ {
523+ IsRunning = false ;
524+
525+ return ;
526+ }
527+
528+ dnsLookup = new DNSLookup ( dnsSettings , customDNSServers ) ;
529+ }
502530
503531 dnsLookup . RecordReceived += DNSLookup_RecordReceived ;
504532 dnsLookup . LookupError += DNSLookup_LookupError ;
0 commit comments