From e1ea5d6ac99fc8b94488ab1ba9ee03d2fea92ee6 Mon Sep 17 00:00:00 2001 From: Jesse Glick Date: Fri, 18 Oct 2024 19:35:05 -0400 Subject: [PATCH] Tolerate IPv6 scoped IPs from `JnlpAgentEndpointResolver.getResolvedHttpProxyAddress` (#769) * Tolerate IPv6 scoped IPs from `JnlpAgentEndpointResolver.getResolvedHttpProxyAddress` * Spotless --- .../engine/JnlpAgentEndpointResolver.java | 11 +++-- .../engine/JnlpAgentEndpointResolverTest.java | 40 +++++++++++++++++++ 2 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 src/test/java/org/jenkinsci/remoting/engine/JnlpAgentEndpointResolverTest.java diff --git a/src/main/java/org/jenkinsci/remoting/engine/JnlpAgentEndpointResolver.java b/src/main/java/org/jenkinsci/remoting/engine/JnlpAgentEndpointResolver.java index 2c5be60a7..6d6b7e936 100644 --- a/src/main/java/org/jenkinsci/remoting/engine/JnlpAgentEndpointResolver.java +++ b/src/main/java/org/jenkinsci/remoting/engine/JnlpAgentEndpointResolver.java @@ -44,6 +44,7 @@ import java.net.SocketAddress; import java.net.SocketTimeoutException; import java.net.URI; +import java.net.URISyntaxException; import java.net.URL; import java.net.URLConnection; import java.nio.charset.StandardCharsets; @@ -486,9 +487,13 @@ public void waitForReady() throws InterruptedException { @CheckForNull static InetSocketAddress getResolvedHttpProxyAddress(@NonNull String host, int port) throws IOException { InetSocketAddress targetAddress = null; - Iterator proxies = ProxySelector.getDefault() - .select(URI.create(String.format("http://%s:%d", host, port))) - .iterator(); + URI uri; + try { + uri = new URI("http", null, host, port, null, null, null); + } catch (URISyntaxException x) { + throw new IOException(x); + } + Iterator proxies = ProxySelector.getDefault().select(uri).iterator(); while (targetAddress == null && proxies.hasNext()) { Proxy proxy = proxies.next(); if (proxy.type() == Proxy.Type.DIRECT) { diff --git a/src/test/java/org/jenkinsci/remoting/engine/JnlpAgentEndpointResolverTest.java b/src/test/java/org/jenkinsci/remoting/engine/JnlpAgentEndpointResolverTest.java new file mode 100644 index 000000000..5c676753f --- /dev/null +++ b/src/test/java/org/jenkinsci/remoting/engine/JnlpAgentEndpointResolverTest.java @@ -0,0 +1,40 @@ +/* + * The MIT License + * + * Copyright 2024 CloudBees, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package org.jenkinsci.remoting.engine; + +import java.net.Inet6Address; +import org.junit.Test; + +public final class JnlpAgentEndpointResolverTest { + + /** @see Inet6Address */ + @Test + public void getResolvedHttpProxyAddressIPv6() throws Exception { + JnlpAgentEndpointResolver.getResolvedHttpProxyAddress("localhost", 12345); + JnlpAgentEndpointResolver.getResolvedHttpProxyAddress("127.0.0.1", 12345); + // Ignore return value, just assert that it does not throw an exception: + JnlpAgentEndpointResolver.getResolvedHttpProxyAddress("0:0:0:0:0:0:0:1%lo", 12345); + } +}