summaryrefslogtreecommitdiffstats
path: root/src/main/java/com/btr/proxy/search/desktop/osx/OsxProxySearchStrategy.java
blob: 71d7a8f758e5b05a51d8f768504080b19c9c024a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package com.btr.proxy.search.desktop.osx;

import java.io.File;
import java.io.IOException;
import java.net.NetworkInterface;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.List;

import com.btr.proxy.search.ProxySearchStrategy;
import com.btr.proxy.search.browser.ie.IELocalByPassFilter;
import com.btr.proxy.search.wpad.WpadProxySearchStrategy;
import com.btr.proxy.selector.direct.NoProxySelector;
import com.btr.proxy.selector.misc.ProtocolDispatchSelector;
import com.btr.proxy.selector.whitelist.ProxyBypassListSelector;
import com.btr.proxy.util.Logger;
import com.btr.proxy.util.Logger.LogLevel;
import com.btr.proxy.util.PListParser;
import com.btr.proxy.util.PListParser.Dict;
import com.btr.proxy.util.PListParser.XmlParseException;
import com.btr.proxy.util.ProxyException;
import com.btr.proxy.util.ProxyUtil;
import com.btr.proxy.util.UriFilter;

/*****************************************************************************
 * Loads the OSX system proxy settings from the settings file.
 * <p>
 * All settings are stored in OSX in a special XML file format.
 * These settings file are named plist files and contain nested dictionaries, arrays and values.
 * </p><p>
 * To parse this file we use a parser that is derived from a plist parser that 
 * comes with the xmlwise XML parser package:
 * </p><p>
 * http://code.google.com/p/xmlwise/
 * </p><p>
 * I modified that parser to work with the default Java XML parsing library.
 *  </p><p>
 * The plist file is located on OSX at:
 * </p><p>
 * /Library/Preferences/SystemConfiguration/preferences.plist
 * </p> 
 * 
 * @author Bernd Rosstauscher (proxyvole@rosstauscher.de) Copyright 2011
 ****************************************************************************/

public class OsxProxySearchStrategy implements ProxySearchStrategy {
	
    public static final String OVERRIDE_SETTINGS_FILE = "com.btr.proxy.osx.settingsFile";
	public static final String OVERRIDE_ACCEPTED_DEVICES = "com.btr.proxy.osx.acceptedDevices"; 
	
	private static final String SETTINGS_FILE = "/Library/Preferences/SystemConfiguration/preferences.plist";
		
	/*************************************************************************
	 * ProxySelector
	 * @see java.net.ProxySelector#ProxySelector()
	 ************************************************************************/
	
	public OsxProxySearchStrategy() {
		super();
	}
	
	/*************************************************************************
	 * Loads the proxy settings and initializes a proxy selector for the OSX
	 * proxy settings.
	 * @return a configured ProxySelector, null if none is found.
	 * @throws ProxyException on file reading error. 
	 ************************************************************************/

	public ProxySelector getProxySelector() throws ProxyException {
		
        Logger.log(getClass(), LogLevel.TRACE, "Detecting OSX proxy settings");
        
        try {
           List<String> acceptedInterfaces = getNetworkInterfaces();
           
           Dict settings = PListParser.load(getSettingsFile());
           Object currentSet = settings.getAtPath("/CurrentSet");
           if (currentSet == null) {
              throw new ProxyException("CurrentSet not defined");
           }
           
           Dict networkSet = (Dict) settings.getAtPath(String.valueOf(currentSet));
           List<?> serviceOrder = (List<?>) networkSet.getAtPath("/Network/Global/IPv4/ServiceOrder");
           if (serviceOrder == null || serviceOrder.size() == 0) {
              throw new ProxyException("ServiceOrder not defined");
           }

           // Look at the Services in priority order and pick the first one that was
           // also accepted above
           Dict proxySettings = null;
           for (int i = 0; i < serviceOrder.size() && proxySettings == null; i++) {
              Object candidateService = serviceOrder.get(i);
              Object networkService = networkSet.getAtPath("/Network/Service/"+candidateService+"/__LINK__");
              if (networkService == null ) {
                 throw new ProxyException("NetworkService not defined.");
              }
              Dict selectedServiceSettings = (Dict) settings.getAtPath(""+networkService);
              String interfaceName = (String) selectedServiceSettings.getAtPath("/Interface/DeviceName");
              if (acceptedInterfaces.contains(interfaceName)) {
                 Logger.log(getClass(), LogLevel.TRACE, "Looking up proxies for device " + interfaceName);
                 proxySettings = (Dict) selectedServiceSettings.getAtPath("/Proxies");
              }
           }
           if (proxySettings == null) {
              return NoProxySelector.getInstance();
           }
           
          return buildSelector(proxySettings);
        } catch (XmlParseException e) {
           throw new ProxyException(e);
        } catch (IOException e) {
           throw new ProxyException(e);
        }
	}

