| 1 |
package com.github.markusbernhardt.proxy.search.desktop.gnome; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.io.IOException; |
| 5 |
import java.net.ProxySelector; |
| 6 |
import java.util.Properties; |
| 7 |
|
| 8 |
import javax.xml.parsers.DocumentBuilder; |
| 9 |
import javax.xml.parsers.DocumentBuilderFactory; |
| 10 |
import javax.xml.parsers.ParserConfigurationException; |
| 11 |
|
| 12 |
import org.w3c.dom.Document; |
| 13 |
import org.w3c.dom.Element; |
| 14 |
import org.w3c.dom.Node; |
| 15 |
import org.w3c.dom.NodeList; |
| 16 |
import org.xml.sax.SAXException; |
| 17 |
|
| 18 |
import com.github.markusbernhardt.proxy.ProxySearchStrategy; |
| 19 |
import com.github.markusbernhardt.proxy.selector.direct.NoProxySelector; |
| 20 |
import com.github.markusbernhardt.proxy.selector.fixed.FixedProxySelector; |
| 21 |
import com.github.markusbernhardt.proxy.selector.misc.ProtocolDispatchSelector; |
| 22 |
import com.github.markusbernhardt.proxy.selector.whitelist.ProxyBypassListSelector; |
| 23 |
import com.github.markusbernhardt.proxy.util.EmptyXMLResolver; |
| 24 |
import com.github.markusbernhardt.proxy.util.Logger; |
| 25 |
import com.github.markusbernhardt.proxy.util.PlatformUtil; |
| 26 |
import com.github.markusbernhardt.proxy.util.ProxyException; |
| 27 |
import com.github.markusbernhardt.proxy.util.ProxyUtil; |
| 28 |
import com.github.markusbernhardt.proxy.util.Logger.LogLevel; |
| 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 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
public class GnomeProxySearchStrategy implements ProxySearchStrategy { |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public GnomeProxySearchStrategy() { |
| 83 |
super(); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
@Override |
| 96 |
public ProxySelector getProxySelector() throws ProxyException { |
| 97 |
|
| 98 |
Logger.log(getClass(), LogLevel.TRACE, "Detecting Gnome proxy settings"); |
| 99 |
|
| 100 |
Properties settings = readSettings(); |
| 101 |
|
| 102 |
String type = settings.getProperty("/system/proxy/mode"); |
| 103 |
ProxySelector result = null; |
| 104 |
if (type == null) { |
| 105 |
String useProxy = settings.getProperty("/system/http_proxy/use_http_proxy"); |
| 106 |
if (useProxy == null) { |
| 107 |
return null; |
| 108 |
} |
| 109 |
type = Boolean.parseBoolean(useProxy) ? "manual" : "none"; |
| 110 |
} |
| 111 |
|
| 112 |
if ("none".equals(type)) { |
| 113 |
Logger.log(getClass(), LogLevel.TRACE, "Gnome uses no proxy"); |
| 114 |
result = NoProxySelector.getInstance(); |
| 115 |
} |
| 116 |
if ("manual".equals(type)) { |
| 117 |
Logger.log(getClass(), LogLevel.TRACE, "Gnome uses manual proxy settings"); |
| 118 |
result = setupFixedProxySelector(settings); |
| 119 |
} |
| 120 |
if ("auto".equals(type)) { |
| 121 |
String pacScriptUrl = settings.getProperty("/system/proxy/autoconfig_url", ""); |
| 122 |
Logger.log(getClass(), LogLevel.TRACE, "Gnome uses autodetect script {0}", pacScriptUrl); |
| 123 |
result = ProxyUtil.buildPacSelectorForUrl(pacScriptUrl); |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
String noProxyList = settings.getProperty("/system/http_proxy/ignore_hosts", null); |
| 128 |
if (result != null && noProxyList != null && noProxyList.trim().length() > 0) { |
| 129 |
Logger.log(getClass(), LogLevel.TRACE, "Gnome uses proxy bypass list: {0}", noProxyList); |
| 130 |
result = new ProxyBypassListSelector(noProxyList, result); |
| 131 |
} |
| 132 |
|
| 133 |
return result; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
@Override |
| 143 |
public String getName() { |
| 144 |
return "gnome"; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
public Properties readSettings() throws ProxyException { |
| 156 |
Properties settings = new Properties(); |
| 157 |
try { |
| 158 |
parseSettings("/system/proxy/", settings); |
| 159 |
parseSettings("/system/http_proxy/", settings); |
| 160 |
} catch (IOException e) { |
| 161 |
Logger.log(getClass(), LogLevel.ERROR, "Gnome settings file error.", e); |
| 162 |
throw new ProxyException(e); |
| 163 |
} |
| 164 |
return settings; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
private File findSettingsFile(String context) { |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
File userDir = new File(PlatformUtil.getUserHomeDir()); |
| 181 |
|
| 182 |
|
| 183 |
StringBuilder path = new StringBuilder(); |
| 184 |
String[] parts = context.split("/"); |
| 185 |
for (String part : parts) { |
| 186 |
path.append(part); |
| 187 |
path.append(File.separator); |
| 188 |
} |
| 189 |
|
| 190 |
File settingsFile = new File(userDir, ".gconf" + File.separator + path.toString() + "%gconf.xml"); |
| 191 |
if (!settingsFile.exists()) { |
| 192 |
Logger.log(getClass(), LogLevel.WARNING, "Gnome settings: {0} not found.", settingsFile); |
| 193 |
return null; |
| 194 |
} |
| 195 |
return settingsFile; |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
private ProxySelector setupFixedProxySelector(Properties settings) { |
| 207 |
if (!hasProxySettings(settings)) { |
| 208 |
return null; |
| 209 |
} |
| 210 |
ProtocolDispatchSelector ps = new ProtocolDispatchSelector(); |
| 211 |
installHttpSelector(settings, ps); |
| 212 |
|
| 213 |
if (useForAllProtocols(settings)) { |
| 214 |
ps.setFallbackSelector(ps.getSelector("http")); |
| 215 |
} else { |
| 216 |
installSecureSelector(settings, ps); |
| 217 |
installFtpSelector(settings, ps); |
| 218 |
installSocksSelector(settings, ps); |
| 219 |
} |
| 220 |
return ps; |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
private boolean useForAllProtocols(Properties settings) { |
| 232 |
return Boolean.parseBoolean(settings.getProperty("/system/http_proxy/use_same_proxy", "false")); |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
private boolean hasProxySettings(Properties settings) { |
| 244 |
String proxyHost = settings.getProperty("/system/http_proxy/host", null); |
| 245 |
return proxyHost != null && proxyHost.length() > 0; |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
private void installHttpSelector(Properties settings, ProtocolDispatchSelector ps) throws NumberFormatException { |
| 259 |
String proxyHost = settings.getProperty("/system/http_proxy/host", null); |
| 260 |
int proxyPort = Integer.parseInt(settings.getProperty("/system/http_proxy/port", "0").trim()); |
| 261 |
if (proxyHost != null && proxyHost.length() > 0 && proxyPort > 0) { |
| 262 |
Logger.log(getClass(), LogLevel.TRACE, "Gnome http proxy is {0}:{1}", proxyHost, proxyPort); |
| 263 |
ps.setSelector("http", new FixedProxySelector(proxyHost.trim(), proxyPort)); |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
private void installSocksSelector(Properties settings, ProtocolDispatchSelector ps) throws NumberFormatException { |
| 278 |
String proxyHost = settings.getProperty("/system/proxy/socks_host", null); |
| 279 |
int proxyPort = Integer.parseInt(settings.getProperty("/system/proxy/socks_port", "0").trim()); |
| 280 |
if (proxyHost != null && proxyHost.length() > 0 && proxyPort > 0) { |
| 281 |
Logger.log(getClass(), LogLevel.TRACE, "Gnome socks proxy is {0}:{1}", proxyHost, proxyPort); |
| 282 |
ps.setSelector("socks", new FixedProxySelector(proxyHost.trim(), proxyPort)); |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
private void installFtpSelector(Properties settings, ProtocolDispatchSelector ps) throws NumberFormatException { |
| 293 |
String proxyHost = settings.getProperty("/system/proxy/ftp_host", null); |
| 294 |
int proxyPort = Integer.parseInt(settings.getProperty("/system/proxy/ftp_port", "0").trim()); |
| 295 |
if (proxyHost != null && proxyHost.length() > 0 && proxyPort > 0) { |
| 296 |
Logger.log(getClass(), LogLevel.TRACE, "Gnome ftp proxy is {0}:{1}", proxyHost, proxyPort); |
| 297 |
ps.setSelector("ftp", new FixedProxySelector(proxyHost.trim(), proxyPort)); |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
private void installSecureSelector(Properties settings, ProtocolDispatchSelector ps) throws NumberFormatException { |
| 308 |
String proxyHost = settings.getProperty("/system/proxy/secure_host", null); |
| 309 |
int proxyPort = Integer.parseInt(settings.getProperty("/system/proxy/secure_port", "0").trim()); |
| 310 |
if (proxyHost != null && proxyHost.length() > 0 && proxyPort > 0) { |
| 311 |
Logger.log(getClass(), LogLevel.TRACE, "Gnome secure proxy is {0}:{1}", proxyHost, proxyPort); |
| 312 |
ps.setSelector("https", new FixedProxySelector(proxyHost.trim(), proxyPort)); |
| 313 |
ps.setSelector("sftp", new FixedProxySelector(proxyHost.trim(), proxyPort)); |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
private Properties parseSettings(String context, Properties settings) throws IOException { |
| 330 |
|
| 331 |
|
| 332 |
File settingsFile = findSettingsFile(context); |
| 333 |
if (settingsFile == null) { |
| 334 |
return settings; |
| 335 |
} |
| 336 |
|
| 337 |
try { |
| 338 |
DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); |
| 339 |
documentBuilder.setEntityResolver(new EmptyXMLResolver()); |
| 340 |
Document doc = documentBuilder.parse(settingsFile); |
| 341 |
Element root = doc.getDocumentElement(); |
| 342 |
Node entry = root.getFirstChild(); |
| 343 |
while (entry != null) { |
| 344 |
if ("entry".equals(entry.getNodeName()) && entry instanceof Element) { |
| 345 |
String entryName = ((Element) entry).getAttribute("name"); |
| 346 |
settings.setProperty(context + entryName, getEntryValue((Element) entry)); |
| 347 |
} |
| 348 |
entry = entry.getNextSibling(); |
| 349 |
} |
| 350 |
} catch (SAXException e) { |
| 351 |
Logger.log(getClass(), LogLevel.ERROR, "Gnome settings parse error", e); |
| 352 |
throw new IOException(e.getMessage()); |
| 353 |
} catch (ParserConfigurationException e) { |
| 354 |
Logger.log(getClass(), LogLevel.ERROR, "Gnome settings parse error", e); |
| 355 |
throw new IOException(e.getMessage()); |
| 356 |
} |
| 357 |
|
| 358 |
return settings; |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
private String getEntryValue(Element entry) { |
| 370 |
String type = entry.getAttribute("type"); |
| 371 |
|
| 372 |
if ("int".equals(type) || "bool".equals(type)) { |
| 373 |
return entry.getAttribute("value"); |
| 374 |
} |
| 375 |
if ("string".equals(type)) { |
| 376 |
NodeList list = entry.getElementsByTagName("stringvalue"); |
| 377 |
if (list.getLength() > 0) { |
| 378 |
return list.item(0).getTextContent(); |
| 379 |
} |
| 380 |
} |
| 381 |
if ("list".equals(type)) { |
| 382 |
StringBuilder result = new StringBuilder(); |
| 383 |
NodeList list = entry.getElementsByTagName("li"); |
| 384 |
|
| 385 |
|
| 386 |
for (int i = 0; i < list.getLength(); i++) { |
| 387 |
if (result.length() > 0) { |
| 388 |
result.append(","); |
| 389 |
} |
| 390 |
result.append(getEntryValue((Element) list.item(i))); |
| 391 |
} |
| 392 |
return result.toString(); |
| 393 |
} |
| 394 |
return null; |
| 395 |
} |
| 396 |
|
| 397 |
} |