mirror of
https://github.com/funkemunky/AntiVPN.git
synced 2026-06-17 00:50:43 +00:00
Improved performance of AntiVPN plugin
This commit is contained in:
@@ -69,10 +69,10 @@ public class AntiVPN extends JavaPlugin {
|
||||
AntiVPN.INSTANCE.vpnAPI.database.saveDatabase();
|
||||
|
||||
print(false, "threads");
|
||||
AntiVPN.INSTANCE.vpnAPI.vpnThread.shutdownNow();
|
||||
AntiVPN.INSTANCE.vpnHandler.checking.set(false);
|
||||
|
||||
print(false, "handlers");
|
||||
AntiVPN.INSTANCE.vpnAPI.vpnThread = null;
|
||||
AntiVPN.INSTANCE.vpnAPI = null;
|
||||
AntiVPN.INSTANCE.vpnHandler = null;
|
||||
AntiVPN.INSTANCE.alertsHandler = null;
|
||||
|
||||
|
||||
@@ -10,45 +10,44 @@ import dev.brighten.pl.listeners.impl.VPNCheckEvent;
|
||||
import dev.brighten.pl.utils.Config;
|
||||
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 java.util.*;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.LinkedBlockingDeque;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
public class VPNHandler {
|
||||
private LinkedList<Tuple<UUID, String>> queue = new LinkedList<>();
|
||||
private AtomicBoolean checking = new AtomicBoolean(false);
|
||||
private List<Tuple<UUID, String>> toAdd = new ArrayList<>();
|
||||
public Map<UUID, String> toKick = new HashMap<>();
|
||||
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 void run() {
|
||||
AntiVPN.INSTANCE.vpnAPI.vpnThread.scheduleAtFixedRate(() -> {
|
||||
if(!checking.get()) {
|
||||
Tuple<UUID, String> element;
|
||||
checking.set(true);
|
||||
while(queue.size() > 0 && (element = queue.poll()) != null) {
|
||||
val response = AntiVPN.INSTANCE.vpnAPI.getResponse(element.two);
|
||||
if(response != null && response.isSuccess()) {
|
||||
UserData data = UserData.getData(element.one);
|
||||
data.response = response;
|
||||
VPNCheckEvent event = new VPNCheckEvent(response);
|
||||
if(Config.fireEvent)
|
||||
RunUtils.task(() -> Bukkit.getPluginManager().callEvent(event), AntiVPN.INSTANCE);
|
||||
thread.execute(() -> {
|
||||
if(checking.get()) {
|
||||
Tuple<UUID, String> value;
|
||||
|
||||
if(response.isProxy()) {
|
||||
if(Config.alertStaff) alert(response, element.one);
|
||||
if(Config.kickPlayers) kick(response, element.one);
|
||||
}
|
||||
} else MiscUtils.printToConsole((response != null) + "?");
|
||||
while((value = queue.poll()) != null) {
|
||||
val response = AntiVPN.INSTANCE.vpnAPI.getResponse(value.two);
|
||||
|
||||
UserData data = UserData.getData(value.one);
|
||||
|
||||
if(data != null && data.getPlayer() != null) {
|
||||
data.response = response;
|
||||
|
||||
alert(response, value.one);
|
||||
kick(response, value.one);
|
||||
} else cached.put(value.one, response);
|
||||
}
|
||||
checking.set(false);
|
||||
queue.addAll(toAdd);
|
||||
toAdd.clear();
|
||||
}
|
||||
}, 0L, 20L, TimeUnit.MILLISECONDS);
|
||||
} else thread.shutdown();
|
||||
});
|
||||
}
|
||||
private void alert(VPNResponse response, UUID uuid) {
|
||||
if(Config.alertBungee) {
|
||||
@@ -65,18 +64,20 @@ public class VPNHandler {
|
||||
} else {
|
||||
Player player = Bukkit.getPlayer(uuid);
|
||||
|
||||
RunUtils.task(() -> {
|
||||
String message = StringUtils.formatString(Config.kickMessage, response);
|
||||
if(player != null)
|
||||
if(player != null) {
|
||||
RunUtils.task(() -> {
|
||||
String message = StringUtils.formatString(Config.kickMessage, response);
|
||||
player.kickPlayer(message);
|
||||
else toKick.put(uuid, message);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void checkPlayer(Player player) {
|
||||
if(!checking.get())
|
||||
queue.add(new Tuple<>(player.getUniqueId(), player.getAddress().getAddress().getHostAddress()));
|
||||
else toAdd.add(new Tuple<>(player.getUniqueId(), player.getAddress().getAddress().getHostAddress()));
|
||||
checkPlayer(player.getUniqueId(), player.getAddress().getAddress().getHostAddress());
|
||||
}
|
||||
|
||||
public void checkPlayer(UUID uuid, String address) {
|
||||
queue.add(new Tuple<>(uuid, address));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,27 +1,42 @@
|
||||
package dev.brighten.pl.listeners;
|
||||
|
||||
import cc.funkemunky.api.utils.Color;
|
||||
import cc.funkemunky.api.utils.Init;
|
||||
import cc.funkemunky.api.utils.Tuple;
|
||||
import dev.brighten.pl.AntiVPN;
|
||||
import dev.brighten.pl.data.UserData;
|
||||
import dev.brighten.pl.utils.Config;
|
||||
import dev.brighten.pl.utils.StringUtils;
|
||||
import lombok.val;
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.AsyncPlayerPreLoginEvent;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.event.player.PlayerLoginEvent;
|
||||
|
||||
@Init
|
||||
public class JoinListener implements Listener {
|
||||
|
||||
@EventHandler(priority = EventPriority.MONITOR)
|
||||
public void onJoin(PlayerJoinEvent event) {
|
||||
if(AntiVPN.INSTANCE.vpnHandler.toKick.containsKey(event.getPlayer().getUniqueId())) {
|
||||
event.getPlayer().kickPlayer(AntiVPN.INSTANCE.vpnHandler.toKick
|
||||
.compute(event.getPlayer().getUniqueId(),
|
||||
(key, val) -> AntiVPN.INSTANCE.vpnHandler.toKick.remove(key)));
|
||||
return;
|
||||
}
|
||||
|
||||
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGH)
|
||||
public void onEvent(AsyncPlayerPreLoginEvent event) {
|
||||
AntiVPN.INSTANCE.vpnHandler.queue
|
||||
.add(new Tuple<>(event.getUniqueId(), event.getAddress().getHostAddress()));
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
|
||||
public void onEvent(PlayerLoginEvent event) {
|
||||
UserData data = UserData.getData(event.getPlayer().getUniqueId());
|
||||
data.getPlayer();
|
||||
AntiVPN.INSTANCE.vpnHandler.checkPlayer(event.getPlayer());
|
||||
|
||||
if(AntiVPN.INSTANCE.vpnHandler.getCached().containsKey(event.getPlayer().getUniqueId())) {
|
||||
val result = AntiVPN.INSTANCE.vpnHandler.getCached().get(event.getPlayer().getUniqueId());
|
||||
if(result.isProxy()) {
|
||||
event.setKickMessage(StringUtils.formatString(Config.kickMessage, result));
|
||||
event.setResult(PlayerLoginEvent.Result.KICK_BANNED);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -17,6 +17,9 @@ public class Config {
|
||||
@ConfigSetting(name = "kick-message")
|
||||
public static String kickMessage = "";
|
||||
|
||||
@ConfigSetting(name = "kick-commands")
|
||||
public static List<String> kickCommands = new ArrayList<>();
|
||||
|
||||
@ConfigSetting(name = "kick-bungee")
|
||||
public static boolean kickBungee = false;
|
||||
|
||||
|
||||
@@ -17,20 +17,16 @@ import org.bukkit.entity.Player;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
|
||||
public class VPNAPI {
|
||||
|
||||
public Database database;
|
||||
public ScheduledExecutorService vpnThread;
|
||||
|
||||
public VPNAPI() {
|
||||
MiscUtils.printToConsole("&cLoading VPNHandler&7...");
|
||||
MiscUtils.printToConsole("&7Setting up Carbon database &eVPN-Cache&7...");
|
||||
database = new FlatfileDatabase("VPN-Cache");
|
||||
MiscUtils.printToConsole("&7Registering listener...");
|
||||
vpnThread = Executors.newScheduledThreadPool(2);
|
||||
|
||||
//Running saveDatabase task.
|
||||
MiscUtils.printToConsole("&7Running database saving task...");
|
||||
|
||||
@@ -21,6 +21,10 @@ kick-message: |-
|
||||
# This will allow the command to be run in Bungee.
|
||||
kick-bungee: false
|
||||
#
|
||||
# Kick Commands #
|
||||
# These will be run on player kicks.
|
||||
kick-commands: []
|
||||
#
|
||||
# VPN Use Alerts #
|
||||
# A great way to find nefarious players quickly (i.e. cheaters) without them knowing what is going on.
|
||||
alert-staff: true
|
||||
|
||||
Reference in New Issue
Block a user