diff --git a/pom.xml b/pom.xml index ae8fce6..f24d1dd 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ dev.brighten.plugin AntiVPN - 1.0-SNAPSHOT + 1.1-SNAPSHOT diff --git a/src/main/java/dev/brighten/pl/config/FlatfileConfig.java b/src/main/java/dev/brighten/pl/config/FlatfileConfig.java deleted file mode 100644 index bdd0d06..0000000 --- a/src/main/java/dev/brighten/pl/config/FlatfileConfig.java +++ /dev/null @@ -1,15 +0,0 @@ -package dev.brighten.pl.config; - -import cc.funkemunky.api.utils.ConfigSetting; -import cc.funkemunky.api.utils.Init; -import cc.funkemunky.api.utils.Priority; - -@Init(priority = Priority.LOW) -public class FlatfileConfig { - - @ConfigSetting(path = "database.flatfile", name = "enabled") - public static boolean enabled = true; - - @ConfigSetting(path = "database.flatfile", name = "database") - public static String database = "kaurivpn"; -} diff --git a/src/main/java/dev/brighten/pl/config/MongoConfig.java b/src/main/java/dev/brighten/pl/config/MongoConfig.java deleted file mode 100644 index dbb430e..0000000 --- a/src/main/java/dev/brighten/pl/config/MongoConfig.java +++ /dev/null @@ -1,30 +0,0 @@ -package dev.brighten.pl.config; - -import cc.funkemunky.api.utils.ConfigSetting; -import cc.funkemunky.api.utils.Init; -import cc.funkemunky.api.utils.Priority; - -@Init(priority = Priority.LOW) -public class MongoConfig { - - @ConfigSetting(path = "database.mongo", name = "enabled") - public static boolean enabled = false; - - @ConfigSetting(path = "database.mongo", name = "ip") - public static String ip = "127.0.0.1"; - - @ConfigSetting(path = "database.mongo", name = "port") - public static int port = 27017; - - @ConfigSetting(path = "database.mongo", name = "username") - public static String username = ""; - - @ConfigSetting(path = "database.mongo", name = "password") - public static String password = ""; - - @ConfigSetting(path = "database.mongo", name = "database") - public static String database = "kaurivpn"; - - @ConfigSetting(path = "database.mongo", name = "authDatabase") - public static String authDatabase = ""; -} diff --git a/src/main/java/dev/brighten/pl/config/MySQLConfig.java b/src/main/java/dev/brighten/pl/config/MySQLConfig.java deleted file mode 100644 index f5b5d68..0000000 --- a/src/main/java/dev/brighten/pl/config/MySQLConfig.java +++ /dev/null @@ -1,31 +0,0 @@ -package dev.brighten.pl.config; - -import cc.funkemunky.api.utils.ConfigSetting; -import cc.funkemunky.api.utils.Init; -import cc.funkemunky.api.utils.Priority; - -@Init(priority = Priority.LOW) -public class MySQLConfig { - - @ConfigSetting(path = "database.mysql", name = "enabled") - public static boolean enabled = false; - - @ConfigSetting(path = "database.mysql", name = "ip") - public static String ip = "127.0.0.1"; - - @ConfigSetting(path = "database.mysql", name = "port") - public static int port = 3306; - - @ConfigSetting(path = "database.mysql", name = "username") - public static String username = "root"; - - @ConfigSetting(path = "database.mysql", name = "password") - public static String password = "password"; - - @ConfigSetting(path = "database.mysql", name = "database") - public static String database = "kaurivpn"; - - @ConfigSetting(path = "database.mysql", name = "ssl") - public static boolean ssl = true; - -} diff --git a/src/main/java/dev/brighten/pl/handlers/VPNHandler.java b/src/main/java/dev/brighten/pl/handlers/VPNHandler.java index 7a952a3..c7a30b8 100755 --- a/src/main/java/dev/brighten/pl/handlers/VPNHandler.java +++ b/src/main/java/dev/brighten/pl/handlers/VPNHandler.java @@ -2,62 +2,30 @@ package dev.brighten.pl.handlers; import cc.funkemunky.api.bungee.BungeeAPI; import cc.funkemunky.api.utils.RunUtils; -import cc.funkemunky.api.utils.Tuple; import dev.brighten.pl.AntiVPN; import dev.brighten.pl.config.Config; import dev.brighten.pl.data.UserData; +import dev.brighten.pl.listeners.impl.VPNCheckEvent; import dev.brighten.pl.utils.StringUtils; import dev.brighten.pl.vpn.VPNResponse; import lombok.Getter; import lombok.val; import org.bukkit.Bukkit; import org.bukkit.entity.Player; +import org.bukkit.event.HandlerList; -import java.util.Deque; import java.util.HashMap; import java.util.Map; import java.util.UUID; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; -import java.util.concurrent.LinkedBlockingDeque; -import java.util.concurrent.atomic.AtomicBoolean; - public class VPNHandler { - public final Deque> queue = new LinkedBlockingDeque<>(); - public final AtomicBoolean checking = new AtomicBoolean(true); @Getter private Map cached = new HashMap<>(); - public ExecutorService thread = Executors.newSingleThreadScheduledExecutor(); + public ExecutorService thread = Executors.newSingleThreadExecutor(); public void run() { - thread.execute(() -> { - if(checking.get()) { - Tuple value; - - while((value = queue.poll()) != null) { - val response = AntiVPN.INSTANCE.vpnAPI.getResponse(value.two); - - if(response != null) { - UserData data = UserData.getData(value.one); - - if(data != null && data.getPlayer() != null) { - data.response = response; - - if(data.response.isProxy() && !data.getPlayer().hasPermission("antivpn.bypass")) { - alert(response, value.one); - kick(response, value.one); - } - } else cached.put(value.one, response); - } else queue.add(value); - } - try { - Thread.sleep(50L); - } catch (InterruptedException e) { - e.printStackTrace(); - } - run(); - } - }); + RunUtils.taskTimerAsync(() -> cached.clear(), 24000, 24000); //Clear cache every 20 minutes } private void alert(VPNResponse response, UUID uuid) { if(Config.alertBungee) { @@ -68,22 +36,21 @@ public class VPNHandler { } private void kick(VPNResponse response, UUID uuid) { - if(Config.kickBungee) { - BungeeAPI.kickPlayer(uuid, StringUtils.formatString(Config.kickMessage, response)); - } else { - Player player = Bukkit.getPlayer(uuid); + RunUtils.task(() -> { //Putting on to main thread otherwise we risk errors. + if(Config.kickBungee) { + BungeeAPI.kickPlayer(uuid, StringUtils.formatString(Config.kickMessage, response)); + } else { + Player player = Bukkit.getPlayer(uuid); - if(player != null) { - RunUtils.task(() -> { + if(player != null) { String message = StringUtils.formatString(Config.kickMessage, response); player.kickPlayer(message); - }); + } } - } + }); } public void shutdown() { - checking.set(false); thread.shutdown(); } @@ -92,6 +59,25 @@ public class VPNHandler { } public void checkPlayer(UUID uuid, String address) { - queue.add(new Tuple<>(uuid, address)); + thread.execute(() -> { + val response = AntiVPN.INSTANCE.vpnAPI.getResponse(address); + + if(response != null) { + UserData data = UserData.getData(uuid); + + VPNCheckEvent vpnCheckEvent = new VPNCheckEvent(uuid, response); + + Bukkit.getPluginManager().callEvent(vpnCheckEvent); //Calling VPN check event for other plugins to use. + + if(data != null && data.getPlayer() != null) { + data.response = response; + + if(data.response.isProxy() && !data.getPlayer().hasPermission("antivpn.bypass")) { + alert(response, uuid); + kick(response, uuid); + } + } else cached.put(uuid, response); + } + }); } } diff --git a/src/main/java/dev/brighten/pl/listeners/JoinListener.java b/src/main/java/dev/brighten/pl/listeners/JoinListener.java index 4f70bd9..75f50ae 100755 --- a/src/main/java/dev/brighten/pl/listeners/JoinListener.java +++ b/src/main/java/dev/brighten/pl/listeners/JoinListener.java @@ -1,10 +1,8 @@ package dev.brighten.pl.listeners; import cc.funkemunky.api.utils.Init; -import cc.funkemunky.api.utils.Tuple; import dev.brighten.pl.AntiVPN; import dev.brighten.pl.config.Config; -import dev.brighten.pl.data.UserData; import dev.brighten.pl.utils.StringUtils; import lombok.val; import org.bukkit.event.EventHandler; @@ -24,7 +22,6 @@ public class JoinListener implements Listener { @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) public void onEvent(PlayerLoginEvent event) { - UserData data = UserData.getData(event.getPlayer().getUniqueId()); if(AntiVPN.INSTANCE.vpnHandler.getCached().containsKey(event.getPlayer().getUniqueId())) { val result = AntiVPN.INSTANCE.vpnHandler.getCached().get(event.getPlayer().getUniqueId()); diff --git a/src/main/java/dev/brighten/pl/listeners/impl/VPNCheckEvent.java b/src/main/java/dev/brighten/pl/listeners/impl/VPNCheckEvent.java index 19b593a..86c3a39 100755 --- a/src/main/java/dev/brighten/pl/listeners/impl/VPNCheckEvent.java +++ b/src/main/java/dev/brighten/pl/listeners/impl/VPNCheckEvent.java @@ -11,7 +11,7 @@ import java.util.UUID; @RequiredArgsConstructor @Getter public class VPNCheckEvent extends Event { - private UUID uuid; + private final UUID uuid; private final VPNResponse response; private static HandlerList handlerList = new HandlerList(); diff --git a/src/main/java/dev/brighten/pl/vpn/VPNAPI.java b/src/main/java/dev/brighten/pl/vpn/VPNAPI.java index 9fee109..45f4c53 100755 --- a/src/main/java/dev/brighten/pl/vpn/VPNAPI.java +++ b/src/main/java/dev/brighten/pl/vpn/VPNAPI.java @@ -1,151 +1,35 @@ package dev.brighten.pl.vpn; -import cc.funkemunky.api.utils.MiscUtils; -import dev.brighten.db.db.*; +import cc.funkemunky.api.utils.RunUtils; import dev.brighten.db.utils.json.JSONException; import dev.brighten.db.utils.json.JSONObject; import dev.brighten.db.utils.json.JsonReader; -import dev.brighten.db.utils.security.hash.Hash; -import dev.brighten.db.utils.security.hash.HashType; import dev.brighten.pl.config.Config; -import dev.brighten.pl.config.FlatfileConfig; -import dev.brighten.pl.config.MongoConfig; -import dev.brighten.pl.config.MySQLConfig; import lombok.val; import org.bukkit.entity.Player; import java.io.IOException; -import java.util.Arrays; -import java.util.Comparator; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.TimeUnit; public class VPNAPI { - public Database database; - - public Hash hashAlgorithm; - - public VPNAPI() { - MiscUtils.printToConsole("&cLoading VPNHandler&7..."); - MiscUtils.printToConsole("&7Setting up Carbon database..."); - if(FlatfileConfig.enabled) { - setupFlatfile(); - } else if(MySQLConfig.enabled && MongoConfig.enabled) { - MiscUtils.printToConsole("&7Both MySQL and Mongo enabled! Defaulting to Flatfile."); - setupFlatfile(); - } else if(MongoConfig.enabled) { - MiscUtils.printToConsole("&7Setting up Mongo database &f" + MongoConfig.database + "&7..."); - database = new MongoDatabase(MongoConfig.database); - - if(MongoConfig.username.length() > 0) { - val authDB = MongoConfig.authDatabase.length() > 0 ? MongoConfig.authDatabase : MongoConfig.database; - - MiscUtils.printToConsole("&7Connecting to database with credentials to " + authDB + "..."); - database.connect(MongoConfig.ip, String.valueOf(MongoConfig.port), - authDB, MongoConfig.username, MongoConfig.password, MongoConfig.database); - } else { - MiscUtils.printToConsole("&7Connecting to database without credentials..."); - database.connect(MongoConfig.ip, String.valueOf(MongoConfig.port), MongoConfig.database); - } - } else if(MySQLConfig.enabled) { - database = new MySQLDatabase(MySQLConfig.database); - - MiscUtils.printToConsole("&7Connecting to MySQL database..."); - database.connect(MySQLConfig.ip, String.valueOf(MySQLConfig.port), MySQLConfig.database, - String.valueOf(MySQLConfig.ssl), MySQLConfig.username, MySQLConfig.password); - } else { - MiscUtils.printToConsole("&7No database enabled! No caching will occur."); - } - - if(database != null) { - MiscUtils.printToConsole("&7Loading database mappings..."); - database.loadMappings(); - } - - MiscUtils.printToConsole("&7Checking hash algorithm " + Config.hashType + "..."); - if(Config.hashIp) { - Hash.loadHashes(); - hashAlgorithm = Hash.getHashByType(Arrays.stream(HashType.values()) - .filter(type -> type.name().equalsIgnoreCase(Config.hashType)).findFirst().orElse(HashType.SHA1)); - MiscUtils.printToConsole("&7Using Hash algorithm &f" + hashAlgorithm.hashType.name() + "&7."); - } else MiscUtils.printToConsole("&7Hashing is not enabled."); - } - public VPNResponse getResponse(Player player) { return getResponse(player.getAddress().getAddress().getHostAddress()); } - private Map cache = new ConcurrentHashMap<>(); + private final Map cache = new ConcurrentHashMap<>(); + + public VPNAPI() { + RunUtils.taskTimerAsync(cache::clear, 24000, 24000); //Clear cache every 20 minutes + } public void cacheReponse(VPNResponse response) { - /*if(database != null && response.isSuccess()) { - try { - //Removing old value if it contains it. - if(database.contains(response.getIp())) database.remove(response.getIp()); - - val json = response.toJson(); - - if(hashAlgorithm != null && json.has("ip")) { - json.put("ip", hashAlgorithm.hash(json.getString("ip"))); - } - - StructureSet set = database.create(response.getIp()); - - for (String key : json.keySet()) { - set.input(key, json.get(key)); - } - - set.save(database); - - } catch (JSONException e) { - e.printStackTrace(); - } - }*/ cache.put(response.getIp(), response); } public VPNResponse getIfCached(String ipAddress) { return cache.getOrDefault(ipAddress, null); - /*if(database != null && database.contains(ipAddress)) { - val list = hashAlgorithm != null - ? database.get(set -> hashAlgorithm.hashEqualsKey(set.getObject("ip"), ipAddress)) - : database.get(ipAddress); - - if(list.size() > 0) { - long timeStamp = System.currentTimeMillis(); - if(list.size() == 1) { - val response = VPNResponse.fromSet(database.get(ipAddress).get(0)); - - if(timeStamp - response.getCacheTime() < TimeUnit.DAYS.toMillis(7)) { - return response; - } else { - database.remove(response.getIp()); - } - } else {; - for (StructureSet set : list) { - if(!set.contains("cacheTime")){ - set.input("cacheTime", timeStamp); - continue; - } - long cacheTime = set.getObject("cacheTime"); - if(timeStamp - cacheTime > TimeUnit.DAYS.toMillis(7)) { - database.remove(set.getId()); - list.remove(set); - } - } - - if(list.size() > 0) { - val ip = list.stream() - .max(Comparator.comparing(set -> (long)set.getObject("cacheTime"))).get(); - - return VPNResponse.fromSet(ip); - } - } - } - } - return null;*/ } public VPNResponse getResponse(String ipAddress) { @@ -172,9 +56,4 @@ public class VPNAPI { } return null; } - - private void setupFlatfile() { - MiscUtils.printToConsole("&7Setting up Flatfile Database &f" + FlatfileConfig.database + "&7..."); - database = new FlatfileDatabase(FlatfileConfig.database); - } } \ No newline at end of file diff --git a/src/main/java/dev/brighten/pl/vpn/VPNResponse.java b/src/main/java/dev/brighten/pl/vpn/VPNResponse.java index 447868d..2f624f5 100755 --- a/src/main/java/dev/brighten/pl/vpn/VPNResponse.java +++ b/src/main/java/dev/brighten/pl/vpn/VPNResponse.java @@ -1,6 +1,5 @@ package dev.brighten.pl.vpn; -import dev.brighten.db.db.StructureSet; import dev.brighten.db.utils.json.JSONException; import dev.brighten.db.utils.json.JSONObject; import lombok.AllArgsConstructor; @@ -11,10 +10,11 @@ import lombok.Setter; @Setter @AllArgsConstructor public class VPNResponse { - private String ip, countryName, countryCode, method, city, isp, timeZone; - private boolean proxy, usedAdvanced, cached, success; - private long cacheTime; - private int queriesLeft; + private String asn, ip, countryName, countryCode, city, timeZone, method, isp; + private boolean proxy, cached, success; + private double latitude, longitude; + private long lastAccess; + private long queriesLeft; public JSONObject toJson() throws JSONException { JSONObject json = new JSONObject(); @@ -27,11 +27,9 @@ public class VPNResponse { json.put("isp", isp); json.put("proxy", proxy); json.put("success", success); - json.put("cacheTime", cacheTime); json.put("timeZone", timeZone); json.put("success", true); json.put("queriesLeft", queriesLeft); - json.put("usedAdvanced", usedAdvanced); json.put("cached", cached); return json; @@ -40,24 +38,13 @@ public class VPNResponse { public static VPNResponse fromJson(String json) throws JSONException { JSONObject jsonObject = new JSONObject(json); - return new VPNResponse(jsonObject.getString("ip"), jsonObject.getString("countryName"), - jsonObject.getString("countryCode"), + return new VPNResponse(jsonObject.getString("asn"), jsonObject.getString("ip"), + jsonObject.getString("countryName"), jsonObject.getString("countryCode"), + jsonObject.getString("city"), jsonObject.getString("timeZone"), jsonObject.has("method") ? jsonObject.getString("method") : "N/A", - jsonObject.getString("city"), jsonObject.getString("isp"), - jsonObject.getString("timeZone"), - jsonObject.getBoolean("proxy"), jsonObject.getBoolean("usedAdvanced"), - jsonObject.getBoolean("cached"), jsonObject.getBoolean("success"), -1, - jsonObject.getInt("queriesLeft")); - } - - public static VPNResponse fromSet(StructureSet set) { - return new VPNResponse(set.getObject("ip"), set.getObject("countryName"), - set.getObject("countryCode"), set.contains("method") ? set.getObject("method") : "N/A", - set.getObject("city"), set.getObject("isp"), - set.getObject("timeZone"), - set.getObject("proxy"), set.getObject("usedAdvanced"), - set.getObject("cached"), set.getObject("success"), - set.contains("cacheTime") ? set.getObject("cacheTime") : 01, - set.getObject("queriesLeft")); + jsonObject.getString("isp"), jsonObject.getBoolean("proxy"), + jsonObject.getBoolean("cached"), jsonObject.getBoolean("success"), + jsonObject.getDouble("latitude"), jsonObject.getDouble("longitude"), + jsonObject.getLong("lastAccess"), jsonObject.getInt("queriesLeft")); } }