	/*************************************************************************
	 * Build a selector from the given settings.
	 * @param proxySettings to parse 
	 * @return the configured selector
	 * @throws ProxyException on error
	 ************************************************************************/
	
	private ProxySelector buildSelector(Dict proxySettings) throws ProxyException {
		ProtocolDispatchSelector ps = new ProtocolDispatchSelector();
		installSelectorForProtocol(proxySettings, ps, "HTTP");
		installSelectorForProtocol(proxySettings, ps, "HTTPS");
		installSelectorForProtocol(proxySettings, ps, "FTP");
		installSelectorForProtocol(proxySettings, ps, "Gopher");
		installSelectorForProtocol(proxySettings, ps, "RTSP");
		installSocksProxy(proxySettings, ps);

		ProxySelector result = ps;
		result = installPacProxyIfAvailable(proxySettings, result);
		result = autodetectProxyIfAvailable(proxySettings, result);

		result = installExceptionList(proxySettings, result);
		result = installSimpleHostFilter(proxySettings, result);
		return result;
	}

	/*************************************************************************
	 * Create a list of Ethernet interfaces that are connected
	 * @return
	 * @throws SocketException 
	 ************************************************************************/
	
	private List<String> getNetworkInterfaces() throws SocketException {
		String override = System.getProperty(OVERRIDE_ACCEPTED_DEVICES);
		if (override != null && override.length() > 0) {
			return Arrays.asList(override.split(";"));
		}

		List<String> acceptedInterfaces = new ArrayList<String>();
		Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
		while (interfaces.hasMoreElements()) {
			NetworkInterface ni = interfaces.nextElement();
			if (isInterfaceAllowed(ni)) {
				acceptedInterfaces.add(ni.getName());
			}
		}
		return acceptedInterfaces;
	}

	/*************************************************************************
	 * Check if a given network interface is interesting for us.
	 * @param ni the interface to check
	 * @return true if accepted else false.
	 * @throws SocketException on error.
	 ************************************************************************/
	
	private boolean isInterfaceAllowed(NetworkInterface ni) throws SocketException {
		return !ni.isLoopback() && 
			   !ni.isPointToPoint() && // Not sure if we should filter the point to point interfaces? 
			   !ni.isVirtual() && 
			   ni.isUp();
	}

	/*************************************************************************
	 * @return
	 ************************************************************************/
	
	private File getSettingsFile() {
		File result = new File(SETTINGS_FILE); 
		String overrideFile = System.getProperty(OVERRIDE_SETTINGS_FILE);
		if (overrideFile != null) { 
			return new File(overrideFile);
		}
		return result;
	}

	/*************************************************************************
	 * @param proxySettings
	 * @param result
	 * @return
	 ************************************************************************/
	
	private ProxySelector installSimpleHostFilter(
			Dict proxySettings, ProxySelector result) {
		if (isActive(proxySettings.get("ExcludeSimpleHostnames"))) {
			List<UriFilter> localBypassFilter = new ArrayList<UriFilter>();
			localBypassFilter.add(new IELocalByPassFilter());
			result = new ProxyBypassListSelector(localBypassFilter, result);
		}
		return result;
	}

	/*************************************************************************
	 * @param proxySettings
	 * @param result
	 * @return
	 ************************************************************************/
	
