Debian Codegraph @

src/main/java/com/github/markusbernhardt/proxy/search/desktop/kde/KdeProxySearchStrategy.java

raw symbols SCIP
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 * Loads the KDE4 proxy settings from the KDE <i>kioslaverc</i> file. This will
22 * load properties from the file
23 * <p>
24 * <i>.kde/share/config/kioslaverc</i>
25 * </P>
26 * starting from the current users home directory.
27 * <p>
28 * The following settings are extracted from the section "[Proxy Settings]":
29 * </p>
30 * <ul>
31 * <li><i>AuthMode</i> -&gt; 0 = no auth., 1 = use login.</li>
32 * <li><i>ProxyType</i> -&gt; 0 = direct 1 = use fixed settings, 2 = use PAC, 3
33 * = automatic (WPAD) , 4 = Use environment variables?</li>
34 * <li><i>Proxy Config</i> Script -&gt; URL to PAC file</li>
35 * <li><i>ftpProxy</i> -&gt; Fixed ftp proxy address e.g.
36 * http://www.ftp-proxy.com:8080</li>
37 * <li><i>httpProxy</i> -&gt; Fixed http proxy e.g
38 * http://www.http-proxy.com:8080</li>
39 * <li><i>httpsProxy</i> -&gt; Fixed https proxy e.g
40 * http://www.https-proxy.com:8080</li>
41 * <li><i>NoProxyFor</i> -&gt; Proxy white list</li>
42 * <li><i>ReversedException</i> -&gt; false = use NoProxyFor, true = revert
43 * meaning of the NoProxyFor list</li>
44 * </ul>
45 *
46 * @author Markus Bernhardt, Copyright 2016
47 * @author Bernd Rosstauscher, Copyright 2009
48 ****************************************************************************/
49
50 public class KdeProxySearchStrategy implements ProxySearchStrategy {
51
52 private KdeSettingsParser settingsParser;
53
54 /*************************************************************************
55 * ProxySelector using the given parser.
56 *
57 * @see java.net.ProxySelector#ProxySelector()
58 ************************************************************************/
59
60 public KdeProxySearchStrategy() {
61 this(new KdeSettingsParser());
62 }
63
64 /*************************************************************************
65 * ProxySelector
66 *
67 * @param settingsParser
68 * the KdeSettingsParser instance to use.
69 * @see java.net.ProxySelector#ProxySelector()
70 ************************************************************************/
71
72 public KdeProxySearchStrategy(KdeSettingsParser settingsParser) {
73 super();
74 this.settingsParser = settingsParser;
75 }
76
77 /*************************************************************************
78 * Loads the proxy settings and initializes a proxy selector for the firefox
79 * proxy settings.
80 *
81 * @return a configured ProxySelector, null if none is found.
82 * @throws ProxyException
83 * on file reading error.
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: // Use no proxy
100 Logger.log(getClass(), LogLevel.TRACE, "Kde uses no proxy");
101 result = NoProxySelector.getInstance();
102 break;
103 case 1: // Fixed settings
104 Logger.log(getClass(), LogLevel.TRACE, "Kde uses manual proxy settings");
105 result = setupFixedProxySelector(settings);
106 break;
107 case 2: // PAC Script
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: // WPAD
113 Logger.log(getClass(), LogLevel.TRACE, "Kde uses WPAD to detect the proxy");
114 result = new WpadProxySearchStrategy().getProxySelector();
115 break;
116 case 4: // Use environment variables
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 * Gets the printable name of the search strategy.
129 *
130 * @return the printable name of the search strategy
131 ************************************************************************/
132
133 @Override
134 public String getName() {
135 return "kde";
136 }
137
138 /*************************************************************************
139 * Reads the settings and stores them in a properties map.
140 *
141 * @return the parsed settings.
142 * @throws ProxyException
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 * Builds an environment variable selector.
156 *
157 * @param settings
158 * the settings to read from.
159 * @return the ProxySelector using environment variables.
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 * Parse the fixed proxy settings and build an ProxySelector for this a
171 * chained configuration.
172 *
173 * @param settings
174 * the proxy settings to evaluate.
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 // Wrap in white list filter.
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 }