From ba7115e5e1e6974e229188cd747e4e21f233185f Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Fri, 13 Jan 2017 13:38:56 +0100 Subject: Minor tweaks --- .../proxy/search/wpad/WpadProxySearchStrategy.java | 4 +- src/main/java/com/btr/proxy/util/ProxyUtil.java | 76 +++++++++++++--------- 2 files changed, 49 insertions(+), 31 deletions(-) diff --git a/src/main/java/com/btr/proxy/search/wpad/WpadProxySearchStrategy.java b/src/main/java/com/btr/proxy/search/wpad/WpadProxySearchStrategy.java index 249a889..e559d62 100644 --- a/src/main/java/com/btr/proxy/search/wpad/WpadProxySearchStrategy.java +++ b/src/main/java/com/btr/proxy/search/wpad/WpadProxySearchStrategy.java @@ -111,9 +111,9 @@ public class WpadProxySearchStrategy implements ProxySearchStrategy { try { fqdn = InetAddress.getLocalHost().getCanonicalHostName(); } catch (UnknownHostException e2) { - // No successfull, try other methods + // Not successful, try other methods } - if (fqdn != null && (index = fqdn.indexOf('.')) != -1) { + if (fqdn != null && !fqdn.matches("\\d+\\.\\d+\\.\\d+\\.\\d+") && (index = fqdn.indexOf('.')) != -1) { ret = tryUrl("http://wpad." + fqdn.substring(index + 1) + "/wpad.dat"); if (ret != null) return ret; diff --git a/src/main/java/com/btr/proxy/util/ProxyUtil.java b/src/main/java/com/btr/proxy/util/ProxyUtil.java index aad9293..6bbaa30 100644 --- a/src/main/java/com/btr/proxy/util/ProxyUtil.java +++ b/src/main/java/com/btr/proxy/util/ProxyUtil.java @@ -4,6 +4,7 @@ import java.net.Proxy; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.concurrent.atomic.AtomicReference; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -11,50 +12,54 @@ import com.btr.proxy.selector.fixed.FixedProxySelector; import com.btr.proxy.selector.pac.PacProxySelector; import com.btr.proxy.selector.pac.PacScriptSource; import com.btr.proxy.selector.pac.UrlPacScriptSource; +import com.btr.proxy.util.Logger.LogLevel; /***************************************************************************** * Small helper class for some common utility methods. - * + * * @author Bernd Rosstauscher (proxyvole@rosstauscher.de) Copyright 2009 ****************************************************************************/ public class ProxyUtil { - + public static final int DEFAULT_PROXY_PORT = 80; - + private static List noProxyList; - private static Pattern pattern = Pattern.compile("\\w*?:?/*([^:/]+):?(\\d*)/?"); - + private static Pattern pattern = Pattern.compile("\\w*?:?/*([^:/]+):?(\\d*)/?"); + /************************************************************************* * Parse host and port out of a proxy variable. + * * @param proxyVar the proxy string. example: http://192.168.10.9:8080/ * @return a FixedProxySelector using this settings, null on parse error. ************************************************************************/ - + public static FixedProxySelector parseProxySettings(String proxyVar) { if (proxyVar == null || proxyVar.trim().length() == 0) { return null; } Matcher matcher = pattern.matcher(proxyVar); if (matcher.matches()) { - String host = matcher.group(1); - int port; - if (!"".equals(matcher.group(2))) { - port = Integer.parseInt(matcher.group(2)); - } else { - port = DEFAULT_PROXY_PORT; - } - return new FixedProxySelector(host.trim(), port); + String host = matcher.group(1); + int port; + if (!"".equals(matcher.group(2))) { + port = Integer.parseInt(matcher.group(2)); + } else { + port = DEFAULT_PROXY_PORT; + } + return new FixedProxySelector(host.trim(), port); } else { - return null; + return null; } } - + /************************************************************************* - * Gets an unmodifiable proxy list that will have as it's only entry an DIRECT proxy. + * Gets an unmodifiable proxy list that will have as it's only entry an + * DIRECT proxy. + * * @return a list with a DIRECT proxy in it. ************************************************************************/ - + public static synchronized List noProxyList() { if (noProxyList == null) { ArrayList list = new ArrayList(1); @@ -63,22 +68,35 @@ public class ProxyUtil { } return noProxyList; } - + /************************************************************************* * Build a PAC proxy selector for the given URL. + * * @param url to fetch the PAC script from. - * @return a PacProxySelector or null if it is not possible to build a working - * selector. + * @return a PacProxySelector or null if it is not possible to build a + * working + * selector. ************************************************************************/ - - public static PacProxySelector buildPacSelectorForUrl(String url) { - PacProxySelector result = null; - PacScriptSource pacSource = new UrlPacScriptSource(url); - if (pacSource.isScriptValid()) { - result = new PacProxySelector(pacSource); + + public static PacProxySelector buildPacSelectorForUrl(final String url) { + Logger.log(ProxyUtil.class, LogLevel.TRACE, "Fetching PAC script from {0}", url); + final AtomicReference result = new AtomicReference<>(); + Thread t = new Thread() { + @Override + public void run() { + PacScriptSource pacSource = new UrlPacScriptSource(url); + if (pacSource.isScriptValid()) { + result.set(new PacProxySelector(pacSource)); + } + } + }; + t.start(); + try { + t.join(2500); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); } - return result; + return result.get(); } - } -- cgit v1.2.3-55-g7522