package mineplex.bungee.status; import java.io.File; import java.io.IOException; import java.net.Socket; import java.util.concurrent.TimeUnit; import net.md_5.bungee.api.config.ListenerInfo; import net.md_5.bungee.api.plugin.Plugin; public class InternetStatus implements Runnable { private Plugin _plugin; private StatusRepository _repository; public InternetStatus(Plugin plugin) { _plugin = plugin; ListenerInfo listenerInfo = _plugin.getProxy().getConfigurationAdapter().getListeners().iterator().next(); boolean us = !new File("eu.dat").exists(); String address = listenerInfo.getHost().getAddress().getHostAddress() + ":" + listenerInfo.getHost().getPort(); _repository = new StatusRepository(address, us); _repository.initialize(); _plugin.getProxy().getScheduler().schedule(_plugin, this, 1L, 1L, TimeUnit.MINUTES); System.out.println("Initialized InternetStatus."); } @Override public void run() { _repository.updateOnlineStatus(isOnline()); } private boolean isOnline() { if (testUrl("www.google.com")) return true; else if (testUrl("www.espn.com")) return true; else if (testUrl("www.bing.com")) return true; return false; } private boolean testUrl(String url) { Socket socket = null; boolean reachable = false; try { socket = new Socket(url, 80); reachable = true; } catch (Exception e) { // Meh i don't care } finally { if (socket != null) { try { socket.close(); } catch (IOException e) { } } } return reachable; } }