@@ -42,7 +42,9 @@ internal sealed class DnsResolver : Resolver
4242 // To prevent excessive re-resolution, we enforce a rate limit on DNS resolution requests.
4343 private static readonly TimeSpan MinimumDnsResolutionRate = TimeSpan . FromSeconds ( 15 ) ;
4444
45- private readonly Uri _address ;
45+ private readonly Uri _originalAddress ;
46+ private readonly string _dnsAddress ;
47+ private readonly int _port ;
4648 private readonly TimeSpan _refreshInterval ;
4749 private readonly ILogger _logger ;
4850
@@ -56,11 +58,18 @@ internal sealed class DnsResolver : Resolver
5658 /// Initializes a new instance of the <see cref="DnsResolver"/> class with the specified target <see cref="Uri"/>.
5759 /// </summary>
5860 /// <param name="address">The target <see cref="Uri"/>.</param>
61+ /// <param name="defaultPort">The default port.</param>
5962 /// <param name="loggerFactory">The logger factory.</param>
6063 /// <param name="refreshInterval">An interval for automatically refreshing the DNS hostname.</param>
61- public DnsResolver ( Uri address , ILoggerFactory loggerFactory , TimeSpan refreshInterval ) : base ( loggerFactory )
64+ public DnsResolver ( Uri address , int defaultPort , ILoggerFactory loggerFactory , TimeSpan refreshInterval ) : base ( loggerFactory )
6265 {
63- _address = address ;
66+ _originalAddress = address ;
67+
68+ // DNS address has the format: dns:[//authority/]host[:port]
69+ // Because the host is specified in the path, the port needs to be parsed manually
70+ var addressParsed = new Uri ( "temp://" + address . AbsolutePath . TrimStart ( '/' ) ) ;
71+ _dnsAddress = addressParsed . Host ;
72+ _port = addressParsed . Port == - 1 ? defaultPort : addressParsed . Port ;
6473 _refreshInterval = refreshInterval ;
6574 _logger = loggerFactory . CreateLogger < DnsResolver > ( ) ;
6675 }
@@ -92,28 +101,25 @@ protected override async Task ResolveAsync(CancellationToken cancellationToken)
92101
93102 _lastResolveStart = SystemClock . UtcNow ;
94103
95- var dnsAddress = _address . AbsolutePath . TrimStart ( '/' ) ;
96-
97- if ( string . IsNullOrEmpty ( dnsAddress ) )
104+ if ( string . IsNullOrEmpty ( _dnsAddress ) )
98105 {
99- throw new InvalidOperationException ( $ "Resolver address '{ _address } ' doesn't have a path .") ;
106+ throw new InvalidOperationException ( $ "Resolver address '{ _originalAddress } ' is not valid. Please use dns:/// for DNS provider .") ;
100107 }
101108
102- DnsResolverLog . StartingDnsQuery ( _logger , _address ) ;
103- var addresses = await Dns . GetHostAddressesAsync ( dnsAddress ) . ConfigureAwait ( false ) ;
109+ DnsResolverLog . StartingDnsQuery ( _logger , _dnsAddress ) ;
110+ var addresses = await Dns . GetHostAddressesAsync ( _dnsAddress ) . ConfigureAwait ( false ) ;
104111
105- DnsResolverLog . ReceivedDnsResults ( _logger , addresses . Length , _address , addresses ) ;
112+ DnsResolverLog . ReceivedDnsResults ( _logger , addresses . Length , _dnsAddress , addresses ) ;
106113
107- var resolvedPort = _address . Port == - 1 ? 80 : _address . Port ;
108- var endpoints = addresses . Select ( a => new BalancerAddress ( a . ToString ( ) , resolvedPort ) ) . ToArray ( ) ;
114+ var endpoints = addresses . Select ( a => new BalancerAddress ( a . ToString ( ) , _port ) ) . ToArray ( ) ;
109115 var resolverResult = ResolverResult . ForResult ( endpoints ) ;
110116 Listener ( resolverResult ) ;
111117 }
112118 catch ( Exception ex )
113119 {
114- var message = $ "Error getting DNS hosts for address '{ _address } '.";
120+ var message = $ "Error getting DNS hosts for address '{ _dnsAddress } '.";
115121
116- DnsResolverLog . ErrorQueryingDns ( _logger , _address , ex ) ;
122+ DnsResolverLog . ErrorQueryingDns ( _logger , _dnsAddress , ex ) ;
117123 Listener ( ResolverResult . ForFailure ( GrpcProtocolHelpers . CreateStatusFromException ( message , ex , StatusCode . Unavailable ) ) ) ;
118124 }
119125 }
@@ -144,14 +150,14 @@ internal static class DnsResolverLog
144150 private static readonly Action < ILogger , TimeSpan , TimeSpan , Exception ? > _startingRateLimitDelay =
145151 LoggerMessage . Define < TimeSpan , TimeSpan > ( LogLevel . Debug , new EventId ( 1 , "StartingRateLimitDelay" ) , "Starting rate limit delay of {DelayDuration}. DNS resolution rate limit is once every {RateLimitDuration}." ) ;
146152
147- private static readonly Action < ILogger , Uri , Exception ? > _startingDnsQuery =
148- LoggerMessage . Define < Uri > ( LogLevel . Trace , new EventId ( 2 , "StartingDnsQuery" ) , "Starting DNS query to get hosts from '{DnsAddress}'." ) ;
153+ private static readonly Action < ILogger , string , Exception ? > _startingDnsQuery =
154+ LoggerMessage . Define < string > ( LogLevel . Trace , new EventId ( 2 , "StartingDnsQuery" ) , "Starting DNS query to get hosts from '{DnsAddress}'." ) ;
149155
150- private static readonly Action < ILogger , int , Uri , string , Exception ? > _receivedDnsResults =
151- LoggerMessage . Define < int , Uri , string > ( LogLevel . Debug , new EventId ( 3 , "ReceivedDnsResults" ) , "Received {ResultCount} DNS results from '{DnsAddress}'. Results: {DnsResults}" ) ;
156+ private static readonly Action < ILogger , int , string , string , Exception ? > _receivedDnsResults =
157+ LoggerMessage . Define < int , string , string > ( LogLevel . Debug , new EventId ( 3 , "ReceivedDnsResults" ) , "Received {ResultCount} DNS results from '{DnsAddress}'. Results: {DnsResults}" ) ;
152158
153- private static readonly Action < ILogger , Uri , Exception ? > _errorQueryingDns =
154- LoggerMessage . Define < Uri > ( LogLevel . Error , new EventId ( 4 , "ErrorQueryingDns" ) , "Error querying DNS hosts for '{DnsAddress}'." ) ;
159+ private static readonly Action < ILogger , string , Exception ? > _errorQueryingDns =
160+ LoggerMessage . Define < string > ( LogLevel . Error , new EventId ( 4 , "ErrorQueryingDns" ) , "Error querying DNS hosts for '{DnsAddress}'." ) ;
155161
156162 private static readonly Action < ILogger , Exception ? > _errorFromRefreshInterval =
157163 LoggerMessage . Define ( LogLevel . Error , new EventId ( 5 , "ErrorFromRefreshIntervalTimer" ) , "Error from refresh interval timer." ) ;
@@ -161,20 +167,20 @@ public static void StartingRateLimitDelay(ILogger logger, TimeSpan delayDuration
161167 _startingRateLimitDelay ( logger , delayDuration , rateLimitDuration , null ) ;
162168 }
163169
164- public static void StartingDnsQuery ( ILogger logger , Uri dnsAddress )
170+ public static void StartingDnsQuery ( ILogger logger , string dnsAddress )
165171 {
166172 _startingDnsQuery ( logger , dnsAddress , null ) ;
167173 }
168174
169- public static void ReceivedDnsResults ( ILogger logger , int resultCount , Uri dnsAddress , IList < IPAddress > dnsResults )
175+ public static void ReceivedDnsResults ( ILogger logger , int resultCount , string dnsAddress , IList < IPAddress > dnsResults )
170176 {
171177 if ( logger . IsEnabled ( LogLevel . Debug ) )
172178 {
173179 _receivedDnsResults ( logger , resultCount , dnsAddress , string . Join ( ", " , dnsResults ) , null ) ;
174180 }
175181 }
176182
177- public static void ErrorQueryingDns ( ILogger logger , Uri dnsAddress , Exception ex )
183+ public static void ErrorQueryingDns ( ILogger logger , string dnsAddress , Exception ex )
178184 {
179185 _errorQueryingDns ( logger , dnsAddress , ex ) ;
180186 }
@@ -211,7 +217,7 @@ public DnsResolverFactory(TimeSpan refreshInterval)
211217 /// <inheritdoc />
212218 public override Resolver Create ( ResolverOptions options )
213219 {
214- return new DnsResolver ( options . Address , options . LoggerFactory , _refreshInterval ) ;
220+ return new DnsResolver ( options . Address , options . DefaultPort , options . LoggerFactory , _refreshInterval ) ;
215221 }
216222 }
217223}
0 commit comments