[2711] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.io;
|
---|
| 3 |
|
---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 |
|
---|
| 6 | import java.io.IOException;
|
---|
| 7 | import java.net.InetSocketAddress;
|
---|
| 8 | import java.net.Proxy;
|
---|
[6248] | 9 | import java.net.Proxy.Type;
|
---|
[2711] | 10 | import java.net.ProxySelector;
|
---|
| 11 | import java.net.SocketAddress;
|
---|
| 12 | import java.net.URI;
|
---|
[6971] | 13 | import java.util.Arrays;
|
---|
[2711] | 14 | import java.util.Collections;
|
---|
[6523] | 15 | import java.util.HashSet;
|
---|
[2711] | 16 | import java.util.List;
|
---|
[6523] | 17 | import java.util.Set;
|
---|
| 18 | import java.util.TreeSet;
|
---|
[2711] | 19 |
|
---|
| 20 | import org.openstreetmap.josm.Main;
|
---|
[2748] | 21 | import org.openstreetmap.josm.gui.preferences.server.ProxyPreferencesPanel;
|
---|
| 22 | import org.openstreetmap.josm.gui.preferences.server.ProxyPreferencesPanel.ProxyPolicy;
|
---|
[12620] | 23 | import org.openstreetmap.josm.tools.Logging;
|
---|
[2711] | 24 |
|
---|
| 25 | /**
|
---|
| 26 | * This is the default proxy selector used in JOSM.
|
---|
[2801] | 27 | *
|
---|
[2711] | 28 | */
|
---|
| 29 | public class DefaultProxySelector extends ProxySelector {
|
---|
[6714] | 30 |
|
---|
| 31 | private static final List<Proxy> NO_PROXY_LIST = Collections.singletonList(Proxy.NO_PROXY);
|
---|
[7187] | 32 |
|
---|
[7025] | 33 | private static final String IPV4_LOOPBACK = "127.0.0.1";
|
---|
| 34 | private static final String IPV6_LOOPBACK = "::1";
|
---|
[6714] | 35 |
|
---|
[2711] | 36 | /**
|
---|
[5266] | 37 | * The {@link ProxySelector} provided by the JDK will retrieve proxy information
|
---|
[2711] | 38 | * from the system settings, if the system property <tt>java.net.useSystemProxies</tt>
|
---|
| 39 | * is defined <strong>at startup</strong>. It has no effect if the property is set
|
---|
| 40 | * later by the application.
|
---|
| 41 | *
|
---|
| 42 | * We therefore read the property at class loading time and remember it's value.
|
---|
| 43 | */
|
---|
[12537] | 44 | private static boolean jvmWillUseSystemProxies;
|
---|
[6265] | 45 | static {
|
---|
[2711] | 46 | String v = System.getProperty("java.net.useSystemProxies");
|
---|
| 47 | if (v != null && v.equals(Boolean.TRUE.toString())) {
|
---|
[12537] | 48 | jvmWillUseSystemProxies = true;
|
---|
[2711] | 49 | }
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | /**
|
---|
[5266] | 53 | * The {@link ProxySelector} provided by the JDK will retrieve proxy information
|
---|
[2711] | 54 | * from the system settings, if the system property <tt>java.net.useSystemProxies</tt>
|
---|
| 55 | * is defined <strong>at startup</strong>. If the property is set later by the application,
|
---|
| 56 | * this has no effect.
|
---|
[2801] | 57 | *
|
---|
[2711] | 58 | * @return true, if <tt>java.net.useSystemProxies</tt> was set to true at class initialization time
|
---|
[2801] | 59 | *
|
---|
[2711] | 60 | */
|
---|
| 61 | public static boolean willJvmRetrieveSystemProxies() {
|
---|
[12537] | 62 | return jvmWillUseSystemProxies;
|
---|
[2711] | 63 | }
|
---|
| 64 |
|
---|
| 65 | private ProxyPolicy proxyPolicy;
|
---|
| 66 | private InetSocketAddress httpProxySocketAddress;
|
---|
| 67 | private InetSocketAddress socksProxySocketAddress;
|
---|
[9078] | 68 | private final ProxySelector delegate;
|
---|
[2711] | 69 |
|
---|
[7005] | 70 | private final Set<String> errorResources = new HashSet<>();
|
---|
| 71 | private final Set<String> errorMessages = new HashSet<>();
|
---|
[6971] | 72 | private Set<String> proxyExceptions;
|
---|
[6523] | 73 |
|
---|
[2711] | 74 | /**
|
---|
| 75 | * A typical example is:
|
---|
| 76 | * <pre>
|
---|
| 77 | * PropertySelector delegate = PropertySelector.getDefault();
|
---|
| 78 | * PropertySelector.setDefault(new DefaultPropertySelector(delegate));
|
---|
| 79 | * </pre>
|
---|
[2801] | 80 | *
|
---|
[2711] | 81 | * @param delegate the proxy selector to delegate to if system settings are used. Usually
|
---|
| 82 | * this is the proxy selector found by ProxySelector.getDefault() before this proxy
|
---|
| 83 | * selector is installed
|
---|
| 84 | */
|
---|
| 85 | public DefaultProxySelector(ProxySelector delegate) {
|
---|
| 86 | this.delegate = delegate;
|
---|
| 87 | initFromPreferences();
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | protected int parseProxyPortValue(String property, String value) {
|
---|
| 91 | if (value == null) return 0;
|
---|
| 92 | int port = 0;
|
---|
| 93 | try {
|
---|
| 94 | port = Integer.parseInt(value);
|
---|
[2733] | 95 | } catch (NumberFormatException e) {
|
---|
[12620] | 96 | Logging.error(tr("Unexpected format for port number in preference ''{0}''. Got ''{1}''.", property, value));
|
---|
| 97 | Logging.error(tr("The proxy will not be used."));
|
---|
[2711] | 98 | return 0;
|
---|
| 99 | }
|
---|
[11100] | 100 | if (port <= 0 || port > 65_535) {
|
---|
[12620] | 101 | Logging.error(tr("Illegal port number in preference ''{0}''. Got {1}.", property, port));
|
---|
| 102 | Logging.error(tr("The proxy will not be used."));
|
---|
[2711] | 103 | return 0;
|
---|
| 104 | }
|
---|
| 105 | return port;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | /**
|
---|
| 109 | * Initializes the proxy selector from the setting in the preferences.
|
---|
[2801] | 110 | *
|
---|
[2711] | 111 | */
|
---|
[6890] | 112 | public final void initFromPreferences() {
|
---|
[2748] | 113 | String value = Main.pref.get(ProxyPreferencesPanel.PROXY_POLICY);
|
---|
[8394] | 114 | if (value.isEmpty()) {
|
---|
[2711] | 115 | proxyPolicy = ProxyPolicy.NO_PROXY;
|
---|
| 116 | } else {
|
---|
[6348] | 117 | proxyPolicy = ProxyPolicy.fromName(value);
|
---|
[2711] | 118 | if (proxyPolicy == null) {
|
---|
[12620] | 119 | Logging.warn(tr("Unexpected value for preference ''{0}'' found. Got ''{1}''. Will use no proxy.",
|
---|
[8509] | 120 | ProxyPreferencesPanel.PROXY_POLICY, value));
|
---|
[2711] | 121 | proxyPolicy = ProxyPolicy.NO_PROXY;
|
---|
| 122 | }
|
---|
| 123 | }
|
---|
[2748] | 124 | String host = Main.pref.get(ProxyPreferencesPanel.PROXY_HTTP_HOST, null);
|
---|
| 125 | int port = parseProxyPortValue(ProxyPreferencesPanel.PROXY_HTTP_PORT, Main.pref.get(ProxyPreferencesPanel.PROXY_HTTP_PORT, null));
|
---|
[6348] | 126 | httpProxySocketAddress = null;
|
---|
| 127 | if (proxyPolicy.equals(ProxyPolicy.USE_HTTP_PROXY)) {
|
---|
| 128 | if (host != null && !host.trim().isEmpty() && port > 0) {
|
---|
| 129 | httpProxySocketAddress = new InetSocketAddress(host, port);
|
---|
| 130 | } else {
|
---|
[12620] | 131 | Logging.warn(tr("Unexpected parameters for HTTP proxy. Got host ''{0}'' and port ''{1}''.", host, port));
|
---|
| 132 | Logging.warn(tr("The proxy will not be used."));
|
---|
[2711] | 133 | }
|
---|
| 134 | }
|
---|
| 135 |
|
---|
[2748] | 136 | host = Main.pref.get(ProxyPreferencesPanel.PROXY_SOCKS_HOST, null);
|
---|
| 137 | port = parseProxyPortValue(ProxyPreferencesPanel.PROXY_SOCKS_PORT, Main.pref.get(ProxyPreferencesPanel.PROXY_SOCKS_PORT, null));
|
---|
[6348] | 138 | socksProxySocketAddress = null;
|
---|
| 139 | if (proxyPolicy.equals(ProxyPolicy.USE_SOCKS_PROXY)) {
|
---|
| 140 | if (host != null && !host.trim().isEmpty() && port > 0) {
|
---|
[8510] | 141 | socksProxySocketAddress = new InetSocketAddress(host, port);
|
---|
[6348] | 142 | } else {
|
---|
[12620] | 143 | Logging.warn(tr("Unexpected parameters for SOCKS proxy. Got host ''{0}'' and port ''{1}''.", host, port));
|
---|
| 144 | Logging.warn(tr("The proxy will not be used."));
|
---|
[2711] | 145 | }
|
---|
| 146 | }
|
---|
[7005] | 147 | proxyExceptions = new HashSet<>(
|
---|
[6971] | 148 | Main.pref.getCollection(ProxyPreferencesPanel.PROXY_EXCEPTIONS,
|
---|
[12279] | 149 | Arrays.asList("localhost", IPV4_LOOPBACK, IPV6_LOOPBACK))
|
---|
[6971] | 150 | );
|
---|
[2711] | 151 | }
|
---|
| 152 |
|
---|
| 153 | @Override
|
---|
| 154 | public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
|
---|
[6248] | 155 | // Just log something. The network stack will also throw an exception which will be caught somewhere else
|
---|
[12620] | 156 | Logging.error(tr("Connection to proxy ''{0}'' for URI ''{1}'' failed. Exception was: {2}",
|
---|
| 157 | sa.toString(), uri.toString(), ioe.toString()));
|
---|
[6523] | 158 | // Remember errors to give a friendly user message asking to review proxy configuration
|
---|
| 159 | errorResources.add(uri.toString());
|
---|
| 160 | errorMessages.add(ioe.toString());
|
---|
[2711] | 161 | }
|
---|
| 162 |
|
---|
[6523] | 163 | /**
|
---|
| 164 | * Returns the set of current proxy resources that failed to be retrieved.
|
---|
| 165 | * @return the set of current proxy resources that failed to be retrieved
|
---|
| 166 | * @since 6523
|
---|
| 167 | */
|
---|
| 168 | public final Set<String> getErrorResources() {
|
---|
[7005] | 169 | return new TreeSet<>(errorResources);
|
---|
[6523] | 170 | }
|
---|
| 171 |
|
---|
| 172 | /**
|
---|
| 173 | * Returns the set of current proxy error messages.
|
---|
| 174 | * @return the set of current proxy error messages
|
---|
| 175 | * @since 6523
|
---|
| 176 | */
|
---|
| 177 | public final Set<String> getErrorMessages() {
|
---|
[7005] | 178 | return new TreeSet<>(errorMessages);
|
---|
[6523] | 179 | }
|
---|
| 180 |
|
---|
| 181 | /**
|
---|
| 182 | * Clear the sets of failed resources and error messages.
|
---|
| 183 | * @since 6523
|
---|
| 184 | */
|
---|
| 185 | public final void clearErrors() {
|
---|
| 186 | errorResources.clear();
|
---|
| 187 | errorMessages.clear();
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | /**
|
---|
| 191 | * Determines if proxy errors have occured.
|
---|
| 192 | * @return {@code true} if errors have occured, {@code false} otherwise.
|
---|
| 193 | * @since 6523
|
---|
| 194 | */
|
---|
| 195 | public final boolean hasErrors() {
|
---|
| 196 | return !errorResources.isEmpty();
|
---|
| 197 | }
|
---|
| 198 |
|
---|
[2711] | 199 | @Override
|
---|
| 200 | public List<Proxy> select(URI uri) {
|
---|
[6971] | 201 | if (uri != null && proxyExceptions.contains(uri.getHost())) {
|
---|
[6714] | 202 | return NO_PROXY_LIST;
|
---|
| 203 | }
|
---|
[2711] | 204 | switch(proxyPolicy) {
|
---|
| 205 | case USE_SYSTEM_SETTINGS:
|
---|
[12537] | 206 | if (!jvmWillUseSystemProxies) {
|
---|
[12620] | 207 | Logging.warn(tr("The JVM is not configured to lookup proxies from the system settings. "+
|
---|
[8510] | 208 | "The property ''java.net.useSystemProxies'' was missing at startup time. Will not use a proxy."));
|
---|
[6714] | 209 | return NO_PROXY_LIST;
|
---|
[2711] | 210 | }
|
---|
| 211 | // delegate to the former proxy selector
|
---|
[6714] | 212 | return delegate.select(uri);
|
---|
[2711] | 213 | case NO_PROXY:
|
---|
[6714] | 214 | return NO_PROXY_LIST;
|
---|
[2711] | 215 | case USE_HTTP_PROXY:
|
---|
| 216 | if (httpProxySocketAddress == null)
|
---|
[6714] | 217 | return NO_PROXY_LIST;
|
---|
| 218 | return Collections.singletonList(new Proxy(Type.HTTP, httpProxySocketAddress));
|
---|
[2711] | 219 | case USE_SOCKS_PROXY:
|
---|
| 220 | if (socksProxySocketAddress == null)
|
---|
[6714] | 221 | return NO_PROXY_LIST;
|
---|
| 222 | return Collections.singletonList(new Proxy(Type.SOCKS, socksProxySocketAddress));
|
---|
[2711] | 223 | }
|
---|
| 224 | // should not happen
|
---|
| 225 | return null;
|
---|
| 226 | }
|
---|
| 227 | }
|
---|