Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forward proxy support for WebSocket #691

Merged
merged 2 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 25 additions & 1 deletion src/main/java/hudson/remoting/Engine.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.net.URI;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.security.AccessController;
import java.security.KeyManagementException;
Expand All @@ -52,6 +53,7 @@
import java.security.interfaces.RSAPublicKey;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -76,7 +78,10 @@
import jakarta.websocket.EndpointConfig;
import jakarta.websocket.HandshakeResponse;
import jakarta.websocket.Session;
import jakarta.websocket.WebSocketContainer;
import net.jcip.annotations.NotThreadSafe;
import org.glassfish.tyrus.client.ClientManager;
import org.glassfish.tyrus.client.ClientProperties;
import org.jenkinsci.remoting.engine.Jnlp4ConnectionState;
import org.jenkinsci.remoting.engine.JnlpAgentEndpoint;
import org.jenkinsci.remoting.engine.JnlpAgentEndpointConfigurator;
Expand Down Expand Up @@ -682,7 +687,26 @@ public void closeRead() throws IOException {
}
hudsonUrl = candidateUrls.get(0);
String wsUrl = hudsonUrl.toString().replaceFirst("^http", "ws");
ContainerProvider.getWebSocketContainer().connectToServer(new AgentEndpoint(),
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
if (container instanceof ClientManager) {
ClientManager client = (ClientManager) container;

String proxyHost = System.getProperty("http.proxyHost", System.getenv("proxy_host"));
String proxyPort = System.getProperty("http.proxyPort");
if (proxyHost != null && "http".equals(hudsonUrl.getProtocol()) && NoProxyEvaluator.shouldProxy(hudsonUrl.getHost())) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compare with JnlpAgentEndpointResolver#openURLConnection

URI proxyUri;
if (proxyPort != null) {
proxyUri = URI.create(String.format("http://%s:%s", proxyHost, proxyPort));
} else {
proxyUri = URI.create(String.format("http://%s", proxyHost));
}
client.getProperties().put(ClientProperties.PROXY_URI, proxyUri);
if (proxyCredentials != null) {
client.getProperties().put(ClientProperties.PROXY_HEADERS, Map.of("Proxy-Authorization", "Basic " + Base64.getEncoder().encodeToString(proxyCredentials.getBytes(StandardCharsets.UTF_8))));
}
}
}
container.connectToServer(new AgentEndpoint(),
ClientEndpointConfig.Builder.create().configurator(headerHandler).build(), URI.create(wsUrl + "wsagents/"));
while (ch.get() == null) {
Thread.sleep(100);
Expand Down
1 change: 0 additions & 1 deletion src/main/java/hudson/remoting/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,6 @@ public void setNoCertificateCheck(boolean ignored) throws NoSuchAlgorithmExcepti
"-direct",
"-tunnel",
"-credentials",
"-proxyCredentials",
"-cert",
"-noCertificateCheck",
"-noKeepAlive"
Expand Down