| 1 |
package com.github.markusbernhardt.proxy.search.desktop.osx; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.io.IOException; |
| 5 |
import java.net.NetworkInterface; |
| 6 |
import java.net.ProxySelector; |
| 7 |
import java.net.SocketException; |
| 8 |
import java.util.ArrayList; |
| 9 |
import java.util.Arrays; |
| 10 |
import java.util.Enumeration; |
| 11 |
import java.util.List; |
| 12 |
|
| 13 |
import com.github.markusbernhardt.proxy.ProxySearchStrategy; |
| 14 |
import com.github.markusbernhardt.proxy.search.browser.ie.IELocalByPassFilter; |
| 15 |
import com.github.markusbernhardt.proxy.search.wpad.WpadProxySearchStrategy; |
| 16 |
import com.github.markusbernhardt.proxy.selector.direct.NoProxySelector; |
| 17 |
import com.github.markusbernhardt.proxy.selector.fixed.FixedProxySelector; |
| 18 |
import com.github.markusbernhardt.proxy.selector.fixed.FixedSocksSelector; |
| 19 |
import com.github.markusbernhardt.proxy.selector.misc.ProtocolDispatchSelector; |
| 20 |
import com.github.markusbernhardt.proxy.selector.whitelist.ProxyBypassListSelector; |
| 21 |
import com.github.markusbernhardt.proxy.util.Logger; |
| 22 |
import com.github.markusbernhardt.proxy.util.Logger.LogLevel; |
| 23 |
import com.github.markusbernhardt.proxy.util.PListParser; |
| 24 |
import com.github.markusbernhardt.proxy.util.PListParser.Dict; |
| 25 |
import com.github.markusbernhardt.proxy.util.PListParser.XmlParseException; |
| 26 |
import com.github.markusbernhardt.proxy.util.ProxyException; |
| 27 |
import com.github.markusbernhardt.proxy.util.ProxyUtil; |
| 28 |
import com.github.markusbernhardt.proxy.util.UriFilter; |
| 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 |
public class OsxProxySearchStrategy implements ProxySearchStrategy { |
| 59 |
|
| 60 |
public static final String OVERRIDE_SETTINGS_FILE = "com.github.markusbernhardt.proxy.osx.settingsFile"; |
| 61 |
public static final String OVERRIDE_ACCEPTED_DEVICES = "com.github.markusbernhardt.proxy.osx.acceptedDevices"; |
| 62 |
|
| 63 |
private static final String SETTINGS_FILE = "/Library/Preferences/SystemConfiguration/preferences.plist"; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public OsxProxySearchStrategy() { |
| 72 |
super(); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
@Override |
| 85 |
public ProxySelector getProxySelector() throws ProxyException { |
| 86 |
|
| 87 |
Logger.log(getClass(), LogLevel.TRACE, "Detecting OSX proxy settings"); |
| 88 |
|
| 89 |
try { |
| 90 |
List<String> acceptedInterfaces = getNetworkInterfaces(); |
| 91 |
|
| 92 |
Dict settings = PListParser.load(getSettingsFile()); |
| 93 |
Object currentSet = settings.getAtPath("/CurrentSet"); |
| 94 |
if (currentSet == null) { |
| 95 |
throw new ProxyException("CurrentSet not defined"); |
| 96 |
} |
| 97 |
|
| 98 |
Dict networkSet = (Dict) settings.getAtPath(String.valueOf(currentSet)); |
| 99 |
|
| 100 |
|
| 101 |
List<?> serviceOrder = (List<?>) networkSet.getAtPath("/Network/Global/IPv4/ServiceOrder"); |
| 102 |
if (serviceOrder == null || serviceOrder.size() == 0) { |
| 103 |
throw new ProxyException("ServiceOrder not defined"); |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
Dict proxySettings = null; |
| 110 |
for (int i = 0; i < serviceOrder.size() && proxySettings == null; i++) { |
| 111 |
Object candidateService = serviceOrder.get(i); |
| 112 |
Object networkService = networkSet.getAtPath("/Network/Service/" + candidateService + "/__LINK__"); |
| 113 |
if (networkService == null) { |
| 114 |
throw new ProxyException("NetworkService not defined."); |
| 115 |
} |
| 116 |
Dict selectedServiceSettings = (Dict) settings.getAtPath("" + networkService); |
| 117 |
String interfaceName = (String) selectedServiceSettings.getAtPath("/Interface/DeviceName"); |
| 118 |
if (acceptedInterfaces.contains(interfaceName)) { |
| 119 |
Logger.log(getClass(), LogLevel.TRACE, "Looking up proxies for device " + interfaceName); |
| 120 |
proxySettings = (Dict) selectedServiceSettings.getAtPath("/Proxies"); |
| 121 |
} |
| 122 |
} |
| 123 |
if (proxySettings == null) { |
| 124 |
return NoProxySelector.getInstance(); |
| 125 |
} |
| 126 |
|
| 127 |
return buildSelector(proxySettings); |
| 128 |
} catch (XmlParseException e) { |
| 129 |
throw new ProxyException(e); |
| 130 |
} catch (IOException e) { |
| 131 |
throw new ProxyException(e); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
@Override |
| 142 |
public String getName() { |
| 143 |
return "osx"; |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
private ProxySelector buildSelector(Dict proxySettings) throws ProxyException { |
| 157 |
ProtocolDispatchSelector ps = new ProtocolDispatchSelector(); |
| 158 |
installSelectorForProtocol(proxySettings, ps, "HTTP"); |
| 159 |
installSelectorForProtocol(proxySettings, ps, "HTTPS"); |
| 160 |
installSelectorForProtocol(proxySettings, ps, "FTP"); |
| 161 |
installSelectorForProtocol(proxySettings, ps, "Gopher"); |
| 162 |
installSelectorForProtocol(proxySettings, ps, "RTSP"); |
| 163 |
installSocksProxy(proxySettings, ps); |
| 164 |
|
| 165 |
ProxySelector result = ps; |
| 166 |
result = installPacProxyIfAvailable(proxySettings, result); |
| 167 |
result = autodetectProxyIfAvailable(proxySettings, result); |
| 168 |
|
| 169 |
if (result != null) { |
| 170 |
result = installExceptionList(proxySettings, result); |
| 171 |
result = installSimpleHostFilter(proxySettings, result); |
| 172 |
} |
| 173 |
return result; |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
private List<String> getNetworkInterfaces() throws SocketException { |
| 184 |
String override = System.getProperty(OVERRIDE_ACCEPTED_DEVICES); |
| 185 |
if (override != null && override.length() > 0) { |
| 186 |
return Arrays.asList(override.split(";")); |
| 187 |
} |
| 188 |
|
| 189 |
List<String> acceptedInterfaces = new ArrayList<String>(); |
| 190 |
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); |
| 191 |
while (interfaces.hasMoreElements()) { |
| 192 |
NetworkInterface ni = interfaces.nextElement(); |
| 193 |
if (isInterfaceAllowed(ni)) { |
| 194 |
acceptedInterfaces.add(ni.getName()); |
| 195 |
} |
| 196 |
} |
| 197 |
return acceptedInterfaces; |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
private boolean isInterfaceAllowed(NetworkInterface ni) throws SocketException { |
| 211 |
return !ni.isLoopback() && !ni.isPointToPoint() && |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
!ni.isVirtual() && ni.isUp(); |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
private File getSettingsFile() { |
| 225 |
File result = new File(SETTINGS_FILE); |
| 226 |
String overrideFile = System.getProperty(OVERRIDE_SETTINGS_FILE); |
| 227 |
if (overrideFile != null) { |
| 228 |
return new File(overrideFile); |
| 229 |
} |
| 230 |
return result; |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
private ProxySelector installSimpleHostFilter(Dict proxySettings, ProxySelector result) { |
| 244 |
if (isActive(proxySettings.get("ExcludeSimpleHostnames"))) { |
| 245 |
List<UriFilter> localBypassFilter = new ArrayList<UriFilter>(); |
| 246 |
localBypassFilter.add(new IELocalByPassFilter()); |
| 247 |
result = new ProxyBypassListSelector(localBypassFilter, result); |
| 248 |
} |
| 249 |
return result; |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
private ProxySelector installExceptionList(Dict proxySettings, ProxySelector result) { |
| 263 |
List<?> proxyExceptions = (List<?>) proxySettings.get("ExceptionsList"); |
| 264 |
if (proxyExceptions != null && proxyExceptions.size() > 0) { |
| 265 |
Logger.log(getClass(), LogLevel.TRACE, "OSX uses proxy bypass list: {0}", proxyExceptions); |
| 266 |
String noProxyList = toCommaSeparatedString(proxyExceptions); |
| 267 |
result = new ProxyBypassListSelector(noProxyList, result); |
| 268 |
} |
| 269 |
return result; |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
private String toCommaSeparatedString(List<?> proxyExceptions) { |
| 281 |
StringBuilder result = new StringBuilder(); |
| 282 |
for (Object object : proxyExceptions) { |
| 283 |
if (result.length() > 0) { |
| 284 |
result.append(","); |
| 285 |
} |
| 286 |
result.append(object); |
| 287 |
} |
| 288 |
return result.toString(); |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
private ProxySelector autodetectProxyIfAvailable(Dict proxySettings, ProxySelector result) throws ProxyException { |
| 304 |
if (isActive(proxySettings.get("ProxyAutoDiscoveryEnable"))) { |
| 305 |
ProxySelector wp = new WpadProxySearchStrategy().getProxySelector(); |
| 306 |
if (wp != null) { |
| 307 |
result = wp; |
| 308 |
} |
| 309 |
} |
| 310 |
return result; |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
private ProxySelector installPacProxyIfAvailable(Dict proxySettings, ProxySelector result) { |
| 324 |
if (isActive(proxySettings.get("ProxyAutoConfigEnable"))) { |
| 325 |
String url = (String) proxySettings.get("ProxyAutoConfigURLString"); |
| 326 |
result = ProxyUtil.buildPacSelectorForUrl(url); |
| 327 |
} |
| 328 |
return result; |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
private void installSocksProxy(Dict proxySettings, ProtocolDispatchSelector ps) { |
| 341 |
if (isActive(proxySettings.get("SOCKSEnable"))) { |
| 342 |
String proxyHost = (String) proxySettings.get("SOCKSProxy"); |
| 343 |
int proxyPort = (Integer) proxySettings.get("SOCKSPort"); |
| 344 |
ps.setSelector("socks", new FixedSocksSelector(proxyHost, proxyPort)); |
| 345 |
Logger.log(getClass(), LogLevel.TRACE, "OSX socks proxy is {0}:{1}", proxyHost, proxyPort); |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
private void installSelectorForProtocol(Dict proxySettings, ProtocolDispatchSelector ps, String protocol) { |
| 362 |
String prefix = protocol.trim(); |
| 363 |
if (isActive(proxySettings.get(prefix + "Enable"))) { |
| 364 |
String proxyHost = (String) proxySettings.get(prefix + "Proxy"); |
| 365 |
int proxyPort = (Integer) proxySettings.get(prefix + "Port"); |
| 366 |
FixedProxySelector fp = new FixedProxySelector(proxyHost, proxyPort); |
| 367 |
ps.setSelector(protocol.toLowerCase(), fp); |
| 368 |
Logger.log(getClass(), LogLevel.TRACE, "OSX uses for {0} the proxy {1}:{2}", protocol, proxyHost, |
| 369 |
proxyPort); |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
private boolean isActive(Object value) { |
| 382 |
return Integer.valueOf(1).equals(value); |
| 383 |
} |
| 384 |
|
| 385 |
} |