From 5209e2b9ea0741902be58e354d4bd54b5ad15af4 Mon Sep 17 00:00:00 2001 From: Draknyte1 Date: Mon, 7 Nov 2016 22:11:04 +1000 Subject: + Added addition checks for availability of the internet. > Should fix #34 and #27 forever, for all nationalities using this mod. (Maybe 4 checks isn't enough though?) --- .../core/handler/events/LoginEventHandler.java | 1 + src/Java/gtPlusPlus/core/util/Utils.java | 4 + .../core/util/networking/NetworkUtils.java | 98 +++++++++++++++++----- 3 files changed, 81 insertions(+), 22 deletions(-) (limited to 'src/Java/gtPlusPlus/core') diff --git a/src/Java/gtPlusPlus/core/handler/events/LoginEventHandler.java b/src/Java/gtPlusPlus/core/handler/events/LoginEventHandler.java index e529d47699..f9abae6d32 100644 --- a/src/Java/gtPlusPlus/core/handler/events/LoginEventHandler.java +++ b/src/Java/gtPlusPlus/core/handler/events/LoginEventHandler.java @@ -36,6 +36,7 @@ public class LoginEventHandler { if (!CORE.isModUpToDate){ Utils.LOG_INFO("You're not using the latest recommended version of GT++, consider updating."); + if (!CORE.MASTER_VERSION.toLowerCase().equals("offline")) Utils.LOG_INFO("Latest version is: "+CORE.MASTER_VERSION); Utils.LOG_INFO("You currently have: "+CORE.VERSION); PlayerUtils.messagePlayer(localPlayerRef, "You're not using the latest recommended version of GT++, consider updating."); diff --git a/src/Java/gtPlusPlus/core/util/Utils.java b/src/Java/gtPlusPlus/core/util/Utils.java index 2ee2de7bba..36a0b717aa 100644 --- a/src/Java/gtPlusPlus/core/util/Utils.java +++ b/src/Java/gtPlusPlus/core/util/Utils.java @@ -47,6 +47,10 @@ public class Utils { public static boolean isModUpToDate(){ + if (CORE.MASTER_VERSION.toLowerCase().equals("offline")){ + return false; + } + if (CORE.MASTER_VERSION.equals(CORE.VERSION.toLowerCase())){ return true; } diff --git a/src/Java/gtPlusPlus/core/util/networking/NetworkUtils.java b/src/Java/gtPlusPlus/core/util/networking/NetworkUtils.java index e2ec1f5d09..aa0f24f559 100644 --- a/src/Java/gtPlusPlus/core/util/networking/NetworkUtils.java +++ b/src/Java/gtPlusPlus/core/util/networking/NetworkUtils.java @@ -2,35 +2,38 @@ package gtPlusPlus.core.util.networking; import java.io.*; import java.net.*; +import java.util.Enumeration; public class NetworkUtils { public static String getContentFromURL(String args) { - if (netIsAvailable()){ - try { - URL url; - // get URL content - url = new URL(args); - URLConnection conn = url.openConnection(); - // open the stream and put it into BufferedReader - BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); - String inputLine; - String tempLine = null; - while ((inputLine = br.readLine()) != null) { - tempLine = inputLine; - } - br.close(); - return tempLine; - } catch (MalformedURLException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); + try { + if (hasValidNetworkInterface()){ + if (netIsAvailableGithub() || netIsAvailableOther() || netIsAvailableBaidu() || netIsAvailableGoogle()){ + try { + URL url; + // get URL content + url = new URL(args); + URLConnection conn = url.openConnection(); + // open the stream and put it into BufferedReader + BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); + String inputLine; + String tempLine = null; + while ((inputLine = br.readLine()) != null) { + tempLine = inputLine; + } + br.close(); + return tempLine; + } + catch (MalformedURLException e) {} + catch (IOException e) {} + } } - } - return null; + } catch (SocketException e) {} + return "offline"; } - private static boolean netIsAvailable() { + private static boolean netIsAvailableGoogle() { try { final URL url = new URL("http://www.google.com"); final URLConnection conn = url.openConnection(); @@ -43,4 +46,55 @@ public class NetworkUtils { } } + private static boolean netIsAvailableBaidu() { + try { + final URL url = new URL("http://www.baidu.com"); + final URLConnection conn = url.openConnection(); + conn.connect(); + return true; + } catch (MalformedURLException e) { + throw new RuntimeException(e); + } catch (IOException e) { + return false; + } + } + + private static boolean netIsAvailableGithub() { + try { + final URL url = new URL("https://github.com/draknyte1/GTplusplus"); + final URLConnection conn = url.openConnection(); + conn.connect(); + return true; + } catch (MalformedURLException e) { + throw new RuntimeException(e); + } catch (IOException e) { + return false; + } + } + + private static boolean netIsAvailableOther() { + try { + int timeout = 2000; + InetAddress[] addresses = InetAddress.getAllByName("www.yahoo.com"); + for (InetAddress address : addresses) { + if (address.isReachable(timeout)) + return true; + return false; + } + } catch (Exception e) { + return false; + } + return false; + } + + private static boolean hasValidNetworkInterface() throws SocketException{ + Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); + while (interfaces.hasMoreElements()) { + NetworkInterface interf = interfaces.nextElement(); + if (interf.isUp() && !interf.isLoopback()) + return true; + } + return false; + } + } -- cgit