1 11 package org.eclipse.core.internal.net; 12 13 import java.net.MalformedURLException ; 14 import java.net.URL ; 15 import java.util.*; 16 17 import org.eclipse.core.net.proxy.IProxyData; 18 import org.eclipse.core.runtime.*; 19 import org.eclipse.core.runtime.preferences.IEclipsePreferences; 20 import org.eclipse.core.runtime.preferences.IEclipsePreferences.*; 21 import org.eclipse.osgi.util.NLS; 22 import org.osgi.service.prefs.BackingStoreException; 23 import org.osgi.service.prefs.Preferences; 24 25 public class ProxyType implements INodeChangeListener, IPreferenceChangeListener { 26 27 30 private static final String PREF_PROXY_DATA_NODE = "proxyData"; private static final String PREF_PROXY_HOST = "host"; private static final String PREF_PROXY_PORT = "port"; private static final String PREF_PROXY_HAS_AUTH = "hasAuth"; 35 38 public static int DO_NOT_VERIFY = 1; 39 public static int VERIFY_EMPTY = 2; 40 public static int VERIFY_EQUAL = 4; 41 42 45 private static final String PROP_SOCKS_SYSTEM_PROPERTY_HANDLING = "org.eclipse.net.core.setSocksSystemProperties"; public static final int ONLY_SET_FOR_1_5_OR_LATER = 0; 47 public static final int ALWAYS_SET = 1; 48 public static final int NEVER_SET = 2; 49 public static int socksSystemPropertySetting; 50 51 54 private static final String INFO_PROXY_USER = "user"; private static final String INFO_PROXY_PASS = "pass"; private static final URL FAKE_URL; 57 static { 58 URL temp = null; 59 try { 60 temp = new URL ("http://org.eclipse.core.net.proxy.auth"); } catch (MalformedURLException e) { 62 } 64 FAKE_URL = temp; 65 String value = System.getProperty(PROP_SOCKS_SYSTEM_PROPERTY_HANDLING); 66 if (value == null) { 67 socksSystemPropertySetting = ONLY_SET_FOR_1_5_OR_LATER; 68 } else if (value.equals("always")) { socksSystemPropertySetting = ALWAYS_SET; 70 } else if (value.equals("never")) { socksSystemPropertySetting = NEVER_SET; 72 } else { 73 socksSystemPropertySetting = ONLY_SET_FOR_1_5_OR_LATER; 74 } 75 } 76 77 private String name; 78 private boolean updatingPreferences; 79 80 public static String convertHostsToPropertyString(String [] value) { 81 StringBuffer buffer = new StringBuffer (); 82 83 if (value == null) 84 return ""; 86 if (value.length > 0) { 87 buffer.append(value[0]); 88 } 89 90 for (int index = 1; index < value.length; index++) { 91 buffer.append('|'); 92 buffer.append(value[index]); 93 } 94 95 return buffer.toString(); 96 } 97 98 public static String [] convertPropertyStringToHosts(String property) { 99 return property.split("\\|"); } 101 102 public ProxyType(String name) { 103 super(); 104 this.name = name; 105 } 106 107 private Preferences getPreferenceNode() { 108 return getParentPreferences().node(getName()); 109 } 110 111 116 private Preferences getParentPreferences() { 117 return Activator.getInstance().getInstancePreferences().node( 118 PREF_PROXY_DATA_NODE); 119 } 120 121 public IProxyData getProxyData(int verifyFlag) { 122 return createProxyData(name, getPreferenceNode(), verifyFlag); 123 } 124 125 private IProxyData createProxyData(String type, Preferences node, int verifyFlag) { 126 String host = node.get(PREF_PROXY_HOST, null); 127 if (host != null && host.length() == 0) 128 host = null; 129 int port = node.getInt(PREF_PROXY_PORT, -1); 130 boolean requiresAuth = node.getBoolean(PREF_PROXY_HAS_AUTH, false); 131 ProxyData proxyData = new ProxyData(type, host, port, requiresAuth); 132 loadProxyAuth(proxyData); 133 if (verifyFlag == VERIFY_EMPTY) { 134 verifySystemPropertiesEmpty(type); 136 } else if (verifyFlag == VERIFY_EQUAL) { 137 verifyDataMatchesSystemProperties(proxyData); 139 } 140 return proxyData; 141 } 142 143 public boolean setProxyData(IProxyData proxyData, boolean proxiesEnabled) { 144 Assert.isTrue(proxyData.getType().equals(getName())); 145 IProxyData oldData = getProxyData(VERIFY_EQUAL); 146 if (oldData.equals(proxyData)) 147 return false; 148 saveProxyAuth(proxyData); 149 try { 150 updatingPreferences = true; 151 updatePreferences(proxyData); 152 } finally { 153 updatingPreferences = false; 154 } 155 updateSystemProperties(proxyData, proxiesEnabled); 156 return true; 157 } 158 159 private void updatePreferences(IProxyData proxyData) { 160 updatePreferences(getPreferenceNode(), proxyData); 161 } 162 163 void updatePreferencesIfMissing(Preferences node, IProxyData proxyData) { 164 Preferences proxyNode = node.node(PREF_PROXY_DATA_NODE).node(getName()); 165 if (node.get(PREF_PROXY_HOST, null) == null) 166 updatePreferences(proxyNode, proxyData); 167 } 168 169 private void updatePreferences(Preferences node, IProxyData proxyData) { 170 if (proxyData.getHost() == null) { 171 try { 172 Preferences parent = node.parent(); 173 node.removeNode(); 174 parent.flush(); 175 } catch (BackingStoreException e) { 176 Activator.logError(NLS.bind( 177 "An error occurred removing the {0} proxy node from the preference store", proxyData.getType()), e); } 179 } else { 180 node.put(PREF_PROXY_HOST, proxyData.getHost()); 181 node.putInt(PREF_PROXY_PORT, proxyData.getPort()); 182 node.putBoolean(PREF_PROXY_HAS_AUTH, proxyData.getUserId() != null); 183 try { 184 node.flush(); 185 } catch (BackingStoreException e) { 186 Activator.logError(NLS.bind( 187 "The {0} proxy node could not be written", proxyData.getType()), e); } 189 } 190 } 191 192 void updateSystemProperties(IProxyData proxyData, boolean proxiesEnabled) { 193 try { 194 if (proxyData.getType().equals(IProxyData.HTTP_PROXY_TYPE)) { 195 updateHttpSystemProperties(proxyData, proxiesEnabled); 196 } else if (proxyData.getType().equals(IProxyData.HTTPS_PROXY_TYPE)) { 197 updateHttpsSystemProperties(proxyData, proxiesEnabled); 198 } else if (proxyData.getType().equals(IProxyData.SOCKS_PROXY_TYPE)) { 199 updateSocksSystemProperties(proxyData, proxiesEnabled); 200 } 201 } catch (SecurityException e) { 202 Activator.logError("A security exception occurred while trying to put the proxy data into the system properties", e); } 204 } 205 206 private boolean verifyDataMatchesSystemProperties(ProxyData proxyData) { 207 try { 208 boolean proxiesEnabled = ProxyManager.getProxyManager().isProxiesEnabled(); 209 if (proxyData.getType().equals(IProxyData.HTTP_PROXY_TYPE)) { 210 return verifyDataMatchesHttpSystemProperties(proxyData, proxiesEnabled); 211 } else if (proxyData.getType().equals(IProxyData.HTTPS_PROXY_TYPE)) { 212 return verifyDataMatchesHttpsSystemProperties(proxyData, proxiesEnabled); 213 } else if (proxyData.getType().equals(IProxyData.SOCKS_PROXY_TYPE)) { 214 return verifyDataMatchesSocksSystemProperties(proxyData, proxiesEnabled); 215 } 216 217 } catch (SecurityException e) { 218 } 220 return true; 221 } 222 223 private boolean verifyDataMatchesHttpSystemProperties(ProxyData proxyData, 224 boolean proxiesEnabled) { 225 if (proxiesEnabled) { 226 boolean verified = true; 227 String dHost = proxyData.getHost(); 228 if (!verifySystemPropertyEquals("http.proxyHost", dHost)) { verified = false; 230 } else if (dHost != null && !Boolean.getBoolean("http.proxySet")) { Activator.logInfo("The HTTP proxy is enabled in the preferences but disabled in the system settings", null); verified = false; 233 } 234 int port = proxyData.getPort(); 235 if (!verifySystemPropertyEquals("http.proxyPort", port == -1 ? null : String.valueOf(port))) { verified = false; 237 } 238 return verified; 239 } 240 return verifyHttpSystemPropertiesEmpty(); 241 } 242 243 private boolean verifyDataMatchesHttpsSystemProperties(ProxyData proxyData, 244 boolean proxiesEnabled) { 245 if (proxiesEnabled) { 246 boolean verified = true; 247 String dHost = proxyData.getHost(); 248 if (!verifySystemPropertyEquals("https.proxyHost", dHost)) { verified = false; 250 } else if (dHost != null && !Boolean.getBoolean("https.proxySet")) { Activator.logInfo("The SSL proxy is enabled in the preferences but disabled in the system settings", null); verified = false; 253 } 254 int port = proxyData.getPort(); 255 if (!verifySystemPropertyEquals("https.proxyPort", port == -1 ? null : String.valueOf(port))) { verified = false; 257 } 258 return verified; 259 } 260 return verifyHttpsSystemPropertiesEmpty(); 261 } 262 263 private boolean verifyDataMatchesSocksSystemProperties(ProxyData proxyData, 264 boolean proxiesEnabled) { 265 if (proxiesEnabled && shouldSetSocksSystemProperties()) { 266 boolean verified = true; 267 String dHost = proxyData.getHost(); 268 if (!verifySystemPropertyEquals("socksProxyHost", dHost)) { verified = false; 270 } 271 int port = proxyData.getPort(); 272 if (!verifySystemPropertyEquals("socksProxyPort", port == -1 ? null : String.valueOf(port))) { verified = false; 274 } 275 return verified; 276 } 277 return verifySocksSystemPropertiesEmpty(); 278 } 279 280 private boolean shouldSetSocksSystemProperties() { 281 if (socksSystemPropertySetting == ALWAYS_SET) 282 return true; 283 if (socksSystemPropertySetting == NEVER_SET) 284 return false; 285 return hasJavaNetProxyClass(); 286 } 287 288 private boolean verifySystemPropertyEquals(String key, String expected) { 289 String value = System.getProperty(key); 290 if (value == expected) 291 return true; 292 if (value == null && expected != null) { 293 Activator.logInfo(NLS.bind("System property {0} is not set but should be {1}.", key, expected), null); return false; 295 } 296 if (value != null && expected == null) { 297 Activator.logInfo(NLS.bind("System property {0} is set to {1} but should not be set.", key, value), null); return false; 299 } 300 if (!value.equals(expected)) { 301 Activator.logInfo(NLS.bind("System property {0} is set to {1} but should be {2}.", new Object [] {key, value, expected }), null); return false; 303 } 304 return true; 305 } 306 307 private boolean verifySystemPropertiesEmpty(String proxyType) { 308 try { 309 if (proxyType.equals(IProxyData.HTTP_PROXY_TYPE)) { 310 return verifyHttpSystemPropertiesEmpty(); 311 } else if (proxyType.equals(IProxyData.HTTPS_PROXY_TYPE)) { 312 return verifyHttpsSystemPropertiesEmpty(); 313 } else if (proxyType.equals(IProxyData.SOCKS_PROXY_TYPE)) { 314 return verifySocksSystemPropertiesEmpty(); 315 } 316 } catch (SecurityException e) { 317 } 319 return true; 320 } 321 322 private boolean verifyHttpSystemPropertiesEmpty() { 323 boolean verified = true; 324 verified &= verifyIsNotSet("http.proxySet"); verified &= verifyIsNotSet("http.proxyHost"); verified &= verifyIsNotSet("http.proxyPort"); verified &= verifyIsNotSet("http.nonProxyHosts"); verified &= verifyIsNotSet("http.proxyUser"); verified &= verifyIsNotSet("http.proxyUserName"); verified &= verifyIsNotSet("http.proxyPassword"); return verified; 332 } 333 334 private boolean verifyIsNotSet(String key) { 335 String value = System.getProperty(key); 336 if (value != null) { 337 Activator.logInfo(NLS.bind("System property {0} has been set to {1} by an external source. This value will be overwritten using the values from the preferences", key, value), null); } 339 return value == null; 340 } 341 342 private boolean verifyHttpsSystemPropertiesEmpty() { 343 boolean verified = true; 344 verified &= verifyIsNotSet("https.proxySet"); verified &= verifyIsNotSet("https.proxyHost"); verified &= verifyIsNotSet("https.proxyPort"); verified &= verifyIsNotSet("https.nonProxyHosts"); verified &= verifyIsNotSet("https.proxyUser"); verified &= verifyIsNotSet("https.proxyUserName"); verified &= verifyIsNotSet("https.proxyPassword"); return verified; 352 } 353 354 private boolean verifySocksSystemPropertiesEmpty() { 355 boolean verified = true; 356 verified &= verifyIsNotSet("socksProxyHost"); verified &= verifyIsNotSet("socksProxyPort"); return verified; 359 } 360 361 public String getName() { 362 return name; 363 } 364 365 private void updateHttpSystemProperties(IProxyData data, boolean proxiesEnabled) { 366 Assert.isTrue(data.getType().equals(IProxyData.HTTP_PROXY_TYPE)); 367 Properties sysProps = System.getProperties(); 368 if (!proxiesEnabled || data.getHost() == null) { 369 sysProps.remove("http.proxySet"); sysProps.remove("http.proxyHost"); sysProps.remove("http.proxyPort"); sysProps.remove("http.nonProxyHosts"); sysProps.remove("http.proxyUser"); sysProps.remove("http.proxyUserName"); sysProps.remove("http.proxyPassword"); } else { 377 sysProps.put("http.proxySet", "true"); sysProps.put("http.proxyHost", data.getHost()); int port = data.getPort(); 380 if (port == -1) { 381 sysProps.remove("http.proxyPort"); } else { 383 sysProps.put("http.proxyPort", String.valueOf(port)); } 385 sysProps.put("http.nonProxyHosts", convertHostsToPropertyString(ProxyManager.getProxyManager().getNonProxiedHosts())); 387 388 String userid = data.getUserId(); 389 String password = data.getPassword(); 390 if (userid == null || password == null || userid.length() == 0 391 || password.length() == 0) { 392 sysProps.remove("http.proxyUser"); sysProps.remove("http.proxyUserName"); sysProps.remove("http.proxyPassword"); } else { 396 sysProps.put("http.proxyUser", userid); sysProps.put("http.proxyUserName", userid); sysProps.put("http.proxyPassword", password); } 400 } 401 } 402 403 private void updateHttpsSystemProperties(IProxyData data, boolean proxiesEnabled) { 404 Assert.isTrue(data.getType().equals(IProxyData.HTTPS_PROXY_TYPE)); 405 Properties sysProps = System.getProperties(); 406 if (!proxiesEnabled || data.getHost() == null) { 407 sysProps.remove("https.proxySet"); sysProps.remove("https.proxyHost"); sysProps.remove("https.proxyPort"); sysProps.remove("https.nonProxyHosts"); sysProps.remove("https.proxyUser"); sysProps.remove("https.proxyUserName"); sysProps.remove("https.proxyPassword"); } else { 415 sysProps.put("https.proxySet", "true"); sysProps.put("https.proxyHost", data.getHost()); int port = data.getPort(); 418 if (port == -1) { 419 sysProps.remove("https.proxyPort"); } else { 421 sysProps.put("https.proxyPort", String.valueOf(port)); } 423 sysProps.put("https.nonProxyHosts", convertHostsToPropertyString(ProxyManager.getProxyManager().getNonProxiedHosts())); 425 426 String userid = data.getUserId(); 427 String password = data.getPassword(); 428 if (userid == null || password == null || userid.length() == 0 429 || password.length() == 0) { 430 sysProps.remove("https.proxyUser"); sysProps.remove("https.proxyUserName"); sysProps.remove("https.proxyPassword"); } else { 434 sysProps.put("https.proxyUser", userid); sysProps.put("https.proxyUserName", userid); sysProps.put("https.proxyPassword", password); } 438 } 439 } 440 441 private void updateSocksSystemProperties(IProxyData data, boolean proxiesEnabled) { 442 Assert.isTrue(data.getType().equals(IProxyData.SOCKS_PROXY_TYPE)); 443 Properties sysProps = System.getProperties(); 444 if (!proxiesEnabled || data.getHost() == null) { 445 sysProps.remove("socksProxyHost"); sysProps.remove("socksProxyPort"); } else { 448 if (!shouldSetSocksSystemProperties()) { 449 if (socksSystemPropertySetting == ONLY_SET_FOR_1_5_OR_LATER) 451 Activator.logError("Setting the SOCKS system properties for a 1.4 VM can interfere with other proxy services (e.g. JSch). Please upgrade to a 1.5 JRE or later if you need to use Java's SOCKS proxy support.", null); return; 453 } 454 sysProps.put("socksProxyHost", data.getHost()); int port = data.getPort(); 456 if (port == -1) { 457 sysProps.remove("socksProxyPort"); } else { 459 sysProps.put("socksProxyPort", String.valueOf(port)); } 461 } 465 } 466 467 public void initialize(boolean proxiesEnabled) { 468 updateSystemProperties(getProxyData(VERIFY_EMPTY), proxiesEnabled); 469 ((IEclipsePreferences)getParentPreferences()).addNodeChangeListener(this); 470 ((IEclipsePreferences)getPreferenceNode()).addPreferenceChangeListener(this); 471 } 472 473 private Map getAuthInfo() { 474 Map authInfo = Platform.getAuthorizationInfo(FAKE_URL, getName(), ""); return authInfo != null ? authInfo : Collections.EMPTY_MAP; 477 } 478 479 private void loadProxyAuth(IProxyData data) { 480 Map authInfo = getAuthInfo(); 481 data.setUserid((String )authInfo.get(INFO_PROXY_USER)); 482 data.setPassword((String )authInfo.get(INFO_PROXY_PASS)); 483 } 484 485 private void saveProxyAuth(IProxyData data) { 486 Map authInfo = getAuthInfo(); 487 if (authInfo.size() == 0) { 488 authInfo = new java.util.HashMap (4); 489 } 490 String proxyUser = data.getUserId(); 491 if (proxyUser != null && data.getHost() != null) { 492 authInfo.put(INFO_PROXY_USER, proxyUser); 493 } else { 494 authInfo.remove(INFO_PROXY_USER); 495 } 496 String proxyPass = data.getPassword(); 497 if (proxyPass != null && data.getHost() != null) { 498 authInfo.put(INFO_PROXY_PASS, proxyPass); 499 } else { 500 authInfo.remove(INFO_PROXY_PASS); 501 } 502 try { 503 if (authInfo.isEmpty()) { 504 Platform.flushAuthorizationInfo(FAKE_URL, getName(), ""); } else { 506 Platform.addAuthorizationInfo(FAKE_URL, getName(), "", authInfo); } 508 } catch (CoreException e) { 509 Activator.logError(e.getMessage(), e); 510 } 511 } 512 513 private synchronized boolean hasJavaNetProxyClass() { 514 try { 515 Class proxyClass = Class.forName("java.net.Proxy"); return proxyClass != null; 517 } catch (ClassNotFoundException e) { 518 } 520 return false; 521 } 522 523 public void added(NodeChangeEvent event) { 524 if (event.getChild().name().equals(getName())) 526 ((IEclipsePreferences)event.getChild()).addPreferenceChangeListener(this); 527 } 528 529 public void removed(NodeChangeEvent event) { 530 } 532 533 public void preferenceChange(PreferenceChangeEvent event) { 534 if (updatingPreferences) 535 return; 536 updateSystemProperties(getProxyData(DO_NOT_VERIFY), ProxyManager.getProxyManager().isProxiesEnabled()); 537 538 } 539 540 } 541 | Popular Tags |