mirror of
https://github.com/funkemunky/AntiVPN.git
synced 2026-05-31 09:31:54 +00:00
Completed legacy plugin
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
|
||||
<groupId>dev.brighten.plugin</groupId>
|
||||
<artifactId>AntiVPN</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<version>1.1-SNAPSHOT</version>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
|
||||
@@ -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";
|
||||
}
|
||||
@@ -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 = "";
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
}
|
||||
@@ -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<Tuple<UUID, String>> queue = new LinkedBlockingDeque<>();
|
||||
public final AtomicBoolean checking = new AtomicBoolean(true);
|
||||
@Getter
|
||||
private Map<UUID, VPNResponse> cached = new HashMap<>();
|
||||
public ExecutorService thread = Executors.newSingleThreadScheduledExecutor();
|
||||
public ExecutorService thread = Executors.newSingleThreadExecutor();
|
||||
|
||||
public void run() {
|
||||
thread.execute(() -> {
|
||||
if(checking.get()) {
|
||||
Tuple<UUID, String> 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);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
@@ -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<String, VPNResponse> cache = new ConcurrentHashMap<>();
|
||||
private final Map<String, VPNResponse> 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);
|
||||
}
|
||||
}
|
||||
@@ -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"));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user