001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.io;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.io.IOException;
007import java.net.InetSocketAddress;
008import java.net.Proxy;
009import java.net.Proxy.Type;
010import java.net.ProxySelector;
011import java.net.SocketAddress;
012import java.net.URI;
013import java.util.Arrays;
014import java.util.Collections;
015import java.util.HashSet;
016import java.util.List;
017import java.util.Set;
018import java.util.TreeSet;
019
020import org.openstreetmap.josm.Main;
021import org.openstreetmap.josm.gui.preferences.server.ProxyPreferencesPanel;
022import org.openstreetmap.josm.gui.preferences.server.ProxyPreferencesPanel.ProxyPolicy;
023
024/**
025 * This is the default proxy selector used in JOSM.
026 *
027 */
028public class DefaultProxySelector extends ProxySelector {
029
030    private static final List<Proxy> NO_PROXY_LIST = Collections.singletonList(Proxy.NO_PROXY);
031
032    private static final String IPV4_LOOPBACK = "127.0.0.1";
033    private static final String IPV6_LOOPBACK = "::1";
034
035    /**
036     * The {@link ProxySelector} provided by the JDK will retrieve proxy information
037     * from the system settings, if the system property <tt>java.net.useSystemProxies</tt>
038     * is defined <strong>at startup</strong>. It has no effect if the property is set
039     * later by the application.
040     *
041     * We therefore read the property at class loading time and remember it's value.
042     */
043    private static boolean JVM_WILL_USE_SYSTEM_PROXIES = false;
044    static {
045        String v = System.getProperty("java.net.useSystemProxies");
046        if (v != null && v.equals(Boolean.TRUE.toString())) {
047            JVM_WILL_USE_SYSTEM_PROXIES = true;
048        }
049    }
050
051    /**
052     * The {@link ProxySelector} provided by the JDK will retrieve proxy information
053     * from the system settings, if the system property <tt>java.net.useSystemProxies</tt>
054     * is defined <strong>at startup</strong>. If the property is set later by the application,
055     * this has no effect.
056     *
057     * @return true, if <tt>java.net.useSystemProxies</tt> was set to true at class initialization time
058     *
059     */
060    public static boolean willJvmRetrieveSystemProxies() {
061        return JVM_WILL_USE_SYSTEM_PROXIES;
062    }
063
064    private ProxyPolicy proxyPolicy;
065    private InetSocketAddress httpProxySocketAddress;
066    private InetSocketAddress socksProxySocketAddress;
067    private ProxySelector delegate;
068
069    private final Set<String> errorResources = new HashSet<>();
070    private final Set<String> errorMessages = new HashSet<>();
071    private Set<String> proxyExceptions;
072
073    /**
074     * A typical example is:
075     * <pre>
076     *    PropertySelector delegate = PropertySelector.getDefault();
077     *    PropertySelector.setDefault(new DefaultPropertySelector(delegate));
078     * </pre>
079     *
080     * @param delegate the proxy selector to delegate to if system settings are used. Usually
081     * this is the proxy selector found by ProxySelector.getDefault() before this proxy
082     * selector is installed
083     */
084    public DefaultProxySelector(ProxySelector delegate) {
085        this.delegate = delegate;
086        initFromPreferences();
087    }
088
089    protected int parseProxyPortValue(String property, String value) {
090        if (value == null) return 0;
091        int port = 0;
092        try {
093            port = Integer.parseInt(value);
094        } catch (NumberFormatException e) {
095            Main.error(tr("Unexpected format for port number in preference ''{0}''. Got ''{1}''.", property, value));
096            Main.error(tr("The proxy will not be used."));
097            return 0;
098        }
099        if (port <= 0 || port >  65535) {
100            Main.error(tr("Illegal port number in preference ''{0}''. Got {1}.", property, port));
101            Main.error(tr("The proxy will not be used."));
102            return 0;
103        }
104        return port;
105    }
106
107    /**
108     * Initializes the proxy selector from the setting in the preferences.
109     *
110     */
111    public final void initFromPreferences() {
112        String value = Main.pref.get(ProxyPreferencesPanel.PROXY_POLICY);
113        if (value.length() == 0) {
114            proxyPolicy = ProxyPolicy.NO_PROXY;
115        } else {
116            proxyPolicy = ProxyPolicy.fromName(value);
117            if (proxyPolicy == null) {
118                Main.warn(tr("Unexpected value for preference ''{0}'' found. Got ''{1}''. Will use no proxy.", ProxyPreferencesPanel.PROXY_POLICY, value));
119                proxyPolicy = ProxyPolicy.NO_PROXY;
120            }
121        }
122        String host = Main.pref.get(ProxyPreferencesPanel.PROXY_HTTP_HOST, null);
123        int port = parseProxyPortValue(ProxyPreferencesPanel.PROXY_HTTP_PORT, Main.pref.get(ProxyPreferencesPanel.PROXY_HTTP_PORT, null));
124        httpProxySocketAddress = null;
125        if (proxyPolicy.equals(ProxyPolicy.USE_HTTP_PROXY)) {
126            if (host != null && !host.trim().isEmpty() && port > 0) {
127                httpProxySocketAddress = new InetSocketAddress(host, port);
128            } else {
129                Main.warn(tr("Unexpected parameters for HTTP proxy. Got host ''{0}'' and port ''{1}''.", host, port));
130                Main.warn(tr("The proxy will not be used."));
131            }
132        }
133
134        host = Main.pref.get(ProxyPreferencesPanel.PROXY_SOCKS_HOST, null);
135        port = parseProxyPortValue(ProxyPreferencesPanel.PROXY_SOCKS_PORT, Main.pref.get(ProxyPreferencesPanel.PROXY_SOCKS_PORT, null));
136        socksProxySocketAddress = null;
137        if (proxyPolicy.equals(ProxyPolicy.USE_SOCKS_PROXY)) {
138            if (host != null && !host.trim().isEmpty() && port > 0) {
139                socksProxySocketAddress = new InetSocketAddress(host,port);
140            } else {
141                Main.warn(tr("Unexpected parameters for SOCKS proxy. Got host ''{0}'' and port ''{1}''.", host, port));
142                Main.warn(tr("The proxy will not be used."));
143            }
144        }
145        proxyExceptions = new HashSet<>(
146            Main.pref.getCollection(ProxyPreferencesPanel.PROXY_EXCEPTIONS,
147                    Arrays.asList(new String[]{"localhost", IPV4_LOOPBACK, IPV6_LOOPBACK}))
148        );
149    }
150
151    @Override
152    public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
153        // Just log something. The network stack will also throw an exception which will be caught somewhere else
154        Main.error(tr("Connection to proxy ''{0}'' for URI ''{1}'' failed. Exception was: {2}", sa.toString(), uri.toString(), ioe.toString()));
155        // Remember errors to give a friendly user message asking to review proxy configuration
156        errorResources.add(uri.toString());
157        errorMessages.add(ioe.toString());
158    }
159
160    /**
161     * Returns the set of current proxy resources that failed to be retrieved.
162     * @return the set of current proxy resources that failed to be retrieved
163     * @since 6523
164     */
165    public final Set<String> getErrorResources() {
166        return new TreeSet<>(errorResources);
167    }
168
169    /**
170     * Returns the set of current proxy error messages.
171     * @return the set of current proxy error messages
172     * @since 6523
173     */
174    public final Set<String> getErrorMessages() {
175        return new TreeSet<>(errorMessages);
176    }
177
178    /**
179     * Clear the sets of failed resources and error messages.
180     * @since 6523
181     */
182    public final void clearErrors() {
183        errorResources.clear();
184        errorMessages.clear();
185    }
186
187    /**
188     * Determines if proxy errors have occured.
189     * @return {@code true} if errors have occured, {@code false} otherwise.
190     * @since 6523
191     */
192    public final boolean hasErrors() {
193        return !errorResources.isEmpty();
194    }
195
196    @Override
197    public List<Proxy> select(URI uri) {
198        if (uri != null && proxyExceptions.contains(uri.getHost())) {
199            return NO_PROXY_LIST;
200        }
201        switch(proxyPolicy) {
202        case USE_SYSTEM_SETTINGS:
203            if (!JVM_WILL_USE_SYSTEM_PROXIES) {
204                Main.warn(tr("The JVM is not configured to lookup proxies from the system settings. The property ''java.net.useSystemProxies'' was missing at startup time.  Will not use a proxy."));
205                return NO_PROXY_LIST;
206            }
207            // delegate to the former proxy selector
208            return delegate.select(uri);
209        case NO_PROXY:
210            return NO_PROXY_LIST;
211        case USE_HTTP_PROXY:
212            if (httpProxySocketAddress == null)
213                return NO_PROXY_LIST;
214            return Collections.singletonList(new Proxy(Type.HTTP, httpProxySocketAddress));
215        case USE_SOCKS_PROXY:
216            if (socksProxySocketAddress == null)
217                return NO_PROXY_LIST;
218            return Collections.singletonList(new Proxy(Type.SOCKS, socksProxySocketAddress));
219        }
220        // should not happen
221        return null;
222    }
223}