From cd1a7dc2eda42c51e990e3cce528449af30def94 Mon Sep 17 00:00:00 2001 From: David Kocher Date: Tue, 6 Jun 2023 14:55:49 +0200 Subject: [PATCH] Return all resolved addresses. --- .../java/ch/cyberduck/core/ResolverTest.java | 18 +++++++++--------- .../main/java/ch/cyberduck/core/Resolver.java | 17 +++++++++++------ .../cyberduck/core/http/CustomDnsResolver.java | 3 ++- .../core/aws/CustomClientConfiguration.java | 2 +- .../core/spectra/SpectraBulkService.java | 4 ++-- 5 files changed, 25 insertions(+), 19 deletions(-) diff --git a/core/dylib/src/test/java/ch/cyberduck/core/ResolverTest.java b/core/dylib/src/test/java/ch/cyberduck/core/ResolverTest.java index 6adfa21a4f9..0355afb7e59 100644 --- a/core/dylib/src/test/java/ch/cyberduck/core/ResolverTest.java +++ b/core/dylib/src/test/java/ch/cyberduck/core/ResolverTest.java @@ -17,7 +17,7 @@ public class ResolverTest { @Test public void testResolve() throws Exception { - assertEquals("52.31.8.231", new Resolver().resolve("cyberduck.io", new DisabledCancelCallback()).getHostAddress()); + assertEquals("52.31.8.231", new Resolver().resolve("cyberduck.io", new DisabledCancelCallback())[0].getHostAddress()); } @Test @@ -30,7 +30,7 @@ public void testFailure() throws Exception { catch(ResolveFailedException e) { // } - assertNotNull(resolver.resolve("cyberduck.io", new DisabledCancelCallback()).getHostAddress()); + assertNotNull(resolver.resolve("cyberduck.io", new DisabledCancelCallback())[0].getHostAddress()); } @Test @@ -48,29 +48,29 @@ public void verify() throws ConnectionCanceledException { catch(ResolveCanceledException e) { // } - assertNotNull(resolver.resolve("cyberduck.io", new DisabledCancelCallback()).getHostAddress()); + assertNotNull(resolver.resolve("cyberduck.io", new DisabledCancelCallback())[0].getHostAddress()); } @Test public void testResolveIPv6Localhost() throws Exception { - assertEquals("localhost", new Resolver().resolve("::1", new DisabledCancelCallback()).getHostName()); - assertEquals("0:0:0:0:0:0:0:1", new Resolver().resolve("::1", new DisabledCancelCallback()).getHostAddress()); + assertEquals("localhost", new Resolver().resolve("::1", new DisabledCancelCallback())[0].getHostName()); + assertEquals("0:0:0:0:0:0:0:1", new Resolver().resolve("::1", new DisabledCancelCallback())[0].getHostAddress()); } @Test @Ignore public void testResolveLinkLocalZoneIndexInterfaceName() throws Exception { - assertEquals("andaman.local", new Resolver().resolve("andaman.local", new DisabledCancelCallback()).getHostName()); - assertEquals("fe80:0:0:0:c62c:3ff:fe0b:8670%en0", new Resolver().resolve("fe80::c62c:3ff:fe0b:8670%en0", new DisabledCancelCallback()).getHostAddress()); + assertEquals("andaman.local", new Resolver().resolve("andaman.local", new DisabledCancelCallback())[0].getHostName()); + assertEquals("fe80:0:0:0:c62c:3ff:fe0b:8670%en0", new Resolver().resolve("fe80::c62c:3ff:fe0b:8670%en0", new DisabledCancelCallback())[0].getHostAddress()); } @Test public void testResolvePublicDNSIPv6Only() throws Exception { - assertEquals("2001:470:a085:999:0:0:0:21", new Resolver().resolve("ftp6.netbsd.org", new DisabledCancelCallback()).getHostAddress()); + assertEquals("2001:470:a085:999:0:0:0:21", new Resolver().resolve("ftp6.netbsd.org", new DisabledCancelCallback())[0].getHostAddress()); } @Test public void testResolvePublicDNSIPv6ForHybrid() throws Exception { - assertEquals("2600:3c02:0:0:f03c:91ff:fe89:e8b1", new Resolver(true).resolve("intronetworks.cs.luc.edu", new DisabledCancelCallback()).getHostAddress()); + assertEquals("2600:3c02:0:0:f03c:91ff:fe89:e8b1", new Resolver(true).resolve("intronetworks.cs.luc.edu", new DisabledCancelCallback())[0].getHostAddress()); } } diff --git a/core/src/main/java/ch/cyberduck/core/Resolver.java b/core/src/main/java/ch/cyberduck/core/Resolver.java index f3accb30958..86c773ab869 100644 --- a/core/src/main/java/ch/cyberduck/core/Resolver.java +++ b/core/src/main/java/ch/cyberduck/core/Resolver.java @@ -34,9 +34,11 @@ import java.text.MessageFormat; import java.time.Duration; import java.util.Arrays; +import java.util.Set; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ThreadFactory; import java.util.concurrent.atomic.AtomicReference; +import java.util.stream.Collectors; import com.google.common.util.concurrent.Uninterruptibles; @@ -63,21 +65,24 @@ public Resolver(final boolean preferIPv6) { * @throws ResolveFailedException If the hostname cannot be resolved * @throws ResolveCanceledException If the lookup has been interrupted */ - public InetAddress resolve(final String hostname, final CancelCallback callback) throws ResolveFailedException, ResolveCanceledException { + public InetAddress[] resolve(final String hostname, final CancelCallback callback) throws ResolveFailedException, ResolveCanceledException { final CountDownLatch signal = new CountDownLatch(1); - final AtomicReference resolved = new AtomicReference<>(); + final AtomicReference> resolved = new AtomicReference<>(); final AtomicReference failure = new AtomicReference<>(); final Thread resolver = threadFactory.newThread(new Runnable() { @Override public void run() { try { final InetAddress[] allByName = InetAddress.getAllByName(hostname); - Arrays.stream(allByName).findFirst().ifPresent(resolved::set); + resolved.set(Arrays.stream(allByName).collect(Collectors.toSet())); if(preferIPv6) { - Arrays.stream(allByName).filter(inetAddress -> inetAddress instanceof Inet6Address).findFirst().ifPresent(resolved::set); + final Set filtered = Arrays.stream(allByName).filter(inetAddress -> inetAddress instanceof Inet6Address).collect(Collectors.toSet()); + if(!filtered.isEmpty()) { + resolved.set(filtered); + } } if(log.isInfoEnabled()) { - log.info(String.format("Resolved %s to %s", hostname, resolved.get().getHostAddress())); + log.info(String.format("Resolved %s to %s", hostname, Arrays.toString(resolved.get().toArray()))); } } catch(UnknownHostException e) { @@ -114,7 +119,7 @@ public void run() { throw new ResolveFailedException( MessageFormat.format(LocaleFactory.localizedString("DNS lookup for {0} failed", "Error"), hostname), failure.get()); } - return resolved.get(); + return resolved.get().toArray(new InetAddress[resolved.get().size()]); } @Override diff --git a/core/src/main/java/ch/cyberduck/core/http/CustomDnsResolver.java b/core/src/main/java/ch/cyberduck/core/http/CustomDnsResolver.java index 1e47843b7f5..e9407967cd3 100644 --- a/core/src/main/java/ch/cyberduck/core/http/CustomDnsResolver.java +++ b/core/src/main/java/ch/cyberduck/core/http/CustomDnsResolver.java @@ -19,6 +19,7 @@ import ch.cyberduck.core.Resolver; import ch.cyberduck.core.exception.ResolveCanceledException; import ch.cyberduck.core.exception.ResolveFailedException; + import org.apache.http.conn.DnsResolver; import java.net.InetAddress; @@ -31,7 +32,7 @@ public class CustomDnsResolver implements DnsResolver { @Override public InetAddress[] resolve(String host) throws UnknownHostException { try { - return new InetAddress[]{resolver.resolve(host, new DisabledCancelCallback())}; + return resolver.resolve(host, new DisabledCancelCallback()); } catch(ResolveFailedException | ResolveCanceledException e) { throw new UnknownHostException(e.getDetail(false)); diff --git a/s3/src/main/java/ch/cyberduck/core/aws/CustomClientConfiguration.java b/s3/src/main/java/ch/cyberduck/core/aws/CustomClientConfiguration.java index 3faa568e418..36c32138a73 100644 --- a/s3/src/main/java/ch/cyberduck/core/aws/CustomClientConfiguration.java +++ b/s3/src/main/java/ch/cyberduck/core/aws/CustomClientConfiguration.java @@ -54,7 +54,7 @@ public CustomClientConfiguration(final Host host, final ThreadLocalHostnameDeleg @Override public InetAddress[] resolve(final String host) throws UnknownHostException { try { - return new InetAddress[]{new Resolver().resolve(host, new DisabledCancelCallback())}; + return new Resolver().resolve(host, new DisabledCancelCallback()); } catch(ResolveFailedException | ResolveCanceledException e) { throw new UnknownHostException(e.getDetail(false)); diff --git a/spectra/src/main/java/ch/cyberduck/core/spectra/SpectraBulkService.java b/spectra/src/main/java/ch/cyberduck/core/spectra/SpectraBulkService.java index d436a037b76..03f0fc8fd67 100644 --- a/spectra/src/main/java/ch/cyberduck/core/spectra/SpectraBulkService.java +++ b/spectra/src/main/java/ch/cyberduck/core/spectra/SpectraBulkService.java @@ -303,8 +303,8 @@ private List query(final Path file, final TransferStatus status, if(StringUtils.equals(node.getEndPoint(), host.getHostname())) { break; } - if(StringUtils.equals(node.getEndPoint(), new Resolver().resolve(host.getHostname(), - new DisabledCancelCallback()).getHostAddress())) { + if(StringUtils.equals(node.getEndPoint(), new Resolver().resolve( + host.getHostname(), new DisabledCancelCallback())[0].getHostAddress())) { break; } log.warn(String.format("Redirect to %s for file %s", node.getEndPoint(), file));