| 1 |
package com.github.markusbernhardt.proxy.search.desktop.kde; |
| 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.env.EnvProxySearchStrategy; |
| 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.misc.ProtocolDispatchSelector; |
| 13 |
import com.github.markusbernhardt.proxy.selector.whitelist.ProxyBypassListSelector; |
| 14 |
import com.github.markusbernhardt.proxy.selector.whitelist.UseProxyWhiteListSelector; |
| 15 |
import com.github.markusbernhardt.proxy.util.Logger; |
| 16 |
import com.github.markusbernhardt.proxy.util.Logger.LogLevel; |
| 17 |
import com.github.markusbernhardt.proxy.util.ProxyException; |
| 18 |
import com.github.markusbernhardt.proxy.util.ProxyUtil; |
| 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 |
public class KdeProxySearchStrategy implements ProxySearchStrategy { |
| 51 |
|
| 52 |
private KdeSettingsParser settingsParser; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public KdeProxySearchStrategy() { |
| 61 |
this(new KdeSettingsParser()); |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
public KdeProxySearchStrategy(KdeSettingsParser settingsParser) { |
| 73 |
super(); |
| 74 |
this.settingsParser = settingsParser; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
@Override |
| 87 |
public ProxySelector getProxySelector() throws ProxyException { |
| 88 |
|
| 89 |
Logger.log(getClass(), LogLevel.TRACE, "Detecting Kde proxy settings"); |
| 90 |
|
| 91 |
Properties settings = readSettings(); |
| 92 |
if (settings == null) { |
| 93 |
return null; |
| 94 |
} |
| 95 |
|
| 96 |
ProxySelector result = null; |
| 97 |
int type = Integer.parseInt(settings.getProperty("ProxyType", "-1")); |
| 98 |
switch (type) { |
| 99 |
case 0: |
| 100 |
Logger.log(getClass(), LogLevel.TRACE, "Kde uses no proxy"); |
| 101 |
result = NoProxySelector.getInstance(); |
| 102 |
break; |
| 103 |
case 1: |
| 104 |
Logger.log(getClass(), LogLevel.TRACE, "Kde uses manual proxy settings"); |
| 105 |
result = setupFixedProxySelector(settings); |
| 106 |
break; |
| 107 |
case 2: |
| 108 |
String pacScriptUrl = settings.getProperty("Proxy Config Script", ""); |
| 109 |
Logger.log(getClass(), LogLevel.TRACE, "Kde uses autodetect script {0}", pacScriptUrl); |
| 110 |
result = ProxyUtil.buildPacSelectorForUrl(pacScriptUrl); |
| 111 |
break; |
| 112 |
case 3: |
| 113 |
Logger.log(getClass(), LogLevel.TRACE, "Kde uses WPAD to detect the proxy"); |
| 114 |
result = new WpadProxySearchStrategy().getProxySelector(); |
| 115 |
break; |
| 116 |
case 4: |
| 117 |
Logger.log(getClass(), LogLevel.TRACE, "Kde reads proxy from environment"); |
| 118 |
result = setupEnvVarSelector(settings); |
| 119 |
break; |
| 120 |
default: |
| 121 |
break; |
| 122 |
} |
| 123 |
|
| 124 |
return result; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
@Override |
| 134 |
public String getName() { |
| 135 |
return "kde"; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
private Properties readSettings() throws ProxyException { |
| 146 |
try { |
| 147 |
return this.settingsParser.parseSettings(); |
| 148 |
} catch (IOException e) { |
| 149 |
Logger.log(getClass(), LogLevel.ERROR, "Can't parse settings.", e); |
| 150 |
throw new ProxyException(e); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
private ProxySelector setupEnvVarSelector(Properties settings) { |
| 163 |
ProxySelector result; |
| 164 |
result = new EnvProxySearchStrategy(settings.getProperty("httpProxy"), settings.getProperty("httpsProxy"), |
| 165 |
settings.getProperty("ftpProxy"), settings.getProperty("NoProxyFor")).getProxySelector(); |
| 166 |
return result; |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
private ProxySelector setupFixedProxySelector(Properties settings) { |
| 178 |
String proxyVar = settings.getProperty("httpProxy", null); |
| 179 |
FixedProxySelector httpPS = ProxyUtil.parseProxySettings(proxyVar); |
| 180 |
if (httpPS == null) { |
| 181 |
Logger.log(getClass(), LogLevel.TRACE, "Kde http proxy is {0}", proxyVar); |
| 182 |
return null; |
| 183 |
} |
| 184 |
|
| 185 |
ProtocolDispatchSelector ps = new ProtocolDispatchSelector(); |
| 186 |
ps.setSelector("http", httpPS); |
| 187 |
|
| 188 |
proxyVar = settings.getProperty("httpsProxy", null); |
| 189 |
FixedProxySelector httpsPS = ProxyUtil.parseProxySettings(proxyVar); |
| 190 |
if (httpsPS != null) { |
| 191 |
Logger.log(getClass(), LogLevel.TRACE, "Kde https proxy is {0}", proxyVar); |
| 192 |
ps.setSelector("https", httpsPS); |
| 193 |
} |
| 194 |
|
| 195 |
proxyVar = settings.getProperty("ftpProxy", null); |
| 196 |
FixedProxySelector ftpPS = ProxyUtil.parseProxySettings(proxyVar); |
| 197 |
if (ftpPS != null) { |
| 198 |
Logger.log(getClass(), LogLevel.TRACE, "Kde ftp proxy is {0}", proxyVar); |
| 199 |
ps.setSelector("ftp", ftpPS); |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
String noProxyList = settings.getProperty("NoProxyFor", null); |
| 204 |
if (noProxyList != null && noProxyList.trim().length() > 0) { |
| 205 |
boolean reverse = "true".equals(settings.getProperty("ReversedException", "false")); |
| 206 |
if (reverse) { |
| 207 |
Logger.log(getClass(), LogLevel.TRACE, "Kde proxy blacklist is {0}", noProxyList); |
| 208 |
return new UseProxyWhiteListSelector(noProxyList, ps); |
| 209 |
} else { |
| 210 |
Logger.log(getClass(), LogLevel.TRACE, "Kde proxy whitelist is {0}", noProxyList); |
| 211 |
return new ProxyBypassListSelector(noProxyList, ps); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
return ps; |
| 216 |
} |
| 217 |
|
| 218 |
} |