Skip to content

Commit

Permalink
Merge pull request #115 from karakun/proxy-fixes
Browse files Browse the repository at this point in the history
Proxy fixes
  • Loading branch information
hendrikebbers authored Dec 6, 2019
2 parents 6bf1eda + 2049879 commit ac9a5bd
Show file tree
Hide file tree
Showing 17 changed files with 105 additions and 68 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.openwebstart.proxy;

import com.openwebstart.jvm.os.OperationSystem;
import com.openwebstart.proxy.config.ManualPacFileProxyProvider;
import com.openwebstart.proxy.config.ManualConfigBasedProxyProvider;
import com.openwebstart.proxy.direct.DirectProxyProvider;
import com.openwebstart.proxy.firefox.FirefoxProxyProvider;
import com.openwebstart.proxy.mac.MacProxyProvider;
import com.openwebstart.proxy.manual.ManualConfigBasedProxyProvider;
import com.openwebstart.proxy.manual.ManualPacFileProxyProvider;
import com.openwebstart.proxy.windows.WindowsProxyProvider;
import net.sourceforge.jnlp.config.DeploymentConfiguration;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,6 @@ private ProxyProvider createProvider(final DeploymentConfiguration config) {

@Override
public List<Proxy> select(final URI uri) {
if (UrlUtils.isLocalhost(uri)) {
LOG.debug("localhost -> NO_PROXY");
return Collections.singletonList(Proxy.NO_PROXY);
}
if (useDirectAfterError.get()) {
LOG.debug("Falling back to NO_PROXY");
return Collections.singletonList(Proxy.NO_PROXY);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.openwebstart.proxy.util.config;
package com.openwebstart.proxy.config;

import com.openwebstart.proxy.ProxyProvider;
import com.openwebstart.proxy.util.CidrUtils;
import net.adoptopenjdk.icedteaweb.Assert;
import net.adoptopenjdk.icedteaweb.logging.Logger;
import net.adoptopenjdk.icedteaweb.logging.LoggerFactory;
import net.sourceforge.jnlp.util.UrlUtils;

import java.net.InetSocketAddress;
import java.net.Proxy;
Expand All @@ -14,6 +14,9 @@
import java.util.List;
import java.util.Objects;

import static com.openwebstart.proxy.util.CidrUtils.isCidrNotation;
import static com.openwebstart.proxy.util.CidrUtils.isInRange;
import static com.openwebstart.proxy.util.CidrUtils.isIpv4;
import static com.openwebstart.proxy.util.ProxyConstants.FTP_SCHEMA;
import static com.openwebstart.proxy.util.ProxyConstants.HTTPS_SCHEMA;
import static com.openwebstart.proxy.util.ProxyConstants.HTTP_SCHEMA;
Expand All @@ -34,6 +37,10 @@ public ConfigBasedProvider(final ProxyConfiguration proxyConfiguration) {
public List<Proxy> select(final URI uri) {
Assert.requireNonNull(uri, "uri");

if (configuration.isBypassLocal() && UrlUtils.isLocalhost(uri)) {
return Collections.singletonList(Proxy.NO_PROXY);
}

if (isExcluded(uri)) {
LOG.debug("URL {} is excluded", uri);
return Collections.singletonList(Proxy.NO_PROXY);
Expand Down Expand Up @@ -73,39 +80,41 @@ public List<Proxy> select(final URI uri) {
}

private boolean isExcluded(final URI uri) {
return configuration.getBypassList()
.stream()
.filter(exclusion -> {
final String host = uri.getHost();

//google.de
if(Objects.equals(host, exclusion)) {
return true;
}

//*.local
if(exclusion.startsWith("*.")) {
return host.endsWith(exclusion.substring(1));
}


final InetSocketAddress socketAddress = new InetSocketAddress(host, uri.getPort());
final String ipAdress = socketAddress.getAddress().getHostAddress();
//169.254.120.4
if(Objects.equals(ipAdress, exclusion)) {
return true;
}

//169.254/16
if(CidrUtils.isCidrNotation(exclusion)) {
if(CidrUtils.isInRange(exclusion, ipAdress)) {
return true;
}
}

return false;
})
.findAny()
.isPresent();
return configuration.getBypassList().stream()
.anyMatch(exclusion -> isExcluded(uri, exclusion));
}

private boolean isExcluded(URI uri, String exclusion) {
final String host = uri.getHost();

// google.de
if (Objects.equals(host, exclusion)) {
return true;
}

// *.local
if (exclusion.startsWith("*.") && host.endsWith(exclusion.substring(1))) {
return true;
}

// .mozilla
if (exclusion.startsWith(".") && host.endsWith(exclusion)) {
return true;
}


final InetSocketAddress socketAddress = new InetSocketAddress(host, uri.getPort());
final String ipAdress = socketAddress.getAddress().getHostAddress();
// 169.254.120.4
if (Objects.equals(ipAdress, exclusion)) {
return true;
}

// 169.254/16
if (isCidrNotation(exclusion) && isIpv4(ipAdress)) {
return isInRange(exclusion, ipAdress);
}

return false;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.openwebstart.proxy.util.config;
package com.openwebstart.proxy.config;

import com.openwebstart.proxy.util.ProxyUtlis;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.openwebstart.proxy.util.config;
package com.openwebstart.proxy.config;

import java.util.ArrayList;
import java.util.Collections;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@ public interface FirefoxConstants {
String SOCKS_PROPERTY_NAME = "network.proxy.socks";

String SOCKS_PORT_PROPERTY_NAME = "network.proxy.socks_port";

String EXCLUSIONS_PROPERTY_NAME = "network.proxy.no_proxies_on";

String HIJACK_LOCALHOST_PROPERTY_NAME = "network.proxy.allow_hijacking_localhost";
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import com.openwebstart.jvm.os.OperationSystem;
import com.openwebstart.proxy.ProxyProvider;
import com.openwebstart.proxy.config.ConfigBasedProvider;
import com.openwebstart.proxy.config.ProxyConfigurationImpl;
import com.openwebstart.proxy.direct.DirectProxyProvider;
import com.openwebstart.proxy.mac.MacProxyProvider;
import com.openwebstart.proxy.util.config.ConfigBasedProvider;
import com.openwebstart.proxy.util.config.ProxyConfigurationImpl;
import com.openwebstart.proxy.util.pac.PacBasedProxyProvider;
import com.openwebstart.proxy.pac.PacBasedProxyProvider;
import com.openwebstart.proxy.windows.WindowsProxyProvider;
import net.adoptopenjdk.icedteaweb.logging.Logger;
import net.adoptopenjdk.icedteaweb.logging.LoggerFactory;
Expand All @@ -15,11 +15,14 @@
import java.net.Proxy;
import java.net.URI;
import java.net.URL;
import java.util.Arrays;
import java.util.List;

import static com.openwebstart.proxy.firefox.FirefoxConstants.AUTO_CONFIG_URL_PROPERTY_NAME;
import static com.openwebstart.proxy.firefox.FirefoxConstants.EXCLUSIONS_PROPERTY_NAME;
import static com.openwebstart.proxy.firefox.FirefoxConstants.FTP_PORT_PROPERTY_NAME;
import static com.openwebstart.proxy.firefox.FirefoxConstants.FTP_PROPERTY_NAME;
import static com.openwebstart.proxy.firefox.FirefoxConstants.HIJACK_LOCALHOST_PROPERTY_NAME;
import static com.openwebstart.proxy.firefox.FirefoxConstants.HTTP_PORT_PROPERTY_NAME;
import static com.openwebstart.proxy.firefox.FirefoxConstants.HTTP_PROPERTY_NAME;
import static com.openwebstart.proxy.firefox.FirefoxConstants.PROXY_TYPE_PROPERTY_NAME;
Expand All @@ -33,6 +36,7 @@
public class FirefoxProxyProvider implements ProxyProvider {

private static final Logger LOG = LoggerFactory.getLogger(FirefoxProxyProvider.class);

private final ProxyProvider internalProvider;

public FirefoxProxyProvider() throws Exception {
Expand Down Expand Up @@ -69,6 +73,11 @@ private ProxyProvider createForManualConfig(final FirefoxPreferences prefs) {
proxyConfiguration.setFtpPort(prefs.getIntValue(FTP_PORT_PROPERTY_NAME, DEFAULT_PROTOCOL_PORT));
proxyConfiguration.setSocksHost(prefs.getStringValue(SOCKS_PROPERTY_NAME));
proxyConfiguration.setSocksPort(prefs.getIntValue(SOCKS_PORT_PROPERTY_NAME, DEFAULT_PROTOCOL_PORT));
proxyConfiguration.setBypassLocal(!prefs.getBooleanValue(HIJACK_LOCALHOST_PROPERTY_NAME, false));

Arrays.stream(prefs.getStringValue(EXCLUSIONS_PROPERTY_NAME).split("[, ]+"))
.forEach(proxyConfiguration::addToBypassList);

return new ConfigBasedProvider(proxyConfiguration);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import com.openwebstart.proxy.ProxyProvider;
import com.openwebstart.proxy.ProxyProviderType;
import com.openwebstart.proxy.config.ConfigBasedProvider;
import com.openwebstart.proxy.config.ProxyConfigurationImpl;
import com.openwebstart.proxy.pac.PacBasedProxyProvider;
import com.openwebstart.proxy.ui.error.ProxyDialogResult;
import com.openwebstart.proxy.ui.error.UnsupportedFeatureDialog;
import com.openwebstart.proxy.util.config.ConfigBasedProvider;
import com.openwebstart.proxy.util.config.ProxyConfigurationImpl;
import com.openwebstart.proxy.util.pac.PacBasedProxyProvider;
import net.adoptopenjdk.icedteaweb.i18n.Translator;
import net.adoptopenjdk.icedteaweb.logging.Logger;
import net.adoptopenjdk.icedteaweb.logging.LoggerFactory;
Expand All @@ -15,12 +15,17 @@
import java.io.IOException;
import java.net.Proxy;
import java.net.URI;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutionException;

public class MacProxyProvider implements ProxyProvider {

private final static Logger LOG = LoggerFactory.getLogger(MacProxyProvider.class);
private static final Logger LOG = LoggerFactory.getLogger(MacProxyProvider.class);

private static final Set<String> LOCALHOST_INDICATORS = new HashSet<>(Arrays.asList("localhost", "*.local"));

private final ProxyProvider internalProvider;

Expand Down Expand Up @@ -71,10 +76,15 @@ public MacProxyProvider() throws IOException, InterruptedException, ExecutionExc
proxyConfiguration.setSocksPort(proxySettings.getSocksPort());
}
proxySettings.getExceptionList().forEach(proxyConfiguration::addToBypassList);
proxyConfiguration.setBypassLocal(bypassLocalhost(proxySettings));
internalProvider = new ConfigBasedProvider(proxyConfiguration);
}
}

private boolean bypassLocalhost(MacProxySettings proxySettings) {
return proxySettings.getExceptionList().stream().anyMatch(LOCALHOST_INDICATORS::contains);
}

private void showUnsupportedFeatureDialog(final String featureKey) {
final String featureName = Translator.getInstance().translate(featureKey);
final ProxyDialogResult result = new UnsupportedFeatureDialog(ProxyProviderType.OPERATION_SYSTEM, featureName).showAndWait();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.openwebstart.proxy.config;
package com.openwebstart.proxy.manual;

import com.openwebstart.proxy.util.config.ConfigBasedProvider;
import com.openwebstart.proxy.util.config.ProxyConfiguration;
import com.openwebstart.proxy.util.config.ProxyConfigurationImpl;
import com.openwebstart.proxy.config.ConfigBasedProvider;
import com.openwebstart.proxy.config.ProxyConfiguration;
import com.openwebstart.proxy.config.ProxyConfigurationImpl;
import net.adoptopenjdk.icedteaweb.Assert;
import net.adoptopenjdk.icedteaweb.StringUtils;
import net.sourceforge.jnlp.config.DeploymentConfiguration;
Expand Down Expand Up @@ -42,6 +42,7 @@ private static ProxyConfiguration createConfiguration(final DeploymentConfigurat
result.setFtpPort(toPort(config.getProperty(KEY_PROXY_FTP_PORT)));
result.setSocksHost(toHost(config.getProperty(KEY_PROXY_SOCKS4_HOST)));
result.setSocksPort(toPort(config.getProperty(KEY_PROXY_SOCKS4_PORT)));
result.setBypassLocal(Boolean.parseBoolean(config.getProperty(KEY_PROXY_BYPASS_LOCAL)));

config.getPropertyAsList(KEY_PROXY_BYPASS_LIST, ',').stream()
.filter(host -> !StringUtils.isBlank(host))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.openwebstart.proxy.config;
package com.openwebstart.proxy.manual;

import com.openwebstart.proxy.util.pac.PacBasedProxyProvider;
import com.openwebstart.proxy.pac.PacBasedProxyProvider;
import net.adoptopenjdk.icedteaweb.Assert;
import net.sourceforge.jnlp.config.ConfigurationConstants;
import net.sourceforge.jnlp.config.DeploymentConfiguration;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.openwebstart.proxy.util.pac;
package com.openwebstart.proxy.pac;

import com.openwebstart.proxy.ProxyProvider;
import net.adoptopenjdk.icedteaweb.Assert;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.openwebstart.proxy.util.pac;
package com.openwebstart.proxy.pac;

public interface PacConstants {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
exception statement from your version.
*/

package com.openwebstart.proxy.util.pac;
package com.openwebstart.proxy.pac;

import net.adoptopenjdk.icedteaweb.IcedTeaWebConstants;
import net.adoptopenjdk.icedteaweb.logging.Logger;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.openwebstart.proxy.util.pac;
package com.openwebstart.proxy.pac;

import net.adoptopenjdk.icedteaweb.logging.Logger;
import net.adoptopenjdk.icedteaweb.logging.LoggerFactory;
Expand All @@ -9,9 +9,9 @@
import java.util.List;
import java.util.Optional;

import static com.openwebstart.proxy.util.pac.PacConstants.DIRECT;
import static com.openwebstart.proxy.util.pac.PacConstants.PROXY;
import static com.openwebstart.proxy.util.pac.PacConstants.SOCKS;
import static com.openwebstart.proxy.pac.PacConstants.DIRECT;
import static com.openwebstart.proxy.pac.PacConstants.PROXY;
import static com.openwebstart.proxy.pac.PacConstants.SOCKS;

//TODO: Class should be refactored
class PacUtils {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.openwebstart.jvm.ui.dialogs.DialogWithResult;
import com.openwebstart.proxy.ProxyProviderType;
import net.adoptopenjdk.icedteaweb.i18n.Translator;
import net.sourceforge.jnlp.proxy.ProxyType;

public class UnsupportedFeatureDialog extends DialogWithResult<ProxyDialogResult> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ public static boolean isCidrNotation(final String value) {
return cidrMatcher.matches();
}

public static boolean isIpv4(final String value) {
final Matcher ipMatcher = ipv4Pattern.matcher(value);
return ipMatcher.matches();
}

public static boolean isInRange(final String cidrv4Notation, final String ipv4Notation) {
final Matcher cidrMatcher = cidrv4Pattern.matcher(cidrv4Notation);
if (cidrMatcher.matches()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.openwebstart.proxy.windows;

import com.openwebstart.proxy.ProxyProvider;
import com.openwebstart.proxy.config.ConfigBasedProvider;
import com.openwebstart.proxy.config.ProxyConfigurationImpl;
import com.openwebstart.proxy.direct.DirectProxyProvider;
import com.openwebstart.proxy.util.config.ConfigBasedProvider;
import com.openwebstart.proxy.util.config.ProxyConfigurationImpl;
import com.openwebstart.proxy.util.pac.PacBasedProxyProvider;
import com.openwebstart.proxy.pac.PacBasedProxyProvider;

import java.net.Proxy;
import java.net.URI;
Expand All @@ -24,6 +24,9 @@

public class WindowsProxyProvider implements ProxyProvider {

// see https://blogs.msdn.microsoft.com/askie/2015/10/12/how-to-configure-proxy-settings-for-ie10-and-ie11-as-iem-is-not-available/
private static final String EXCLUDE_LOCALHOST_MAGIC_VALUE = "<local>";

private final ProxyProvider internalProvider;

public WindowsProxyProvider() throws Exception {
Expand Down Expand Up @@ -78,6 +81,7 @@ public WindowsProxyProvider() throws Exception {
if (overrideHostsValue != null) {
Arrays.asList(overrideHostsValue.getValue().split(Pattern.quote(";"))).forEach(p -> proxyConfiguration.addToBypassList(p));
}
proxyConfiguration.setBypassLocal(proxyConfiguration.getBypassList().contains(EXCLUDE_LOCALHOST_MAGIC_VALUE));
internalProvider = new ConfigBasedProvider(proxyConfiguration);
} else {
//TODO: is this correct?
Expand Down

0 comments on commit ac9a5bd

Please sign in to comment.