| 1 |
package com.github.markusbernhardt.proxy; |
| 2 |
|
| 3 |
import java.awt.GraphicsEnvironment; |
| 4 |
import java.net.ProxySelector; |
| 5 |
import java.text.MessageFormat; |
| 6 |
import java.util.ArrayList; |
| 7 |
import java.util.List; |
| 8 |
|
| 9 |
import com.github.markusbernhardt.proxy.search.browser.firefox.FirefoxProxySearchStrategy; |
| 10 |
import com.github.markusbernhardt.proxy.search.browser.ie.IEProxySearchStrategy; |
| 11 |
import com.github.markusbernhardt.proxy.search.desktop.DesktopProxySearchStrategy; |
| 12 |
import com.github.markusbernhardt.proxy.search.desktop.gnome.GnomeDConfProxySearchStrategy; |
| 13 |
import com.github.markusbernhardt.proxy.search.desktop.gnome.GnomeProxySearchStrategy; |
| 14 |
import com.github.markusbernhardt.proxy.search.desktop.kde.KdeProxySearchStrategy; |
| 15 |
import com.github.markusbernhardt.proxy.search.desktop.win.WinProxySearchStrategy; |
| 16 |
import com.github.markusbernhardt.proxy.search.env.EnvProxySearchStrategy; |
| 17 |
import com.github.markusbernhardt.proxy.search.java.JavaProxySearchStrategy; |
| 18 |
import com.github.markusbernhardt.proxy.selector.misc.BufferedProxySelector; |
| 19 |
import com.github.markusbernhardt.proxy.selector.misc.BufferedProxySelector.CacheScope; |
| 20 |
import com.github.markusbernhardt.proxy.selector.misc.ProxyListFallbackSelector; |
| 21 |
import com.github.markusbernhardt.proxy.selector.pac.PacProxySelector; |
| 22 |
import com.github.markusbernhardt.proxy.util.Logger; |
| 23 |
import com.github.markusbernhardt.proxy.util.Logger.LogBackEnd; |
| 24 |
import com.github.markusbernhardt.proxy.util.Logger.LogLevel; |
| 25 |
import com.github.markusbernhardt.proxy.util.PlatformUtil; |
| 26 |
import com.github.markusbernhardt.proxy.util.ProxyException; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public class ProxySearch implements ProxySearchStrategy { |
| 46 |
|
| 47 |
private static final int DEFAULT_PAC_CACHE_SIZE = 20; |
| 48 |
|
| 49 |
private static final long DEFAULT_PAC_CACHE_TTL = 1000 * 60 * 10; |
| 50 |
|
| 51 |
|
| 52 |
private static final CacheScope DEFAULT_PAC_CACHE_SCOPE = CacheScope.CACHE_SCOPE_HOST; |
| 53 |
|
| 54 |
private List<ProxySearchStrategy> strategies; |
| 55 |
private int pacCacheSize; |
| 56 |
private long pacCacheTTL; |
| 57 |
private CacheScope pacCacheScope; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public enum Strategy { |
| 64 |
|
| 65 |
OS_DEFAULT, |
| 66 |
|
| 67 |
BROWSER, |
| 68 |
|
| 69 |
FIREFOX, |
| 70 |
|
| 71 |
IE, |
| 72 |
|
| 73 |
ENV_VAR, |
| 74 |
|
| 75 |
WIN, |
| 76 |
|
| 77 |
KDE, |
| 78 |
|
| 79 |
GNOME, |
| 80 |
|
| 81 |
JAVA |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public ProxySearch() { |
| 89 |
super(); |
| 90 |
this.strategies = new ArrayList<ProxySearchStrategy>(); |
| 91 |
this.pacCacheSize = DEFAULT_PAC_CACHE_SIZE; |
| 92 |
this.pacCacheTTL = DEFAULT_PAC_CACHE_TTL; |
| 93 |
this.pacCacheScope = DEFAULT_PAC_CACHE_SCOPE; |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
public static ProxySearch getDefaultProxySearch() { |
| 104 |
ProxySearch s = new ProxySearch(); |
| 105 |
|
| 106 |
|
| 107 |
boolean headless = GraphicsEnvironment.isHeadless(); |
| 108 |
|
| 109 |
if (headless) { |
| 110 |
s.addStrategy(Strategy.JAVA); |
| 111 |
s.addStrategy(Strategy.OS_DEFAULT); |
| 112 |
s.addStrategy(Strategy.ENV_VAR); |
| 113 |
} else { |
| 114 |
s.addStrategy(Strategy.JAVA); |
| 115 |
s.addStrategy(Strategy.BROWSER); |
| 116 |
s.addStrategy(Strategy.OS_DEFAULT); |
| 117 |
s.addStrategy(Strategy.ENV_VAR); |
| 118 |
} |
| 119 |
Logger.log(ProxySearch.class, LogLevel.TRACE, "Using default search priority: {0}", s); |
| 120 |
|
| 121 |
return s; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
public void addStrategy(Strategy strategy) { |
| 132 |
switch (strategy) { |
| 133 |
case OS_DEFAULT: |
| 134 |
this.strategies.add(new DesktopProxySearchStrategy()); |
| 135 |
break; |
| 136 |
case BROWSER: |
| 137 |
this.strategies.add(getDefaultBrowserStrategy()); |
| 138 |
break; |
| 139 |
case FIREFOX: |
| 140 |
this.strategies.add(new FirefoxProxySearchStrategy()); |
| 141 |
break; |
| 142 |
case IE: |
| 143 |
this.strategies.add(new IEProxySearchStrategy()); |
| 144 |
break; |
| 145 |
case ENV_VAR: |
| 146 |
this.strategies.add(new EnvProxySearchStrategy()); |
| 147 |
break; |
| 148 |
case WIN: |
| 149 |
this.strategies.add(new WinProxySearchStrategy()); |
| 150 |
break; |
| 151 |
case KDE: |
| 152 |
this.strategies.add(new KdeProxySearchStrategy()); |
| 153 |
break; |
| 154 |
case GNOME: |
| 155 |
this.strategies.add(new GnomeDConfProxySearchStrategy()); |
| 156 |
this.strategies.add(new GnomeProxySearchStrategy()); |
| 157 |
break; |
| 158 |
case JAVA: |
| 159 |
this.strategies.add(new JavaProxySearchStrategy()); |
| 160 |
break; |
| 161 |
default: |
| 162 |
throw new IllegalArgumentException("Unknown strategy code!"); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
public void setPacCacheSettings(int size, long ttl, CacheScope cacheScope) { |
| 183 |
this.pacCacheSize = size; |
| 184 |
this.pacCacheTTL = ttl; |
| 185 |
this.pacCacheScope = cacheScope; |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
private ProxySearchStrategy getDefaultBrowserStrategy() { |
| 195 |
switch (PlatformUtil.getDefaultBrowser()) { |
| 196 |
case IE: |
| 197 |
return new IEProxySearchStrategy(); |
| 198 |
case FIREFOX: |
| 199 |
return new FirefoxProxySearchStrategy(); |
| 200 |
} |
| 201 |
return null; |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
@Override |
| 212 |
public ProxySelector getProxySelector() { |
| 213 |
Logger.log(getClass(), LogLevel.TRACE, "Executing search strategies to find proxy selector"); |
| 214 |
for (ProxySearchStrategy strat : this.strategies) { |
| 215 |
try { |
| 216 |
ProxySelector selector = strat.getProxySelector(); |
| 217 |
if (selector != null) { |
| 218 |
selector = installBufferingAndFallbackBehaviour(selector); |
| 219 |
Logger.log(getClass(), LogLevel.INFO, "Proxy found for " + strat.getName()); |
| 220 |
|
| 221 |
return selector; |
| 222 |
} else { |
| 223 |
Logger.log(getClass(), LogLevel.INFO, |
| 224 |
"No proxy found for " + strat.getName() + ". Trying next one."); |
| 225 |
} |
| 226 |
} catch (ProxyException e) { |
| 227 |
Logger.log(getClass(), LogLevel.DEBUG, "Strategy {0} failed trying next one.", e); |
| 228 |
|
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
return null; |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
@Override |
| 242 |
public String getName() { |
| 243 |
return "default"; |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
private ProxySelector installBufferingAndFallbackBehaviour(ProxySelector selector) { |
| 256 |
if (selector instanceof PacProxySelector) { |
| 257 |
if (this.pacCacheSize > 0) { |
| 258 |
selector = new BufferedProxySelector(this.pacCacheSize, this.pacCacheTTL, selector, pacCacheScope); |
| 259 |
} |
| 260 |
selector = new ProxyListFallbackSelector(selector); |
| 261 |
} |
| 262 |
return selector; |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
@Override |
| 271 |
public String toString() { |
| 272 |
StringBuilder sb = new StringBuilder("Proxy search: "); |
| 273 |
for (ProxySearchStrategy strat : this.strategies) { |
| 274 |
sb.append(strat); |
| 275 |
sb.append(" "); |
| 276 |
} |
| 277 |
return sb.toString(); |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
public static void main(String[] args) { |
| 289 |
ProxySearch ps = ProxySearch.getDefaultProxySearch(); |
| 290 |
Logger.setBackend(new LogBackEnd() { |
| 291 |
|
| 292 |
public void log(Class<?> clazz, LogLevel loglevel, String msg, Object... params) { |
| 293 |
System.out.println(MessageFormat.format(msg, params)); |
| 294 |
} |
| 295 |
|
| 296 |
public boolean isLogginEnabled(LogLevel logLevel) { |
| 297 |
return true; |
| 298 |
} |
| 299 |
}); |
| 300 |
ps.getProxySelector(); |
| 301 |
} |
| 302 |
|
| 303 |
} |