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

64bit windows system: use reg query tool to detect AutoconfigURL from registry #228

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.Reader;
import java.io.StringWriter;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.InetAddress;
Expand Down Expand Up @@ -101,7 +103,43 @@ public static boolean autoDetectProxy (Application app)
log.info("Detected no proxy settings in the registry.");
}

} catch (Throwable t) {
}
catch (UnsatisfiedLinkError ul) {
log.info("Now trying to read windows registry using reg query tool..");
try {
Process process = Runtime.getRuntime().exec(AUTO_CONFIG_URL);
StreamReader reader = new StreamReader(process.getInputStream());

reader.start();
process.waitFor();
reader.join();

String result = reader.getResult();
int p = result.indexOf(REGSTR_TOKEN);

if (p >= 0) {
String rpac = result.substring(p + REGSTR_TOKEN.length()).trim();

URL configURL = app.getConfigResource().getRemote();
log.info("PAC-ConfigURL: " + rpac);
Reader acjs = new InputStreamReader(new URL(rpac).openStream());
// technically we should be returning all this info and trying each proxy
// in succession, but that's complexity we'll leave for another day

for (String proxy : findPACProxiesForURL(acjs, configURL)) {
if (proxy.startsWith("PROXY ")) {
String[] hostPort = splitHostPort(proxy.substring(6));
host = hostPort[0];
port = hostPort[1];
break;
}
}
}
} catch (Exception ex) {
log.info("Failed to find proxy settings in Windows registry", "error", ex);
}
}
catch (Throwable t) {
log.info("Failed to find proxy settings in Windows registry", "error", t);
}
}
Expand All @@ -121,6 +159,29 @@ public static boolean autoDetectProxy (Application app)
initProxy(app, host, port, null, null);
return true;
}

static class StreamReader extends Thread {
private InputStream is;
private StringWriter sw;

StreamReader(InputStream is) {
this.is = is;
sw = new StringWriter();
}

public void run() {
try {
int c;
while ((c = is.read()) != -1)
sw.write(c);
}
catch (IOException e) { ; }
}

String getResult() {
return sw.toString();
}
}

public static boolean canLoadWithoutProxy (URL rurl, int timeoutSeconds)
{
Expand Down Expand Up @@ -291,4 +352,10 @@ private static String[] splitHostPort (String hostPort) {

protected static final String PROXY_REGISTRY =
"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings";
protected static final String REGQUERY_UTIL = "reg query ";
protected static final String REGSTR_TOKEN = "REG_SZ";
protected static final String REGDWORD_TOKEN = "REG_DWORD";
protected static final String AUTO_CONFIG_URL = REGQUERY_UTIL +
"\"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\"
+ "Internet Settings\" /v AutoconfigURL";
}