Debian Codegraph @

src/main/java/com/github/markusbernhardt/proxy/search/browser/firefox/FirefoxProxySearchStrategy.java

raw symbols SCIP
1 package com.github.markusbernhardt.proxy.search.browser.firefox;
2
3 import java.io.IOException;
4 import java.net.ProxySelector;
5 import java.util.Properties;
6
7 import com.github.markusbernhardt.proxy.ProxySearchStrategy;
8 import com.github.markusbernhardt.proxy.search.desktop.DesktopProxySearchStrategy;
9 import com.github.markusbernhardt.proxy.search.wpad.WpadProxySearchStrategy;
10 import com.github.markusbernhardt.proxy.selector.direct.NoProxySelector;
11 import com.github.markusbernhardt.proxy.selector.fixed.FixedProxySelector;
12 import com.github.markusbernhardt.proxy.selector.fixed.FixedSocksSelector;
13 import com.github.markusbernhardt.proxy.selector.misc.ProtocolDispatchSelector;
14 import com.github.markusbernhardt.proxy.selector.whitelist.ProxyBypassListSelector;
15 import com.github.markusbernhardt.proxy.util.Logger;
16 import com.github.markusbernhardt.proxy.util.Logger.LogLevel;
17 import com.github.markusbernhardt.proxy.util.PlatformUtil;
18 import com.github.markusbernhardt.proxy.util.PlatformUtil.Platform;
19 import com.github.markusbernhardt.proxy.util.ProxyException;
20 import com.github.markusbernhardt.proxy.util.ProxyUtil;
21
22 /*****************************************************************************
23 * Loads the Firefox3 proxy settings from the users Firefox3 settings. This will
24 * load the file <i>prefs.js</i> that is located in the
25 * <p>
26 * <i>.mozilla/firefox/(profile)/</i> folder.
27 * </p>
28 *
29 * See
30 * <a href="https://developer.mozilla.org/En/Mozilla_Networking_Preferences">
31 * Mozilla_Networking_Preferences</a> for an explanation of the proxy settings.
32 * <p>
33 * The following settings are extracted from this file:
34 * </p>
35 * Some generic settings:<br>
36 * <ul>
37 * <li><i>network.proxy.type</i> -&gt; n/a = use system settings, 0 = direct, 1
38 * = Fixed proxy settings, 2 = proxy script (PAC), 3 = also direct , 4 = auto
39 * detect (WPAD)</li>
40 * <li><i>network.proxy.share_proxy_settings</i> -&gt; true = use same proxy for
41 * all protocols</li>
42 * <li><i>network.proxy.no_proxies_on</i> -&gt; a comma separated ignore list.
43 * </li>
44 * <li><i>network.proxy.autoconfig_url</i> -&gt; a URL to an proxy configuration
45 * script</li>
46 * </ul>
47 * Host names and ports per protocol are stored in the following settings:
48 * <ul>
49 * <li><i>network.proxy.http</i></li>
50 * <li><i>network.proxy.http_port</i></li>
51 * <li><i>network.proxy.ssl</i></li>
52 * <li><i>network.proxy.ssl_port</i></li>
53 * <li><i>network.proxy.ftp</i></li>
54 * <li><i>network.proxy.ftp_port</i></li>
55 * <li><i>network.proxy.gopher</i></li>
56 * <li><i>network.proxy.gopher_port</i></li>
57 * <li><i>network.proxy.socks</i></li>
58 * <li><i>network.proxy.socks_port</i></li>
59 * <li><i>network.proxy.socks_version</i> -&gt; 4 or 5</li>
60 * </ul>
61 * <p>
62 * Note that if there are more than one profile the first profile found will be
63 * used.
64 * </p>
65 *
66 * @author Markus Bernhardt, Copyright 2016
67 * @author Bernd Rosstauscher, Copyright 2009
68 ****************************************************************************/
69
70 public class FirefoxProxySearchStrategy implements ProxySearchStrategy {
71
72 private FirefoxProfileSource profileScanner;
73 private FirefoxSettingParser settingsParser;
74
75 /*************************************************************************
76 * ProxySelector
77 *
78 * @see java.net.ProxySelector#ProxySelector()
79 ************************************************************************/
80
81 public FirefoxProxySearchStrategy() {
82 super();
83 if (PlatformUtil.getCurrentPlattform() == Platform.WIN) {
84 this.profileScanner = new WinFirefoxProfileSource();
85 } else if (PlatformUtil.getCurrentPlattform() == Platform.MAC_OS) {
86 this.profileScanner = new OsxFirefoxProfileSource();
87 } else {
88 this.profileScanner = new LinuxFirefoxProfileSource();
89 }
90 this.settingsParser = new FirefoxSettingParser();
91 }
92
93 /*************************************************************************
94 * Loads the proxy settings and initializes a proxy selector for the firefox
95 * proxy settings.
96 *
97 * @return a configured ProxySelector, null if none is found.
98 * @throws ProxyException
99 * on file reading error.
100 ************************************************************************/
101
102 @Override
103 public ProxySelector getProxySelector() throws ProxyException {
104 Logger.log(getClass(), LogLevel.TRACE, "Detecting Firefox settings.");
105
106 Properties settings = readSettings();
107
108 ProxySelector result = null;
109 int type = Integer.parseInt(settings.getProperty("network.proxy.type", "-1"));
110 switch (type) {
111 case -1: // Use system settings
112 Logger.log(getClass(), LogLevel.TRACE, "Firefox uses system settings");
113 result = new DesktopProxySearchStrategy().getProxySelector();
114 break;
115 case 0: // Use no proxy
116 Logger.log(getClass(), LogLevel.TRACE, "Firefox uses no proxy");
117 result = NoProxySelector.getInstance();
118 break;
119 case 1: // Fixed settings
120 Logger.log(getClass(), LogLevel.TRACE, "Firefox uses manual settings");
121 result = setupFixedProxySelector(settings);
122 break;
123 case 2: // PAC Script
124 String pacScriptUrl = settings.getProperty("network.proxy.autoconfig_url", "");
125 Logger.log(getClass(), LogLevel.TRACE, "Firefox uses script (PAC) {0}", pacScriptUrl);
126 result = ProxyUtil.buildPacSelectorForUrl(pacScriptUrl);
127 break;
128 case 3: // Backward compatibility to netscape.
129 Logger.log(getClass(), LogLevel.TRACE, "Netscape compability mode -> uses no proxy");
130 result = NoProxySelector.getInstance();
131 break;
132 case 4: // WPAD auto config
133 Logger.log(getClass(), LogLevel.TRACE, "Firefox uses automatic detection (WPAD)");
134 result = new WpadProxySearchStrategy().getProxySelector();
135 break;
136 default:
137 break;
138 }
139
140 // Wrap in white list filter.
141 String noProxyList = settings.getProperty("network.proxy.no_proxies_on", null);
142 if (result != null && noProxyList != null && noProxyList.trim().length() > 0) {
143 Logger.log(getClass(), LogLevel.TRACE, "Firefox uses proxy bypass list for: {0}", noProxyList);
144 result = new ProxyBypassListSelector(noProxyList, result);
145 }
146
147 return result;
148 }
149
150 /*************************************************************************
151 * Gets the printable name of the search strategy.
152 *
153 * @return the printable name of the search strategy
154 ************************************************************************/
155
156 @Override
157 public String getName() {
158 return "firefox";
159 }
160
161 /*************************************************************************
162 * Reads the settings file and stores all settings in a Properties map.
163 *
164 * @return the parsed settings.
165 * @throws ProxyException
166 * on read error.
167 ************************************************************************/
168
169 public Properties readSettings() throws ProxyException {
170 try {
171 Properties settings = settingsParser.parseSettings(profileScanner);
172 return settings;
173 } catch (IOException e) {
174 throw new ProxyException("No Firefox installation found");
175 }
176 }
177
178 /*************************************************************************
179 * Parse the fixed proxy settings and build an ProxySelector for this a
180 * chained configuration.
181 *
182 * @param settings
183 * the proxy settings to evaluate.
184 ************************************************************************/
185
186 private ProxySelector setupFixedProxySelector(Properties settings) {
187 ProtocolDispatchSelector ps = new ProtocolDispatchSelector();
188 installHttpProxy(ps, settings);
189 if (isProxyShared(settings)) {
190 installSharedProxy(ps);
191 } else {
192 installFtpProxy(ps, settings);
193 installSecureProxy(ps, settings);
194 installSocksProxy(ps, settings);
195 }
196 return ps;
197 }
198
199 /*************************************************************************
200 * @param ps
201 * @param settings
202 * @throws NumberFormatException
203 ************************************************************************/
204
205 private void installFtpProxy(ProtocolDispatchSelector ps, Properties settings) throws NumberFormatException {
206 installSelectorForProtocol(ps, settings, "ftp");
207 }
208
209 /*************************************************************************
210 * @param ps
211 * @param settings
212 * @throws NumberFormatException
213 ************************************************************************/
214
215 private void installHttpProxy(ProtocolDispatchSelector ps, Properties settings) throws NumberFormatException {
216 installSelectorForProtocol(ps, settings, "http");
217 }
218
219 /*************************************************************************
220 * Checks if the "share proxy settings" option is set
221 *
222 * @param settings
223 * to parse
224 * @return true if the option is set else false
225 ************************************************************************/
226
227 private boolean isProxyShared(Properties settings) {
228 return Boolean.TRUE.toString()
229 .equals(settings.getProperty("network.proxy.share_proxy_settings", "false").toLowerCase());
230 }
231
232 /*************************************************************************
233 * @param ps
234 ************************************************************************/
235
236 private void installSharedProxy(ProtocolDispatchSelector ps) {
237 ProxySelector httpProxy = ps.getSelector("http");
238 if (httpProxy != null) {
239 ps.setFallbackSelector(httpProxy);
240 }
241 }
242
243 /*************************************************************************
244 * @param ps
245 * @param settings
246 * @throws NumberFormatException
247 ************************************************************************/
248
249 private void installSocksProxy(ProtocolDispatchSelector ps, Properties settings) throws NumberFormatException {
250 String proxyHost = settings.getProperty("network.proxy.socks", null);
251 int proxyPort = Integer.parseInt(settings.getProperty("network.proxy.socks_port", "0"));
252 if (proxyHost != null && proxyPort != 0) {
253 Logger.log(getClass(), LogLevel.TRACE, "Firefox socks proxy is {0}:{1}", proxyHost, proxyPort);
254 ps.setSelector("socks", new FixedSocksSelector(proxyHost, proxyPort));
255 }
256 }
257
258 /*************************************************************************
259 * @param ps
260 * @param settings
261 * @throws NumberFormatException
262 ************************************************************************/
263
264 private void installSecureProxy(ProtocolDispatchSelector ps, Properties settings) throws NumberFormatException {
265 String proxyHost = settings.getProperty("network.proxy.ssl", null);
266 int proxyPort = Integer.parseInt(settings.getProperty("network.proxy.ssl_port", "0"));
267 if (proxyHost != null && proxyPort != 0) {
268 Logger.log(getClass(), LogLevel.TRACE, "Firefox secure proxy is {0}:{1}", proxyHost, proxyPort);
269 ps.setSelector("https", new FixedProxySelector(proxyHost, proxyPort));
270 ps.setSelector("sftp", new FixedProxySelector(proxyHost, proxyPort));
271 }
272 }
273
274 /*************************************************************************
275 * Installs a proxy selector for the given protocol when settings are
276 * available.
277 *
278 * @param ps
279 * a ProtocolDispatchSelector to configure.
280 * @param settings
281 * to read the config from.
282 * @param protocol
283 * to configure.
284 * @throws NumberFormatException
285 ************************************************************************/
286
287 private void installSelectorForProtocol(ProtocolDispatchSelector ps, Properties settings, String protocol)
288 throws NumberFormatException {
289
290 String proxyHost = settings.getProperty("network.proxy." + protocol, null);
291 int proxyPort = Integer.parseInt(settings.getProperty("network.proxy." + protocol + "_port", "0"));
292 if (proxyHost != null && proxyPort != 0) {
293 Logger.log(getClass(), LogLevel.TRACE, "Firefox " + protocol + " proxy is {0}:{1}", proxyHost, proxyPort);
294 ps.setSelector(protocol, new FixedProxySelector(proxyHost, proxyPort));
295 }
296 }
297
298 }