23
23
*/
24
24
package hudson .cli ;
25
25
26
- import com .trilead .ssh2 .crypto .Base64 ;
27
26
import com .trilead .ssh2 .crypto .PEMDecoder ;
28
- import com .trilead .ssh2 .signature .DSASHA1Verify ;
29
- import com .trilead .ssh2 .signature .RSASHA1Verify ;
30
27
import hudson .cli .client .Messages ;
31
28
import hudson .remoting .Channel ;
32
29
import hudson .remoting .PingThread ;
48
45
import java .io .InputStream ;
49
46
import java .io .OutputStream ;
50
47
import java .net .HttpURLConnection ;
48
+ import java .net .InetSocketAddress ;
51
49
import java .net .Socket ;
52
50
import java .net .URL ;
53
51
import java .net .URLConnection ;
@@ -93,8 +91,8 @@ public CLI(URL jenkins, ExecutorService exec) throws IOException, InterruptedExc
93
91
pool = exec !=null ? exec : Executors .newCachedThreadPool ();
94
92
95
93
Channel channel = null ;
96
- int clip = getCliTcpPort (url );
97
- if (clip >= 0 ) {
94
+ InetSocketAddress clip = getCliTcpPort (url );
95
+ if (clip != null ) {
98
96
// connect via CLI port
99
97
try {
100
98
channel = connectViaCliPort (jenkins , url , clip );
@@ -132,10 +130,9 @@ protected void onDead() {
132
130
return ch ;
133
131
}
134
132
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 ());
139
136
DataOutputStream dos = new DataOutputStream (s .getOutputStream ());
140
137
dos .writeUTF ("Protocol:CLI-connect" );
141
138
@@ -145,19 +142,24 @@ private Channel connectViaCliPort(URL jenkins, String url, int clip) throws IOEx
145
142
}
146
143
147
144
/**
148
- * If the server advertises CLI port , returns it .
145
+ * If the server advertises CLI endpoint , returns its location .
149
146
*/
150
- private int getCliTcpPort (String url ) throws IOException {
147
+ private InetSocketAddress getCliTcpPort (String url ) throws IOException {
151
148
URLConnection head = new URL (url ).openConnection ();
152
149
try {
153
150
head .connect ();
154
151
} catch (IOException e ) {
155
152
throw (IOException )new IOException ("Failed to connect to " +url ).initCause (e );
156
153
}
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
+
158
159
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 ));
161
163
}
162
164
163
165
/**
0 commit comments