package com.btr.proxy.util; import java.net.Proxy; import java.net.ProxySelector; 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; 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_HTTP_PROXY_PORT = 80; public static final int DEFAULT_SOCKS_PROXY_PORT = 1080; private static List noProxyList; private static Pattern pattern = Pattern.compile("^\\s*(\\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 ProxySelector parseProxySettings(String proxyVar) { return parseProxySettings(proxyVar, Proxy.Type.HTTP, 0); } public static ProxySelector parseProxySettings(String proxyVar, Proxy.Type fallback, int fallbackPort) { if (proxyVar == null || proxyVar.trim().length() == 0) { return null; } Matcher matcher = pattern.matcher(proxyVar); if (matcher.matches()) { String host = matcher.group(2); int port = -1; if (!"".equals(matcher.group(3))) { port = Integer.parseInt(matcher.group(3)); } String stype = matcher.group(1).toLowerCase(); Proxy.Type type; if (stype.isEmpty()) { type = fallback; } else if (stype.startsWith("socks")) { type = Proxy.Type.SOCKS; } else { type = Proxy.Type.HTTP; } if (port == -1 && fallbackPort > 0) { port = fallbackPort; } if (port == -1) { if (type == Proxy.Type.HTTP) { port = DEFAULT_HTTP_PROXY_PORT; } else if (type == Proxy.Type.SOCKS) { port = DEFAULT_SOCKS_PROXY_PORT; } } return new FixedProxySelector(type, host.trim(), port); } else { return null; } } /************************************************************************* * 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); list.add(Proxy.NO_PROXY); noProxyList = Collections.unmodifiableList(list); } 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. ************************************************************************/ 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.get(); } }