2 // Getdown - application installer, patcher and launcher
3 // Copyright (C) 2004-2018 Getdown authors
4 // https://github.com/threerings/getdown/blob/master/LICENSE
6 package com.threerings.getdown.launcher;
9 import java.io.FileInputStream;
10 import java.io.FileOutputStream;
11 import java.io.IOException;
12 import java.io.InputStream;
13 import java.io.PrintStream;
14 import java.net.Authenticator;
15 import java.net.HttpURLConnection;
16 import java.net.InetSocketAddress;
17 import java.net.PasswordAuthentication;
18 import java.net.Proxy;
20 import java.net.URLConnection;
21 import java.util.Iterator;
22 import java.util.ServiceLoader;
24 import jalview.util.LaunchUtils;
26 import ca.beq.util.win32.registry.RegistryKey;
27 import ca.beq.util.win32.registry.RegistryValue;
28 import ca.beq.util.win32.registry.RootKey;
30 import com.threerings.getdown.data.Application;
31 import com.threerings.getdown.spi.ProxyAuth;
32 import com.threerings.getdown.util.Config;
33 import com.threerings.getdown.util.ConnectionUtil;
34 import com.threerings.getdown.util.LaunchUtil;
35 import com.threerings.getdown.util.StringUtil;
37 import static com.threerings.getdown.Log.log;
39 public class ProxyUtil {
41 public static boolean autoDetectProxy (Application app)
43 String host = null, port = null;
45 // check for a proxy configured via system properties
46 if (System.getProperty("https.proxyHost") != null) {
47 host = System.getProperty("https.proxyHost");
48 port = System.getProperty("https.proxyPort");
50 if (StringUtil.isBlank(host) && System.getProperty("http.proxyHost") != null) {
51 host = System.getProperty("http.proxyHost");
52 port = System.getProperty("http.proxyPort");
55 // check the Windows registry
56 if (StringUtil.isBlank(host) && LaunchUtil.isWindows()) {
58 String rhost = null, rport = null;
59 boolean enabled = false;
60 RegistryKey.initialize();
61 RegistryKey r = new RegistryKey(RootKey.HKEY_CURRENT_USER, PROXY_REGISTRY);
62 for (Iterator<?> iter = r.values(); iter.hasNext(); ) {
63 RegistryValue value = (RegistryValue)iter.next();
64 if (value.getName().equals("ProxyEnable")) {
65 enabled = value.getStringValue().equals("1");
67 if (value.getName().equals("ProxyServer")) {
68 String strval = value.getStringValue();
69 int cidx = strval.indexOf(":");
71 rport = strval.substring(cidx+1);
72 strval = strval.substring(0, cidx);
81 log.info("Detected no proxy settings in the registry.");
83 } catch (Throwable t) {
84 log.info("Failed to find proxy settings in Windows registry", "error", t);
88 // look for a proxy.txt file
89 if (StringUtil.isBlank(host)) {
90 String[] hostPort = loadProxy(app);
95 // look in jalview_properties
96 String[] hostPortAuthUser = jalviewProxyProperties(app);
97 host = hostPortAuthUser[0];
98 port = hostPortAuthUser[1];
99 boolean proxyAuth = Boolean.parseBoolean(hostPortAuthUser[2]);
100 String username = hostPortAuthUser[3];
102 if (StringUtil.isBlank(host)) {
106 // yay, we found a proxy configuration, configure it in the app
107 initProxy(app, host, port, username, null);
111 public static String[] jalviewProxyProperties(Application app) {
114 boolean proxyAuth = false;
115 String username = null;
116 LaunchUtils.loadChannelProps(app.getAppDir());
117 if (LaunchUtils.getBooleanUserPreference("USE_PROXY")) {
118 host = LaunchUtils.getUserPreference("PROXY_SERVER_HTTPS");
119 port = LaunchUtils.getUserPreference("PROXY_PORT_HTTPS");
120 if (StringUtil.isBlank(host)) {
121 host = LaunchUtils.getUserPreference("PROXY_SERVER");
122 port = LaunchUtils.getUserPreference("PROXY_PORT");
124 proxyAuth = LaunchUtils.getBooleanUserPreference("PROXY_AUTH");
126 username = LaunchUtils.getUserPreference("PROXY_AUTH_USERNAME");
129 return new String[]{ host, port, String.valueOf(proxyAuth), username };
132 public static boolean canLoadWithoutProxy (URL rurl)
134 log.info("Testing whether proxy is needed, via: " + rurl);
136 // try to make a HEAD request for this URL (use short connect and read timeouts)
137 URLConnection conn = ConnectionUtil.open(Proxy.NO_PROXY, rurl, 5, 5);
138 if (conn instanceof HttpURLConnection) {
139 HttpURLConnection hcon = (HttpURLConnection)conn;
141 hcon.setRequestMethod("HEAD");
143 // make sure we got a satisfactory response code
144 int rcode = hcon.getResponseCode();
145 if (rcode == HttpURLConnection.HTTP_PROXY_AUTH ||
146 rcode == HttpURLConnection.HTTP_FORBIDDEN) {
147 log.warning("Got an 'HTTP credentials needed' response", "code", rcode);
155 // if the appbase is not an HTTP/S URL (like file:), then we don't need a proxy
158 } catch (IOException ioe) {
159 log.info("Failed to HEAD " + rurl + ": " + ioe);
160 log.info("We probably need a proxy, but auto-detection failed.");
165 public static void configProxy (Application app, String host, String port,
166 String username, String password) {
167 // save our proxy host and port in a local file
168 saveProxy(app, host, port);
170 // save our credentials via the SPI
171 if (!StringUtil.isBlank(username) && !StringUtil.isBlank(password)) {
172 ServiceLoader<ProxyAuth> loader = ServiceLoader.load(ProxyAuth.class);
173 Iterator<ProxyAuth> iterator = loader.iterator();
174 String appDir = app.getAppDir().getAbsolutePath();
175 while (iterator.hasNext()) {
176 iterator.next().saveCredentials(appDir, username, password);
180 // also configure them in the app
181 initProxy(app, host, port, username, password);
184 public static String[] loadProxy (Application app) {
185 File pfile = app.getLocalPath("proxy.txt");
186 if (pfile.exists()) {
188 Config pconf = Config.parseConfig(pfile, Config.createOpts(false));
189 return new String[] { pconf.getString("host"), pconf.getString("port") };
190 } catch (IOException ioe) {
191 log.warning("Failed to read '" + pfile + "': " + ioe);
194 return new String[] { null, null};
197 public static void saveProxy (Application app, String host, String port) {
198 File pfile = app.getLocalPath("proxy.txt");
199 try (PrintStream pout = new PrintStream(new FileOutputStream(pfile))) {
200 if (!StringUtil.isBlank(host)) {
201 pout.println("host = " + host);
203 if (!StringUtil.isBlank(port)) {
204 pout.println("port = " + port);
206 } catch (IOException ioe) {
207 log.warning("Error creating proxy file '" + pfile + "': " + ioe);
211 public static void initProxy (Application app, String host, String port,
212 String username, String password)
214 System.out.println("**** initProxy(app, '"+host+"', "+port+", '"+username+"', "+(password==null?"null":"*x"+password.length())+")");
215 // check whether we have saved proxy credentials
216 String appDir = app.getAppDir().getAbsolutePath();
217 ServiceLoader<ProxyAuth> loader = ServiceLoader.load(ProxyAuth.class);
218 Iterator<ProxyAuth> iter = loader.iterator();
219 ProxyAuth.Credentials creds = iter.hasNext() ? iter.next().loadCredentials(appDir) : null;
221 username = creds.username;
222 password = creds.password;
224 boolean haveCreds = !StringUtil.isBlank(username) && !StringUtil.isBlank(password);
226 int pport = StringUtil.isBlank(port) ? 80 : Integer.valueOf(port);
227 log.info("Using proxy", "host", host, "port", pport, "haveCreds", haveCreds);
228 app.proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(host, pport));
231 final String fuser = username;
232 final char[] fpass = password.toCharArray();
233 Authenticator.setDefault(new Authenticator() {
234 @Override protected PasswordAuthentication getPasswordAuthentication () {
235 return new PasswordAuthentication(fuser, fpass);
241 protected static final String PROXY_REGISTRY =
242 "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings";