| 1 19 20 package com.sslexplorer.agent.client; 21 22 import java.io.BufferedReader ; 23 import java.io.File ; 24 import java.io.FileInputStream ; 25 import java.io.IOException ; 26 import java.io.InputStreamReader ; 27 import java.text.MessageFormat ; 28 import java.util.Enumeration ; 29 import java.util.Hashtable ; 30 import java.util.Properties ; 31 import java.util.StringTokenizer ; 32 import java.util.Vector ; 33 34 import ca.beq.util.win32.registry.RegistryKey; 35 import ca.beq.util.win32.registry.RootKey; 36 37 import com.sslexplorer.agent.client.util.Utils; 38 39 44 public class ProxyUtil { 45 46 static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(ProxyUtil.class); 48 50 57 public static BrowserProxySettings lookupFirefoxProxySettings() throws IOException { 58 59 try { 60 61 Vector proxies = new Vector (); 62 Vector bypassAddr = new Vector (); 63 64 File home = new File (Utils.getHomeDirectory()); 65 File firefoxAppData; 66 67 if (System.getProperty("os.name") != null && System.getProperty("os.name").startsWith("Windows")) { firefoxAppData = new File (home, "Application Data\\Mozilla\\Firefox\\profiles.ini"); } else { 70 firefoxAppData = new File (home, ".mozilla/firefox/profiles.ini"); } 72 73 BufferedReader reader = null; 75 Hashtable profiles = new Hashtable (); 76 String line; 77 try { 78 reader = new BufferedReader (new InputStreamReader (new FileInputStream (firefoxAppData))); 79 String currentProfileName = ""; 81 while ((line = reader.readLine()) != null) { 82 line = line.trim(); 83 if (line.startsWith("[") && line.endsWith("]")) { currentProfileName = line.substring(1, line.length() - 1); 85 continue; 86 } 87 88 if (line.startsWith("Path=")) { profiles.put(currentProfileName, new File (firefoxAppData.getParent(), line.substring(5))); 90 } 91 } 92 } finally { 93 if (reader != null) { 94 reader.close(); 95 } 96 } 97 98 101 File prefsJS; 102 String profileName; 103 for (Enumeration e = profiles.keys(); e.hasMoreElements();) { 104 profileName = (String ) e.nextElement(); 105 prefsJS = new File ((File ) profiles.get(profileName), "prefs.js"); Properties props = new Properties (); 107 reader = null; 108 try { 109 reader = new BufferedReader (new InputStreamReader (new FileInputStream (prefsJS))); 110 while ((line = reader.readLine()) != null) { 111 line = line.trim(); 112 if (line.startsWith("user_pref(\"")) { int idx = line.indexOf("\"", 11); if (idx == -1) 115 continue; 116 String pref = line.substring(11, idx); 117 118 int pos = idx + 1; 120 121 idx = line.indexOf("\"", idx + 1); 124 String value; 125 if (idx == -1) { 126 idx = line.indexOf(" ", pos); 129 if (idx == -1) 130 continue; 131 132 int idx2 = line.indexOf(")", pos); 134 if (idx2 == -1) 135 continue; 136 137 value = line.substring(idx + 1, idx2); 138 139 } else { 140 141 int idx2 = line.indexOf("\"", idx + 1); 144 if (idx2 == -1) 145 continue; 146 147 value = line.substring(idx + 1, idx2); 148 } 149 150 props.put(pref, value); 151 152 } 153 } 154 } finally { 155 if (reader != null) { 156 reader.close(); 157 } 158 } 159 ProxyInfo p; 160 164 if ("1".equals(props.get("network.proxy.type"))) { if (props.containsKey("network.proxy.ftp")) { p = createProxyInfo("ftp=" + props.get("network.proxy.ftp") + ":" + props.get("network.proxy.ftp_port"), "Firefox Profile [" + profileName + "]" ); proxies.addElement(p); 169 } 170 171 if (props.containsKey("network.proxy.http")) { p = createProxyInfo("http=" + props.get("network.proxy.http") + ":" + props.get("network.proxy.http_port"), "Firefox Profile [" + profileName + "]"); proxies.addElement(p); 175 } 176 177 if (props.containsKey("network.proxy.ssl")) { p = createProxyInfo("ssl=" + props.get("network.proxy.ssl") + ":" + props.get("network.proxy.ssl_port"), "Firefox Profile [" + profileName + "]"); proxies.addElement(p); 181 } 182 183 if (props.containsKey("network.proxy.socks")) { p = createProxyInfo("socks=" + props.get("network.proxy.socks") + ":" + props.get("network.proxy.socks_port"), "Firefox Profile [" + profileName + "]"); proxies.addElement(p); 187 } 188 189 if (props.containsKey("network.proxy.no_proxies_on")) { 191 StringTokenizer tokens = new StringTokenizer (props.getProperty("network.proxy.no_proxies_on"), ","); 193 while (tokens.hasMoreTokens()) { 194 bypassAddr.addElement(((String ) tokens.nextToken()).trim()); 195 } 196 197 } 198 } 199 } 200 201 BrowserProxySettings bps = new BrowserProxySettings(); 202 bps.setBrowser("Mozilla Firefox"); bps.setProxies(new ProxyInfo[proxies.size()]); 204 proxies.copyInto(bps.getProxies()); 205 bps.setBypassAddr(new String [bypassAddr.size()]); 206 bypassAddr.copyInto(bps.getBypassAddr()); 207 return bps; 208 209 } catch (Throwable t) { 210 throw new IOException ("Failed to get proxy information from Firefox profiles: " + t.getMessage()); } 212 } 213 214 220 public static BrowserProxySettings lookupIEProxySettings() throws IOException { 221 222 try { 223 Vector addresses = new Vector (); 224 Vector proxies = new Vector (); 225 String proxyServerValue = null; 226 String proxyOveride = null; 227 228 231 232 if (Utils.isSupportedJRE("+1.3") && Utils.isSupportedPlatform("Windows") && 233 !(Utils.isSupportedOSVersion("+6.0") && Utils.isSupportedJRE("+1.6"))) { 234 235 239 RegistryKey key = new RegistryKey(RootKey.HKEY_CURRENT_USER, 240 "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"); 242 if (key != null && key.hasValue("ProxyEnable")) { 247 if (key.getValue("ProxyEnable").getStringValue().equals("1")) { 249 if (key.hasValue("ProxyServer")) { 254 proxyServerValue = key.getValue("ProxyServer").getStringValue(); 256 if (key.hasValue("ProxyOverride")) { 262 proxyOveride = key.getValue("ProxyOverride").getStringValue(); } 264 } 265 } else { 266 267 } 268 } 269 } else { 270 if (System.getProperty("java.vendor").startsWith("Microsoft")) { try { 272 Class clazz = Class.forName("com.ms.lang.RegKey"); int userRoot = clazz.getField("USER_ROOT").getInt(null); int keyOpenAll = clazz.getField("KEYOPEN_ALL").getInt(null); log.info(Messages.getString("ProxyUtil.lookingForRoot")); Object rootKey = clazz.getMethod("getRootKey", new Class [] { int.class }).invoke(null, new Object [] { new Integer (userRoot) }); 280 log.info(Messages.getString("ProxyUtil.getIERegistryKey")); Object key = clazz.getConstructor(new Class [] { clazz, String .class, int.class }).newInstance( 284 new Object [] { rootKey, "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", new Integer (keyOpenAll) }); 286 log.info(Messages.getString("ProxyUtil.checkingIfProxyEnabled")); if (((Integer ) (clazz.getMethod("getIntValue", new Class [] { String .class }).invoke(key, new Object [] { "ProxyEnable" }))).intValue() == 1) { log.info(Messages.getString("ProxyUtil.gettingProxyServerList")); proxyServerValue = (String ) (clazz.getMethod("getStringValue", new Class [] { String .class, String .class }).invoke(key, new Object [] { "ProxyServer", "" })); log.info(Messages.getString("ProxyUtil.gettingProxyOverides")); proxyOveride = (String ) (clazz.getMethod("getStringValue", new Class [] { String .class, String .class }) .invoke(key, new Object [] { "ProxyOverride", "" })); } 302 } catch (Throwable t) { 303 t.printStackTrace(); 304 } 305 306 } else { 307 log.info(MessageFormat.format(Messages.getString("ProxyUtil.unsupportedJavaRuntime"), new Object [] { System.getProperty("java.version"), System.getProperty("java.vendor") } ) ); } 311 312 } 313 314 ProxyInfo p; 315 316 if (proxyServerValue != null && proxyServerValue.indexOf(';') > -1) { 317 321 StringTokenizer tokens = new StringTokenizer (proxyServerValue, ";"); 323 while (tokens.hasMoreTokens()) { 324 p = createProxyInfo(tokens.nextToken(), "IE Proxy Settings"); proxies.addElement(p); 326 } 327 328 } else if (proxyServerValue != null) { 329 333 p = createProxyInfo(proxyServerValue, "IE Proxy Settings"); proxies.addElement(p); 335 } 336 337 BrowserProxySettings bps = new BrowserProxySettings(); 338 bps.setBrowser("Internet Explorer"); bps.setProxies(new ProxyInfo[proxies.size()]); 340 proxies.copyInto(bps.getProxies()); 341 if (proxyOveride != null) { 342 343 StringTokenizer tokens = new StringTokenizer (proxyOveride, ";"); 345 while (tokens.hasMoreTokens()) { 346 addresses.addElement(tokens.nextToken()); 347 } 348 } 349 350 bps.setBypassAddr(new String [addresses.size()]); 351 addresses.copyInto(bps.getBypassAddr()); 352 return bps; 353 354 } catch (Throwable t) { 355 t.printStackTrace(); 356 throw new IOException (MessageFormat.format( 357 Messages.getString("ProxyUtil.failedToLookupIEProxies"), new Object [] { t.getMessage() } ) ); } 359 } 360 361 private static ProxyInfo createProxyInfo(String proxyInfo, String sourceIdent) { 362 int idx = proxyInfo.indexOf('='); 363 int idx2 = proxyInfo.indexOf(':'); 364 ProxyInfo proxy = new ProxyInfo(idx > -1 ? proxyInfo.substring(0, idx) : "all", "", "", proxyInfo.substring( (idx > -1 ? idx + 1 : 0), idx2), Integer.parseInt(proxyInfo.substring(idx2 + 1)), sourceIdent); 366 return proxy; 367 } 368 369 375 public static void main(String [] args) throws Exception { 376 BrowserProxySettings settings = lookupFirefoxProxySettings(); 377 log.info("Browser = " + settings.getBrowser()); ProxyInfo[] info = settings.getProxies(); 381 for (int i = 0; i < info.length; i++) { 382 log.info(" " + info[i].toUri() + " [" + info[i].getSourceIdent() + "]"); } 386 } 387 388 } 389 | Popular Tags |