| 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 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
public class FirefoxProxySearchStrategy implements ProxySearchStrategy { |
| 71 |
|
| 72 |
private FirefoxProfileSource profileScanner; |
| 73 |
private FirefoxSettingParser settingsParser; |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 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 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 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: |
| 112 |
Logger.log(getClass(), LogLevel.TRACE, "Firefox uses system settings"); |
| 113 |
result = new DesktopProxySearchStrategy().getProxySelector(); |
| 114 |
break; |
| 115 |
case 0: |
| 116 |
Logger.log(getClass(), LogLevel.TRACE, "Firefox uses no proxy"); |
| 117 |
result = NoProxySelector.getInstance(); |
| 118 |
break; |
| 119 |
case 1: |
| 120 |
Logger.log(getClass(), LogLevel.TRACE, "Firefox uses manual settings"); |
| 121 |
result = setupFixedProxySelector(settings); |
| 122 |
break; |
| 123 |
case 2: |
| 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: |
| 129 |
Logger.log(getClass(), LogLevel.TRACE, "Netscape compability mode -> uses no proxy"); |
| 130 |
result = NoProxySelector.getInstance(); |
| 131 |
break; |
| 132 |
case 4: |
| 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 |
|
| 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 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
@Override |
| 157 |
public String getName() { |
| 158 |
return "firefox"; |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 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 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 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 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
private void installFtpProxy(ProtocolDispatchSelector ps, Properties settings) throws NumberFormatException { |
| 206 |
installSelectorForProtocol(ps, settings, "ftp"); |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
private void installHttpProxy(ProtocolDispatchSelector ps, Properties settings) throws NumberFormatException { |
| 216 |
installSelectorForProtocol(ps, settings, "http"); |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 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 |
|
| 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 |
|
| 245 |
|
| 246 |
|
| 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 |
|
| 260 |
|
| 261 |
|
| 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 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 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 |
} |