diff --git a/launcher/src/main/java/com/threerings/getdown/launcher/ProxyUtil.java b/launcher/src/main/java/com/threerings/getdown/launcher/ProxyUtil.java index 8962d35b..d0389a06 100644 --- a/launcher/src/main/java/com/threerings/getdown/launcher/ProxyUtil.java +++ b/launcher/src/main/java/com/threerings/getdown/launcher/ProxyUtil.java @@ -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; @@ -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); } } @@ -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) { @@ -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"; }