Debian Codegraph @

src/main/java/com/github/markusbernhardt/proxy/search/browser/ie/IEProxySearchStrategy.java

raw symbols SCIP
1 package com.github.markusbernhardt.proxy.search.browser.ie;
2
3 import java.io.ByteArrayInputStream;
4 import java.io.IOException;
5 import java.net.ProxySelector;
6 import java.util.ArrayList;
7 import java.util.List;
8 import java.util.Properties;
9
10 import com.github.markusbernhardt.proxy.ProxySearchStrategy;
11 import com.github.markusbernhardt.proxy.jna.win.WinHttp;
12 import com.github.markusbernhardt.proxy.jna.win.WinHttpCurrentUserIEProxyConfig;
13 import com.github.markusbernhardt.proxy.selector.fixed.FixedProxySelector;
14 import com.github.markusbernhardt.proxy.selector.misc.ProtocolDispatchSelector;
15 import com.github.markusbernhardt.proxy.selector.pac.PacProxySelector;
16 import com.github.markusbernhardt.proxy.selector.whitelist.ProxyBypassListSelector;
17 import com.github.markusbernhardt.proxy.util.Logger;
18 import com.github.markusbernhardt.proxy.util.Logger.LogLevel;
19 import com.github.markusbernhardt.proxy.util.ProxyException;
20 import com.github.markusbernhardt.proxy.util.ProxyUtil;
21 import com.github.markusbernhardt.proxy.util.UriFilter;
22 import com.sun.jna.platform.win32.WTypes.LPWSTR;
23 import com.sun.jna.platform.win32.WinDef.DWORD;
24
25 /*****************************************************************************
26 * Extracts the proxy settings for Microsoft Internet Explorer. The settings are
27 * read by invoking native Windows API methods.
28 *
29 * @author Bernd Rosstauscher ([email protected]) Copyright 2009
30 ****************************************************************************/
31
32 public class IEProxySearchStrategy implements ProxySearchStrategy {
33
34 /*************************************************************************
35 * getProxySelector
36 *
37 * @see com.github.markusbernhardt.proxy.ProxySearchStrategy#getProxySelector()
38 ************************************************************************/
39
40 @Override
41 public ProxySelector getProxySelector() throws ProxyException {
42
43 Logger.log(getClass(), LogLevel.TRACE, "Detecting IE proxy settings");
44
45 IEProxyConfig ieProxyConfig = readIEProxyConfig();
46
47 ProxySelector result = createPacSelector(ieProxyConfig);
48 if (result == null) {
49 result = createFixedProxySelector(ieProxyConfig);
50 }
51 return result;
52 }
53
54 /*************************************************************************
55 * Gets the printable name of the search strategy.
56 *
57 * @return the printable name of the search strategy
58 ************************************************************************/
59
60 @Override
61 public String getName() {
62 return "IE";
63 }
64
65 /*************************************************************************
66 * Loads the settings from the windows registry.
67 *
68 * @return WinIESettings containing all proxy settings.
69 ************************************************************************/
70
71 public IEProxyConfig readIEProxyConfig() {
72
73 // Retrieve the IE proxy configuration.
74 WinHttpCurrentUserIEProxyConfig winHttpCurrentUserIeProxyConfig = new WinHttpCurrentUserIEProxyConfig();
75 boolean result = WinHttp.INSTANCE.WinHttpGetIEProxyConfigForCurrentUser(winHttpCurrentUserIeProxyConfig);
76 if (!result) {
77 return null;
78 }
79
80 // Create IEProxyConfig instance
81 return new IEProxyConfig(winHttpCurrentUserIeProxyConfig.fAutoDetect,
82 winHttpCurrentUserIeProxyConfig.lpszAutoConfigUrl != null
83 ? winHttpCurrentUserIeProxyConfig.lpszAutoConfigUrl.getValue() : null,
84 winHttpCurrentUserIeProxyConfig.lpszProxy != null ? winHttpCurrentUserIeProxyConfig.lpszProxy.getValue()
85 : null,
86 winHttpCurrentUserIeProxyConfig.lpszProxyBypass != null
87 ? winHttpCurrentUserIeProxyConfig.lpszProxyBypass.getValue() : null);
88
89 }
90
91 /*************************************************************************
92 * Parses the settings and creates an PAC ProxySelector for it.
93 *
94 * @param ieSettings
95 * the IE settings to use.
96 * @return a PacProxySelector the selector or null.
97 ************************************************************************/
98
99 private PacProxySelector createPacSelector(IEProxyConfig ieProxyConfig) {
100 String pacUrl = null;
101
102 if (ieProxyConfig.isAutoDetect()) {
103 Logger.log(getClass(), LogLevel.TRACE, "Autodetecting script URL.");
104 // This will take some time.
105 DWORD dwAutoDetectFlags = new DWORD(
106 WinHttp.WINHTTP_AUTO_DETECT_TYPE_DHCP | WinHttp.WINHTTP_AUTO_DETECT_TYPE_DNS_A);
107 LPWSTR ppwszAutoConfigUrl = new LPWSTR();
108 boolean result = WinHttp.INSTANCE.WinHttpDetectAutoProxyConfigUrl(dwAutoDetectFlags, ppwszAutoConfigUrl);
109 if (result) {
110 pacUrl = ppwszAutoConfigUrl.getValue();
111 }
112 }
113 if (pacUrl == null) {
114 pacUrl = ieProxyConfig.getAutoConfigUrl();
115 }
116 if (pacUrl != null && pacUrl.trim().length() > 0) {
117 Logger.log(getClass(), LogLevel.TRACE, "IE uses script: " + pacUrl);
118
119 // Fix for issue 9
120 // If the IE has a file URL and it only starts has 2 slashes,
121 // add a third so it can be properly converted to the URL class
122 if (pacUrl.startsWith("file://") && !pacUrl.startsWith("file:///")) {
123 pacUrl = "file:///" + pacUrl.substring(7);
124 }
125 return ProxyUtil.buildPacSelectorForUrl(pacUrl);
126 }
127
128 return null;
129 }
130
131 /*************************************************************************
132 * Parses the proxy settings into an ProxySelector.
133 *
134 * @param ieSettings
135 * the settings to use.
136 * @return a ProxySelector, null if no settings are set.
137 * @throws ProxyException
138 * on error.
139 ************************************************************************/
140
141 private ProxySelector createFixedProxySelector(IEProxyConfig ieProxyConfig) throws ProxyException {
142 String proxyString = ieProxyConfig.getProxy();
143 String bypassList = ieProxyConfig.getProxyBypass();
144 if (proxyString == null) {
145 return null;
146 }
147 Logger.log(getClass(), LogLevel.TRACE, "IE uses manual settings: {0} with bypass list: {1}", proxyString,
148 bypassList);
149
150 Properties p = parseProxyList(proxyString);
151
152 ProtocolDispatchSelector ps = new ProtocolDispatchSelector();
153 addSelectorForProtocol(p, "http", ps);
154 addSelectorForProtocol(p, "https", ps);
155 addSelectorForProtocol(p, "ftp", ps);
156 addSelectorForProtocol(p, "gopher", ps);
157 addSelectorForProtocol(p, "socks", ps);
158 addFallbackSelector(p, ps);
159
160 ProxySelector result = setByPassListOnSelector(bypassList, ps);
161 return result;
162 }
163
164 /*************************************************************************
165 * Installs the proxy exclude list on the given selector.
166 *
167 * @param bypassList
168 * the list of urls / hostnames to ignore.
169 * @param ps
170 * the proxy selector to wrap.
171 * @return a wrapped proxy selector that will handle the bypass list.
172 ************************************************************************/
173
174 private ProxySelector setByPassListOnSelector(String bypassList, ProtocolDispatchSelector ps) {
175 if (bypassList != null && bypassList.trim().length() > 0) {
176 ProxyBypassListSelector result;
177 if ("<local>".equals(bypassList.trim())) {
178 result = buildLocalBypassSelector(ps);
179 } else {
180 bypassList = bypassList.replace(';', ',');
181 result = new ProxyBypassListSelector(bypassList, ps);
182 }
183 return result;
184 }
185 return ps;
186 }
187
188 /*************************************************************************
189 * Wraps the given selector to handle "local" addresses
190 *
191 * @param ps
192 * the proxy selector to wrap.
193 * @return a wrapped proxy selector that will handle the local addresses.
194 ************************************************************************/
195
196 private ProxyBypassListSelector buildLocalBypassSelector(ProtocolDispatchSelector ps) {
197 List<UriFilter> localBypassFilter = new ArrayList<UriFilter>();
198 localBypassFilter.add(new IELocalByPassFilter());
199 return new ProxyBypassListSelector(localBypassFilter, ps);
200 }
201
202 /*************************************************************************
203 * Installs a fallback selector that is used whenever no protocol specific
204 * selector is defined.
205 *
206 * @param settings
207 * to take the proxy settings from.
208 * @param ps
209 * to install the created selector on.
210 ************************************************************************/
211
212 private void addFallbackSelector(Properties settings, ProtocolDispatchSelector ps) {
213 String proxy = settings.getProperty("default");
214 if (proxy != null) {
215 ps.setFallbackSelector(ProxyUtil.parseProxySettings(proxy));
216 }
217 }
218
219 /*************************************************************************
220 * Creates a selector for a given protocol. The proxy will be taken from the
221 * settings and installed on the dispatch selector.
222 *
223 * @param settings
224 * to take the proxy settings from.
225 * @param protocol
226 * to create a selector for.
227 * @param ps
228 * to install the created selector on.
229 ************************************************************************/
230
231 private void addSelectorForProtocol(Properties settings, String protocol, ProtocolDispatchSelector ps) {
232 String proxy = settings.getProperty(protocol);
233 if (proxy != null) {
234 FixedProxySelector protocolSelector = ProxyUtil.parseProxySettings(proxy);
235 ps.setSelector(protocol, protocolSelector);
236 }
237 }
238
239 /*************************************************************************
240 * Parses the proxy list and splits it by protocol.
241 *
242 * @param proxyString
243 * the proxy list string
244 * @return Properties with separated settings.
245 * @throws ProxyException
246 * on parse error.
247 ************************************************************************/
248
249 private Properties parseProxyList(String proxyString) throws ProxyException {
250 Properties p = new Properties();
251 if (proxyString.indexOf('=') == -1) {
252 p.setProperty("default", proxyString);
253 } else {
254 try {
255 proxyString = proxyString.replace(';', '\n');
256 p.load(new ByteArrayInputStream(proxyString.getBytes("ISO-8859-1")));
257 } catch (IOException e) {
258 Logger.log(getClass(), LogLevel.ERROR, "Error reading IE settings as properties: {0}", e);
259
260 throw new ProxyException(e);
261 }
262 }
263 return p;
264 }
265
266 }