-
Notifications
You must be signed in to change notification settings - Fork 566
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
4.x - Custom DNS resolvers for Nima (#4876)
* Custom DNS resolving Signed-off-by: David Kral <david.k.kral@oracle.com>
- Loading branch information
Showing
20 changed files
with
806 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
nima/webclient/webclient/src/main/java/io/helidon/nima/webclient/ConnectionKey.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright (c) 2022 Oracle and/or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.helidon.nima.webclient; | ||
|
||
import io.helidon.nima.common.tls.Tls; | ||
import io.helidon.nima.webclient.spi.DnsResolver; | ||
|
||
/** | ||
* Connection key instance contains all needed connection related information. | ||
* | ||
* @param scheme uri address scheme | ||
* @param host uri address host | ||
* @param port uri address port | ||
* @param tls TLS to be used in connection | ||
* @param dnsResolver DNS resolver to be used | ||
* @param dnsAddressLookup DNS address lookup strategy | ||
*/ | ||
public record ConnectionKey(String scheme, | ||
String host, | ||
int port, | ||
Tls tls, | ||
DnsResolver dnsResolver, | ||
DnsAddressLookup dnsAddressLookup) { } |
97 changes: 97 additions & 0 deletions
97
...ient/webclient/src/main/java/io/helidon/nima/webclient/DefaultDnsAddressLookupFinder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* Copyright (c) 2022 Oracle and/or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.helidon.nima.webclient; | ||
|
||
import java.net.Inet6Address; | ||
import java.net.InetAddress; | ||
import java.net.NetworkInterface; | ||
import java.net.SocketException; | ||
import java.util.Enumeration; | ||
|
||
import io.helidon.common.LazyValue; | ||
|
||
/** | ||
* Heavily inspired by Netty. | ||
*/ | ||
final class DefaultDnsAddressLookupFinder { | ||
|
||
private static final System.Logger LOGGER = System.getLogger(DefaultDnsAddressLookupFinder.class.getName()); | ||
|
||
/** | ||
* {@code true} if IPv4 should be used even if the system supports both IPv4 and IPv6. | ||
*/ | ||
private static final LazyValue<Boolean> IPV4_PREFERRED = LazyValue.create(() -> { | ||
return Boolean.getBoolean("java.net.preferIPv4Stack"); | ||
}); | ||
|
||
/** | ||
* {@code true} if an IPv6 address should be preferred when a host has both an IPv4 address and an IPv6 address. | ||
*/ | ||
private static final LazyValue<Boolean> IPV6_PREFERRED = LazyValue.create(() -> { | ||
return Boolean.getBoolean("java.net.preferIPv6Addresses"); | ||
}); | ||
|
||
private static final LazyValue<DnsAddressLookup> DEFAULT_IP_VERSION = LazyValue.create(() -> { | ||
if (IPV4_PREFERRED.get() || !anyInterfaceSupportsIpV6()) { | ||
return DnsAddressLookup.IPV4; | ||
} else { | ||
if (IPV6_PREFERRED.get()) { | ||
return DnsAddressLookup.IPV6_PREFERRED; | ||
} else { | ||
return DnsAddressLookup.IPV4_PREFERRED; | ||
} | ||
} | ||
}); | ||
|
||
private DefaultDnsAddressLookupFinder() { | ||
throw new IllegalStateException("This class should not be instantiated"); | ||
} | ||
|
||
static DnsAddressLookup defaultDnsAddressLookup() { | ||
return DEFAULT_IP_VERSION.get(); | ||
} | ||
|
||
/** | ||
* Returns {@code true} if any {@link NetworkInterface} supports {@code IPv6}, {@code false} otherwise. | ||
*/ | ||
private static boolean anyInterfaceSupportsIpV6() { | ||
try { | ||
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); | ||
while (interfaces.hasMoreElements()) { | ||
NetworkInterface networkInterface = interfaces.nextElement(); | ||
Enumeration<InetAddress> addresses = networkInterface.getInetAddresses(); | ||
while (addresses.hasMoreElements()) { | ||
InetAddress inetAddress = addresses.nextElement(); | ||
if (inetAddress instanceof Inet6Address | ||
&& !inetAddress.isAnyLocalAddress() | ||
&& !inetAddress.isLoopbackAddress() | ||
&& !inetAddress.isLinkLocalAddress()) { | ||
return true; | ||
} | ||
} | ||
} | ||
} catch (SocketException ignore) { | ||
if (LOGGER.isLoggable(System.Logger.Level.INFO)) { | ||
LOGGER.log(System.Logger.Level.INFO, | ||
"Unable to detect if any interface supports IPv6, assuming IPv4-only", | ||
ignore); | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
nima/webclient/webclient/src/main/java/io/helidon/nima/webclient/DefaultDnsResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright (c) 2022 Oracle and/or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.helidon.nima.webclient; | ||
|
||
import java.net.InetAddress; | ||
import java.net.UnknownHostException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import io.helidon.nima.webclient.spi.DnsResolver; | ||
|
||
final class DefaultDnsResolver implements DnsResolver { | ||
|
||
private final Map<String, InetAddress> hostnameAddresses = new HashMap<>(); | ||
|
||
@Override | ||
public InetAddress resolveAddress(String hostname, DnsAddressLookup dnsAddressLookup) { | ||
return hostnameAddresses.computeIfAbsent(hostname, host -> { | ||
try { | ||
InetAddress[] processed = dnsAddressLookup.filter(InetAddress.getAllByName(hostname)); | ||
if (processed.length == 0) { | ||
throw new RuntimeUnknownHostException("No IP version " + dnsAddressLookup.name() + " found for host " + host); | ||
} | ||
return processed[0]; | ||
} catch (UnknownHostException e) { | ||
throw new RuntimeUnknownHostException(e); | ||
} | ||
}); | ||
} | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
...bclient/webclient/src/main/java/io/helidon/nima/webclient/DefaultDnsResolverProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright (c) 2022 Oracle and/or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.helidon.nima.webclient; | ||
|
||
import io.helidon.common.Weight; | ||
import io.helidon.common.Weighted; | ||
import io.helidon.nima.webclient.spi.DnsResolver; | ||
import io.helidon.nima.webclient.spi.DnsResolverProvider; | ||
|
||
/** | ||
* Provider of the {@link DefaultDnsResolver} instance. | ||
*/ | ||
@Weight(Weighted.DEFAULT_WEIGHT) | ||
public class DefaultDnsResolverProvider implements DnsResolverProvider { | ||
|
||
/** | ||
* Create new instance of the {@link DefaultDnsResolverProvider}. | ||
* This should be used only for purposes of SPI. | ||
*/ | ||
public DefaultDnsResolverProvider() { | ||
} | ||
|
||
@Override | ||
public String resolverName() { | ||
return "default"; | ||
} | ||
|
||
@Override | ||
public DnsResolver createDnsResolver() { | ||
return new DefaultDnsResolver(); | ||
} | ||
} |
Oops, something went wrong.