Changeset 18663 in josm for trunk


Ignore:
Timestamp:
2023-02-15T16:39:22+01:00 (22 months ago)
Author:
taylor.smock
Message:

Fix #22497: Add setting to only proxy specific hosts (patch by TrickyFoxy)

Location:
trunk/src/org/openstreetmap/josm
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/preferences/server/ProxyPreferencesPanel.java

    r18389 r18663  
    2020import java.util.Map;
    2121import java.util.Optional;
     22import java.util.ArrayList;
    2223
    2324import javax.swing.BorderFactory;
     
    3233import org.openstreetmap.josm.gui.widgets.JosmTextField;
    3334import org.openstreetmap.josm.gui.widgets.VerticallyScrollablePanel;
     35import org.openstreetmap.josm.gui.widgets.EditableList;
    3436import org.openstreetmap.josm.io.DefaultProxySelector;
    3537import org.openstreetmap.josm.io.ProxyPolicy;
     
    4749public class ProxyPreferencesPanel extends VerticallyScrollablePanel {
    4850
     51    private static final long serialVersionUID = -2479852374189976764L;
     52
    4953    static final class AutoSizePanel extends JPanel {
     54        private static final long serialVersionUID = 7469560761925020366L;
     55
    5056        AutoSizePanel() {
    5157            super(new GridBagLayout());
     
    6571    private final JosmTextField tfProxyHttpUser = new JosmTextField(20);
    6672    private final JosmPasswordField tfProxyHttpPassword = new JosmPasswordField(20);
     73    private final EditableList tfExceptionHosts = new EditableList(tr("No proxy for"));
     74    private final EditableList tfIncludeHosts = new EditableList(tr("Proxy only for"));
    6775
    6876    private JPanel pnlHttpProxyConfigurationPanel;
    6977    private JPanel pnlSocksProxyConfigurationPanel;
     78    private JPanel pnlIncludeOrExcludeHostsPanel;
    7079
    7180    /**
     
    168177        pnl.add(tfProxySocksPort, gc);
    169178        tfProxySocksPort.setMinimumSize(tfProxySocksPort.getPreferredSize());
     179
     180        // add an extra spacer, otherwise the layout is broken
     181        gc.gridy = 2;
     182        gc.gridx = 0;
     183        gc.gridwidth = 2;
     184        gc.fill = GridBagConstraints.BOTH;
     185        gc.weightx = 1.0;
     186        gc.weighty = 1.0;
     187        pnl.add(new JPanel(), gc);
     188        return pnl;
     189    }
     190
     191    protected final JPanel buildExceptionIncludesHostsProxyConfigurationPanel() {
     192        JPanel pnl = new AutoSizePanel();
     193        GridBagConstraints gc = new GridBagConstraints();
     194        gc.anchor = GridBagConstraints.LINE_START;
     195        gc.insets = new Insets(5, 5, 0, 0);
     196        gc.weightx = 0.0;
     197        pnl.add(new JLabel(tr("No proxy for (hosts):")), gc);
     198
     199        gc.gridx = 1;
     200        gc.weightx = 0.0;
     201        gc.fill = GridBagConstraints.NONE;
     202        pnl.add(new JLabel(tr("Proxy only for (hosts):")), gc);
     203
     204        gc.gridy = 1;
     205        gc.gridx = 0;
     206        gc.weightx = 1.0;
     207        gc.fill = HORIZONTAL;
     208        tfExceptionHosts.setMinimumSize(tfExceptionHosts.getPreferredSize());
     209        pnl.add(tfExceptionHosts, gc);
     210
     211        gc.gridx = 1;
     212        gc.weightx = 1.0;
     213        gc.fill = HORIZONTAL;
     214        tfIncludeHosts.setMinimumSize(tfIncludeHosts.getPreferredSize());
     215        pnl.add(tfIncludeHosts, gc);
    170216
    171217        // add an extra spacer, otherwise the layout is broken
     
    215261        pnlSocksProxyConfigurationPanel = buildSocksProxyConfigurationPanel();
    216262        pnl.add(pnlSocksProxyConfigurationPanel, GBC.eop().fill(HORIZONTAL));
     263
     264        pnl.add(Box.createVerticalGlue(), GBC.eol().fill());
     265
     266        // the panel with the exception and includes hosts
     267        pnlIncludeOrExcludeHostsPanel = buildExceptionIncludesHostsProxyConfigurationPanel();
     268        pnl.add(pnlIncludeOrExcludeHostsPanel, GBC.eop().fill(HORIZONTAL));
    217269
    218270        pnl.add(Box.createVerticalGlue(), GBC.eol().fill());
     
    239291        tfProxySocksHost.setText(pref.get(DefaultProxySelector.PROXY_SOCKS_HOST, ""));
    240292        tfProxySocksPort.setText(pref.get(DefaultProxySelector.PROXY_SOCKS_PORT, ""));
     293        tfExceptionHosts.setItems(pref.getList(DefaultProxySelector.PROXY_EXCEPTIONS, new ArrayList<>()));
     294        tfIncludeHosts.setItems(pref.getList(DefaultProxySelector.PROXY_INCLUDES, new ArrayList<>()));
    241295
    242296        if (pp == ProxyPolicy.USE_SYSTEM_SETTINGS && !DefaultProxySelector.willJvmRetrieveSystemProxies()) {
     
    278332
    279333        rbProxyPolicy.get(ProxyPolicy.USE_SYSTEM_SETTINGS).setEnabled(DefaultProxySelector.willJvmRetrieveSystemProxies());
     334
     335        boolean proxyEnabled = !rbProxyPolicy.get(ProxyPolicy.NO_PROXY).isSelected();
     336        for (Component c : pnlIncludeOrExcludeHostsPanel.getComponents()) {
     337            c.setEnabled(proxyEnabled);
     338        }
    280339    }
    281340
     
    312371        pref.put(DefaultProxySelector.PROXY_SOCKS_HOST, tfProxySocksHost.getText());
    313372        pref.put(DefaultProxySelector.PROXY_SOCKS_PORT, tfProxySocksPort.getText());
     373        pref.putList(DefaultProxySelector.PROXY_EXCEPTIONS, tfExceptionHosts.getItems());
     374        pref.putList(DefaultProxySelector.PROXY_INCLUDES, tfIncludeHosts.getItems());
     375
    314376
    315377        // update the proxy selector
  • trunk/src/org/openstreetmap/josm/io/DefaultProxySelector.java

    r18211 r18663  
    4444    /** Property key for proxy exceptions list */
    4545    public static final String PROXY_EXCEPTIONS = "proxy.exceptions";
     46    /**
     47     * Property key for hosts that should be proxied (if this is set, only specified hosts should be proxied)
     48     * @since 18663
     49     */
     50    public static final String PROXY_INCLUDES = "proxy.includes.hosts";
    4651
    4752    private static final List<Proxy> NO_PROXY_LIST = Collections.singletonList(Proxy.NO_PROXY);
     
    8792    private final Set<String> errorMessages = new HashSet<>();
    8893    private Set<String> proxyExceptions;
     94    private Set<String> proxyIncludes;
    8995
    9096    /**
     
    165171                    Arrays.asList("localhost", IPV4_LOOPBACK, IPV6_LOOPBACK))
    166172        );
     173        proxyIncludes = new HashSet<>(
     174            Config.getPref().getList(PROXY_INCLUDES,
     175                    Collections.emptyList())
     176        );
    167177    }
    168178
     
    215225    @Override
    216226    public List<Proxy> select(URI uri) {
    217         if (uri != null && proxyExceptions.contains(uri.getHost())) {
     227        // This is specified in ProxySelector#select, "@throws IllegalArgumentException if the argument is null"
     228        if (uri == null) {
     229            throw new IllegalArgumentException("URI cannot be null");
     230        }
     231        if (proxyExceptions.contains(uri.getHost())) {
    218232            return NO_PROXY_LIST;
    219233        }
    220         switch(proxyPolicy) {
     234        if (!proxyIncludes.isEmpty() && !proxyIncludes.contains(uri.getHost())) {
     235            return NO_PROXY_LIST;
     236        }
     237        switch (proxyPolicy) {
    221238        case USE_SYSTEM_SETTINGS:
    222239            if (!jvmWillUseSystemProxies) {
    223                 Logging.warn(tr("The JVM is not configured to lookup proxies from the system settings. "+
     240                Logging.warn(tr("The JVM is not configured to lookup proxies from the system settings. " +
    224241                        "The property ''java.net.useSystemProxies'' was missing at startup time.  Will not use a proxy."));
    225242                return NO_PROXY_LIST;
     
    239256        }
    240257        // should not happen
    241         return null;
     258        return Collections.emptyList();
    242259    }
    243260}
Note: See TracChangeset for help on using the changeset viewer.