From 553201c90cd80441d9311adc90a639f252a44092 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Wed, 2 Aug 2017 15:46:30 +0200 Subject: Rewrite proxy string parsing, regex was a bit wonky --- src/main/java/com/btr/proxy/util/ProxyUtil.java | 56 +++++++++++++++---------- 1 file changed, 33 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/btr/proxy/util/ProxyUtil.java b/src/main/java/com/btr/proxy/util/ProxyUtil.java index bd3680d..97f2af2 100644 --- a/src/main/java/com/btr/proxy/util/ProxyUtil.java +++ b/src/main/java/com/btr/proxy/util/ProxyUtil.java @@ -6,8 +6,6 @@ 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; @@ -28,7 +26,6 @@ public class ProxyUtil { 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. @@ -45,15 +42,18 @@ public class ProxyUtil { 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; + + Proxy.Type type; + int port = -1; + String host; + + int protoIdx = proxyVar.indexOf(":/"); + if (protoIdx == -1) { + type = fallback; + } else { + String stype = proxyVar.substring(0, protoIdx); + proxyVar = proxyVar.substring(protoIdx + 2); + proxyVar = proxyVar.replaceAll("(^/+|/+$)", ""); if (stype.isEmpty()) { type = fallback; } else if (stype.startsWith("socks")) { @@ -61,20 +61,30 @@ public class ProxyUtil { } else { type = Proxy.Type.HTTP; } - if (port == -1 && fallbackPort > 0) { - port = fallbackPort; + } + + int portIdx = proxyVar.lastIndexOf(':'); + if (portIdx == -1) { + host = proxyVar; + } else { + String portStr = proxyVar.substring(portIdx + 1).replaceAll("[^0-9]", ""); + if (!portStr.isEmpty()) { + port = Integer.parseInt(portStr); } - if (port == -1) { - if (type == Proxy.Type.HTTP) { - port = DEFAULT_HTTP_PROXY_PORT; - } else if (type == Proxy.Type.SOCKS) { - port = DEFAULT_SOCKS_PROXY_PORT; - } + host = proxyVar.substring(0, portIdx); + } + + 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; } + return new FixedProxySelector(type, host.trim(), port); } /************************************************************************* -- cgit v1.2.3-55-g7522