Debian Codegraph @

src/main/java/com/github/markusbernhardt/proxy/search/wpad/WpadProxySearchStrategy.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.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 * Uses automatic proxy script search (WPAD) to find an PAC file automatically.
20 * <p>
21 * Note: at the moment only the DNS name guessing schema is implemented. All
22 * others are missing.
23 * </p>
24 * <p>
25 * For more information about WPAD:
26 * <a href="http://en.wikipedia.org/wiki/Web_Proxy_Autodiscovery_Protocol">
27 * Web_Proxy_Autodiscovery_Protocol</a>
28 * </p>
29 * <p>
30 * Outdated RFC draft: <a href=
31 * "http://www.web-cache.com/Writings/Internet-Drafts/draft-ietf-wrec-wpad-01.txt">
32 * draft-ietf-wrec-wpad-01.txt</a>
33 * </p>
34 *
35 * @author Markus Bernhardt, Copyright 2016
36 * @author Bernd Rosstauscher, Copyright 2009
37 ****************************************************************************/
38
39 public class WpadProxySearchStrategy implements ProxySearchStrategy {
40
41 /*************************************************************************
42 * Constructor
43 ************************************************************************/
44
45 public WpadProxySearchStrategy() {
46 super();
47 }
48
49 /*************************************************************************
50 * Loads the proxy settings from a PAC file. The location of the PAC file is
51 * determined automatically.
52 *
53 * @return a configured ProxySelector, null if none is found.
54 * @throws ProxyException
55 * on error.
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 * Gets the printable name of the search strategy.
80 *
81 * @return the printable name of the search strategy
82 ************************************************************************/
83
84 @Override
85 public String getName() {
86 return "wpad";
87 }
88
89 /*************************************************************************
90 * Loads the settings and stores them in a properties map.
91 *
92 * @return the settings.
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 // Ignore and return empty properties.
109 return new Properties();
110 }
111 }
112
113 /*************************************************************************
114 * Uses DNS to find the script URL. Attention: this detection method is
115 * known to have some severe security issues.
116 *
117 * @return the URL, null if not found.
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 // if we are already on TLD level then escape
135 if (fqdn.indexOf('.') == -1) {
136 break;
137 }
138
139 // Try to connect to URL
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 // Not a real error, try next address
154 }
155
156 index = fqdn.indexOf('.');
157 }
158
159 return result;
160 }
161
162 /*************************************************************************
163 * Uses DHCP to find the script URL.
164 *
165 * @return the URL, null if not found.
166 ************************************************************************/
167
168 private String detectScriptUrlPerDHCP() {
169 Logger.log(getClass(), LogLevel.DEBUG, "Searching per DHCP not supported yet.");
170 // TODO Rossi 28.04.2009 Not implemented yet.
171 return null;
172 }
173
174 }