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

[JENKINS-28289] Ensure proxy exclusion list is used while getting proxy for JNLP clients #55

Merged
merged 3 commits into from Nov 12, 2015
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -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;
@@ -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);
}

@@ -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
@@ -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

@@ -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);
}
@@ -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
@@ -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;

This comment has been minimized.

Copy link
@jtnord

jtnord Sep 8, 2015

Member

break?

This comment has been minimized.

Copy link
@hypnoce

hypnoce Sep 8, 2015

Author Contributor

The while condition already checks if targetAddress is null.

This comment has been minimized.

Copy link
@jtnord

jtnord Sep 8, 2015

Member

missed that :-(

}
}
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());

This comment has been minimized.

Copy link
@jtnord

jtnord Sep 8, 2015

Member

logging not syserr

This comment has been minimized.

Copy link
@hypnoce
}
}
}
return targetAddress;
}
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.