Skip to content

Commit

Permalink
Merge pull request #55 from hypnoce/master
Browse files Browse the repository at this point in the history
[JENKINS-28289] Ensure proxy exclusion list is used while getting proxy for JNLP clients
  • Loading branch information
oleg-nenashev committed Nov 12, 2015
2 parents bfbcfb3 + f5d6dd0 commit a4cc02d
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 26 deletions.
46 changes: 21 additions & 25 deletions src/main/java/hudson/remoting/Engine.java
Expand Up @@ -31,7 +31,7 @@
import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.URL;
import java.util.Collections;
Expand Down Expand Up @@ -283,7 +283,7 @@ public void run() {
}

private void onConnectionRejected(String greeting) throws InterruptedException {
events.error(new Exception("The server rejected the connection: "+greeting));
events.error(new Exception("The server rejected the connection: " + greeting));
Thread.sleep(10*1000);
}

Expand All @@ -304,30 +304,21 @@ private Socket connect(String host, String port) throws IOException, Interrupted
events.status(msg);
int retry = 1;
while(true) {
boolean isHttpProxy = false;
InetSocketAddress targetAddress = null;
try {
boolean isProxy = false;
Socket s;
if (System.getProperty("http.proxyHost") != null) {
String proxyHost = System.getProperty("http.proxyHost");
String proxyPort = System.getProperty("http.proxyPort", "80");
s = new Socket(proxyHost, Integer.parseInt(proxyPort));
isProxy = true;
Socket s = null;
targetAddress = Util.getResolvedHttpProxyAddress(host, Integer.parseInt(port));

if(targetAddress == null) {
targetAddress = new InetSocketAddress(host, Integer.parseInt(port));
} else {
String httpProxy = System.getenv("http_proxy");
if (httpProxy != null) {
try {
URL url = new URL(httpProxy);
s = new Socket(url.getHost(), url.getPort());
isProxy = true;
} catch (MalformedURLException e) {
System.err.println("Not use http_proxy environment variable which is invalid: "+e.getMessage());
s = new Socket(host, Integer.parseInt(port));
}
} else {
s = new Socket(host, Integer.parseInt(port));
}
isHttpProxy = true;
}

s = new Socket();
s.connect(targetAddress);

s.setTcpNoDelay(true); // we'll do buffering by ourselves

// set read time out to avoid infinite hang. the time out should be long enough so as not
Expand All @@ -336,7 +327,7 @@ private Socket connect(String host, String port) throws IOException, Interrupted
// is gone.
s.setSoTimeout(30*60*1000); // 30 mins. See PingThread for the ping interval

if (isProxy) {
if (isHttpProxy) {
String connectCommand = String.format("CONNECT %s:%s HTTP/1.1\r\nHost: %s\r\n\r\n", host, port, host);
s.getOutputStream().write(connectCommand.getBytes("UTF-8")); // TODO: internationalized domain names

Expand All @@ -351,8 +342,13 @@ private Socket connect(String host, String port) throws IOException, Interrupted
}
return s;
} catch (IOException e) {
if(retry++>10)
throw (IOException)new IOException("Failed to connect to "+host+':'+port).initCause(e);
if(retry++>10) {
String suffix = "";
if(isHttpProxy) {
suffix = " through proxy " + targetAddress.toString();
}
throw new IOException("Failed to connect to " + host + ':' + port + suffix, e);
}
Thread.sleep(1000*10);
events.status(msg+" (retrying:"+retry+")",e);
}
Expand Down
38 changes: 37 additions & 1 deletion src/main/java/hudson/remoting/Util.java
Expand Up @@ -7,12 +7,14 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.ProxySelector;
import java.net.URI;
import java.net.URLConnection;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.URL;
import java.util.Iterator;

/**
* Misc. I/O utilities
Expand Down Expand Up @@ -131,4 +133,38 @@ static URLConnection openURLConnection(URL url) throws IOException {
}
return con;
}

static InetSocketAddress getResolvedHttpProxyAddress(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();
while (targetAddress == null && proxies.hasNext()) {
Proxy proxy = proxies.next();
if(proxy.type() == Proxy.Type.DIRECT) {
break;
}
if(proxy.type() == Proxy.Type.HTTP) {
final SocketAddress address = proxy.address();
if (!(address instanceof InetSocketAddress)) {
System.err.println("Unsupported proxy address type " + (address != null ? address.getClass() : "null"));
continue;
}
InetSocketAddress proxyAddress = (InetSocketAddress) address;
if(proxyAddress.isUnresolved())
proxyAddress = new InetSocketAddress(proxyAddress.getHostName(), proxyAddress.getPort());
targetAddress = proxyAddress;
}
}
if(targetAddress == null) {
String httpProxy = System.getenv("http_proxy");
if(httpProxy != null) {
try {
URL url = new URL(httpProxy);
targetAddress = new InetSocketAddress(url.getHost(), url.getPort());
} catch (MalformedURLException e) {
System.err.println("Not use http_proxy environment variable which is invalid: "+e.getMessage());
}
}
}
return targetAddress;
}
}

0 comments on commit a4cc02d

Please sign in to comment.