diff options
Diffstat (limited to 'netx/net/sourceforge/jnlp')
8 files changed, 1399 insertions, 11 deletions
diff --git a/netx/net/sourceforge/jnlp/browser/BrowserAwareProxySelector.java b/netx/net/sourceforge/jnlp/browser/BrowserAwareProxySelector.java index fb08325..331b954 100644 --- a/netx/net/sourceforge/jnlp/browser/BrowserAwareProxySelector.java +++ b/netx/net/sourceforge/jnlp/browser/BrowserAwareProxySelector.java @@ -54,6 +54,8 @@ import java.util.Map; import net.sourceforge.jnlp.runtime.JNLPProxySelector; import net.sourceforge.jnlp.runtime.JNLPRuntime; +import net.sourceforge.jnlp.runtime.PacEvaluator; +import net.sourceforge.jnlp.runtime.PacEvaluatorFactory; /** * A ProxySelector which can read proxy settings from a browser's @@ -85,6 +87,8 @@ public class BrowserAwareProxySelector extends JNLPProxySelector { private String browserSocks4ProxyHost; private int browserSocks4ProxyPort; + private PacEvaluator browserProxyAutoConfig = null; + /** * Create a new instance of this class, reading configuration fropm the browser */ @@ -128,6 +132,12 @@ public class BrowserAwareProxySelector extends JNLPProxySelector { e.printStackTrace(); } + if (browserProxyType == BROWSER_PROXY_TYPE_PAC) { + if (browserAutoConfigUrl != null) { + browserProxyAutoConfig = PacEvaluatorFactory.getPacEvaluator(browserAutoConfigUrl); + } + } + browserUseSameProxy = Boolean.valueOf(prefs.get("network.proxy.share_proxy_settings")); browserHttpProxyHost = prefs.get("network.proxy.http"); @@ -216,8 +226,21 @@ public class BrowserAwareProxySelector extends JNLPProxySelector { * browser. */ private List<Proxy> getFromBrowserPAC(URI uri) { - System.err.println(R("RPRoxyPacNotImplemented")); - return Arrays.asList(new Proxy[] { Proxy.NO_PROXY }); + if (browserAutoConfigUrl == null || uri.getScheme().equals("socket")) { + return Arrays.asList(new Proxy[] { Proxy.NO_PROXY }); + } + + List<Proxy> proxies = new ArrayList<Proxy>(); + + try { + String proxiesString = browserProxyAutoConfig.getProxies(uri.toURL()); + proxies.addAll(getProxiesFromPacResult(proxiesString)); + } catch (MalformedURLException e) { + e.printStackTrace(); + proxies.add(Proxy.NO_PROXY); + } + + return proxies; } /** diff --git a/netx/net/sourceforge/jnlp/resources/Messages.properties b/netx/net/sourceforge/jnlp/resources/Messages.properties index 204509c..a002bc3 100644 --- a/netx/net/sourceforge/jnlp/resources/Messages.properties +++ b/netx/net/sourceforge/jnlp/resources/Messages.properties @@ -144,7 +144,7 @@ RNestedJarExtration=Unable to extract nested jar. RUnexpected=Unexpected {0} at {1}
RConfigurationError=Fatal error while reading the configuration
RConfigurationFatal=ERROR: a fatal error has occurred while loading configuration. Perhaps a global configuration was required but could not be found
-RPRoxyPacNotImplemented=Using Proxy Auto Config (PAC) files is not supported yet.
+RPRoxyPacNotSupported=Using Proxy Auto Config (PAC) files is not supported.
RProxyFirefoxNotFound=Unable to use Firefox's proxy settings. Using "DIRECT" as proxy type.
RProxyFirefoxOptionNotImplemented=Browser proxy option "{0}" ({1}) not supported yet.
diff --git a/netx/net/sourceforge/jnlp/runtime/FakePacEvaluator.java b/netx/net/sourceforge/jnlp/runtime/FakePacEvaluator.java new file mode 100644 index 0000000..98ff9d1 --- /dev/null +++ b/netx/net/sourceforge/jnlp/runtime/FakePacEvaluator.java @@ -0,0 +1,54 @@ +/* FakePacEvaluator.java + Copyright (C) 2011 Red Hat, Inc. + +This file is part of IcedTea. + +IcedTea is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License as published by +the Free Software Foundation, version 2. + +IcedTea 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 +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with IcedTea; see the file COPYING. If not, write to +the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. +*/ + +package net.sourceforge.jnlp.runtime; + +import static net.sourceforge.jnlp.runtime.Translator.R; +import java.net.URL; + +/** + * A dummy PacEvaluator that always returns "DIRECT" + */ +public class FakePacEvaluator implements PacEvaluator { + @Override + public String getProxies(URL url) { + if (JNLPRuntime.isDebug()) { + System.err.println(R("RPRoxyPacNotSupported")); + } + return "DIRECT"; + } +} diff --git a/netx/net/sourceforge/jnlp/runtime/JNLPProxySelector.java b/netx/net/sourceforge/jnlp/runtime/JNLPProxySelector.java index cf7d6a8..db5a9b7 100644 --- a/netx/net/sourceforge/jnlp/runtime/JNLPProxySelector.java +++ b/netx/net/sourceforge/jnlp/runtime/JNLPProxySelector.java @@ -16,7 +16,6 @@ package net.sourceforge.jnlp.runtime; -import static net.sourceforge.jnlp.runtime.Translator.R; import java.io.IOException; import java.net.InetAddress; import java.net.InetSocketAddress; @@ -52,6 +51,8 @@ public abstract class JNLPProxySelector extends ProxySelector { /** 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; @@ -96,8 +97,7 @@ public abstract class JNLPProxySelector extends ProxySelector { proxyType = Integer.valueOf(config.getProperty(DeploymentConfiguration.KEY_PROXY_TYPE)); - String autoConfigString = config - .getProperty(DeploymentConfiguration.KEY_PROXY_AUTO_CONFIG_URL); + String autoConfigString = config.getProperty(DeploymentConfiguration.KEY_PROXY_AUTO_CONFIG_URL); if (autoConfigString != null) { try { autoConfigUrl = new URL(autoConfigString); @@ -106,6 +106,10 @@ public abstract class JNLPProxySelector extends ProxySelector { } } + if (autoConfigUrl != null) { + pacEvaluator = PacEvaluatorFactory.getPacEvaluator(autoConfigUrl); + } + bypassList = new ArrayList<String>(); String proxyBypass = config.getProperty(DeploymentConfiguration.KEY_PROXY_BYPASS_LIST); if (proxyBypass != null) { @@ -333,14 +337,22 @@ public abstract class JNLPProxySelector extends ProxySelector { * * @return a List of valid Proxy objects */ - private List<Proxy> getFromPAC(URI uri) { - if (autoConfigUrl == null) { + protected List<Proxy> getFromPAC(URI uri) { + if (autoConfigUrl == null || uri.getScheme().equals("socket")) { return Arrays.asList(new Proxy[] { Proxy.NO_PROXY }); } - // TODO implement this by reading and using the PAC file - System.err.println(R("RPRoxyPacNotImplemented")); - 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; } /** @@ -351,5 +363,56 @@ public abstract class JNLPProxySelector extends ProxySelector { */ 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; + } } diff --git a/netx/net/sourceforge/jnlp/runtime/PacEvaluator.java b/netx/net/sourceforge/jnlp/runtime/PacEvaluator.java new file mode 100644 index 0000000..936602e --- /dev/null +++ b/netx/net/sourceforge/jnlp/runtime/PacEvaluator.java @@ -0,0 +1,56 @@ +/* PacEvaluator.java + Copyright (C) 2011 Red Hat, Inc. + +This file is part of IcedTea. + +IcedTea is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License as published by +the Free Software Foundation, version 2. + +IcedTea 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 +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with IcedTea; see the file COPYING. If not, write to +the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. +*/ + +package net.sourceforge.jnlp.runtime; + +import java.net.URL; + +/** + * This interface represents an object which can evaluate Proxy Auto Config + * files. + */ +public interface PacEvaluator { + /** + * Get the proxies for accessing a given URL. The result is obtained by + * evaluating the PAC file with the given url (and the host) as input. + * + * @param url the url for which a proxy is desired + * @return a list of proxies in a string like + * <pre>"PROXY foo.example.com:8080; PROXY bar.example.com:8080; DIRECT"</pre> + */ + public String getProxies(URL url); +} diff --git a/netx/net/sourceforge/jnlp/runtime/PacEvaluatorFactory.java b/netx/net/sourceforge/jnlp/runtime/PacEvaluatorFactory.java new file mode 100644 index 0000000..9ed6217 --- /dev/null +++ b/netx/net/sourceforge/jnlp/runtime/PacEvaluatorFactory.java @@ -0,0 +1,107 @@ +/* PacEvaluatorFactory.java + Copyright (C) 2011 Red Hat, Inc. + +This file is part of IcedTea. + +IcedTea is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License as published by +the Free Software Foundation, version 2. + +IcedTea 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 +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with IcedTea; see the file COPYING. If not, write to +the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. +*/ + +package net.sourceforge.jnlp.runtime; + +import java.io.IOException; +import java.io.InputStream; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.net.URL; +import java.util.Properties; + + +public class PacEvaluatorFactory { + + public static PacEvaluator getPacEvaluator(URL pacUrl) { + boolean useRhino = false; + + ClassLoader cl = PacEvaluatorFactory.class.getClassLoader(); + if (cl == null) { + cl = ClassLoader.getSystemClassLoader(); + } + InputStream in = cl.getResourceAsStream("net/sourceforge/jnlp/build.properties"); + Properties properties = null; + try { + properties = new Properties(); + properties.load(in); + } catch (IOException e) { + if (JNLPRuntime.isDebug()) { + e.printStackTrace(); + } + } finally { + try { + in.close(); + } catch (IOException e) { + if (JNLPRuntime.isDebug()) { + e.printStackTrace(); + } + } + } + + if (properties == null) { + return new FakePacEvaluator(); + } + + String available = properties.getProperty("rhino.available"); + useRhino = Boolean.valueOf(available); + + if (useRhino) { + try { + Class<?> evaluator = Class.forName("net.sourceforge.jnlp.runtime.RhinoBasedPacEvaluator"); + Constructor<?> constructor = evaluator.getConstructor(URL.class); + return (PacEvaluator) constructor.newInstance(pacUrl); + } catch (ClassNotFoundException e) { + // ignore + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + if (e.getCause() != null) { + e.getCause().printStackTrace(); + } + } + } + + return new FakePacEvaluator(); + } +} diff --git a/netx/net/sourceforge/jnlp/runtime/RhinoBasedPacEvaluator.java b/netx/net/sourceforge/jnlp/runtime/RhinoBasedPacEvaluator.java new file mode 100644 index 0000000..d5e4ffb --- /dev/null +++ b/netx/net/sourceforge/jnlp/runtime/RhinoBasedPacEvaluator.java @@ -0,0 +1,255 @@ +/* RhinoBasedPacEvaluator.java + Copyright (C) 2011 Red Hat, Inc. + +This file is part of IcedTea. + +IcedTea is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License as published by +the Free Software Foundation, version 2. + +IcedTea 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 +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with IcedTea; see the file COPYING. If not, write to +the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. +*/ + +package net.sourceforge.jnlp.runtime; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.SocketPermission; +import java.net.URL; +import java.security.AccessControlContext; +import java.security.AccessController; +import java.security.Permissions; +import java.security.PrivilegedAction; +import java.security.ProtectionDomain; + +import net.sourceforge.jnlp.util.TimedHashMap; + +import org.mozilla.javascript.Context; +import org.mozilla.javascript.Function; +import org.mozilla.javascript.Scriptable; + +/** + * Represents a Proxy Auto Config file. This object can be used to evaluate the + * proxy file to find the proxy for a given url. + * + * @see http://en.wikipedia.org/wiki/Proxy_auto-config#The_PAC_file + */ +public class RhinoBasedPacEvaluator implements PacEvaluator { + + private final String pacHelperFunctionContents; + private final String pacContents; + private final URL pacUrl; + private final TimedHashMap<String, String> cache; + + /** + * Initialize a new object by using the PAC file located at the given URL. + * + * @param pacUrl the url of the PAC file to use + */ + public RhinoBasedPacEvaluator(URL pacUrl) { + if (JNLPRuntime.isDebug()) { + System.err.println("Using the Rhino based PAC evaluator for url " + pacUrl); + } + pacHelperFunctionContents = getHelperFunctionContents(); + this.pacUrl = pacUrl; + pacContents = getPacContents(pacUrl); + cache = new TimedHashMap<String, String>(); + } + + /** + * Get the proxies for accessing a given URL. The result is obtained by + * evaluating the PAC file with the given url (and the host) as input. + * + * This method performs caching of the result. + * + * @param url the url for which a proxy is desired + * @return a list of proxies in a string like + * <pre>"PROXY foo.example.com:8080; PROXY bar.example.com:8080; DIRECT"</pre> + * + * @see #getProxiesWithoutCaching(URL) + */ + public String getProxies(URL url) { + String cachedResult = getFromCache(url); + if (cachedResult != null) { + return cachedResult; + } + + String result = getProxiesWithoutCaching(url); + addToCache(url, cachedResult); + return result; + } + + /** + * Get the proxies for accessing a given URL. The result is obtained by + * evaluating the PAC file with the given url (and the host) as input. + * + * @param url the url for which a proxy is desired + * @return a list of proxies in a string like + * <pre>"PROXY example.com:3128; DIRECT"</pre> + * + * @see #getProxies(URL) + */ + private String getProxiesWithoutCaching(URL url) { + if (pacHelperFunctionContents == null) { + System.err.println("Error loading pac functions"); + return "DIRECT"; + } + + EvaluatePacAction evaluatePacAction = new EvaluatePacAction(pacContents, pacUrl.toString(), + pacHelperFunctionContents, url); + Permissions p = new Permissions(); + p.add(new RuntimePermission("accessClassInPackage.org.mozilla.javascript")); + p.add(new SocketPermission("*", "resolve")); + ProtectionDomain pd = new ProtectionDomain(null, p); + AccessControlContext context = new AccessControlContext(new ProtectionDomain[] { pd }); + + return AccessController.doPrivileged(evaluatePacAction, context); + } + + /** + * Returns the contents of file at pacUrl as a String. + */ + private String getPacContents(URL pacUrl) { + StringBuilder contents = null; + try { + String line = null; + BufferedReader pacReader = new BufferedReader(new InputStreamReader(pacUrl.openStream())); + contents = new StringBuilder(); + while ((line = pacReader.readLine()) != null) { + // System.out.println(line); + contents = contents.append(line).append("\n"); + } + } catch (IOException e) { + contents = null; + } + + return (contents != null) ? contents.toString() : null; + } + + /** + * Returns the pac helper functions as a String. The functions are read + * from net/sourceforge/jnlp/resources/pac-funcs.js + */ + private String getHelperFunctionContents() { + StringBuilder contents = null; + try { + String line; + ClassLoader cl = this.getClass().getClassLoader(); + if (cl == null) { + cl = ClassLoader.getSystemClassLoader(); + } + InputStream in = cl.getResourceAsStream("net/sourceforge/jnlp/runtime/pac-funcs.js"); + BufferedReader pacFuncsReader = new BufferedReader(new InputStreamReader(in)); + contents = new StringBuilder(); + while ((line = pacFuncsReader.readLine()) != null) { + // System.out.println(line); + contents = contents.append(line).append("\n"); + } + } catch (IOException e) { + e.printStackTrace(); + contents = null; + } + + return (contents != null) ? contents.toString() : null; + } + + /** + * Gets an entry from the cache + */ + private String getFromCache(URL url) { + String lookupString = url.getProtocol() + "://" + url.getHost(); + String result = cache.get(lookupString); + return result; + } + + /** + * Adds an entry to the cache + */ + private void addToCache(URL url, String proxyResult) { + String lookupString = url.getAuthority() + "://" + url.getHost(); + cache.put(lookupString, proxyResult); + } + + /** + * Helper classs to run remote javascript code (specified by the user as + * PAC URL) inside a sandbox. + */ + private static class EvaluatePacAction implements PrivilegedAction<String> { + + private String pacContents; + private String pacUrl; + private String pacFuncsContents; + private URL url; + + public EvaluatePacAction(String pacContents, String pacUrl, String pacFuncsContents, URL url) { + this.pacContents = pacContents; + this.pacUrl = pacUrl; + this.pacFuncsContents = pacFuncsContents; + this.url = url; + } + + public String run() { + Context cx = Context.enter(); + try { + /* + * TODO defense in depth. + * + * This is already running within a sandbox, but we can (and we + * should) lock it down further. Look into ClassShutter. + */ + Scriptable scope = cx.initStandardObjects(); + // any optimization level greater than -1 will trigger code generation + // and this block will then need classloader permissions + cx.setOptimizationLevel(-1); + Object result = null; + result = cx.evaluateString(scope, pacFuncsContents, "internal", 1, null); + result = cx.evaluateString(scope, pacContents, pacUrl, 1, null); + + Object functionObj = scope.get("FindProxyForURL", scope); + if (!(functionObj instanceof Function)) { + System.err.println("FindProxyForURL not found"); + return null; + } else { + Function findProxyFunction = (Function) functionObj; + + Object[] args = { url.toString(), url.getHost() }; + result = findProxyFunction.call(cx, scope, scope, args); + return (String) result; + } + } catch (Exception e) { + e.printStackTrace(); + return "DIRECT"; + } finally { + Context.exit(); + } + } + } + +} diff --git a/netx/net/sourceforge/jnlp/runtime/pac-funcs.js b/netx/net/sourceforge/jnlp/runtime/pac-funcs.js new file mode 100644 index 0000000..716def6 --- /dev/null +++ b/netx/net/sourceforge/jnlp/runtime/pac-funcs.js @@ -0,0 +1,830 @@ +/* pac-funcs.js + Copyright (C) 2011 Red Hat, Inc. + +This file is part of IcedTea. + +IcedTea is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License as published by +the Free Software Foundation, version 2. + +IcedTea 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 +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with IcedTea; see the file COPYING. If not, write to +the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. +*/ + +/* + * These helper functions are required to be able to parse Proxy Auto Config + * (PAC) files. PAC files will use these helper functions to decide the best + * proxy for connecting to a host. + * + * This implementation is based on the description of the functions at: + * http://web.archive.org/web/20060424005037/wp.netscape.com/eng/mozilla/2.0/relnotes/demo/proxy-live.html + */ + +/** + * Returns true if the host does not contain a domain (there are no dots) + */ +function isPlainHostName(host) { + if (host.indexOf(".") === -1) { + return true; + } else { + return false; + } +} + +/** + * Returns true if the host is part of the domain (the host ends with domain) + */ +function dnsDomainIs(host, domain) { + var loc = host.lastIndexOf(domain); + if (loc === -1) { + return false; + } + if (loc + domain.length === host.length) { + // host ends with domain + return true; + } + return false; +} + +/** + * Returns true if the host is an exact match of hostdom or if host is not a + * fully qualified name but has the same hostname as hostdom + */ +function localHostOrDomainIs(host, hostdom) { + if (host === hostdom) { + // exact match + return true; + } + var firstdot = hostdom.indexOf("."); + if (firstdot === -1) { + // hostdom has no dots + return false; + } + if (host === hostdom.substring(0, firstdot)) { + // hostname matches + return true; + } + return false; +} + +/** + * Returns true if the host name can be resolved. + */ +function isResolvable(host) { + try { + java.net.InetAddress.getByName(host); + return true; + } catch (e) { + //if (e.javaException instanceof java.net.UnknownHostException) { + return false; + //} else { + // throw e; + //} + } +} + +/** + * Return true if the ip address of the host matches the pattern given the mask. + */ +function isInNet(host, pattern, mask) { + if (!isResolvable(host)) { + return false; + } + + var hostIp = dnsResolve(host); + var hostParts = hostIp.split("."); + var patternParts = pattern.split("."); + var maskParts = mask.split("."); + + if (hostParts.length !== 4 || + patternParts.length !== hostParts.length || + maskParts.length !== hostParts.length) { + return false; + } + + var matched = true; + for (var i = 0; i < hostParts.length; i++) { + var partMatches = (hostParts[i] & maskParts[i]) === (patternParts[i] & maskParts[i]); + matched = matched && partMatches; + } + + return matched; +} + +/** + * Returns the IP address of the host as a string + */ +function dnsResolve(host) { + return java.net.InetAddress.getByName(host).getHostAddress() + ""; +} + +/** + * Returns the local IP address + */ +function myIpAddress() { + return java.net.InetAddress.getLocalHost().getHostAddress() + ""; +} + +/** + * Returns the number of domains in a hostname + */ +function dnsDomainLevels(host) { + var levels = 0; + for (var i = 0; i < host.length; i++) { + if (host[i] === '.') { + levels++; + } + } + return levels; +} + +/** + * Returns true if the shell expression matches the given input string + */ +function shExpMatch(str, shExp) { + + // TODO support all special characters + // right now we support only * and ? + + + try { + + // turn shExp into a regular expression + + var work = ""; + + // escape characters + for (var i = 0; i < shExp.length; i++) { + var ch = shExp[i]; + switch (ch) { + case "\\": work = work + "\\\\"; break; + case "^": work = work + "\\^"; break; + case "$": work = work + "\\$"; break; + case "+": work = work + "\\+"; break; + case ".": work = work + "\\."; break; + case "(": work = work + "\\("; break; + case ")": work = work + "\\)"; break; + case "{": work = work + "\\{"; break; + case "}": work = work + "\\}"; break; + case "[": work = work + "\\["; break; + case "]": work = work + "\\]"; break; + + case "?": work = work + ".{1}"; break; + case "*": work = work + ".*"; break; + + default: + work = work + ch; + } + + } + + var regExp = "^" + work + "$"; + + // match + //java.lang.System.out.println("") + //java.lang.System.out.println("Input String : " + str); + //java.lang.System.out.println("Input Pattern : " + shExp); + //java.lang.System.out.println("RegExp : " + regExp.toString()); + var match = str.match(regExp); + + if (match === null) { + return false; + } else { + return true; + } + + + } catch (e) { + return false; + } + +} + + +/** + * Returns true if the current weekday matches the desired weekday(s) + * + * Possible ways of calling: + * weekdayRange(wd1); + * weekdayRange(wd1, "GMT"); + * weekdayRange(wd1, wd2); + * weekdayRange(wd1, wd2, "GMT"); + * + * Where wd1 and wd2 are one of "SUN", "MON", "TUE", "WED", "THU", "FRI" and + * "SAT" + * + * The argument "GMT", if present, is always the last argument + */ +function weekdayRange() { + var wd1; + var wd2; + var gmt = false; + + function isWeekDay(day) { + if (day === "SUN" || day === "MON" || day === "TUE" || day === "WED" || + day === "THU" || day === "FRI" || day === "SAT") { + return true; + } + return false; + } + + function strToWeekDay(str) { + switch (str) { + case "SUN": return 0; + case "MON": return 1; + case "TUE": return 2; + case "WED": return 3; + case "THU": return 4; + case "FRI": return 5; + case "SAT": return 6; + default: return 0; + } + } + + if (arguments.length > 1) { + if (arguments[arguments.length-1] === "GMT") { + gmt = true; + arguments.splice(0,arguments.length-1); + } + } + + if (arguments.length === 0) { return false; } + + wd1 = arguments[0]; + + if (!isWeekDay(wd1)) { return false; } + + var today = new Date().getDay(); + if (arguments.length >= 2) { + // return true if current weekday is between wd1 and wd2 (inclusive) + wd2 = arguments[1]; + if (!isWeekDay(wd2)) { return false; } + + var day1 = strToWeekDay(wd1); + var day2 = strToWeekDay(wd2); + + if (day1 <= day2) { + if ( day1 <= today && today <= day2) { + return true; + } + return false; + } else { + if (day1 <= today || today <= day2) { + return true; + } + return false; + } + } else { + // return true if the current weekday is wd1 + if (strToWeekDay(wd1) === today) { + return true; + } + return false; + } +} + +/** + * Returns true if the current date matches the given date(s) + * + * Possible ways of calling: + * dateRange(day) + * dateRange(day1, day2) + * dateRange(month) + * dateRange(month1, month2) + * dateRange(year) + * dateRange(year1, year2) + * dateRange(day1, month1, day2, month2) + * dateRange(month1, year1, month2, year2) + * dateRange(day1, month1, year1, day2, month2, year2) + * + * The parameter "GMT" may additionally be passed as the last argument in any + * of the above ways of calling. + */ +function dateRange() { + + // note: watch out for wrapping around of dates. date ranges, like + // month=9 to month=8, wrap around and cover the entire year. this + // makes everything more interesting + + var gmt; + if (arguments.length > 1) { + if (arguments[arguments.length-1] === "GMT") { + gmt = true; + arguments.splice(0,arguments.length-1); + } + } + + function isDate(date) { + if (typeof(date) === 'number' && (date <= 31 && date >= 1)) { + return true; + } + return false; + } + + function strToMonth(month) { + switch (month) { + case "JAN": return 0; + case "FEB": return 1; + case "MAR": return 2; + case "APR": return 3; + case "MAY": return 4; + case "JUN": return 5; + case "JUL": return 6; + case "AUG": return 7; + case "SEP": return 8; + case "OCT": return 9; + case "NOV": return 10; + case "DEC": return 11; + default: return 0; + } + } + + function isMonth(month) { + if (month === "JAN" || month === "FEB" || month === "MAR" || + month === "APR" || month === "MAY" || month === "JUN" || + month === "JUL" || month === "AUG" || month === "SEP" || + month === "OCT" || month === "NOV" || month === "DEC") { + return true; + } + return false; + } + + function isYear(year) { + if (typeof(year) === 'number') { + return true; + } + return false; + } + + function inDateRange(today, date1, date2) { + if (date1 <= date2) { + if (date1 <= today.getDate() && today.getDate() <= date2) { + return true; + } else { + return false; + } + } else { + if (date1 <= today.getDate() || today.getDate() <= date2) { + return true; + } else { + return false; + } + } + } + + function inMonthRange(today, month1, month2) { + if (month1 <= month2) { + if (month1 <= today.getMonth() && today.getMonth() <= month2) { + return true; + } else { + return false; + } + } else { + if (month1 <= today.getMonth() || today.getMonth() <= month2) { + return true; + } else { + return false; + } + } + } + + function inYearRange(today, year1, year2) { + if (year1 <= today.getYear() && today.getYear() <= year2) { + return true; + } else { + return false; + } + } + + function inMonthDateRange(today, date1, month1, date2, month2) { + if (month1 === month2) { + if (today.getMonth() === month1) { + if (date1 <= today.getDate() && today.getDate() <= date2) { + return true; + } else { + return false; + } + } else { + if (date1 <= date2) { + return false; + } else { + return true; + } + } + } else if (month1 < month2) { + if (month1 <= today.getMonth() && today.getMonth() <= month2) { + if (today.getMonth() === month1) { + if (today.getDate() >= date1) { + return true; + } else { + return false; + } + } else if (today.getMonth() === month2) { + if (today.getDate() <= date2) { + return true; + } else { + return false; + } + } else { + return true; + } + } else { + return false; + } + } else { + if (month1 <= today.getMonth() || today.getMonth() <= month2) { + if (today.getMonth() === month1) { + if (today.getDate() >= date1) { + return true; + } else { + return false; + } + } else if (today.getMonth() === month2) { + if (today.getDate() <= date2) { + return true; + } else { + return false; + } + } else { + return true; + } + } else { + return false; + } + } + } + + function inYearMonthRange(today, month1, year1, month2, year2) { + if (year1 === year2) { + if (today.getYear() === year1) { + if (month1 <= today.getMonth() && today.getMonth() <= month2) { + return true; + } else { + return false; + } + } else { + return false; + } + } + if (year1 < year2) { + if (year1 <= today.getYear() && today.getYear() <= year2) { + if (today.getYear() === year1) { + if (today.getMonth() >= month1) { + return true; + } else { + return false; + } + } else if (today.getYear() === year2) { + if (today.getMonth() <= month2) { + return true; + } else { + return false; + } + } else { + return true; + } + } else { + return false; + } + } else { + return false; + } + + } + + function inYearMonthDateRange(today, date1, month1, year1, date2, month2, year2) { + if (year1 === year2) { + if (year1 === today.getYear()) { + if ((month1 <= today.getMonth()) && (today.getMonth() <= month2)) { + if (month1 === month2) { + if (date1 <= today.getDate() && today.getDate() <= date2) { + return true; + } else { + return false; + } + } else if (today.getMonth() === month1) { + if (today.getDate() >= date1) { + return true; + } else { + return false; + } + } else if (today.getMonth() === month2) { + if (today.getDate() <= date2) { + return true; + } else { + return false; + } + } else { + return true; + } + } else { + return false; + } + } else { + return false; + } + } else if (year1 < year2) { + if (year1 <= today.getYear() && today.getYear() <= year2) { + if (today.getYear() === year1) { + if (today.getMonth() === month1) { + if (today.getDate() >= date1) { + return true; + } else { + return false; + } + } else if (today.getMonth() > month1) { + return true; + } else { + return false; + } + } else if (today.getYear() === year2) { + if (today.getMonth() <= month2) { + + } else { + return true; + } + } else { + return true; + } + } else { + return false; + } + } else { + return false; + } + } + + // TODO: change date to gmt, whatever + var today = new Date(); + + var arg1; + var arg2; + var arg3; + var arg4; + var arg5; + var arg6; + + switch (arguments.length) { + case 1: + var arg = arguments[0]; + if (isDate(arg)) { + if (today.getDate() === arg) { + return true; + } else { + return false; + } + } else if (isMonth(arg)) { + if (strToMonth(arg) === today.getMonth()) { + return true; + } else { + return false; + } + } else { // year + if (today.getYear() === arg) { + return true; + } else { + return false; + } + } + case 2: + arg1 = arguments[0]; + arg2 = arguments[1]; + if (isDate(arg1) && isDate(arg2)) { + var date1 = arg1; + var date2 = arg2; + + return inDateRange(today, date1, date2); + + } else if (isMonth(arg1) && isMonth(arg2)) { + var month1 = strToMonth(arg1); + var month2 = strToMonth(arg2); + + return inMonthRange(today, month1, month2); + + } else if (isYear(arg1) && isYear(arg2)) { + var year1 = arg1; + var year2 = arg2; + + return inYearRange(today, year1, year2); + } else { + return false; + } + case 4: + arg1 = arguments[0]; + arg2 = arguments[1]; + arg3 = arguments[2]; + arg4 = arguments[3]; + + if (isDate(arg1) && isMonth(arg2) && isDate(arg3) && isMonth(arg4)) { + var date1 = arg1; + var month1 = strToMonth(arg2); + var date2 = arg3; + var month2 = strToMonth(arg4); + + return inMonthDateRange(today, date1, month1, date2, month2); + + } else if (isMonth(arg1) && isYear(arg2) && isMonth(arg3) && isYear(arg4)) { + var month1 = strToMonth(arg1); + var year1 = arg2; + var month2 = strToMonth(arg3); + var year2 = arg4; + + return inYearMonthRange(today, month1, year1, month2, year2); + } else { + return false; + } + case 6: + arg1 = arguments[0]; + arg2 = arguments[1]; + arg3 = arguments[2]; + arg4 = arguments[3]; + arg5 = arguments[4]; + arg6 = arguments[5]; + if (isDate(arg1) && isMonth(arg2) && isYear(arg3) && + isDate(arg4) && isMonth(arg5) && isYear(arg6)) { + var day1 = arg1; + var month1 = strToMonth(arg2); + var year1 = arg3; + var day2 = arg4; + var month2 = strToMonth(arg5); + var year2 = arg6; + + return inYearMonthDateRange(today, day1, month1, year1, day2, month2, year2); + } else { + return false; + } + default: + return false; + } + +} + +/** + * Returns true if the current time matches the range given + * + * timeRange(hour) + * timeRange(hour1, hour2) + * timeRange(hour1, min1, hour2, min2) + * timeRange(hour1, min1, sec1, hour2, min2, sec2) + * + * The string "GMT" can be used as the last additional parameter in addition to + * the methods listed above. + */ +function timeRange() { + + // watch out for wrap around of times + + var gmt; + if (arguments.length > 1) { + if (arguments[arguments.length-1] === "GMT") { + gmt = true; + arguments.splice(0,arguments.length-1); + } + } + + function isHour(hour) { + if (typeof(hour) === "number" && ( 0 <= hour && hour <= 23)) { + return true; + } else { + return false; + } + } + + function isMin(minute) { + if (typeof(minute) === "number" && (0 <= minute && minute <= 59)) { + return true; + } else { + return false; + } + } + + function inHourRange(now, hour1, hour2) { + if (hour1 === hour2) { + if (now.getHours() === hour1) { + return true; + } else { + return false; + } + } else if (hour1 < hour2) { + if (hour1 <= now.getHours() && now.getHours() <= hour2) { + return true; + } else { + return false; + } + } else { + if (hour1 <= now.getHours() || now.getHours() <= hour2) { + return true; + } else { + return false; + } + } + } + + function inHourMinuteRange(now, hour1, min1, hour2, min2) { + if (hour1 == hour2) { + if (now.getHours() == hour1) { + if (min1 <= min2) { + if (min1 <= now.getMinutes() && now.getMinutes() <= min2) { + return true; + } else { + return false; + } + } else { + if (min1 <= now.getMinutes() || now.getMinutes() <= min2) { + return true; + } else { + return false; + } + } + } else { + if (min1 <= min2) { + return false; + } else { + return true; + } + } + } else if (hour1 < hour2) { + if (hour1 <= now.getHours() && now.getHours() <= hour2) { + return true; + } else { + return false; + } + } else { + if (hour1 <= now.getHours() || now.getHours() <= hour2) { + return true; + } else { + return false; + } + } + } + + var today = new Date(); + + switch (arguments.length) { + case 1: + var hour = arguments[0]; + if (today.getHours() === hour) { + return true; + } else { + return false; + } + case 2: + var hour1 = arguments[0]; + var hour2 = arguments[1]; + if (isHour(hour1) && isHour(hour2)) { + return inHourRange(today, hour1, hour2); + } else { + return false; + } + case 4: + var hour1 = arguments[0]; + var min1 = arguments[1]; + var hour2 = arguments[2]; + var min2 = arguments[3]; + + if (isHour(hour1) && isMin(min1) && isHour(hour2) && isMin(min2)) { + return inHourMinuteRange(today, hour1, min1, hour2, min2); + } else { + return false; + } + + case 6: + var hour1 = arguments[0]; + var min1 = arguments[1]; + var sec1 = arguments[2]; + var hour2 = arguments[3]; + var min2 = arguments[4]; + var sec2 = arguments[5]; + + // TODO handle seconds properly + + return inHourMinuteRange(today, hour1, min1, hour2, min2); + default: + return false; + } +} + |