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 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
|
// Copyright (C) 2010 Red Hat, Inc.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
package net.sourceforge.jnlp.runtime;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.SocketAddress;
import java.net.URI;
import java.net.URL;
import java.net.UnknownHostException;
import java.net.Proxy.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;
import net.sourceforge.jnlp.config.DeploymentConfiguration;
/**
* A ProxySelector specific to JNLPs. This proxy uses the deployment
* configuration to determine what to do.
*
* @see java.net.ProxySelector
*/
public abstract class JNLPProxySelector extends ProxySelector {
public static final int PROXY_TYPE_UNKNOWN = -1;
public static final int PROXY_TYPE_NONE = 0;
public static final int PROXY_TYPE_MANUAL = 1;
public static final int PROXY_TYPE_AUTO = 2;
public static final int PROXY_TYPE_BROWSER = 3;
/** The default port to use as a fallback. Currently squid's default port */
public static final int FALLBACK_PROXY_PORT = 3128;
private PacEvaluator pacEvaluator = null;
/** The proxy type. See PROXY_TYPE_* constants */
private int proxyType = PROXY_TYPE_UNKNOWN;
/** the URL to the PAC file */
private URL autoConfigUrl = null;
/** a list of URLs that should be bypassed for proxy purposes */
private List<String> bypassList = null;
/** whether localhost should be bypassed for proxy purposes */
private boolean bypassLocal = false;
/**
* whether the http proxy should be used for https and ftp protocols as well
*/
private boolean sameProxy = false;
private String proxyHttpHost;
private int proxyHttpPort;
private String proxyHttpsHost;
private int proxyHttpsPort;
private String proxyFtpHost;
private int proxyFtpPort;
private String proxySocks4Host;
private int proxySocks4Port;
// FIXME what is this? where should it be used?
private String overrideHosts = null;
/**
* Creates a new JNLPProxySelector.
*/
public JNLPProxySelector() {
parseConfiguration();
}
/**
* Initialize this ProxySelector by reading the configuration
*/
private void parseConfiguration() {
DeploymentConfiguration config = JNLPRuntime.getConfiguration();
proxyType = Integer.valueOf(config.getProperty(DeploymentConfiguration.KEY_PROXY_TYPE));
String autoConfigString = config.getProperty(DeploymentConfiguration.KEY_PROXY_AUTO_CONFIG_URL);
if (autoConfigString != null) {
try {
autoConfigUrl = new URL(autoConfigString);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
if (autoConfigUrl != null) {
pacEvaluator = PacEvaluatorFactory.getPacEvaluator(autoConfigUrl);
}
bypassList = new ArrayList<String>();
String proxyBypass = config.getProperty(DeploymentConfiguration.KEY_PROXY_BYPASS_LIST);
if (proxyBypass != null) {
StringTokenizer tokenizer = new StringTokenizer(proxyBypass, ",");
while (tokenizer.hasMoreTokens()) {
String host = tokenizer.nextToken();
if (host != null && host.trim().length() != 0) {
bypassList.add(host);
}
}
}
bypassLocal = Boolean.valueOf(config
.getProperty(DeploymentConfiguration.KEY_PROXY_BYPASS_LOCAL));
sameProxy = Boolean.valueOf(config.getProperty(DeploymentConfiguration.KEY_PROXY_SAME));
proxyHttpHost = getHost(config, DeploymentConfiguration.KEY_PROXY_HTTP_HOST);
proxyHttpPort = getPort(config, DeploymentConfiguration.KEY_PROXY_HTTP_PORT);
proxyHttpsHost = getHost(config, DeploymentConfiguration.KEY_PROXY_HTTPS_HOST);
proxyHttpsPort = getPort(config, DeploymentConfiguration.KEY_PROXY_HTTPS_PORT);
proxyFtpHost = getHost(config, DeploymentConfiguration.KEY_PROXY_FTP_HOST);
proxyFtpPort = getPort(config, DeploymentConfiguration.KEY_PROXY_FTP_PORT);
proxySocks4Host = getHost(config, DeploymentConfiguration.KEY_PROXY_SOCKS4_HOST);
proxySocks4Port = getPort(config, DeploymentConfiguration.KEY_PROXY_SOCKS4_PORT);
overrideHosts = config.getProperty(DeploymentConfiguration.KEY_PROXY_OVERRIDE_HOSTS);
}
/**
* Uses the given key to get a host from the configuraion
*/
private String getHost(DeploymentConfiguration config, String key) {
String proxyHost = config.getProperty(key);
if (proxyHost != null) {
proxyHost = proxyHost.trim();
}
return proxyHost;
}
/**
* Uses the given key to get a port from the configuration
*/
private int getPort(DeploymentConfiguration config, String key) {
int proxyPort = FALLBACK_PROXY_PORT;
String port;
port = config.getProperty(key);
if (port != null && port.trim().length() != 0) {
try {
proxyPort = Integer.valueOf(port);
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
return proxyPort;
}
/**
* {@inheritDoc}
*/
@Override
public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
ioe.printStackTrace();
}
/**
* {@inheritDoc}
*/
@Override
public List<Proxy> select(URI uri) {
if (JNLPRuntime.isDebug()) {
System.out.println("Selecting proxy for: " + uri);
}
if (inBypassList(uri)) {
List<Proxy> proxies = Arrays.asList(new Proxy[] { Proxy.NO_PROXY });
if (JNLPRuntime.isDebug()) {
System.out.println("Selected proxies: " + Arrays.toString(proxies.toArray()));
}
return proxies;
}
List<Proxy> proxies = new ArrayList<Proxy>();
switch (proxyType) {
case PROXY_TYPE_MANUAL:
proxies.addAll(getFromConfiguration(uri));
break;
case PROXY_TYPE_AUTO:
proxies.addAll(getFromPAC(uri));
break;
case PROXY_TYPE_BROWSER:
proxies.addAll(getFromBrowser(uri));
break;
case PROXY_TYPE_UNKNOWN:
// fall through
case PROXY_TYPE_NONE:
// fall through
default:
proxies.add(Proxy.NO_PROXY);
break;
}
if (JNLPRuntime.isDebug()) {
System.out.println("Selected proxies: " + Arrays.toString(proxies.toArray()));
}
return proxies;
}
/**
* Returns true if the uri should be bypassed for proxy purposes
*/
private boolean inBypassList(URI uri) {
try {
String scheme = uri.getScheme();
/* scheme can be http/https/ftp/socket */
if (scheme.equals("http") || scheme.equals("https") || scheme.equals("ftp")) {
URL url = uri.toURL();
if (bypassLocal && isLocalHost(url.getHost())) {
return true;
}
if (bypassList.contains(url.getHost())) {
return true;
}
} else if (scheme.equals("socket")) {
String host = uri.getSchemeSpecificPart().split(":")[0];
if (bypassLocal && isLocalHost(host)) {
return true;
}
if (bypassList.contains(host)) {
return true;
}
}
} catch (MalformedURLException e) {
return false;
}
return false;
}
/**
* Returns true if the host is the hostname or the IP address of the
* localhost
*/
private boolean isLocalHost(String host) {
try {
if (InetAddress.getByName(host).isLoopbackAddress()) {
return true;
}
} catch (UnknownHostException e1) {
// continue
}
try {
if (host.equals(InetAddress.getLocalHost().getHostName())) {
return true;
}
} catch (UnknownHostException e) {
// continue
}
try {
if (host.equals(InetAddress.getLocalHost().getHostAddress())) {
return true;
}
} catch (UnknownHostException e) {
// continue
}
return false;
}
/**
* Returns a list of proxies by using the information in the deployment
* configuration
*
* @param uri
* @return a List of Proxy objects
*/
private List<Proxy> getFromConfiguration(URI uri) {
List<Proxy> proxies = new ArrayList<Proxy>();
String scheme = uri.getScheme();
if (sameProxy) {
SocketAddress sa = new InetSocketAddress(proxyHttpHost, proxyHttpPort);
Proxy proxy;
if (scheme.equals("socket")) {
proxy = new Proxy(Type.SOCKS, sa);
} else {
proxy = new Proxy(Type.HTTP, sa);
}
proxies.add(proxy);
} else if (scheme.equals("http")) {
SocketAddress sa = new InetSocketAddress(proxyHttpHost, proxyHttpPort);
proxies.add(new Proxy(Type.HTTP, sa));
} else if (scheme.equals("https")) {
SocketAddress sa = new InetSocketAddress(proxyHttpsHost, proxyHttpsPort);
proxies.add(new Proxy(Type.HTTP, sa));
} else if (scheme.equals("ftp")) {
SocketAddress sa = new InetSocketAddress(proxyFtpHost, proxyFtpPort);
proxies.add(new Proxy(Type.HTTP, sa));
} else if (scheme.equals("socket")) {
SocketAddress sa = new InetSocketAddress(proxySocks4Host, proxySocks4Port);
proxies.add(new Proxy(Type.SOCKS, sa));
} else {
proxies.add(Proxy.NO_PROXY);
}
return proxies;
}
/**
* Returns a list of proxies by using the Proxy Auto Config (PAC) file. See
* http://en.wikipedia.org/wiki/Proxy_auto-config#The_PAC_file for more
* information.
*
* @return a List of valid Proxy objects
*/
protected List<Proxy> getFromPAC(URI uri) {
if (autoConfigUrl == null || uri.getScheme().equals("socket")) {
return Arrays.asList(new Proxy[] { Proxy.NO_PROXY });
}
List<Proxy> proxies = new ArrayList<Proxy>();
try {
String proxiesString = pacEvaluator.getProxies(uri.toURL());
proxies.addAll(getProxiesFromPacResult(proxiesString));
} catch (MalformedURLException e) {
e.printStackTrace();
proxies.add(Proxy.NO_PROXY);
}
return proxies;
}
/**
* Returns a list of proxies by querying the browser
*
* @param uri the uri to get proxies for
* @return a list of proxies
*/
protected abstract List<Proxy> getFromBrowser(URI uri);
/**
* Converts a proxy string from a browser into a List of Proxy objects
* suitable for java.
* @param pacString a string indicating proxies. For example
* "PROXY foo.bar:3128; DIRECT"
* @return a list of Proxy objects represeting the parsed string.
*/
public static List<Proxy> getProxiesFromPacResult(String pacString) {
List<Proxy> proxies = new ArrayList<Proxy>();
String[] tokens = pacString.split(";");
for (String token: tokens) {
if (token.startsWith("PROXY")) {
String hostPortPair = token.substring("PROXY".length()).trim();
if (!hostPortPair.contains(":")) {
continue;
}
String host = hostPortPair.split(":")[0];
int port;
try {
port = Integer.valueOf(hostPortPair.split(":")[1]);
} catch (NumberFormatException nfe) {
continue;
}
SocketAddress sa = new InetSocketAddress(host, port);
proxies.add(new Proxy(Type.HTTP, sa));
} else if (token.startsWith("SOCKS")) {
String hostPortPair = token.substring("SOCKS".length()).trim();
if (!hostPortPair.contains(":")) {
continue;
}
String host = hostPortPair.split(":")[0];
int port;
try {
port = Integer.valueOf(hostPortPair.split(":")[1]);
} catch (NumberFormatException nfe) {
continue;
}
SocketAddress sa = new InetSocketAddress(host, port);
proxies.add(new Proxy(Type.SOCKS, sa));
} else if (token.startsWith("DIRECT")) {
proxies.add(Proxy.NO_PROXY);
} else {
if (JNLPRuntime.isDebug()) {
System.out.println("Unrecognized proxy token: " + token);
}
}
}
return proxies;
}
}
|