Skip to content

Commit

Permalink
Tolerate IPv6 scoped IPs from `JnlpAgentEndpointResolver.getResolvedH…
Browse files Browse the repository at this point in the history
…ttpProxyAddress` (#769)

* Tolerate IPv6 scoped IPs from `JnlpAgentEndpointResolver.getResolvedHttpProxyAddress`

* Spotless
  • Loading branch information
jglick authored Oct 18, 2024
1 parent 6b8b20e commit e1ea5d6
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -486,9 +487,13 @@ public void waitForReady() throws InterruptedException {
@CheckForNull
static InetSocketAddress getResolvedHttpProxyAddress(@NonNull String host, int port) throws IOException {
InetSocketAddress targetAddress = null;
Iterator<Proxy> 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<Proxy> proxies = ProxySelector.getDefault().select(uri).iterator();
while (targetAddress == null && proxies.hasNext()) {
Proxy proxy = proxies.next();
if (proxy.type() == Proxy.Type.DIRECT) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit e1ea5d6

Please sign in to comment.