diff options
8 files changed, 33 insertions, 20 deletions
diff --git a/src/main/java/com/btr/proxy/search/browser/firefox/FirefoxProxySearchStrategy.java b/src/main/java/com/btr/proxy/search/browser/firefox/FirefoxProxySearchStrategy.java index 228bc8c..77868e2 100644 --- a/src/main/java/com/btr/proxy/search/browser/firefox/FirefoxProxySearchStrategy.java +++ b/src/main/java/com/btr/proxy/search/browser/firefox/FirefoxProxySearchStrategy.java @@ -13,6 +13,7 @@ import com.btr.proxy.selector.misc.ProtocolDispatchSelector; import com.btr.proxy.selector.whitelist.ProxyBypassListSelector; import com.btr.proxy.util.Logger; import com.btr.proxy.util.Logger.LogLevel; +import com.btr.proxy.util.MiscUtil; import com.btr.proxy.util.PlatformUtil; import com.btr.proxy.util.PlatformUtil.Platform; import com.btr.proxy.util.ProxyException; @@ -91,7 +92,7 @@ public class FirefoxProxySearchStrategy implements ProxySearchStrategy { Properties settings = readSettings(); ProxySelector result = null; - int type = Integer.parseInt(settings.getProperty("network.proxy.type", "-1")); + int type = MiscUtil.parseInt(settings.getProperty("network.proxy.type", "-1")); switch (type) { case -1: // Use system settings Logger.log(getClass(), LogLevel.TRACE, "Firefox uses system settings"); @@ -236,7 +237,7 @@ public class FirefoxProxySearchStrategy implements ProxySearchStrategy { private void installSecureProxy(ProtocolDispatchSelector ps, Properties settings) throws NumberFormatException { String proxyHost = settings.getProperty("network.proxy.ssl", null); - int proxyPort = Integer.parseInt(settings.getProperty("network.proxy.ssl_port", "0")); + int proxyPort = MiscUtil.parseInt(settings.getProperty("network.proxy.ssl_port", "0")); if (proxyHost != null) { Logger.log(getClass(), LogLevel.TRACE, "Firefox secure proxy is {0}:{1}", proxyHost, proxyPort); ProxySelector p = ProxyUtil.parseProxySettings(proxyHost, Proxy.Type.HTTP, proxyPort); @@ -258,7 +259,7 @@ public class FirefoxProxySearchStrategy implements ProxySearchStrategy { Properties settings, String protocol) throws NumberFormatException { String proxyHost = settings.getProperty("network.proxy."+protocol, null); - int proxyPort = Integer.parseInt(settings.getProperty("network.proxy."+protocol+"_port", "0")); + int proxyPort = MiscUtil.parseInt(settings.getProperty("network.proxy."+protocol+"_port", "0")); if (proxyHost != null) { Proxy.Type type; if (protocol.startsWith("socks")) { diff --git a/src/main/java/com/btr/proxy/search/desktop/gnome/GnomeProxySearchStrategy.java b/src/main/java/com/btr/proxy/search/desktop/gnome/GnomeProxySearchStrategy.java index 10d237a..00f7fcc 100644 --- a/src/main/java/com/btr/proxy/search/desktop/gnome/GnomeProxySearchStrategy.java +++ b/src/main/java/com/btr/proxy/search/desktop/gnome/GnomeProxySearchStrategy.java @@ -23,6 +23,7 @@ import com.btr.proxy.selector.whitelist.ProxyBypassListSelector; import com.btr.proxy.util.EmptyXMLResolver; import com.btr.proxy.util.Logger; import com.btr.proxy.util.Logger.LogLevel; +import com.btr.proxy.util.MiscUtil; import com.btr.proxy.util.ProxyException; import com.btr.proxy.util.ProxyUtil; @@ -218,7 +219,7 @@ public class GnomeProxySearchStrategy implements ProxySearchStrategy { private void installHttpSelector(Properties settings, ProtocolDispatchSelector ps) throws NumberFormatException { String proxyHost = settings.getProperty("/system/http_proxy/host", null); - int proxyPort = Integer.parseInt(settings.getProperty("/system/http_proxy/port", "0").trim()); + int proxyPort = MiscUtil.parseInt(settings.getProperty("/system/http_proxy/port", "0").trim()); if (proxyHost != null && proxyHost.length() > 0) { Logger.log(getClass(), LogLevel.TRACE, "Gnome http proxy is {0}:{1}", proxyHost, proxyPort); ps.setSelector("http", ProxyUtil.parseProxySettings(proxyHost.trim(), Proxy.Type.HTTP, proxyPort)); @@ -235,7 +236,7 @@ public class GnomeProxySearchStrategy implements ProxySearchStrategy { private void installSocksSelector(Properties settings, ProtocolDispatchSelector ps) throws NumberFormatException { String proxyHost = settings.getProperty("/system/proxy/socks_host", null); - int proxyPort = Integer.parseInt(settings.getProperty("/system/proxy/socks_port", "0").trim()); + int proxyPort = MiscUtil.parseInt(settings.getProperty("/system/proxy/socks_port", "0").trim()); if (proxyHost != null && proxyHost.length() > 0) { Logger.log(getClass(), LogLevel.TRACE, "Gnome socks proxy is {0}:{1}", proxyHost, proxyPort); ps.setSelector("socks", ProxyUtil.parseProxySettings(proxyHost.trim(), Proxy.Type.SOCKS, proxyPort)); @@ -251,7 +252,7 @@ public class GnomeProxySearchStrategy implements ProxySearchStrategy { private void installFtpSelector(Properties settings, ProtocolDispatchSelector ps) throws NumberFormatException { String proxyHost = settings.getProperty("/system/proxy/ftp_host", null); - int proxyPort = Integer.parseInt(settings.getProperty("/system/proxy/ftp_port", "0").trim()); + int proxyPort = MiscUtil.parseInt(settings.getProperty("/system/proxy/ftp_port", "0").trim()); if (proxyHost != null && proxyHost.length() > 0) { Logger.log(getClass(), LogLevel.TRACE, "Gnome ftp proxy is {0}:{1}", proxyHost, proxyPort); ps.setSelector("ftp", ProxyUtil.parseProxySettings(proxyHost.trim(), Proxy.Type.HTTP, proxyPort)); @@ -268,7 +269,7 @@ public class GnomeProxySearchStrategy implements ProxySearchStrategy { private void installSecureSelector(Properties settings, ProtocolDispatchSelector ps) throws NumberFormatException { String proxyHost = settings.getProperty("/system/proxy/secure_host", null); - int proxyPort = Integer.parseInt(settings.getProperty("/system/proxy/secure_port", "0").trim()); + int proxyPort = MiscUtil.parseInt(settings.getProperty("/system/proxy/secure_port", "0").trim()); if (proxyHost != null && proxyHost.length() > 0) { Logger.log(getClass(), LogLevel.TRACE, "Gnome secure proxy is {0}:{1}", proxyHost, proxyPort); ProxySelector ps2 = ProxyUtil.parseProxySettings(proxyHost.trim(), Proxy.Type.HTTP, proxyPort); diff --git a/src/main/java/com/btr/proxy/search/desktop/kde/KdeProxySearchStrategy.java b/src/main/java/com/btr/proxy/search/desktop/kde/KdeProxySearchStrategy.java index de81c4c..ecac104 100644 --- a/src/main/java/com/btr/proxy/search/desktop/kde/KdeProxySearchStrategy.java +++ b/src/main/java/com/btr/proxy/search/desktop/kde/KdeProxySearchStrategy.java @@ -8,14 +8,12 @@ import com.btr.proxy.search.ProxySearchStrategy; import com.btr.proxy.search.env.EnvProxySearchStrategy; import com.btr.proxy.search.wpad.WpadProxySearchStrategy; import com.btr.proxy.selector.direct.NoProxySelector; -import com.btr.proxy.selector.fixed.FixedProxySelector; import com.btr.proxy.selector.misc.ProtocolDispatchSelector; -import com.btr.proxy.selector.pac.PacProxySelector; -import com.btr.proxy.selector.pac.UrlPacScriptSource; import com.btr.proxy.selector.whitelist.ProxyBypassListSelector; import com.btr.proxy.selector.whitelist.UseProxyWhiteListSelector; import com.btr.proxy.util.Logger; import com.btr.proxy.util.Logger.LogLevel; +import com.btr.proxy.util.MiscUtil; import com.btr.proxy.util.ProxyException; import com.btr.proxy.util.ProxyUtil; @@ -84,7 +82,7 @@ public class KdeProxySearchStrategy implements ProxySearchStrategy { } ProxySelector result = null; - int type = Integer.parseInt(settings.getProperty("ProxyType", "-1")); + int type = MiscUtil.parseInt(settings.getProperty("ProxyType", "-1")); switch (type) { case 0: // Use no proxy Logger.log(getClass(), LogLevel.TRACE, "Kde uses no proxy"); diff --git a/src/main/java/com/btr/proxy/search/java/JavaProxySearchStrategy.java b/src/main/java/com/btr/proxy/search/java/JavaProxySearchStrategy.java index f1853b7..83e746e 100644 --- a/src/main/java/com/btr/proxy/search/java/JavaProxySearchStrategy.java +++ b/src/main/java/com/btr/proxy/search/java/JavaProxySearchStrategy.java @@ -9,6 +9,7 @@ import com.btr.proxy.selector.misc.ProtocolDispatchSelector; import com.btr.proxy.selector.whitelist.ProxyBypassListSelector; import com.btr.proxy.util.Logger; import com.btr.proxy.util.Logger.LogLevel; +import com.btr.proxy.util.MiscUtil; /***************************************************************************** * Reads some java system properties and extracts the proxy settings from them. @@ -96,7 +97,7 @@ public class JavaProxySearchStrategy implements ProxySearchStrategy { String port = System.getProperty("socksProxyPort", "1080"); if (host != null && host.trim().length() > 0) { Logger.log(getClass(), LogLevel.TRACE, "Socks proxy {0}:{1} found", host, port); - ps.setSelector("socks", new FixedProxySelector(Proxy.Type.SOCKS, host, Integer.parseInt(port))); + ps.setSelector("socks", new FixedProxySelector(Proxy.Type.SOCKS, host, MiscUtil.parseInt(port))); } } @@ -122,7 +123,7 @@ public class JavaProxySearchStrategy implements ProxySearchStrategy { Logger.log(getClass(), LogLevel.TRACE, protocol.toUpperCase()+" proxy {0}:{1} found using whitelist: {2}", host, port, whiteList); - ProxySelector protocolSelector = new FixedProxySelector(Proxy.Type.HTTP, host, Integer.parseInt(port)); + ProxySelector protocolSelector = new FixedProxySelector(Proxy.Type.HTTP, host, MiscUtil.parseInt(port)); if (whiteList.trim().length() > 0) { protocolSelector = new ProxyBypassListSelector(whiteList, protocolSelector); } diff --git a/src/main/java/com/btr/proxy/selector/pac/PacProxySelector.java b/src/main/java/com/btr/proxy/selector/pac/PacProxySelector.java index 765a376..a8441d6 100644 --- a/src/main/java/com/btr/proxy/selector/pac/PacProxySelector.java +++ b/src/main/java/com/btr/proxy/selector/pac/PacProxySelector.java @@ -8,8 +8,11 @@ import java.net.SocketAddress; import java.net.URI;
import java.util.ArrayList;
import java.util.List;
+
+import com.btr.proxy.selector.direct.NoProxySelector;
import com.btr.proxy.util.Logger;
import com.btr.proxy.util.Logger.LogLevel;
+import com.btr.proxy.util.MiscUtil;
import com.btr.proxy.util.ProxyUtil;
@@ -123,6 +126,9 @@ public class PacProxySelector extends ProxySelector { List<Proxy> proxies = new ArrayList<Proxy>();
String parseResult = this.pacScriptParser.evaluate(uri.toString(),
uri.getHost());
+ if (parseResult == null) {
+ return ProxyUtil.noProxyList();
+ }
String[] proxyDefinitions = parseResult.split("[;]");
for (String proxyDef : proxyDefinitions) {
if (proxyDef.trim().length() > 0) {
@@ -162,19 +168,23 @@ public class PacProxySelector extends ProxySelector { // Check proxy type.
Proxy.Type type = Proxy.Type.HTTP;
+ Integer port = ProxyUtil.DEFAULT_HTTP_PROXY_PORT;
+
if (proxyDef.toUpperCase().startsWith(PAC_SOCKS)) {
type = Proxy.Type.SOCKS;
+ port = ProxyUtil.DEFAULT_SOCKS_PROXY_PORT;
}
- String host = proxyDef.substring(6);
- Integer port = ProxyUtil.DEFAULT_HTTP_PROXY_PORT;
+ String host = proxyDef.substring(6).trim(); // because PROXY and SOCKS are both 5 chars... ;)
// Split port from host
int indexOfPort = host.indexOf(':');
if (indexOfPort != -1) {
- port = Integer.parseInt(host.substring(indexOfPort+1).trim());
+ port = MiscUtil.parseInt(host.substring(indexOfPort+1).trim());
host = host.substring(0, indexOfPort).trim();
}
+ if (port < 0 || port > 65535)
+ return Proxy.NO_PROXY;
SocketAddress adr = InetSocketAddress.createUnresolved(host, port);
return new Proxy(type, adr);
diff --git a/src/main/java/com/btr/proxy/selector/pac/UrlPacScriptSource.java b/src/main/java/com/btr/proxy/selector/pac/UrlPacScriptSource.java index c31b2d0..085d056 100644 --- a/src/main/java/com/btr/proxy/selector/pac/UrlPacScriptSource.java +++ b/src/main/java/com/btr/proxy/selector/pac/UrlPacScriptSource.java @@ -14,6 +14,7 @@ import java.net.URL; import com.btr.proxy.util.Logger; import com.btr.proxy.util.Logger.LogLevel; +import com.btr.proxy.util.MiscUtil; /***************************************************************************** * Script source that will load the content of a PAC file from an webserver. @@ -207,7 +208,7 @@ public class UrlPacScriptSource implements PacScriptSource { String prop = System.getProperty(overrideProperty); if (prop != null && prop.trim().length() > 0) { try { - timeout = Integer.parseInt(prop.trim()); + timeout = MiscUtil.parseInt(prop.trim()); } catch (NumberFormatException e) { Logger.log(getClass(), LogLevel.DEBUG, "Invalid override property : {0}={1}", overrideProperty, prop); // In this case use the default value. diff --git a/src/main/java/com/btr/proxy/selector/whitelist/IpRangeFilter.java b/src/main/java/com/btr/proxy/selector/whitelist/IpRangeFilter.java index 293f520..9084d9a 100644 --- a/src/main/java/com/btr/proxy/selector/whitelist/IpRangeFilter.java +++ b/src/main/java/com/btr/proxy/selector/whitelist/IpRangeFilter.java @@ -4,6 +4,7 @@ import java.net.InetAddress; import java.net.URI; import java.net.UnknownHostException; +import com.btr.proxy.util.MiscUtil; import com.btr.proxy.util.UriFilter; /***************************************************************************** @@ -39,7 +40,7 @@ public class IpRangeFilter implements UriFilter { throw new IllegalArgumentException("IP range is not valid:"+matchTo); } - this.numOfBits = Integer.parseInt(parts[1].trim()); + this.numOfBits = MiscUtil.parseInt(parts[1].trim()); } /************************************************************************* diff --git a/src/main/java/com/btr/proxy/util/ProxyUtil.java b/src/main/java/com/btr/proxy/util/ProxyUtil.java index bfb3f70..857b680 100644 --- a/src/main/java/com/btr/proxy/util/ProxyUtil.java +++ b/src/main/java/com/btr/proxy/util/ProxyUtil.java @@ -76,7 +76,7 @@ public class ProxyUtil { } else { String portStr = proxyVar.substring(portIdx + 1).replaceAll("[^0-9]", ""); if (!portStr.isEmpty()) { - port = Integer.parseInt(portStr); + port = MiscUtil.parseInt(portStr); } host = proxyVar.substring(0, portIdx); } @@ -84,7 +84,7 @@ public class ProxyUtil { if (port == -1 && fallbackPort > 0) { port = fallbackPort; } - if (port == -1) { + if (port < 0 || port > 65535) { if (type == Proxy.Type.HTTP) { port = DEFAULT_HTTP_PROXY_PORT; } else if (type == Proxy.Type.SOCKS) { |