Finished up the proxy jockeying. Now we peek into the registry to see if

they've already got a proxy configuration. Oh how we love to go native.
This commit is contained in:
Michael Bayne
2004-07-30 22:37:15 +00:00
parent e18d279b3b
commit e4c326279b
4 changed files with 71 additions and 153 deletions
+9
View File
@@ -14,6 +14,7 @@
-ClassPath:
/usr/local/jdk1.4/jre/lib/rt.jar
dist/classes
lib/jRegistryKey.jar
../../projects/samskivert/dist/samskivert.jar
../../lib/commons-io.jar
../../lib/javaws.jar
@@ -37,6 +38,14 @@ com.sun.*
-ExcludeMethods:
-IncludeClassesUnconditional:
ca.beq.util.win32.registry.RegistryException
ca.beq.util.win32.registry.RegistryKey
ca.beq.util.win32.registry.RegistryValue
ca.beq.util.win32.registry.RootKey
ca.beq.util.win32.registry.ValueIterator
ca.beq.util.win32.registry.ValueType
-General:
nomakepublic
fornamedetection
Binary file not shown.
@@ -1,5 +1,5 @@
//
// $Id: Getdown.java,v 1.28 2004/07/30 21:45:28 mdb Exp $
// $Id: Getdown.java,v 1.29 2004/07/30 22:37:15 mdb Exp $
package com.threerings.getdown.launcher;
@@ -27,10 +27,13 @@ import java.net.URL;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ResourceBundle;
// import org.apache.commons.io.TeeOutputStream;
import ca.beq.util.win32.registry.RegistryKey;
import ca.beq.util.win32.registry.RegistryValue;
import ca.beq.util.win32.registry.RootKey;
import com.samskivert.swing.util.SwingUtil;
import com.samskivert.text.MessageUtil;
@@ -42,7 +45,6 @@ import com.threerings.getdown.data.Resource;
import com.threerings.getdown.tools.Patcher;
import com.threerings.getdown.util.ConfigUtil;
import com.threerings.getdown.util.ProgressObserver;
import com.threerings.getdown.util.ProxyUtil;
/**
* Manages the main control for the Getdown application updater and
@@ -142,10 +144,7 @@ public class Getdown extends Thread
}
// also configure them in the JVM
System.setProperty("http.proxyHost", host);
if (!StringUtil.blank(port)) {
System.setProperty("http.proxyPort", port);
}
setProxyProperties(host, port);
}
// clear out our UI
@@ -170,25 +169,48 @@ public class Getdown extends Thread
return true;
}
// TODO: look in the Vinders registry
// look in the Vinders registry
try {
String host = null, port = null;
boolean enabled = false;
RegistryKey.initialize();
RegistryKey r = new RegistryKey(
RootKey.HKEY_CURRENT_USER, PROXY_REGISTRY);
for (Iterator iter = r.values(); iter.hasNext(); ) {
RegistryValue value = (RegistryValue)iter.next();
if (value.getName().equals("ProxyEnable")) {
enabled = value.getStringValue().equals("1");
}
if (value.getName().equals("ProxyServer")) {
String strval = value.getStringValue();
int cidx = strval.indexOf(":");
if (cidx != -1) {
port = strval.substring(cidx+1);
strval = strval.substring(0, cidx);
}
host = strval;
}
}
if (enabled) {
setProxyProperties(host, port);
return true;
} else {
Log.info("Detected no proxy settings in the registry.");
}
} catch (Throwable t) {
Log.info("Failed to find proxy settings in Windows registry " +
"[error=" + t + "].");
}
// otherwise look for and read our proxy.txt file
File pfile = _app.getLocalPath("proxy.txt");
if (pfile.exists()) {
try {
HashMap pconf = ConfigUtil.parseConfig(pfile, false);
String proxyHost = (String)pconf.get("host");
String proxyPort = (String)pconf.get("port");
if (!StringUtil.blank(proxyHost)) {
System.setProperty("http.proxyHost", proxyHost);
if (!StringUtil.blank(proxyPort)) {
System.setProperty("http.proxyPort", proxyPort);
}
Log.info("Using proxy [host=" + proxyHost +
", port=" + proxyPort + "].");
}
// now we can get on with getting down
setProxyProperties((String)pconf.get("host"),
(String)pconf.get("port"));
return true;
} catch (IOException ioe) {
@@ -196,10 +218,10 @@ public class Getdown extends Thread
}
}
// otherwise we'll need to try to detect our proxy settings; first
// we have to initialize our application to get some sort of
// interface configuration and the appbase URL
Log.info("Attempting to detect proxy settings...");
// otherwise see if we actually need a proxy; first we have to
// initialize our application to get some sort of interface
// configuration and the appbase URL
Log.info("Checking whether we need to use a proxy...");
try {
_ifc = _app.init(true);
} catch (IOException ioe) {
@@ -242,6 +264,20 @@ public class Getdown extends Thread
return false;
}
/**
* Configures the JVM proxy system properties.
*/
protected void setProxyProperties (String host, String port)
{
if (!StringUtil.blank(host)) {
System.setProperty("http.proxyHost", host);
if (!StringUtil.blank(port)) {
System.setProperty("http.proxyPort", port);
}
Log.info("Using proxy [host=" + host + ", port=" + port + "].");
}
}
/**
* Does the actual application validation, update and launching
* business.
@@ -598,4 +634,6 @@ public class Getdown extends Thread
protected static final int MAX_LOOPS = 5;
protected static final long MIN_EXIST_TIME = 5000L;
protected static final String PROXY_REGISTRY =
"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings";
}
@@ -1,129 +0,0 @@
//
// $Id: ProxyUtil.java,v 1.2 2004/07/29 18:46:57 mdb Exp $
package com.threerings.getdown.util;
import java.net.URL;
import com.threerings.getdown.Log;
/**
* Code to detect a user's proxy settings.
*/
public class ProxyUtil
{
/**
* Detects whether or not there are proxy settings that should be used
* by this JVM.
*
* @return true if there are such settings, false if not.
*/
public static boolean detectProxy (URL sampleURL)
{
// first look for a proxy that's already set
if ((_proxyIP = System.getProperty("proxyHost")) != null) {
_proxyPort = System.getProperty("proxyPort");
return true;
}
if ((_proxyIP = System.getProperty("http.proxyHost")) != null) {
_proxyPort = System.getProperty("http.proxyPort");
return true;
}
try {
// Look around for the 1.4.X plugin proxy detection
// class... Without it, cannot autodetect...
Class t = Class.forName("com.sun.java.browser.net.ProxyService");
com.sun.java.browser.net.ProxyInfo[] pi =
com.sun.java.browser.net.ProxyService.getProxyInfo(sampleURL);
if (pi == null || pi.length == 0) {
Log.info("1.4.X reported NULL proxy (no proxy assumed).");
return false;
} else {
Log.info("1.4.X Proxy info getProxy: " + pi[0].getHost() +
" getPort: " + pi[0].getPort() +
" isSocks: " + pi[0].isSocks());
_proxyIP = pi[0].getHost();
_proxyPort = "" + pi[0].getPort();
Log.info("Detected proxy " + _proxyIP + " port " + _proxyPort);
return true;
}
} catch (Exception ee) {
Log.info("Sun Plugin 1.4.X proxy detection class not " +
"found: " + ee + ". Trying failover detection...");
}
try {
String key = "javaplugin.proxy.config.list";
String proxyList = (String)
System.getProperties().getProperty(key);
proxyList = proxyList.toUpperCase();
Log.info("Plugin proxy config list: " + proxyList);
// Using HTTP proxy as proxy for HTTP proxy tunnelled SSL
// socket (should be listed FIRST)....
if (proxyList != null) {
// 6.0.0 1/14/03 1.3.1_06 appears to omit HTTP portion of
// 6.0.reported proxy list... Mod to accomodate this...
// Expecting proxyList of "HTTP=XXX.XXX.XXX.XXX:Port"
// OR "XXX.XXX.XXX.XXX:Port" & assuming HTTP...
if (proxyList.indexOf("HTTP=") > -1) {
_proxyIP = proxyList.substring(
proxyList.indexOf("HTTP=") + 5,
proxyList.indexOf(":"));
} else {
_proxyIP = proxyList.substring(
0, proxyList.indexOf(":"));
}
int endOfPort = proxyList.indexOf(",");
if (endOfPort < 1) {
endOfPort = proxyList.length();
}
_proxyPort = proxyList.substring(proxyList.indexOf(":") + 1,
endOfPort);
Log.info("Detected proxy " + _proxyIP + " port " + _proxyPort);
return true;
}
} catch (Exception e) {
Log.warning("Exception during failover auto proxy detect: " + e);
}
return false;
}
/**
* Returns the IP of the detected proxy. This is only valid after a
* call to {@link #detectProxy} that returns true.
*/
public static String getProxyIP ()
{
return _proxyIP;
}
/**
* Returns the port of the detected proxy. This is only valid after a
* call to {@link #detectProxy} that returns true.
*/
public static String getProxyPort ()
{
return _proxyPort;
}
/**
* Sets the necessary system properties to enact the use of this proxy
* host and port.
*/
public static void configureProxy (String host, String port)
{
System.setProperty("http.proxyHost", host);
System.setProperty("http.proxyPort", port);
}
protected static String _proxyIP;
protected static String _proxyPort = "80";
protected static boolean _useProxy = false;
}