	private ProxySelector installExceptionList(
			Dict proxySettings, ProxySelector result) {
		List<?> proxyExceptions = (List<?>) proxySettings.get("ExceptionsList");
		if (proxyExceptions != null && proxyExceptions.size() > 0) {
			Logger.log(getClass(), LogLevel.TRACE, "OSX uses proxy bypass list: {0}", proxyExceptions);
			String noProxyList = toCommaSeparatedString(proxyExceptions);
			result = new ProxyBypassListSelector(noProxyList, result);
		}
		return result;
	}

	/*************************************************************************
	 * Convert a list to a comma separated list.
	 * @param proxyExceptions list of elements.
	 * @return a comma separated string of the list's content.
	 ************************************************************************/
	
	private String toCommaSeparatedString(List<?> proxyExceptions) {
		StringBuilder result = new StringBuilder();
		for (Object object : proxyExceptions) {
			if (result.length() > 0) {
				result.append(",");
			}
			result.append(object);
		}
		return result.toString();
	}

	/*************************************************************************
	 * @param proxySettings
	 * @param result
	 * @return
	 * @throws ProxyException
	 ************************************************************************/
	
	private ProxySelector autodetectProxyIfAvailable(
			Dict proxySettings, ProxySelector result)
			throws ProxyException {
		if (isActive(proxySettings.get("ProxyAutoDiscoveryEnable"))) {
			ProxySelector wp = new WpadProxySearchStrategy().getProxySelector();
			if (wp != null) {
				result = wp;
			}
		}
		return result;
	}

	/*************************************************************************
	 * @param proxySettings
	 * @param result
	 * @return
	 ************************************************************************/
	
	private ProxySelector installPacProxyIfAvailable(Dict proxySettings,
			ProxySelector result) {
		if (isActive(proxySettings.get("ProxyAutoConfigEnable"))) {
			String url = (String) proxySettings.get("ProxyAutoConfigURLString");
			result = ProxyUtil.buildPacSelectorForUrl(url);
		}
		return result;
	}

	/*************************************************************************
	 * Build a socks proxy and set it for the socks protocol.
	 * @param proxySettings to read the config values from.
	 * @param ps the ProtocolDispatchSelector to install the new proxy on.
	 ************************************************************************/
	
	private void installSocksProxy(Dict proxySettings,
			ProtocolDispatchSelector ps) {
		if (isActive(proxySettings.get("SOCKSEnable"))) {
			String proxyHost = (String) proxySettings.get("SOCKSProxy");
			int proxyPort = (Integer) proxySettings.get("SOCKSPort");
		    ps.setSelector("socks", ProxyUtil.parseProxySettings(proxyHost.trim(), Proxy.Type.SOCKS, proxyPort));
			Logger.log(getClass(), LogLevel.TRACE, "OSX socks proxy is {0}:{1}", proxyHost, proxyPort);
		}
	}

	/*************************************************************************
	 * Installs a proxy selector for the given protocoll on the ProtocolDispatchSelector
	 * @param proxySettings to read the config for the procotol from.
	 * @param ps the ProtocolDispatchSelector to install the new selector on.
	 * @param protocol to use.
	 ************************************************************************/
	
	private void installSelectorForProtocol(Dict proxySettings,
			ProtocolDispatchSelector ps, String protocol) {
		String prefix = protocol.trim(); 
		if (isActive(proxySettings.get(prefix+"Enable"))) {
			String proxyHost = (String) proxySettings.get(prefix+"Proxy");
			int proxyPort = (Integer) proxySettings.get(prefix+"Port");
			ProxySelector fp = ProxyUtil.parseProxySettings(proxyHost.trim(), Proxy.Type.HTTP, proxyPort);
			ps.setSelector(protocol.toLowerCase(), fp);
			Logger.log(getClass(), LogLevel.TRACE, "OSX uses for {0} the proxy {1}:{2}", protocol, proxyHost, proxyPort);
		}
	}

	/*************************************************************************
	 * Checks if the given value is set to "on". 
	 * @param value the value to test.
	 * @return true if it is set else false.
	 ************************************************************************/
	
	private boolean isActive(Object value) {
		return Integer.valueOf(1).equals(value);
	}

}