Allow to config dns bind address in DnsNameResolver (#11061)
Motivation: The DnsResolver default start address listen to "0.0.0.0", which may be not what the user wants. Modification: Add localAddress as a param of DnsNameResolver and its builder Result: The DnsNameResolver's bind address can be configured.
This commit is contained in:
parent
0da12a6fc2
commit
06739ed890
@ -378,6 +378,35 @@ public class DnsNameResolver extends InetNameResolver {
|
|||||||
int ndots,
|
int ndots,
|
||||||
boolean decodeIdn,
|
boolean decodeIdn,
|
||||||
boolean completeOncePreferredResolved) {
|
boolean completeOncePreferredResolved) {
|
||||||
|
this(eventLoop, channelFactory, socketChannelFactory, resolveCache, cnameCache, authoritativeDnsServerCache,
|
||||||
|
null, dnsQueryLifecycleObserverFactory, queryTimeoutMillis, resolvedAddressTypes,
|
||||||
|
recursionDesired, maxQueriesPerResolve, traceEnabled, maxPayloadSize, optResourceEnabled,
|
||||||
|
hostsFileEntriesResolver, dnsServerAddressStreamProvider, searchDomains, ndots, decodeIdn,
|
||||||
|
completeOncePreferredResolved);
|
||||||
|
}
|
||||||
|
|
||||||
|
DnsNameResolver(
|
||||||
|
EventLoop eventLoop,
|
||||||
|
ChannelFactory<? extends DatagramChannel> channelFactory,
|
||||||
|
ChannelFactory<? extends SocketChannel> socketChannelFactory,
|
||||||
|
final DnsCache resolveCache,
|
||||||
|
final DnsCnameCache cnameCache,
|
||||||
|
final AuthoritativeDnsServerCache authoritativeDnsServerCache,
|
||||||
|
SocketAddress localAddress,
|
||||||
|
DnsQueryLifecycleObserverFactory dnsQueryLifecycleObserverFactory,
|
||||||
|
long queryTimeoutMillis,
|
||||||
|
ResolvedAddressTypes resolvedAddressTypes,
|
||||||
|
boolean recursionDesired,
|
||||||
|
int maxQueriesPerResolve,
|
||||||
|
boolean traceEnabled,
|
||||||
|
int maxPayloadSize,
|
||||||
|
boolean optResourceEnabled,
|
||||||
|
HostsFileEntriesResolver hostsFileEntriesResolver,
|
||||||
|
DnsServerAddressStreamProvider dnsServerAddressStreamProvider,
|
||||||
|
String[] searchDomains,
|
||||||
|
int ndots,
|
||||||
|
boolean decodeIdn,
|
||||||
|
boolean completeOncePreferredResolved) {
|
||||||
super(eventLoop);
|
super(eventLoop);
|
||||||
this.queryTimeoutMillis = queryTimeoutMillis > 0
|
this.queryTimeoutMillis = queryTimeoutMillis > 0
|
||||||
? queryTimeoutMillis
|
? queryTimeoutMillis
|
||||||
@ -444,7 +473,12 @@ public class DnsNameResolver extends InetNameResolver {
|
|||||||
});
|
});
|
||||||
|
|
||||||
channelFuture = responseHandler.channelActivePromise;
|
channelFuture = responseHandler.channelActivePromise;
|
||||||
ChannelFuture future = b.register();
|
final ChannelFuture future;
|
||||||
|
if (localAddress == null) {
|
||||||
|
future = b.register();
|
||||||
|
} else {
|
||||||
|
future = b.bind(localAddress);
|
||||||
|
}
|
||||||
Throwable cause = future.cause();
|
Throwable cause = future.cause();
|
||||||
if (cause != null) {
|
if (cause != null) {
|
||||||
if (cause instanceof RuntimeException) {
|
if (cause instanceof RuntimeException) {
|
||||||
|
@ -25,6 +25,7 @@ import io.netty.resolver.HostsFileEntriesResolver;
|
|||||||
import io.netty.resolver.ResolvedAddressTypes;
|
import io.netty.resolver.ResolvedAddressTypes;
|
||||||
import io.netty.util.concurrent.Future;
|
import io.netty.util.concurrent.Future;
|
||||||
|
|
||||||
|
import java.net.SocketAddress;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -42,6 +43,7 @@ public final class DnsNameResolverBuilder {
|
|||||||
private DnsCache resolveCache;
|
private DnsCache resolveCache;
|
||||||
private DnsCnameCache cnameCache;
|
private DnsCnameCache cnameCache;
|
||||||
private AuthoritativeDnsServerCache authoritativeDnsServerCache;
|
private AuthoritativeDnsServerCache authoritativeDnsServerCache;
|
||||||
|
private SocketAddress localAddress;
|
||||||
private Integer minTtl;
|
private Integer minTtl;
|
||||||
private Integer maxTtl;
|
private Integer maxTtl;
|
||||||
private Integer negativeTtl;
|
private Integer negativeTtl;
|
||||||
@ -201,6 +203,16 @@ public final class DnsNameResolverBuilder {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configure the address that will be used to bind too. If `null` the default will be used.
|
||||||
|
* @param localAddress the bind address
|
||||||
|
* @return {@code this}
|
||||||
|
*/
|
||||||
|
public DnsNameResolverBuilder localAddress(SocketAddress localAddress) {
|
||||||
|
this.localAddress = localAddress;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the minimum and maximum TTL of the cached DNS resource records (in seconds). If the TTL of the DNS
|
* Sets the minimum and maximum TTL of the cached DNS resource records (in seconds). If the TTL of the DNS
|
||||||
* resource record returned by the DNS server is less than the minimum TTL or greater than the maximum TTL,
|
* resource record returned by the DNS server is less than the minimum TTL or greater than the maximum TTL,
|
||||||
@ -465,6 +477,7 @@ public final class DnsNameResolverBuilder {
|
|||||||
resolveCache,
|
resolveCache,
|
||||||
cnameCache,
|
cnameCache,
|
||||||
authoritativeDnsServerCache,
|
authoritativeDnsServerCache,
|
||||||
|
localAddress,
|
||||||
dnsQueryLifecycleObserverFactory,
|
dnsQueryLifecycleObserverFactory,
|
||||||
queryTimeoutMillis,
|
queryTimeoutMillis,
|
||||||
resolvedAddressTypes,
|
resolvedAddressTypes,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user