77import javax .net .ssl .SSLHandshakeException ;
88import java .io .IOException ;
99import java .net .HttpURLConnection ;
10+ import java .net .InetSocketAddress ;
1011import java .net .ProtocolException ;
12+ import java .net .Proxy ;
13+ import java .net .ProxySelector ;
14+ import java .net .SocketException ;
1115import java .net .SocketTimeoutException ;
16+ import java .net .URI ;
17+ import java .net .URISyntaxException ;
1218import java .net .URL ;
1319import java .net .UnknownHostException ;
20+ import java .util .Iterator ;
21+ import java .util .List ;
1422
1523public class HTTPSUtil {
1624
@@ -26,7 +34,13 @@ public class HTTPSUtil {
2634 public static void checkHTTPSSupport (LogService log ) {
2735 HttpURLConnection connection = null ;
2836 try {
29- connection = (HttpURLConnection ) new URL (secureURL ).openConnection ();
37+ Proxy proxy = detectProxy (secureURL );
38+ if (proxy != null ) {
39+ System .out .println ("Detected proxy " + proxy );
40+ connection = (HttpURLConnection ) new URL (secureURL ).openConnection (proxy );
41+ } else {
42+ connection = (HttpURLConnection ) new URL (secureURL ).openConnection ();
43+ }
3044 } catch (IOException e ) {
3145 e .printStackTrace ();
3246 }
@@ -56,11 +70,40 @@ public static void checkHTTPSSupport(LogService log) {
5670 "Please contact your system administrator to enable communication via HTTPS." ;
5771 if (log != null ) log .warn (msg );
5872 else System .out .println ("[WARNING] " + msg );
73+ } catch (SocketException e ) {
74+ secureMode = false ;
75+ String msg = "SocketException while trying to update securely via HTTPS. Will fall back to HTTP. This is a security risk. " +
76+ "Try downloading a new version of this software." ;
77+ if (log != null ) log .warn (msg );
78+ else System .out .println ("[WARNING] " + msg );
5979 } catch (IOException e ) {
6080 e .printStackTrace ();
6181 }
6282 }
6383
84+ private static Proxy detectProxy (String url ) {
85+ // System.setProperty("java.net.useSystemProxies", "true");
86+ List l = null ;
87+ try {
88+ l = ProxySelector .getDefault ().select (new URI (url ));
89+ }
90+ catch (URISyntaxException e ) {
91+ e .printStackTrace ();
92+ }
93+ if (l != null ) {
94+ for (Iterator iter = l .iterator (); iter .hasNext ();) {
95+ java .net .Proxy proxy = (java .net .Proxy ) iter .next ();
96+
97+ InetSocketAddress addr = (InetSocketAddress ) proxy .address ();
98+
99+ if (addr != null ) {
100+ return proxy ;
101+ }
102+ }
103+ }
104+ return null ;
105+ }
106+
64107 /**
65108 * @return whether this ImageJ instance can handle HTTPS
66109 */
0 commit comments