summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/main/java/com/btr/proxy/util/ProxyUtil.java56
1 files 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<Proxy> 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);
}
/*************************************************************************