Ticket #22497: Include_proxy_hosts.patch

File Include_proxy_hosts.patch, 9.8 KB (added by TrickyFoxy, 2 years ago)

Patch

  • src/org/openstreetmap/josm/gui/preferences/server/ProxyPreferencesPanel.java

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
    diff --git a/src/org/openstreetmap/josm/gui/preferences/server/ProxyPreferencesPanel.java b/src/org/openstreetmap/josm/gui/preferences/server/ProxyPreferencesPanel.java
    a b  
    1919import java.util.EnumMap;
    2020import java.util.Map;
    2121import java.util.Optional;
     22import java.util.ArrayList;
    2223
    2324import javax.swing.BorderFactory;
    2425import javax.swing.Box;
     
    3132import org.openstreetmap.josm.gui.widgets.JosmPasswordField;
    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;
    3638import org.openstreetmap.josm.io.auth.CredentialsAgent;
     
    6466    private final JosmTextField tfProxySocksPort = new JosmTextField(5);
    6567    private final JosmTextField tfProxyHttpUser = new JosmTextField(20);
    6668    private final JosmPasswordField tfProxyHttpPassword = new JosmPasswordField(20);
     69    private final EditableList tfExceptionHosts = new EditableList(tr("No proxy for"));
     70    private final EditableList tfIncludeHosts = new EditableList(tr("Proxy only for"));
    6771
    6872    private JPanel pnlHttpProxyConfigurationPanel;
    6973    private JPanel pnlSocksProxyConfigurationPanel;
     74    private JPanel pnlExceptionIncludesHostsProxyConfigurationPanel;
    7075
    7176    /**
    7277     * Builds the panel for the HTTP proxy configuration
     
    179184        return pnl;
    180185    }
    181186
     187    protected final JPanel buildExceptionIncludesHostsProxyConfigurationPanel() {
     188        JPanel pnl = new AutoSizePanel();
     189        GridBagConstraints gc = new GridBagConstraints();
     190        gc.anchor = GridBagConstraints.LINE_START;
     191        gc.insets = new Insets(5, 5, 0, 0);
     192        gc.weightx = 0.0;
     193        pnl.add(new JLabel(tr("No proxy for (hosts):")), gc);
     194
     195        gc.gridx = 1;
     196        gc.weightx = 0.0;
     197        gc.fill = GridBagConstraints.NONE;
     198        pnl.add(new JLabel(tr("Proxy only for (hosts):")), gc);
     199
     200        gc.gridy = 1;
     201        gc.gridx = 0;
     202        gc.weightx = 1.0;
     203        gc.fill = HORIZONTAL;
     204        tfExceptionHosts.setMinimumSize(tfExceptionHosts.getPreferredSize());
     205        pnl.add(tfExceptionHosts, gc);
     206
     207        gc.gridx = 1;
     208        gc.weightx = 1.0;
     209        gc.fill = HORIZONTAL;
     210        tfIncludeHosts.setMinimumSize(tfIncludeHosts.getPreferredSize());
     211        pnl.add(tfIncludeHosts, gc);
     212
     213        // add an extra spacer, otherwise the layout is broken
     214        gc.gridy = 2;
     215        gc.gridx = 0;
     216        gc.gridwidth = 2;
     217        gc.fill = GridBagConstraints.BOTH;
     218        gc.weightx = 1.0;
     219        gc.weighty = 1.0;
     220        pnl.add(new JPanel(), gc);
     221        return pnl;
     222    }
     223
    182224    protected final JPanel buildProxySettingsPanel() {
    183225        JPanel pnl = new JPanel(new GridBagLayout());
    184226
     
    217259
    218260        pnl.add(Box.createVerticalGlue(), GBC.eol().fill());
    219261
     262        // the panel with the exception and includes hosts
     263        pnlExceptionIncludesHostsProxyConfigurationPanel = buildExceptionIncludesHostsProxyConfigurationPanel();
     264        pnl.add(pnlExceptionIncludesHostsProxyConfigurationPanel, GBC.eop().fill(HORIZONTAL));
     265
     266        pnl.add(Box.createVerticalGlue(), GBC.eol().fill());
     267
    220268        return pnl;
    221269    }
    222270
     
    238286        tfProxyHttpPort.setText(pref.get(DefaultProxySelector.PROXY_HTTP_PORT, ""));
    239287        tfProxySocksHost.setText(pref.get(DefaultProxySelector.PROXY_SOCKS_HOST, ""));
    240288        tfProxySocksPort.setText(pref.get(DefaultProxySelector.PROXY_SOCKS_PORT, ""));
     289        tfExceptionHosts.setItems(pref.getList(DefaultProxySelector.PROXY_EXCEPTIONS, new ArrayList<String>()));
     290        tfIncludeHosts.setItems(pref.getList(DefaultProxySelector.PROXY_INCLUDES, new ArrayList<String>()));
    241291
    242292        if (pp == ProxyPolicy.USE_SYSTEM_SETTINGS && !DefaultProxySelector.willJvmRetrieveSystemProxies()) {
    243293            Logging.warn(tr("JOSM is configured to use proxies from the system setting, but the JVM is not configured to retrieve them. " +
     
    277327        }
    278328
    279329        rbProxyPolicy.get(ProxyPolicy.USE_SYSTEM_SETTINGS).setEnabled(DefaultProxySelector.willJvmRetrieveSystemProxies());
     330
     331        boolean proxyEnabled = !rbProxyPolicy.get(ProxyPolicy.NO_PROXY).isSelected();
     332        for (Component c : pnlExceptionIncludesHostsProxyConfigurationPanel.getComponents()) {
     333            c.setEnabled(proxyEnabled);
     334        }
    280335    }
    281336
    282337    class ProxyPolicyChangeListener implements ItemListener {
     
    311366        pref.put(DefaultProxySelector.PROXY_HTTP_PORT, tfProxyHttpPort.getText());
    312367        pref.put(DefaultProxySelector.PROXY_SOCKS_HOST, tfProxySocksHost.getText());
    313368        pref.put(DefaultProxySelector.PROXY_SOCKS_PORT, tfProxySocksPort.getText());
     369        pref.putList(DefaultProxySelector.PROXY_EXCEPTIONS, tfExceptionHosts.getItems());
     370        pref.putList(DefaultProxySelector.PROXY_INCLUDES, tfIncludeHosts.getItems());
     371
    314372
    315373        // update the proxy selector
    316374        ProxySelector selector = ProxySelector.getDefault();
  • src/org/openstreetmap/josm/io/DefaultProxySelector.java

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
    diff --git a/src/org/openstreetmap/josm/io/DefaultProxySelector.java b/src/org/openstreetmap/josm/io/DefaultProxySelector.java
    a b  
    4343    public static final String PROXY_PASS = "proxy.pass";
    4444    /** Property key for proxy exceptions list */
    4545    public static final String PROXY_EXCEPTIONS = "proxy.exceptions";
     46    /** Property key for proxy include list */
     47    public static final String PROXY_INCLUDES = "proxy.includes";
    4648
    4749    private static final List<Proxy> NO_PROXY_LIST = Collections.singletonList(Proxy.NO_PROXY);
    4850
     
    8688    private final Set<String> errorResources = new HashSet<>();
    8789    private final Set<String> errorMessages = new HashSet<>();
    8890    private Set<String> proxyExceptions;
     91    private Set<String> proxyIncludes;
    8992
    9093    /**
    9194     * A typical example is:
     
    161164            }
    162165        }
    163166        proxyExceptions = new HashSet<>(
    164             Config.getPref().getList(PROXY_EXCEPTIONS,
    165                     Arrays.asList("localhost", IPV4_LOOPBACK, IPV6_LOOPBACK))
     167                Config.getPref().getList(PROXY_EXCEPTIONS,
     168                        Arrays.asList("localhost", IPV4_LOOPBACK, IPV6_LOOPBACK))
    166169        );
     170        proxyIncludes = new HashSet<>(
     171                Config.getPref().getList(PROXY_INCLUDES,
     172                        Arrays.asList())
     173        );
    167174    }
    168175
    169176    @Override
     
    217224        if (uri != null && proxyExceptions.contains(uri.getHost())) {
    218225            return NO_PROXY_LIST;
    219226        }
    220         switch(proxyPolicy) {
    221         case USE_SYSTEM_SETTINGS:
    222             if (!jvmWillUseSystemProxies) {
    223                 Logging.warn(tr("The JVM is not configured to lookup proxies from the system settings. "+
    224                         "The property ''java.net.useSystemProxies'' was missing at startup time.  Will not use a proxy."));
    225                 return NO_PROXY_LIST;
    226             }
    227             // delegate to the former proxy selector
    228             return delegate.select(uri);
    229         case NO_PROXY:
    230             return NO_PROXY_LIST;
    231         case USE_HTTP_PROXY:
    232             if (httpProxySocketAddress == null)
    233                 return NO_PROXY_LIST;
    234             return Collections.singletonList(new Proxy(Type.HTTP, httpProxySocketAddress));
    235         case USE_SOCKS_PROXY:
    236             if (socksProxySocketAddress == null)
    237                 return NO_PROXY_LIST;
    238             return Collections.singletonList(new Proxy(Type.SOCKS, socksProxySocketAddress));
     227        switch (proxyPolicy) {
     228            case USE_SYSTEM_SETTINGS:
     229                if (!jvmWillUseSystemProxies) {
     230                    Logging.warn(tr("The JVM is not configured to lookup proxies from the system settings. " +
     231                            "The property ''java.net.useSystemProxies'' was missing at startup time.  Will not use a proxy."));
     232                    return NO_PROXY_LIST;
     233                }
     234                if (!proxyIncludes.isEmpty() && !proxyIncludes.contains(uri.getHost())){
     235                    return NO_PROXY_LIST;
     236                }
     237                // delegate to the former proxy selector
     238                return delegate.select(uri);
     239            case NO_PROXY:
     240                return NO_PROXY_LIST;
     241            case USE_HTTP_PROXY:
     242                if (httpProxySocketAddress == null)
     243                    return NO_PROXY_LIST;
     244                if (!proxyIncludes.isEmpty() && !proxyIncludes.contains(uri.getHost())){
     245                    return NO_PROXY_LIST;
     246                }
     247                return Collections.singletonList(new Proxy(Type.HTTP, httpProxySocketAddress));
     248            case USE_SOCKS_PROXY:
     249                if (socksProxySocketAddress == null)
     250                    return NO_PROXY_LIST;
     251                if (!proxyIncludes.isEmpty() && !proxyIncludes.contains(uri.getHost())){
     252                    return NO_PROXY_LIST;
     253                }
     254                return Collections.singletonList(new Proxy(Type.SOCKS, socksProxySocketAddress));
    239255        }
    240256        // should not happen
    241257        return null;