From 9143246a77d8a07398556b34dd812d0dbef0519c Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Fri, 5 Jun 2015 11:16:57 +0200 Subject: Add HTTP connect timeout --- .../proxy/search/wpad/WpadProxySearchStrategy.java | 138 +++++++++++---------- .../btr/proxy/selector/pac/UrlPacScriptSource.java | 4 +- 2 files changed, 77 insertions(+), 65 deletions(-) diff --git a/src/main/java/com/btr/proxy/search/wpad/WpadProxySearchStrategy.java b/src/main/java/com/btr/proxy/search/wpad/WpadProxySearchStrategy.java index cf01fd3..cac3fe0 100644 --- a/src/main/java/com/btr/proxy/search/wpad/WpadProxySearchStrategy.java +++ b/src/main/java/com/btr/proxy/search/wpad/WpadProxySearchStrategy.java @@ -14,7 +14,6 @@ import java.net.URI; import java.net.URISyntaxException; import java.net.URL; import java.net.URLConnection; -import java.net.UnknownHostException; import java.util.List; import java.util.Properties; @@ -28,7 +27,7 @@ import com.btr.proxy.util.ProxyUtil; /***************************************************************************** * Uses automatic proxy script search (WPAD) to find an PAC file automatically. *
- * Note: at the moment only the DNS name guessing schema is implemented. + * Note: at the moment only the DNS name guessing schema is implemented. * All others are missing. *
* For more information about WPAD:
@@ -41,20 +40,21 @@ import com.btr.proxy.util.ProxyUtil;
****************************************************************************/
public class WpadProxySearchStrategy implements ProxySearchStrategy {
-
+
/*************************************************************************
* Constructor
************************************************************************/
-
+
public WpadProxySearchStrategy() {
super();
}
-
+
/*************************************************************************
- * Loads the proxy settings from a PAC file.
+ * Loads the proxy settings from a PAC file.
* The location of the PAC file is determined automatically.
+ *
* @return a configured ProxySelector, null if none is found.
- * @throws ProxyException on error.
+ * @throws ProxyException on error.
************************************************************************/
public ProxySelector getProxySelector() throws ProxyException {
@@ -75,12 +75,13 @@ public class WpadProxySearchStrategy implements ProxySearchStrategy {
throw new ProxyException(e);
}
}
-
+
/*************************************************************************
* Loads the settings and stores them in a properties map.
+ *
* @return the settings.
************************************************************************/
-
+
public Properties readSettings() {
try {
String pacScriptUrl = detectScriptUrlPerDHCP();
@@ -101,61 +102,60 @@ public class WpadProxySearchStrategy implements ProxySearchStrategy {
/*************************************************************************
* Uses DNS to find the script URL.
- * Attention: this detection method is known to have some severe security issues.
+ * Attention: this detection method is known to have some severe security
+ * issues.
+ *
* @return the URL, null if not found.
************************************************************************/
-
+
private String detectScriptUrlPerDNS() throws IOException {
String result = null;
// String fqdn = InetAddress.getLocalHost().getCanonicalHostName();
-
+ // TODO: Java way of finding all search domains? Seems not trivial
+ // Windows workaround could be running ipconfig /all and parsing...
Logger.log(getClass(), LogLevel.TRACE, "Searching per DNS guessing.");
// Logger.log(getClass(), LogLevel.INFO, "fqdn: ", fqdn);
- /** Reading address from "/etc/resolv.conf"; file looks like:
- *
- # Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
- # DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
- nameserver 127.0.1.1
- search ruf.uni-freiburg.de lp.ruf.uni-freiburg.de
- */
-
FileReader fr = new FileReader("/etc/resolv.conf");
BufferedReader br = new BufferedReader(fr);
-
- String input;
+ ;
String[] addresses = null;
- // using the 4th line of the file.
- while ((input = br.readLine()) != null) {
- if (input.startsWith("search")) {
- // the first one is "search" and afterwards addresses are following.
- addresses = input.substring(6).split(" ");
- break;
+ try {
+ String input;
+ while ((input = br.readLine()) != null) {
+ if (input.startsWith("search")) {
+ // the first one is "search" and afterwards addresses are following.
+ addresses = input.substring(6).split(" ");
+ break;
+ }
}
+ } finally {
+ br.close();
}
-
+
if (addresses == null) {
addresses = new String[]{""};
}
-
-
+
+ String addr;
for (int i = 0; i < addresses.length; ++i) {
- String address = addresses[i];
- int index = -1;
+ String address = addresses[i].trim();
+ if (address.length() < 1)
+ continue;
do {
- if (index != -1) {
- address = address.substring(index);
- } else {
- address = "";
- }
-
// Try to connect to URL
try {
- address = ".uni-freiburg.de";
- URL lookupURL = new URL("http://wpad"+ address +"/wpad.dat");
- Logger.log(getClass(), LogLevel.TRACE, "Trying url: {0}", lookupURL);
-
+ if (address.length() != 0)
+ addr = "." + address;
+ else
+ addr = address;
+ URL lookupURL = new URL("http://wpad" + addr + "/wpad.dat");
+ Logger.log(getClass(), LogLevel.DEBUG, "Trying url: {0}", lookupURL);
+
HttpURLConnection con = (HttpURLConnection) lookupURL.openConnection(Proxy.NO_PROXY);
+ con.setConnectTimeout(1000);
+ con.setReadTimeout(2000);
+ con.setRequestMethod("HEAD");
con.setInstanceFollowRedirects(true);
con.setRequestProperty("accept", "application/x-ns-proxy-autoconfig");
if (con.getResponseCode() == 200) {
@@ -163,44 +163,52 @@ public class WpadProxySearchStrategy implements ProxySearchStrategy {
return result;
}
con.disconnect();
- } catch (UnknownHostException e) {
+ } catch (Exception e) {
Logger.log(getClass(), LogLevel.DEBUG, "Not available!");
// Not a real error, try next address
}
if (address.length() == 0) {
break;
}
- index = address.indexOf('.', 1);
+ int index = address.indexOf('.');
+ if (index != -1 && index < address.length())
+ address = address.substring(index + 1);
+ else
+ address = "";
+ if (address.indexOf('.') == -1 || address.length() < 7) // don't construct something like wpad.de....
+ address = "";
} while (true);
}
-
+
return null;
}
/*************************************************************************
* Uses DHCP to find the script URL.
+ *
* @return the URL, null if not found.
************************************************************************/
-
+
private String detectScriptUrlPerDHCP() {
Logger.log(getClass(), LogLevel.DEBUG, "Searching per DHCP not supported yet.");
// TODO Rossi 28.04.2009 Not implemented yet.
return null;
}
-
+
// Main method for testing.
- public static void main( String[] args ) throws IOException {
+ public static void main(String[] args) throws IOException {
WpadProxySearchStrategy wPSS = new WpadProxySearchStrategy();
-// System.setProperty("com.btr.proxy.pac.overrideLocalIP", "10.0.0.1");
+ // System.setProperty("com.btr.proxy.pac.overrideLocalIP", "10.0.0.1");
try {
ProxySelector pS = wPSS.getProxySelector();
ProxySelector.setDefault(pS);
List