Skip to content

Commit 9713527

Browse files
committed
Advertise the host name to enable direct connection.
1 parent 78c1d5d commit 9713527

File tree

4 files changed

+28
-14
lines changed

4 files changed

+28
-14
lines changed

changelog.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@
5555
<!-- Record your changes in the trunk here. -->
5656
<div id="trunk" style="display:none"><!--=TRUNK-BEGIN=-->
5757
<ul class=image>
58+
<li class=rfe>
59+
CLI now supports routing TCP/IP requests without going through HTTP reverse proxy.
5860
<li class=rfe>
5961
If reload fails, don't let the partially loaded state running, or risk the user overwriting the configs they have.
6062
(<a href="https://issues.jenkins-ci.org/browse/JENKINS-11204">issue 11204</a>)

cli/src/main/java/hudson/cli/CLI.java

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@
2323
*/
2424
package hudson.cli;
2525

26-
import com.trilead.ssh2.crypto.Base64;
2726
import com.trilead.ssh2.crypto.PEMDecoder;
28-
import com.trilead.ssh2.signature.DSASHA1Verify;
29-
import com.trilead.ssh2.signature.RSASHA1Verify;
3027
import hudson.cli.client.Messages;
3128
import hudson.remoting.Channel;
3229
import hudson.remoting.PingThread;
@@ -48,6 +45,7 @@
4845
import java.io.InputStream;
4946
import java.io.OutputStream;
5047
import java.net.HttpURLConnection;
48+
import java.net.InetSocketAddress;
5149
import java.net.Socket;
5250
import java.net.URL;
5351
import java.net.URLConnection;
@@ -93,8 +91,8 @@ public CLI(URL jenkins, ExecutorService exec) throws IOException, InterruptedExc
9391
pool = exec!=null ? exec : Executors.newCachedThreadPool();
9492

9593
Channel channel = null;
96-
int clip = getCliTcpPort(url);
97-
if(clip>=0) {
94+
InetSocketAddress clip = getCliTcpPort(url);
95+
if(clip!=null) {
9896
// connect via CLI port
9997
try {
10098
channel = connectViaCliPort(jenkins, url, clip);
@@ -132,10 +130,9 @@ protected void onDead() {
132130
return ch;
133131
}
134132

135-
private Channel connectViaCliPort(URL jenkins, String url, int clip) throws IOException {
136-
String host = new URL(url).getHost();
137-
LOGGER.fine("Trying to connect directly via TCP/IP to port "+clip+" of "+host);
138-
Socket s = new Socket(host,clip);
133+
private Channel connectViaCliPort(URL jenkins, String url, InetSocketAddress endpoint) throws IOException {
134+
LOGGER.fine("Trying to connect directly via TCP/IP to "+endpoint);
135+
Socket s = new Socket(endpoint.getHostName(),endpoint.getPort());
139136
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
140137
dos.writeUTF("Protocol:CLI-connect");
141138

@@ -145,19 +142,24 @@ private Channel connectViaCliPort(URL jenkins, String url, int clip) throws IOEx
145142
}
146143

147144
/**
148-
* If the server advertises CLI port, returns it.
145+
* If the server advertises CLI endpoint, returns its location.
149146
*/
150-
private int getCliTcpPort(String url) throws IOException {
147+
private InetSocketAddress getCliTcpPort(String url) throws IOException {
151148
URLConnection head = new URL(url).openConnection();
152149
try {
153150
head.connect();
154151
} catch (IOException e) {
155152
throw (IOException)new IOException("Failed to connect to "+url).initCause(e);
156153
}
157-
String p = head.getHeaderField("X-Hudson-CLI-Port");
154+
String p = head.getHeaderField("X-Jenkins-CLI-Port");
155+
if (p==null) p = head.getHeaderField("X-Hudson-CLI-Port"); // backward compatibility
156+
String h = head.getHeaderField("X-Jenkins-CLI-Host");
157+
if (h==null) h = head.getURL().getHost();
158+
158159
flushURLConnection(head);
159-
if(p==null) return -1;
160-
return Integer.parseInt(p);
160+
if (p==null) return null;
161+
162+
return new InetSocketAddress(h,Integer.parseInt(p));
161163
}
162164

163165
/**

core/src/main/java/hudson/TcpSlaveAgentListener.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,15 @@ public String toString() {
364364
private static final Logger LOGGER = Logger.getLogger(TcpSlaveAgentListener.class.getName());
365365

366366
private static final String COOKIE_NAME = TcpSlaveAgentListener.class.getName()+".cookie";
367+
368+
/**
369+
* Host name that we advertise the CLI client to connect to.
370+
* This is primarily for those who have reverse proxies in place such that the HTTP host name
371+
* and the CLI TCP/IP connection host names are different.
372+
*
373+
* TODO: think about how to expose this (including whether this needs to be exposed at all.)
374+
*/
375+
public static String CLI_HOST_NAME = System.getProperty(TcpSlaveAgentListener.class.getName()+".hostName");
367376
}
368377

369378
/*

core/src/main/resources/lib/layout/layout.jelly

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ THE SOFTWARE.
7777
<!-- advertise the CLI TCP port -->
7878
<st:header name="X-Hudson-CLI-Port" value="${app.tcpSlaveAgentListener.port}"/>
7979
<st:header name="X-Jenkins-CLI-Port" value="${app.tcpSlaveAgentListener.port}"/>
80+
<st:header name="X-Jenkins-CLI-Host" value="${app.tcpSlaveAgentListener.CLI_HOST_NAME}"/>
8081
</j:if>
8182
<j:forEach var="pd" items="${h.pageDecorators}">
8283
<st:include it="${pd}" page="httpHeaders.jelly" optional="true"/>

0 commit comments

Comments
 (0)