+
+ // proxy properties methods
+ public static void clearProxyProperties()
+ {
+ setProxyProperties(null, null, null, null, null, null, null, null,
+ null);
+ }
+
+ public static void resetProxyProperties()
+ {
+ setProxyProperties(startupProxyProperties[0], startupProxyProperties[1],
+ startupProxyProperties[2], startupProxyProperties[3],
+ startupProxyProperties[4],
+ startupProxyProperties[5] == null ? null
+ : startupProxyProperties[5].toCharArray(),
+ startupProxyProperties[6],
+ startupProxyProperties[7] == null ? null
+ : startupProxyProperties[7].toCharArray(),
+ startupProxyProperties[8]);
+ StringBuilder sb = new StringBuilder();
+ sb.append("Setting proxy properties to: http.proxyHost=")
+ .append(startupProxyProperties[0]).append(", http.proxyPort=")
+ .append(startupProxyProperties[1])
+ .append(startupProxyProperties[4] != null
+ && !startupProxyProperties[4].isEmpty()
+ ? " [" + startupProxyProperties[4] + "]"
+ : "")
+ .append(", https.proxyHost=").append(startupProxyProperties[2])
+ .append(", https.proxyPort=").append(startupProxyProperties[3])
+ .append(startupProxyProperties[6] != null
+ && !startupProxyProperties[6].isEmpty()
+ ? " [" + startupProxyProperties[6] + "]"
+ : "");
+
+ Console.debug(sb.toString());
+ }
+
+ public static void setProxyPropertiesFromPreferences()
+ {
+ setProxyPropertiesFromPreferences(Cache.PROXYTYPE_SYSTEM);
+ }
+
+ public static void setProxyPropertiesFromPreferences(
+ String previousProxyType)
+ {
+ String proxyType = Cache.getDefault("USE_PROXY",
+ Cache.PROXYTYPE_SYSTEM);
+ if (previousProxyType != null
+ && !proxyType.equals(Cache.PROXYTYPE_CUSTOM) // always apply
+ // customProxy
+ && proxyType.equals(previousProxyType))
+ {
+ // no change
+ return;
+ }
+ switch (proxyType)
+ {
+ case Cache.PROXYTYPE_NONE:
+ if (!previousProxyType.equals(proxyType))
+ {
+ Console.info("Setting no proxy settings");
+ Cache.setProxyProperties(null, null, null, null, null, null, null,
+ null, null);
+ }
+ break;
+ case Cache.PROXYTYPE_CUSTOM:
+ // always re-set a custom proxy -- it might have changed, particularly
+ // password
+ Console.info("Setting custom proxy settings");
+ boolean proxyAuthSet = Cache.getDefault("PROXY_AUTH", false);
+ Cache.setProxyProperties(Cache.getDefault("PROXY_SERVER", null),
+ Cache.getDefault("PROXY_PORT", null),
+ Cache.getDefault("PROXY_SERVER_HTTPS", null),
+ Cache.getDefault("PROXY_PORT_HTTPS", null),
+ proxyAuthSet ? Cache.getDefault("PROXY_AUTH_USERNAME", "")
+ : null,
+ proxyAuthSet ? Cache.proxyAuthPassword : null,
+ proxyAuthSet ? Cache.getDefault("PROXY_AUTH_USERNAME", "")
+ : null,
+ proxyAuthSet ? Cache.proxyAuthPassword : null, "localhost");
+ break;
+ default: // system proxy settings by default
+ Console.info("Setting system proxy settings");
+ Cache.resetProxyProperties();
+ }
+ }
+
+ public static void setProxyProperties(String httpHost, String httpPort,
+ String httpsHost, String httpsPort, String httpUser,
+ char[] httpPassword, String httpsUser, char[] httpsPassword,
+ String nonProxyHosts)
+ {
+ setOrClearSystemProperty("http.proxyHost", httpHost);
+ setOrClearSystemProperty("http.proxyPort", httpPort);
+ setOrClearSystemProperty("https.proxyHost", httpsHost);
+ setOrClearSystemProperty("https.proxyPort", httpsPort);
+ setOrClearSystemProperty("http.proxyUser", httpUser);
+ setOrClearSystemProperty("https.proxyUser", httpsUser);
+ // note: passwords for http.proxyPassword and https.proxyPassword are sent
+ // via the Authenticator, properties do not need to be set
+
+ // are we using a custom proxy (password prompt might be required)?
+ boolean customProxySet = getDefault("USE_PROXY", PROXYTYPE_SYSTEM)
+ .equals(PROXYTYPE_CUSTOM);
+
+ /*
+ * A bug in Java means the AuthCache does not get reset, so once it has working credentials,
+ * it never asks for more, so changing the Authenticator has no effect (as getPasswordAuthentication()
+ * is not re-called).
+ * This could lead to password leak to a hostile proxy server, so I'm putting in a hack to clear
+ * the AuthCache.
+ * see https://www.generacodice.com/en/articolo/154918/Reset-the-Authenticator-credentials
+ * ...
+ * Turns out this is only accessible in Java 8, and not in Java 9 onwards, so commenting out
+ */
+ /*
+ try
+ {
+ sun.net.www.protocol.http.AuthCacheValue
+ .setAuthCache(new sun.net.www.protocol.http.AuthCacheImpl());
+ } catch (Throwable t)
+ {
+ Cache.error(t.getMessage());
+ Cache.debug(getStackTraceString(t));
+ }
+ */
+
+ if (httpUser != null || httpsUser != null)
+ {
+ try
+ {
+ char[] displayHttpPw = new char[httpPassword == null ? 0
+ : httpPassword.length];
+ Arrays.fill(displayHttpPw, '*');
+ Console.debug(
+ "CACHE Proxy: setting new Authenticator with httpUser='"
+ + httpUser + "' httpPassword='" + displayHttpPw
+ + "'");
+ if (!Platform.isJS())
+ /* *
+ * java.net.Authenticator not implemented in SwingJS yet
+ *
+ * @j2sIgnore
+ *
+ */
+ {
+ Authenticator.setDefault(new Authenticator()
+ {
+ @Override
+ protected PasswordAuthentication getPasswordAuthentication()
+ {
+ if (getRequestorType() == RequestorType.PROXY)
+ {
+ String protocol = getRequestingProtocol();
+ boolean needProxyPasswordSet = false;
+ if (customProxySet &&
+ // we have a username but no password for the scheme being
+ // requested
+ (protocol.equalsIgnoreCase("http")
+ && (httpUser != null && httpUser.length() > 0
+ && (httpPassword == null
+ || httpPassword.length == 0)))
+ || (protocol.equalsIgnoreCase("https")
+ && (httpsUser != null
+ && httpsUser.length() > 0
+ && (httpsPassword == null
+ || httpsPassword.length == 0))))
+ {
+ // open Preferences -> Connections
+ String message = MessageManager
+ .getString("label.proxy_password_required");
+ Preferences.openPreferences(
+ Preferences.TabRef.CONNECTIONS_TAB, message);
+ Preferences.getInstance()
+ .proxyAuthPasswordCheckHighlight(true, true);
+ }
+ else
+ {
+ try
+ {
+ if (protocol.equalsIgnoreCase("http")
+ && getRequestingHost()
+ .equalsIgnoreCase(httpHost)
+ && getRequestingPort() == Integer
+ .valueOf(httpPort))
+ {
+ Console.debug(
+ "AUTHENTICATOR returning PasswordAuthentication(\""
+ + httpUser + "\", '"
+ + new String(displayHttpPw) + "')");
+ return new PasswordAuthentication(httpUser,
+ httpPassword);
+ }
+ if (protocol.equalsIgnoreCase("https")
+ && getRequestingHost()
+ .equalsIgnoreCase(httpsHost)
+ && getRequestingPort() == Integer
+ .valueOf(httpsPort))
+ {
+ char[] displayHttpsPw = new char[httpPassword.length];
+ Arrays.fill(displayHttpsPw, '*');
+ Console.debug(
+ "AUTHENTICATOR returning PasswordAuthentication(\""
+ + httpsUser + "\", '" + displayHttpsPw
+ + "'");
+ return new PasswordAuthentication(httpsUser,
+ httpsPassword);
+ }
+ } catch (NumberFormatException e)
+ {
+ Console.error("Problem with proxy port values [http:"
+ + httpPort + ", https:" + httpsPort + "]");
+ }
+ Console.debug(
+ "AUTHENTICATOR after trying to get PasswordAuthentication");
+ }
+ }
+ // non proxy request
+ Console.debug("AUTHENTICATOR returning null");
+ return null;
+ }
+ });
+ } // end of j2sIgnore for java.net.Authenticator
+
+ // required to re-enable basic authentication (should be okay for a
+ // local proxy)
+ Console.debug(
+ "AUTHENTICATOR setting property 'jdk.http.auth.tunneling.disabledSchemes' to \"\"");
+ System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");
+ } catch (SecurityException e)
+ {
+ Console.error("Could not set default Authenticator");
+ Console.debug(getStackTraceString(e));
+ }
+ }
+ else
+ {
+ // reset the Authenticator to protect http.proxyUser and
+ // http.proxyPassword Just In Case
+ /* as noted above, due to bug in java this doesn't work if the sun.net.www.protocol.http.AuthCache
+ * has working credentials. No workaround for Java 11.
+ */
+ if (!Platform.isJS())
+ /* *
+ * java.net.Authenticator not implemented in SwingJS yet
+ *
+ * @j2sIgnore
+ *
+ */
+ {
+ Console.debug(
+ "AUTHENTICATOR setting default Authenticator to null");
+ Authenticator.setDefault(null);
+ }
+ }
+
+ // nonProxyHosts not currently configurable in Preferences
+ Console.debug(
+ "AUTHENTICATOR setting property 'http.nonProxyHosts' to \""
+ + nonProxyHosts + "\"");
+ setOrClearSystemProperty("http.nonProxyHosts", nonProxyHosts);
+ }
+
+ public static void setOrClearSystemProperty(String key, char[] value)
+ {
+ setOrClearSystemProperty(key,
+ (value == null) ? null : new String(value));
+ }
+
+ public static void setOrClearSystemProperty(String key, String value)
+ {
+ if (key == null)
+ {
+ return;
+ }
+ if (value == null)
+ {
+ System.clearProperty(key);
+ }
+ else
+ {
+ System.setProperty(key, value);
+ }
+ }
+
+ /**
+ * Getdown appbase methods
+ */
+
+ private static final String releaseAppbase;
+
+ private static String getdownAppbase;
+
+ private static String getdownDistDir;
+
+ static
+ {
+ if (!Platform.isJS())
+ {
+ Float specversion = Float
+ .parseFloat(System.getProperty("java.specification.version"));
+ releaseAppbase = (specversion < 9)
+ ? "https://www.jalview.org/getdown/release/1.8"
+ : "https://www.jalview.org/getdown/release/11";
+ }
+ else
+ {
+ // this value currenly made up, can be changed to URL that will be
+ // "https://www.jalview.org/jalview-js/swingjs/j2s/build_properties"
+ releaseAppbase = "https://www.jalview.org/jalview-js";
+ getdownAppbase = releaseAppbase;
+ getdownDistDir = "/swingjs/j2s";
+ }
+ }
+
+ // look for properties (passed in by getdown) otherwise default to release
+ private static void setGetdownAppbase()
+ {
+ if (getdownAppbase != null)
+ {
+ return;
+ }
+ String appbase = System.getProperty("getdownappbase");
+ String distDir = System.getProperty("getdowndistdir");
+ if (appbase == null)
+ {
+ appbase = buildProperties.getProperty("GETDOWNAPPBASE");
+ distDir = buildProperties.getProperty("GETDOWNAPPDISTDIR");
+ }
+ if (appbase == null)
+ {
+ appbase = releaseAppbase;
+ distDir = "release";
+ }
+ if (appbase.endsWith("/"))
+ {
+ appbase = appbase.substring(0, appbase.length() - 1);
+ }
+ if (distDir == null)
+ {
+ distDir = appbase.equals(releaseAppbase) ? "release" : "alt";
+ }
+ getdownAppbase = appbase;
+ getdownDistDir = distDir;
+ }
+
+ public static String getGetdownAppbase()
+ {
+ setGetdownAppbase();
+ return getdownAppbase;
+ }
+
+ public static String getAppbaseBuildProperties()
+ {
+ String appbase = getGetdownAppbase();
+ return appbase + "/" + getdownDistDir + "/build_properties";
+ }
+
+ private static final Collection<String> bootstrapProperties = new ArrayList<>(
+ Arrays.asList(JALVIEWLOGLEVEL, BOOTSTRAP_TEST));
+
+ public static Properties bootstrapProperties(String filename)
+ {
+ Properties bootstrapProps = new Properties();
+ File file = null;
+ if (filename != null)
+ {
+ file = new File(filename);
+ }
+ if (file == null || !file.exists())
+ {
+ String channelPrefsFilename = ChannelProperties
+ .getProperty("preferences.filename");
+ String propertiesFilename = System.getProperty("user.home")
+ + File.separatorChar + channelPrefsFilename;
+ file = new File(propertiesFilename);
+ }
+ if (file == null || !file.exists())
+ {
+ String releasePrefsFilename = fallbackPropertiesFile;
+ String releasePropertiesFilename = System.getProperty("user.home")
+ + File.separatorChar + releasePrefsFilename;
+ file = new File(releasePropertiesFilename);
+ }
+
+ if (filename == null)
+ return null;
+ if (!file.exists())
+ {
+ System.err.println("Could not load bootstrap preferences file '"
+ + filename + "'");
+ return null;
+ }
+
+ try
+ {
+ FileInputStream in = new FileInputStream(file.getAbsoluteFile());
+ Properties props = new Properties();
+ props.load(in);
+ for (String prop : bootstrapProperties)
+ {
+ if (props.containsKey(prop))
+ bootstrapProps.put(prop, props.getProperty(prop));
+ }
+ } catch (FileNotFoundException e)
+ {
+ System.err.println("Could not find bootstrap preferences file '"
+ + file.getAbsolutePath() + "'");
+ } catch (IOException e)
+ {
+ System.err.println(
+ "IOException when loading bootstrap preferences file '"
+ + file.getAbsolutePath() + "'");
+ }
+ return bootstrapProps;
+ }