From df53b12c42252be8ffe9aa1eb1a9adf3002f1545 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Tue, 1 Aug 2017 15:45:48 +0200 Subject: Make proxy type detection more intelligent, decouple from target protocol --- src/main/java/com/btr/proxy/util/ProxyUtil.java | 42 +++++++++++++++++++------ 1 file changed, 33 insertions(+), 9 deletions(-) (limited to 'src/main/java/com/btr/proxy/util/ProxyUtil.java') diff --git a/src/main/java/com/btr/proxy/util/ProxyUtil.java b/src/main/java/com/btr/proxy/util/ProxyUtil.java index 6bbaa30..bd3680d 100644 --- a/src/main/java/com/btr/proxy/util/ProxyUtil.java +++ b/src/main/java/com/btr/proxy/util/ProxyUtil.java @@ -1,6 +1,7 @@ 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; @@ -22,10 +23,12 @@ import com.btr.proxy.util.Logger.LogLevel; public class ProxyUtil { - public static final int DEFAULT_PROXY_PORT = 80; + 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("\\w*?:?/*([^:/]+):?(\\d*)/?"); + private static Pattern pattern = Pattern.compile("^\\s*(\\w*?):?/*([^:/]+):?(\\d*)/?"); /************************************************************************* * Parse host and port out of a proxy variable. @@ -34,20 +37,41 @@ public class ProxyUtil { * @return a FixedProxySelector using this settings, null on parse error. ************************************************************************/ - public static FixedProxySelector parseProxySettings(String proxyVar) { + 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(1); - int port; - if (!"".equals(matcher.group(2))) { - port = Integer.parseInt(matcher.group(2)); + 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 { - port = DEFAULT_PROXY_PORT; + 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(host.trim(), port); + return new FixedProxySelector(type, host.trim(), port); } else { return null; } -- cgit v1.2.3-55-g7522