| 1 |
package com.github.markusbernhardt.proxy.search.desktop.gnome; |
| 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.selector.direct.NoProxySelector; |
| 9 |
import com.github.markusbernhardt.proxy.selector.fixed.FixedProxySelector; |
| 10 |
import com.github.markusbernhardt.proxy.selector.misc.ProtocolDispatchSelector; |
| 11 |
import com.github.markusbernhardt.proxy.selector.whitelist.ProxyBypassListSelector; |
| 12 |
import com.github.markusbernhardt.proxy.util.Logger; |
| 13 |
import com.github.markusbernhardt.proxy.util.ProxyException; |
| 14 |
import com.github.markusbernhardt.proxy.util.ProxyUtil; |
| 15 |
import com.github.markusbernhardt.proxy.util.Logger.LogLevel; |
| 16 |
|
| 17 |
import java.io.BufferedReader; |
| 18 |
import java.io.InputStreamReader; |
| 19 |
|
| 20 |
|
| 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 |
public class GnomeDConfProxySearchStrategy implements ProxySearchStrategy { |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
public GnomeDConfProxySearchStrategy() { |
| 77 |
super(); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
@Override |
| 90 |
public ProxySelector getProxySelector() throws ProxyException { |
| 91 |
|
| 92 |
Logger.log(getClass(), LogLevel.TRACE, "Detecting Gnome proxy settings"); |
| 93 |
|
| 94 |
Properties settings = readSettings(); |
| 95 |
|
| 96 |
String type = settings.getProperty("org.gnome.system.proxy mode"); |
| 97 |
Logger.log(getClass(), LogLevel.TRACE, "Mode is :{0}", type); |
| 98 |
ProxySelector result = null; |
| 99 |
if (type == null) { |
| 100 |
String useProxy = settings.getProperty("org.gnome.system.proxy.http enabled"); |
| 101 |
if (useProxy == null) { |
| 102 |
return null; |
| 103 |
} |
| 104 |
type = Boolean.parseBoolean(useProxy) ? "manual" : "none"; |
| 105 |
} |
| 106 |
|
| 107 |
if ("none".equals(type)) { |
| 108 |
Logger.log(getClass(), LogLevel.TRACE, "Gnome uses no proxy"); |
| 109 |
result = NoProxySelector.getInstance(); |
| 110 |
} |
| 111 |
if ("manual".equals(type)) { |
| 112 |
Logger.log(getClass(), LogLevel.TRACE, "Gnome uses manual proxy settings"); |
| 113 |
result = setupFixedProxySelector(settings); |
| 114 |
} |
| 115 |
if ("auto".equals(type)) { |
| 116 |
String pacScriptUrl = settings.getProperty("org.gnome.system.proxy autoconfig-url", ""); |
| 117 |
Logger.log(getClass(), LogLevel.TRACE, "Gnome uses autodetect script {0}", pacScriptUrl); |
| 118 |
result = ProxyUtil.buildPacSelectorForUrl(pacScriptUrl); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
String noProxyList = settings.getProperty("org.gnome.system.proxy ignore-hosts", null); |
| 123 |
if (result != null && noProxyList != null && noProxyList.trim().length() > 0) { |
| 124 |
Logger.log(getClass(), LogLevel.TRACE, "Gnome uses proxy bypass list: {0}", noProxyList); |
| 125 |
result = new ProxyBypassListSelector(noProxyList, result); |
| 126 |
} |
| 127 |
|
| 128 |
return result; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
@Override |
| 138 |
public String getName() { |
| 139 |
return "gnome"; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
public Properties readSettings() throws ProxyException { |
| 151 |
Properties settings = new Properties(); |
| 152 |
try { |
| 153 |
parseSettings(settings); |
| 154 |
} catch (IOException e) { |
| 155 |
Logger.log(getClass(), LogLevel.ERROR, "Gnome settings read error.", e); |
| 156 |
throw new ProxyException(e); |
| 157 |
} |
| 158 |
return settings; |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
private ProxySelector setupFixedProxySelector(Properties settings) { |
| 170 |
if (!hasProxySettings(settings)) { |
| 171 |
return null; |
| 172 |
} |
| 173 |
ProtocolDispatchSelector ps = new ProtocolDispatchSelector(); |
| 174 |
installHttpSelector(settings, ps); |
| 175 |
|
| 176 |
if (useForAllProtocols(settings)) { |
| 177 |
ps.setFallbackSelector(ps.getSelector("http")); |
| 178 |
} else { |
| 179 |
installSecureSelector(settings, ps); |
| 180 |
installFtpSelector(settings, ps); |
| 181 |
installSocksSelector(settings, ps); |
| 182 |
} |
| 183 |
return ps; |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
private boolean useForAllProtocols(Properties settings) { |
| 195 |
return Boolean.parseBoolean(settings.getProperty("org.gnome.system.proxy use-same-proxy", "false")); |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
private boolean hasProxySettings(Properties settings) { |
| 207 |
String proxyHost = settings.getProperty("org.gnome.system.proxy.http host", null); |
| 208 |
return proxyHost != null && proxyHost.length() > 0; |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
private void installHttpSelector(Properties settings, ProtocolDispatchSelector ps) throws NumberFormatException { |
| 222 |
String proxyHost = settings.getProperty("org.gnome.system.proxy.http host", null); |
| 223 |
int proxyPort = Integer.parseInt(settings.getProperty("org.gnome.system.proxy.http port", "0").trim()); |
| 224 |
if (proxyHost != null && proxyHost.length() > 0 && proxyPort > 0) { |
| 225 |
Logger.log(getClass(), LogLevel.TRACE, "Gnome http proxy is {0}:{1}", proxyHost, proxyPort); |
| 226 |
ps.setSelector("http", new FixedProxySelector(proxyHost.trim(), proxyPort)); |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
private void installSocksSelector(Properties settings, ProtocolDispatchSelector ps) throws NumberFormatException { |
| 241 |
String proxyHost = settings.getProperty("org.gnome.system.proxy.socks host", null); |
| 242 |
int proxyPort = Integer.parseInt(settings.getProperty("org.gnome.system.proxy.socks port", "0").trim()); |
| 243 |
if (proxyHost != null && proxyHost.length() > 0 && proxyPort > 0) { |
| 244 |
Logger.log(getClass(), LogLevel.TRACE, "Gnome socks proxy is {0}:{1}", proxyHost, proxyPort); |
| 245 |
ps.setSelector("socks", new FixedProxySelector(proxyHost.trim(), proxyPort)); |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
private void installFtpSelector(Properties settings, ProtocolDispatchSelector ps) throws NumberFormatException { |
| 256 |
String proxyHost = settings.getProperty("org.gnome.system.proxy.ftp host", null); |
| 257 |
int proxyPort = Integer.parseInt(settings.getProperty("org.gnome.system.proxy.ftp port", "0").trim()); |
| 258 |
if (proxyHost != null && proxyHost.length() > 0 && proxyPort > 0) { |
| 259 |
Logger.log(getClass(), LogLevel.TRACE, "Gnome ftp proxy is {0}:{1}", proxyHost, proxyPort); |
| 260 |
ps.setSelector("ftp", new FixedProxySelector(proxyHost.trim(), proxyPort)); |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
private void installSecureSelector(Properties settings, ProtocolDispatchSelector ps) throws NumberFormatException { |
| 271 |
String proxyHost = settings.getProperty("org.gnome.system.proxy.https host", null); |
| 272 |
int proxyPort = Integer.parseInt(settings.getProperty("org.gnome.system.proxy.https port", "0").trim()); |
| 273 |
if (proxyHost != null && proxyHost.length() > 0 && proxyPort > 0) { |
| 274 |
Logger.log(getClass(), LogLevel.TRACE, "Gnome secure proxy is {0}:{1}", proxyHost, proxyPort); |
| 275 |
ps.setSelector("https", new FixedProxySelector(proxyHost.trim(), proxyPort)); |
| 276 |
ps.setSelector("sftp", new FixedProxySelector(proxyHost.trim(), proxyPort)); |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
private Properties parseSettings(Properties settings) throws IOException { |
| 293 |
String line; |
| 294 |
Logger.log(getClass(), LogLevel.TRACE, "exec gsettings list-recursively org.gnome.system.proxy"); |
| 295 |
Process p = Runtime.getRuntime().exec("gsettings list-recursively org.gnome.system.proxy"); |
| 296 |
BufferedReader bri = new BufferedReader(new InputStreamReader(p.getInputStream())); |
| 297 |
|
| 298 |
while ((line = bri.readLine()) != null) { |
| 299 |
Logger.log(getClass(), LogLevel.TRACE, line); |
| 300 |
int schemaSep = line.indexOf(" "); |
| 301 |
int keySep = line.indexOf(" ", schemaSep + 1); |
| 302 |
String entry = line.substring(0, keySep); |
| 303 |
String value = line.substring(keySep + 1).replaceAll("'", ""); |
| 304 |
|
| 305 |
|
| 306 |
if (value.matches("\\[.*\\]")) { |
| 307 |
value = value.replaceAll("\\[|\\]| ", ""); |
| 308 |
} |
| 309 |
|
| 310 |
Logger.log(getClass(), LogLevel.TRACE, "prop is: {0}|{1}", entry, value); |
| 311 |
settings.setProperty(entry, value); |
| 312 |
} |
| 313 |
try { |
| 314 |
p.waitFor(); |
| 315 |
} catch (InterruptedException ex) { |
| 316 |
Logger.log(getClass(), LogLevel.ERROR, ex.getMessage()); |
| 317 |
} |
| 318 |
return settings; |
| 319 |
} |
| 320 |
|
| 321 |
} |