src/main/java/com/github/markusbernhardt/proxy/search/wpad/WpadProxySearchStrategyWithDHPC.java
raw symbols SCIP| 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.search.wpad.dhcp.DHCPMessage; |
| 14 | import com.github.markusbernhardt.proxy.util.Logger; |
| 15 | import com.github.markusbernhardt.proxy.util.ProxyException; |
| 16 | import com.github.markusbernhardt.proxy.util.ProxyUtil; |
| 17 | import com.github.markusbernhardt.proxy.util.Logger.LogLevel; |
| 18 | |
| 19 | /***************************************************************************** |
| 20 | * Uses automatic proxy script search (WPAD) to find an PAC file automatically. |
| 21 | * <p> |
| 22 | * Note: at the moment only the DNS name guessing schema is implemented. All |
| 23 | * others are missing. |
| 24 | * </p> |
| 25 | * <p> |
| 26 | * For more information about WPAD: |
| 27 | * <a href="http://en.wikipedia.org/wiki/Web_Proxy_Autodiscovery_Protocol"> |
| 28 | * Web_Proxy_Autodiscovery_Protocol</a> |
| 29 | * </p> |
| 30 | * <p> |
| 31 | * Outdated RFC draft: <a href= |
| 32 | * "http://www.web-cache.com/Writings/Internet-Drafts/draft-ietf-wrec-wpad-01.txt"> |
| 33 | * draft-ietf-wrec-wpad-01.txt</a> |
| 34 | * </p> |
| 35 | * |
| 36 | * @author Markus Bernhardt, Copyright 2016 |
| 37 | * @author Bernd Rosstauscher, Copyright 2009 |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | public class WpadProxySearchStrategyWithDHPC implements ProxySearchStrategy { |
| 41 | |
| 42 | /************************************************************************* |
| 43 | * Constructor |
| 44 | ************************************************************************/ |
| 45 | |
| 46 | public WpadProxySearchStrategyWithDHPC() { |
| 47 | super(); |
| 48 | } |
| 49 | |
| 50 | /************************************************************************* |
| 51 | * Loads the proxy settings from a PAC file. The location of the PAC file is |
| 52 | * determined automatically. |
| 53 | * |
| 54 | * @return a configured ProxySelector, null if none is found. |
| 55 | * @throws ProxyException |
| 56 | * on error. |
| 57 | ************************************************************************/ |
| 58 | |
| 59 | @Override |
| 60 | public ProxySelector getProxySelector() throws ProxyException { |
| 61 | try { |
| 62 | Logger.log(getClass(), LogLevel.TRACE, "Using WPAD to find a proxy"); |
| 63 | |
| 64 | String pacScriptUrl = detectScriptUrlPerDHCP(); |
| 65 | if (pacScriptUrl == null) { |
| 66 | pacScriptUrl = detectScriptUrlPerDNS(); |
| 67 | } |
| 68 | if (pacScriptUrl == null) { |
| 69 | return null; |
| 70 | } |
| 71 | Logger.log(getClass(), LogLevel.TRACE, "PAC script url found: {0}", pacScriptUrl); |
| 72 | return ProxyUtil.buildPacSelectorForUrl(pacScriptUrl); |
| 73 | } catch (IOException e) { |
| 74 | Logger.log(getClass(), LogLevel.ERROR, "Error during WPAD search.", e); |
| 75 | throw new ProxyException(e); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /************************************************************************* |
| 80 | * Gets the printable name of the search strategy. |
| 81 | * |
| 82 | * @return the printable name of the search strategy |
| 83 | ************************************************************************/ |
| 84 | |
| 85 | @Override |
| 86 | public String getName() { |
| 87 | return "wpad"; |
| 88 | } |
| 89 | |
| 90 | /************************************************************************* |
| 91 | * Loads the settings and stores them in a properties map. |
| 92 | * |
| 93 | * @return the settings. |
| 94 | ************************************************************************/ |
| 95 | |
| 96 | public Properties readSettings() { |
| 97 | try { |
| 98 | String pacScriptUrl = detectScriptUrlPerDHCP(); |
| 99 | if (pacScriptUrl == null) { |
| 100 | pacScriptUrl = detectScriptUrlPerDNS(); |
| 101 | } |
| 102 | if (pacScriptUrl == null) { |
| 103 | return null; |
| 104 | } |
| 105 | Properties result = new Properties(); |
| 106 | result.setProperty("url", pacScriptUrl); |
| 107 | return result; |
| 108 | } catch (IOException e) { |
| 109 | // Ignore and return empty properties. |
| 110 | return new Properties(); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /************************************************************************* |
| 115 | * Uses DNS to find the script URL. Attention: this detection method is |
| 116 | * known to have some severe security issues. |
| 117 | * |
| 118 | * @return the URL, null if not found. |
| 119 | ************************************************************************/ |
| 120 | |
| 121 | private String detectScriptUrlPerDNS() throws IOException { |
| 122 | String result = null; |
| 123 | String fqdn = InetAddress.getLocalHost().getCanonicalHostName(); |
| 124 | |
| 125 | Logger.log(getClass(), LogLevel.TRACE, "Searching per DNS guessing."); |
| 126 | |
| 127 | int index = fqdn.indexOf('.'); |
| 128 | while (index != -1 && result == null) { |
| 129 | fqdn = fqdn.substring(index + 1); |
| 130 | |
| 131 | // if we are already on TLD level then escape |
| 132 | if (fqdn.indexOf('.') == -1) { |
| 133 | break; |
| 134 | } |
| 135 | |
| 136 | // Try to connect to URL |
| 137 | try { |
| 138 | URL lookupURL = new URL("http://wpad." + fqdn + "/wpad.dat"); |
| 139 | Logger.log(getClass(), LogLevel.TRACE, "Trying url: {0}", lookupURL); |
| 140 | |
| 141 | HttpURLConnection con = (HttpURLConnection) lookupURL.openConnection(Proxy.NO_PROXY); |
| 142 | con.setInstanceFollowRedirects(true); |
| 143 | con.setRequestProperty("accept", "application/x-ns-proxy-autoconfig"); |
| 144 | if (con.getResponseCode() == 200) { |
| 145 | result = lookupURL.toString(); |
| 146 | } |
| 147 | con.disconnect(); |
| 148 | } catch (UnknownHostException e) { |
| 149 | Logger.log(getClass(), LogLevel.DEBUG, "Not available!"); |
| 150 | // Not a real error, try next address |
| 151 | } |
| 152 | |
| 153 | index = fqdn.indexOf('.'); |
| 154 | } |
| 155 | |
| 156 | return result; |
| 157 | } |
| 158 | |
| 159 | /************************************************************************* |
| 160 | * Uses DHCP to find the script URL. |
| 161 | * |
| 162 | * @return the URL, null if not found. |
| 163 | ************************************************************************/ |
| 164 | |
| 165 | private String detectScriptUrlPerDHCP() { |
| 166 | Logger.log(getClass(), LogLevel.DEBUG, "Searching per DHCP not supported yet."); |
| 167 | // TODO Rossi 28.04.2009 Not implemented yet. |
| 168 | return null; |
| 169 | } |
| 170 | |
| 171 | // Sends DHCPREQUEST Message and returns server message |
| 172 | @SuppressWarnings("unused") |
| 173 | private DHCPMessage SendRequest(DHCPMessage offerMessageIn) { |
| 174 | // DHCPSocket bindSocket = new DHCPSocket(DHCPMessage.CLIENT_PORT); |
| 175 | // |
| 176 | // DHCPMessage messageOut = new DHCPMessage(DHCPMessage.SERVER_PORT); |
| 177 | // DHCPMessage messageIn = new DHCPMessage(DHCPMessage.CLIENT_PORT); |
| 178 | // try { |
| 179 | // messageOut = offerMessageIn; |
| 180 | // messageOut.setOp((byte) 1); // setup message to send a DCHPREQUEST |
| 181 | // byte[] opt = new byte[1]; |
| 182 | // opt[0] = DHCPMessage.DHCPREQUEST; |
| 183 | // messageOut.setOption(DHCPMessage.DHCPDISCOVER, opt); // change |
| 184 | // message type |
| 185 | // messageOut.setOption(REQUESTED_IP, offerMessageIn.getYiaddr()); |
| 186 | // bindSocket.send(messageOut); // send DHCPREQUEST |
| 187 | // System.out.print(this.getName()); |
| 188 | // System.out.print(" sending DHCPREQUEST for "); |
| 189 | // System.out.println(bytesToString(offerMessageIn |
| 190 | // .getOption(REQUESTED_IP))); |
| 191 | // boolean sentinal = true; |
| 192 | // while (sentinal) { |
| 193 | // if (bindSocket.receive(messageIn)) { |
| 194 | // if (messageOut.getXid() == messageIn.getXid()) { |
| 195 | // sentinal = false; |
| 196 | // } else { |
| 197 | // bindSocket.send(messageOut); |
| 198 | // } |
| 199 | // } else { |
| 200 | // bindSocket.send(messageOut); |
| 201 | // } |
| 202 | // } |
| 203 | // } catch (SocketException e) { |
| 204 | // System.err.println(e); |
| 205 | // } catch (IOException e) { |
| 206 | // System.err.println(e); |
| 207 | // } // end catch |
| 208 | // return messageIn; |
| 209 | return null; |
| 210 | } |
| 211 | |
| 212 | } |