| 1 |
package com.github.markusbernhardt.proxy.search.wpad; |
| 2 |
|
| 3 |
import java.io.IOException; |
| 4 |
import java.net.HttpURLConnection; |
| 5 |
import java.net.InetAddress; |
| 6 |
import java.net.Proxy; |
| 7 |
import java.net.ProxySelector; |
| 8 |
import java.net.URL; |
| 9 |
import java.net.UnknownHostException; |
| 10 |
import java.util.Properties; |
| 11 |
|
| 12 |
import com.github.markusbernhardt.proxy.ProxySearchStrategy; |
| 13 |
import com.github.markusbernhardt.proxy.util.Logger; |
| 14 |
import com.github.markusbernhardt.proxy.util.ProxyException; |
| 15 |
import com.github.markusbernhardt.proxy.util.ProxyUtil; |
| 16 |
import com.github.markusbernhardt.proxy.util.Logger.LogLevel; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public class WpadProxySearchStrategy implements ProxySearchStrategy { |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public WpadProxySearchStrategy() { |
| 46 |
super(); |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
@Override |
| 59 |
public ProxySelector getProxySelector() throws ProxyException { |
| 60 |
try { |
| 61 |
Logger.log(getClass(), LogLevel.TRACE, "Using WPAD to find a proxy"); |
| 62 |
|
| 63 |
String pacScriptUrl = detectScriptUrlPerDHCP(); |
| 64 |
if (pacScriptUrl == null) { |
| 65 |
pacScriptUrl = detectScriptUrlPerDNS(); |
| 66 |
} |
| 67 |
if (pacScriptUrl == null) { |
| 68 |
return null; |
| 69 |
} |
| 70 |
Logger.log(getClass(), LogLevel.TRACE, "PAC script url found: {0}", pacScriptUrl); |
| 71 |
return ProxyUtil.buildPacSelectorForUrl(pacScriptUrl); |
| 72 |
} catch (IOException e) { |
| 73 |
Logger.log(getClass(), LogLevel.ERROR, "Error during WPAD search.", e); |
| 74 |
throw new ProxyException(e); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
@Override |
| 85 |
public String getName() { |
| 86 |
return "wpad"; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public Properties readSettings() { |
| 96 |
try { |
| 97 |
String pacScriptUrl = detectScriptUrlPerDHCP(); |
| 98 |
if (pacScriptUrl == null) { |
| 99 |
pacScriptUrl = detectScriptUrlPerDNS(); |
| 100 |
} |
| 101 |
if (pacScriptUrl == null) { |
| 102 |
return null; |
| 103 |
} |
| 104 |
Properties result = new Properties(); |
| 105 |
result.setProperty("url", pacScriptUrl); |
| 106 |
return result; |
| 107 |
} catch (IOException e) { |
| 108 |
|
| 109 |
return new Properties(); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
private String detectScriptUrlPerDNS() throws IOException { |
| 121 |
String result = null; |
| 122 |
|
| 123 |
String fqdn = InetAddress.getLocalHost().getCanonicalHostName(); |
| 124 |
if (fqdn.equalsIgnoreCase("localhost") || fqdn.length() == 0 || Character.isDigit(fqdn.charAt(0))) { |
| 125 |
return result; |
| 126 |
} |
| 127 |
|
| 128 |
Logger.log(getClass(), LogLevel.TRACE, "Searching per DNS guessing."); |
| 129 |
|
| 130 |
int index = fqdn.indexOf('.'); |
| 131 |
while (index != -1 && result == null) { |
| 132 |
fqdn = fqdn.substring(index + 1); |
| 133 |
|
| 134 |
|
| 135 |
if (fqdn.indexOf('.') == -1) { |
| 136 |
break; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
try { |
| 141 |
URL lookupURL = new URL("http://wpad." + fqdn + "/wpad.dat"); |
| 142 |
Logger.log(getClass(), LogLevel.TRACE, "Trying url: {0}", lookupURL); |
| 143 |
|
| 144 |
HttpURLConnection con = (HttpURLConnection) lookupURL.openConnection(Proxy.NO_PROXY); |
| 145 |
con.setInstanceFollowRedirects(true); |
| 146 |
con.setRequestProperty("accept", "application/x-ns-proxy-autoconfig"); |
| 147 |
if (con.getResponseCode() == 200) { |
| 148 |
result = lookupURL.toString(); |
| 149 |
} |
| 150 |
con.disconnect(); |
| 151 |
} catch (UnknownHostException e) { |
| 152 |
Logger.log(getClass(), LogLevel.DEBUG, "Not available!"); |
| 153 |
|
| 154 |
} |
| 155 |
|
| 156 |
index = fqdn.indexOf('.'); |
| 157 |
} |
| 158 |
|
| 159 |
return result; |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
private String detectScriptUrlPerDHCP() { |
| 169 |
Logger.log(getClass(), LogLevel.DEBUG, "Searching per DHCP not supported yet."); |
| 170 |
|
| 171 |
return null; |
| 172 |
} |
| 173 |
|
| 174 |
} |