diff --git a/src/com/massivecraft/massivecore/cmd/massivecore/CmdMassiveCoreCmdurl.java b/src/com/massivecraft/massivecore/cmd/massivecore/CmdMassiveCoreCmdurl.java index a517681c..1af3ff89 100644 --- a/src/com/massivecraft/massivecore/cmd/massivecore/CmdMassiveCoreCmdurl.java +++ b/src/com/massivecraft/massivecore/cmd/massivecore/CmdMassiveCoreCmdurl.java @@ -67,7 +67,7 @@ public class CmdMassiveCoreCmdurl extends MassiveCommand { try { - final List lines = WebUtil.getUrlLines(url, 5000); + final List lines = WebUtil.getLines(url); sync(new Runnable() { @Override diff --git a/src/com/massivecraft/massivecore/util/WebUtil.java b/src/com/massivecraft/massivecore/util/WebUtil.java index 853d147d..911ce265 100644 --- a/src/com/massivecraft/massivecore/util/WebUtil.java +++ b/src/com/massivecraft/massivecore/util/WebUtil.java @@ -7,24 +7,42 @@ import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; +import java.net.URLConnection; +import java.security.cert.CertificateException; +import java.security.cert.X509Certificate; import java.util.ArrayList; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import javax.net.ssl.HostnameVerifier; +import javax.net.ssl.HttpsURLConnection; +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLSession; +import javax.net.ssl.SSLSocketFactory; +import javax.net.ssl.TrustManager; +import javax.net.ssl.X509TrustManager; + public class WebUtil { // -------------------------------------------- // - // ASYNC GET TOUCH + // CONSTANTS // -------------------------------------------- // - private static final ExecutorService executor = Executors.newSingleThreadExecutor(); + public final static String USER_AGENT = "MassiveCore/1.2.3"; + public final static int CONNECT_TIMEOUT = 10000; - public static void asyncGetTouch(String url) + // -------------------------------------------- // + // TOUCH + // -------------------------------------------- // + + private static final ExecutorService EXECUTOR = Executors.newSingleThreadExecutor(); + + public static void touch(String url, boolean synchronous) { try { - asyncGetTouch(new URL(url)); + touch(new URL(url), synchronous); } catch (MalformedURLException e) { @@ -32,26 +50,41 @@ public class WebUtil } } - public static void asyncGetTouch(final URL url) + public static void touch(final URL url, boolean synchronous) { - executor.execute(new Runnable() + if (synchronous) { - @Override - public void run() + touch(url); + } + else + { + EXECUTOR.execute(new Runnable() { - try + @Override + public void run() { - InputStream inputStream = null; - - inputStream = url.openStream(); - inputStream.close(); + touch(url); } - catch (IOException e) - { - e.printStackTrace(); - } - } - }); + }); + } + } + + protected static void touch(final URL url) + { + HttpURLConnection uc = null; + try + { + uc = openHttpUrlConnection(url); + uc.connect(); + } + catch (IOException e) + { + e.printStackTrace(); + } + finally + { + try { uc.disconnect(); } catch (Exception ignored) {} + } } // -------------------------------------------- // @@ -59,7 +92,7 @@ public class WebUtil // -------------------------------------------- // // This one should be run async since it's very slow. - public static List getUrlLines(URL url, int timeoutMillis) throws IOException + public static List getLines(URL url) throws IOException { HttpURLConnection uc = null; InputStream is = null; @@ -68,10 +101,7 @@ public class WebUtil try { - uc = (HttpURLConnection) url.openConnection(); - uc.setConnectTimeout(timeoutMillis); - uc.setRequestMethod("GET"); - uc.addRequestProperty("User-Agent", "Mozilla/4.76"); + uc = openHttpUrlConnection(url); uc.connect(); is = uc.getInputStream(); @@ -96,5 +126,65 @@ public class WebUtil } } + // -------------------------------------------- // + // UTIL + // -------------------------------------------- // + + public static HttpURLConnection openHttpUrlConnection(URL url) throws IOException + { + // Create Ret + HttpURLConnection ret = (HttpURLConnection) url.openConnection(); + + // Fill Ret + trustConnection(ret); + ret.setConnectTimeout(CONNECT_TIMEOUT); + ret.setRequestMethod("GET"); + ret.addRequestProperty("User-Agent", USER_AGENT); + + // Return Ret + return ret; + } + + // -------------------------------------------- // + // SSL TRUST + // -------------------------------------------- // + + public static final HostnameVerifier TRUSTING_HOSTNAME_VERIFIER = new HostnameVerifier() + { + @Override public boolean verify(String hostname, SSLSession session) { return true; } + }; + + public static final X509TrustManager TRUSTING_TRUST_MANAGER = new X509TrustManager() + { + @Override public X509Certificate[] getAcceptedIssuers() { return null; } + @Override public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { } + @Override public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { } + }; + + public static final SSLSocketFactory TRUSTING_SOCKET_FACTORY; + + static + { + SSLSocketFactory socketFactory = null; + try + { + SSLContext ctx = SSLContext.getInstance("TLS"); + ctx.init(null, new TrustManager[]{ TRUSTING_TRUST_MANAGER }, null); + socketFactory = ctx.getSocketFactory(); + } + catch (Exception e) + { + e.printStackTrace(); + } + TRUSTING_SOCKET_FACTORY = socketFactory; + } + + public static void trustConnection(URLConnection connection) + { + if ( ! (connection instanceof HttpsURLConnection)) return; + HttpsURLConnection httpsConnection = (HttpsURLConnection) connection; + httpsConnection.setSSLSocketFactory(TRUSTING_SOCKET_FACTORY); + httpsConnection.setHostnameVerifier(TRUSTING_HOSTNAME_VERIFIER); + } }