Compare commits

..

13 Commits

Author SHA1 Message Date
Dawson Hessler c4c2d20732 Completed legacy plugin 2021-06-15 10:27:30 -04:00
Dawson Hessler 83d1c23088 Fixing CPU issue 2020-10-02 12:16:37 -04:00
Dawson Hessler 8205c4b8e7 Added bypass permission
antivpn.bypass
2020-09-09 13:09:35 -04:00
Dawson Hessler ea6c5dd281 Updating to latest API and version of Atlas 2020-09-08 14:17:39 -04:00
funkemunky 98abc4ab94 Updated for latest Atlas and improved performance a lot 2020-01-22 12:40:30 -05:00
Dawson Hessler 4cb921bfb0 Revert "Updated antivpn for later version"
This reverts commit 6f128bc2b7.
2020-01-22 09:22:42 -05:00
Dawson Hessler 09467575da Merge branch 'master' of https://github.com/funkemunky/AntiVPN 2020-01-22 09:22:33 -05:00
Dawson Hessler 6f128bc2b7 Updated antivpn for later version 2020-01-22 09:21:18 -05:00
funkemunky 9afb143a3d Improved performance of AntiVPN plugin 2020-01-21 09:25:19 -05:00
funkemunky 72f8598624 Update AntiVPN.java 2020-01-15 09:02:41 -05:00
funkemunky 3afcdc371d Update JoinListener.java 2020-01-13 09:26:03 -05:00
funkemunky c789aa58af Added data, info, and reload command 2020-01-13 09:18:46 -05:00
funkemunky 5ef4492e84 Added backwards and forwards compatibility for Atlas 1.6+ 2020-01-13 08:33:56 -05:00
18 changed files with 391 additions and 220 deletions
+5 -4
View File
@@ -6,7 +6,7 @@
<groupId>dev.brighten.plugin</groupId>
<artifactId>AntiVPN</artifactId>
<version>1.0-SNAPSHOT</version>
<version>1.1-SNAPSHOT</version>
<build>
<plugins>
@@ -32,7 +32,7 @@
<repositories>
<repository>
<id>funkemunky-releases</id>
<url>http://nexus.funkemunky.cc/content/repositories/releases/</url>
<url>https://nexus.funkemunky.cc/content/repositories/releases/</url>
</repository>
</repositories>
@@ -47,11 +47,12 @@
<groupId>org.github.spigot</groupId>
<artifactId>1.8.8</artifactId>
<version>1.8.8</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>cc.funkemunky.plugins</groupId>
<groupId>cc.funkemunky.utils</groupId>
<artifactId>Atlas</artifactId>
<version>1.6.6</version>
<version>1.8.4</version>
<scope>provided</scope>
</dependency>
</dependencies>
+11 -11
View File
@@ -28,7 +28,14 @@ public class AntiVPN extends JavaPlugin {
}
public void enable() {
System.out.println("Enabling Atlas hook...");
if(Bukkit.getPluginManager().getPlugin("Atlas") == null) {
System.out.println("Atlas not found! Disabling...");
this.disable();
return;
}
saveDefaultConfig();
print(true, "scanner");
Atlas.getInstance().initializeScanner(this, true, true);
@@ -50,16 +57,13 @@ public class AntiVPN extends JavaPlugin {
print(false, "tasks");
Bukkit.getScheduler().cancelTasks(this);
print("Save", "database");
AntiVPN.INSTANCE.vpnAPI.database.saveDatabase();
print(false, "threads");
AntiVPN.INSTANCE.vpnAPI.vpnThread.shutdown();
AntiVPN.INSTANCE.vpnHandler.shutdown();
print(false, "removing commands");
Atlas.getInstance().getCommandManager(this).unregisterCommands();
print(false, "handlers");
AntiVPN.INSTANCE.vpnAPI.vpnThread = null;
AntiVPN.INSTANCE.vpnHandler = null;
AntiVPN.INSTANCE.alertsHandler = null;
MiscUtils.printToConsole("&aCompleted shutdown.");
}
@@ -67,8 +71,4 @@ public class AntiVPN extends JavaPlugin {
private void print(boolean enable, String task) {
MiscUtils.printToConsole((enable ? "&7Enabling " : "&7Disabling ") + task + "...");
}
private void print(String custom, String task) {
MiscUtils.printToConsole("&7" + custom + "ing " + task + "...");
}
}
+2 -1
View File
@@ -9,7 +9,8 @@ import dev.brighten.pl.AntiVPN;
public class AlertsCommand {
@Command(name = "kaurivpn.alerts", description = "toggle vpn alerts",
display = "alerts", playerOnly = true, permission = {"kvpn.alerts", "kvpn.command.alerts"})
display = "alerts", playerOnly = true, aliases = {"antivpn.alerts"},
permission = {"kvpn.alerts", "kvpn.command.alerts"})
public void onCommand(CommandAdapter cmd) {
boolean toggled = AntiVPN.INSTANCE.alertsHandler.toggleAlerts(cmd.getPlayer());
cmd.getSender().sendMessage(toggled
+117
View File
@@ -0,0 +1,117 @@
package dev.brighten.pl.commands;
import cc.funkemunky.api.commands.ancmd.Command;
import cc.funkemunky.api.commands.ancmd.CommandAdapter;
import cc.funkemunky.api.utils.Color;
import cc.funkemunky.api.utils.Init;
import cc.funkemunky.api.utils.MiscUtils;
import dev.brighten.db.utils.json.JSONException;
import dev.brighten.pl.AntiVPN;
import dev.brighten.pl.data.UserData;
import lombok.val;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
@Init(commands = true)
public class InfoCommand {
private static String LINE = MiscUtils.line(Color.Dark_Gray);
@Command(name = "kaurivpn.info", description = "view a player's vpn info.", display = "info [player]",
permission = "kvpn.command.info", aliases = {"antivpn.info"})
public void onCommand(CommandAdapter cmd) {
UserData data;
if(cmd.getArgs().length == 0) {
if(cmd.getPlayer() != null) data = UserData.getData(cmd.getPlayer().getUniqueId());
else {
cmd.getSender().sendMessage(Color.Red + "You cannot view your own info as you're not a player.");
return;
}
} else {
Player player;
if((player = Bukkit.getPlayer(cmd.getArgs()[0])) != null) {
data = UserData.getData(player.getUniqueId());
} else {
cmd.getSender().sendMessage(Color.Red + "Could not find that player. Is he/she even online?");
return;
}
}
sendData(cmd, data);
}
private static void sendData(CommandAdapter cmd, UserData data) {
if(data.response != null) {
cmd.getSender().sendMessage(LINE);
sendMsg(cmd, "&6&l" + data.getPlayer().getName() + "'s Information");
sendMsg(cmd, "");
try {
val json = data.response.toJson();
json.keySet().stream()
.filter(key -> {
switch(key) {
case "ip":
case "city":
case "success":
case "queriesLeft":
case "locationString":
case "usedAdvanced":
return false;
default:
return true;
}
})
.forEach(key -> {
try {
sendMsg(cmd, "&7" + key.toUpperCase() + "&8: &f" + json.get(key));
} catch (JSONException e) {
e.printStackTrace();
}
});
} catch (JSONException e) {
sendMsg(cmd, "&cThere was an error parsing the VPN response.");
e.printStackTrace();
}
cmd.getSender().sendMessage(LINE);
} else if(AntiVPN.INSTANCE.vpnHandler.getCached().containsKey(data.uuid)) {
data.response = AntiVPN.INSTANCE.vpnHandler.getCached()
.computeIfPresent(data.uuid,
(key, value) -> AntiVPN.INSTANCE.vpnHandler.getCached().remove(key));
cmd.getSender().sendMessage(LINE);
sendMsg(cmd, "&6&l" + data.getPlayer().getName() + "'s Information");
sendMsg(cmd, "");
try {
val json = data.response.toJson();
json.keySet().stream()
.filter(key -> {
switch(key) {
case "ip":
case "city":
case "success":
case "queriesLeft":
case "locationString":
case "usedAdvanced":
return false;
default:
return true;
}
})
.forEach(key -> {
try {
sendMsg(cmd, "&7" + key.toUpperCase() + "&8: &f" + json.get(key));
} catch (JSONException e) {
e.printStackTrace();
}
});
} catch (JSONException e) {
sendMsg(cmd, "&cThere was an error parsing the VPN response.");
e.printStackTrace();
}
cmd.getSender().sendMessage(LINE);
} else cmd.getSender().sendMessage(Color.Red + "This user was not checked for a vpn.");
}
private static void sendMsg(CommandAdapter cmd, String msg) {
cmd.getSender().sendMessage(Color.translate(msg));
}
}
+35
View File
@@ -0,0 +1,35 @@
package dev.brighten.pl.commands;
import cc.funkemunky.api.commands.ancmd.Command;
import cc.funkemunky.api.commands.ancmd.CommandAdapter;
import cc.funkemunky.api.utils.Color;
import cc.funkemunky.api.utils.Init;
import cc.funkemunky.api.utils.MathUtils;
import dev.brighten.pl.AntiVPN;
@Init(commands = true)
public class ReloadCommand {
@Command(name = "kaurivpn.reload", description = "reload KauriVPN.", display = "reload",
aliases = {"antivpn.reload"}, permission = "kvpn.command.reload")
public void onCommand(CommandAdapter cmd) {
long start = System.nanoTime();
task(cmd, "Reloading config");
AntiVPN.INSTANCE.reloadConfig();
task(cmd, "Unloading KauriVPN");
AntiVPN.INSTANCE.disable();
task(cmd, "Loading KauriVPN");
AntiVPN.INSTANCE.enable();
double complete = System.nanoTime() - start / 1E6D;
cmd.getSender().sendMessage(Color.Green + "Reload completed in "
+ MathUtils.round(complete, 2) + "ms.");
}
private static void task(CommandAdapter adapter, String task) {
adapter.getSender().sendMessage(Color.translate("&7" + task + "..."));
}
}
+4 -3
View File
@@ -4,14 +4,15 @@ import cc.funkemunky.api.Atlas;
import cc.funkemunky.api.commands.ancmd.Command;
import cc.funkemunky.api.commands.ancmd.CommandAdapter;
import cc.funkemunky.api.utils.Init;
import dev.brighten.pl.AntiVPN;
@Init(commands = true)
public class VpnCommand {
@Command(name = "kaurivpn", description = "The Kauri AntiVPN main command.",
aliases = {"antivpn", "avpn", "kvpn"}, permission = "kvpn.command")
aliases = {"antivpn"}, permission = "kvpn.command")
public void onCommand(CommandAdapter cmd) {
Atlas.getInstance().getCommandManager().runHelpMessage(cmd,
cmd.getSender(), Atlas.getInstance().getCommandManager().getDefaultScheme());
Atlas.getInstance().getCommandManager(AntiVPN.INSTANCE).runHelpMessage(cmd,
cmd.getSender(), Atlas.getInstance().getCommandManager(AntiVPN.INSTANCE).getDefaultScheme());
}
}
@@ -1,12 +1,13 @@
package dev.brighten.pl.utils;
package dev.brighten.pl.config;
import cc.funkemunky.api.utils.ConfigSetting;
import cc.funkemunky.api.utils.Init;
import cc.funkemunky.api.utils.Priority;
import java.util.ArrayList;
import java.util.List;
@Init
@Init(priority = Priority.HIGH)
public class Config {
@ConfigSetting(name = "license")
public static String license = "";
@@ -17,6 +18,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;
@@ -37,4 +41,11 @@ public class Config {
@ConfigSetting(name = "fire-event")
public static boolean fireEvent = true;
/* Database Stuff */
@ConfigSetting(path = "database.general", name = "hashIp")
public static boolean hashIp = true;
@ConfigSetting(path = "database.general", name = "hashType")
public static String hashType = "SHA2";
}
+39
View File
@@ -0,0 +1,39 @@
package dev.brighten.pl.data;
import dev.brighten.pl.vpn.VPNResponse;
import lombok.RequiredArgsConstructor;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
@RequiredArgsConstructor
public class UserData {
public final static Map<UUID, UserData> dataMap = Collections.synchronizedMap(new HashMap<>());
public final UUID uuid;
private Player player;
public VPNResponse response;
public boolean hasAlerts;
public static UserData getData(UUID uuid) {
return dataMap.computeIfAbsent(uuid, key -> {
if(Bukkit.getPlayer(uuid) != null) {
UserData data = new UserData(uuid);
dataMap.put(key, data);
return data;
}
return null;
});
}
public Player getPlayer() {
if(player == null) {
return player = Bukkit.getPlayer(uuid);
}
return player;
}
}
+6 -12
View File
@@ -1,20 +1,17 @@
package dev.brighten.pl.handlers;
import cc.funkemunky.api.utils.JsonMessage;
import dev.brighten.pl.utils.Config;
import dev.brighten.pl.config.Config;
import dev.brighten.pl.data.UserData;
import dev.brighten.pl.utils.StringUtils;
import dev.brighten.pl.vpn.VPNResponse;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
public class AlertsHandler {
private Set<Player> hasAlerts = new HashSet<>();
public void sendAlert(UUID uuid, VPNResponse response) {
JsonMessage message = new JsonMessage();
@@ -24,8 +21,8 @@ public class AlertsHandler {
.addHoverText(Config.alertHoverMessage.stream()
.map(string -> StringUtils.formatString(string, response)
.replace("%player%", player.getName())).toArray(String[]::new));
hasAlerts.parallelStream().filter(Objects::nonNull)
.forEach(message::sendToPlayer);
UserData.dataMap.values().parallelStream().filter(data -> data.hasAlerts)
.forEach(data -> message.sendToPlayer(data.getPlayer()));
}
//TODO When updated Atlas releases, add this functionality.
@@ -34,11 +31,8 @@ public class AlertsHandler {
}
public boolean toggleAlerts(Player player) {
boolean contains;
UserData data = UserData.getData(player.getUniqueId());
if(contains = hasAlerts.contains(player)) hasAlerts.remove(player);
else hasAlerts.add(player);
return !contains;
return data.hasAlerts = !data.hasAlerts;
}
}
+52 -48
View File
@@ -1,51 +1,31 @@
package dev.brighten.pl.handlers;
import cc.funkemunky.api.Atlas;
import cc.funkemunky.api.utils.MiscUtils;
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.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 org.bukkit.event.HandlerList;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
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<>();
@Getter
private Map<UUID, VPNResponse> cached = new HashMap<>();
public ExecutorService thread = Executors.newSingleThreadExecutor();
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()) {
VPNCheckEvent event = new VPNCheckEvent(response);
if(Config.fireEvent)
RunUtils.task(() -> Bukkit.getPluginManager().callEvent(event), AntiVPN.INSTANCE);
if(response.isProxy()) {
if(Config.alertStaff) alert(response, element.one);
if(Config.kickPlayers) kick(response, element.one);
}
} else MiscUtils.printToConsole((response != null) + "?");
}
checking.set(false);
queue.addAll(toAdd);
toAdd.clear();
}
}, 0L, 20L, TimeUnit.MILLISECONDS);
RunUtils.taskTimerAsync(() -> cached.clear(), 24000, 24000); //Clear cache every 20 minutes
}
private void alert(VPNResponse response, UUID uuid) {
if(Config.alertBungee) {
@@ -56,24 +36,48 @@ public class VPNHandler {
}
private void kick(VPNResponse response, UUID uuid) {
if(Config.kickBungee) {
Atlas.getInstance().getBungeeManager().getBungeeAPI()
.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);
RunUtils.task(() -> {
String message = StringUtils.formatString(Config.kickMessage, response);
if(player != null)
if(player != null) {
String message = StringUtils.formatString(Config.kickMessage, response);
player.kickPlayer(message);
else toKick.put(uuid, message);
});
}
}
}
});
}
public void shutdown() {
thread.shutdown();
}
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) {
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);
}
});
}
}
+20 -9
View File
@@ -2,23 +2,34 @@ package dev.brighten.pl.listeners;
import cc.funkemunky.api.utils.Init;
import dev.brighten.pl.AntiVPN;
import dev.brighten.pl.config.Config;
import dev.brighten.pl.utils.StringUtils;
import lombok.val;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.AsyncPlayerPreLoginEvent;
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.checkPlayer(event.getUniqueId(), event.getAddress().getHostAddress());
}
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onEvent(PlayerLoginEvent event) {
if(AntiVPN.INSTANCE.vpnHandler.getCached().containsKey(event.getPlayer().getUniqueId())) {
val result = AntiVPN.INSTANCE.vpnHandler.getCached().get(event.getPlayer().getUniqueId());
if(result.isProxy() && !event.getPlayer().hasPermission("antivpn.bypass")) {
event.setKickMessage(StringUtils.formatString(Config.kickMessage, result));
event.setResult(PlayerLoginEvent.Result.KICK_BANNED);
}
}
AntiVPN.INSTANCE.vpnHandler.checkPlayer(event.getPlayer());
}
}
+1 -1
View File
@@ -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,32 +0,0 @@
package dev.brighten.pl.utils;
import cc.funkemunky.carbon.utils.json.JSONException;
import cc.funkemunky.carbon.utils.json.JSONObject;
import java.io.*;
import java.net.URL;
import java.nio.charset.Charset;
public class JsonReader {
private static String readAll(Reader rd) throws IOException {
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = rd.read()) != -1) {
sb.append((char) cp);
}
return sb.toString();
}
public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
InputStream is = new URL(url).openStream();
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
String jsonText = readAll(rd);
JSONObject json = new JSONObject(jsonText);
return json;
} finally {
is.close();
}
}
}
+1 -1
View File
@@ -1,7 +1,7 @@
package dev.brighten.pl.utils;
import cc.funkemunky.api.utils.Color;
import cc.funkemunky.carbon.utils.json.JSONException;
import dev.brighten.db.utils.json.JSONException;
import dev.brighten.pl.vpn.VPNResponse;
import lombok.val;
+19 -65
View File
@@ -1,83 +1,35 @@
package dev.brighten.pl.vpn;
import cc.funkemunky.api.utils.MathUtils;
import cc.funkemunky.api.utils.MiscUtils;
import cc.funkemunky.api.utils.RunUtils;
import cc.funkemunky.carbon.db.Database;
import cc.funkemunky.carbon.db.StructureSet;
import cc.funkemunky.carbon.db.flatfile.FlatfileDatabase;
import cc.funkemunky.carbon.utils.Pair;
import cc.funkemunky.carbon.utils.json.JSONException;
import cc.funkemunky.carbon.utils.json.JSONObject;
import dev.brighten.pl.AntiVPN;
import dev.brighten.pl.utils.Config;
import dev.brighten.pl.utils.JsonReader;
import dev.brighten.db.utils.json.JSONException;
import dev.brighten.db.utils.json.JSONObject;
import dev.brighten.db.utils.json.JsonReader;
import dev.brighten.pl.config.Config;
import lombok.val;
import org.bukkit.entity.Player;
import java.io.IOException;
import java.util.Objects;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
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...");
RunUtils.taskTimerAsync(database::saveDatabase, AntiVPN.INSTANCE, 0, 20 * 60 * 2);
}
public VPNResponse getResponse(Player player) {
return getResponse(player.getAddress().getAddress().getHostAddress());
}
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(response.isSuccess()) {
try {
//Removing old value if it contains it.
if(database.contains(response.getIp())) database.remove(response.getIp());
val json = response.toJson();
val pairs = json.keySet().stream().map(key -> {
try {
return new Pair<>(key, json.get(key));
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}).filter(Objects::nonNull).toArray(Pair[]::new);
StructureSet set = database.createStructure(response.getIp(), pairs);
if(MathUtils.getDelta(set.getObjects().size(), pairs.length) > 1) {
MiscUtils.printToConsole("&cThere was an error saving response for IP &f"
+ response.getIp() + "&c. &7Removing from database...");
database.remove(response.getIp());
}
} catch (JSONException e) {
e.printStackTrace();
}
}
cache.put(response.getIp(), response);
}
public VPNResponse getIfCached(String ipAddress) {
if(database.contains(ipAddress)) {
return VPNResponse.fromSet(database.get(ipAddress));
} else {
return null;
}
return cache.getOrDefault(ipAddress, null);
}
public VPNResponse getResponse(String ipAddress) {
@@ -92,11 +44,13 @@ public class VPNAPI {
JSONObject object = JsonReader.readJsonFromUrl(url);
val toCacheAndReturn = VPNResponse.fromJson(object.toString());
if(object.has("success") && object.getBoolean("success")) {
val toCacheAndReturn = VPNResponse.fromJson(object.toString());
cacheReponse(toCacheAndReturn);
cacheReponse(toCacheAndReturn);
return toCacheAndReturn;
return toCacheAndReturn;
} else System.out.println("failed");
} catch (IOException | JSONException e) {
e.printStackTrace();
}
+13 -30
View File
@@ -1,8 +1,7 @@
package dev.brighten.pl.vpn;
import cc.funkemunky.carbon.db.StructureSet;
import cc.funkemunky.carbon.utils.json.JSONException;
import cc.funkemunky.carbon.utils.json.JSONObject;
import dev.brighten.db.utils.json.JSONException;
import dev.brighten.db.utils.json.JSONObject;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
@@ -11,10 +10,11 @@ import lombok.Setter;
@Setter
@AllArgsConstructor
public class VPNResponse {
private String ip, countryName, countryCode, method, state, city, isp, timeZone, locationString;
private boolean proxy, usedAdvanced, cached, success;
private double score;
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();
@@ -22,18 +22,14 @@ public class VPNResponse {
json.put("ip", ip);
json.put("countryName", countryName);
json.put("countryCode", countryCode);
json.put("state", state);
json.put("city", city);
json.put("method", method);
json.put("isp", isp);
json.put("score", score);
json.put("proxy", proxy);
json.put("success", success);
json.put("timeZone", timeZone);
json.put("success", true);
json.put("queriesLeft", queriesLeft);
json.put("locationString", locationString);
json.put("usedAdvanced", usedAdvanced);
json.put("cached", cached);
return json;
@@ -42,26 +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"),
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("countryCode"), jsonObject.getString("state"),
jsonObject.getString("city"), jsonObject.getString("isp"),
jsonObject.getString("timeZone"), jsonObject.getString("locationString"),
jsonObject.getBoolean("proxy"), jsonObject.getBoolean("usedAdvanced"),
jsonObject.getString("isp"), jsonObject.getBoolean("proxy"),
jsonObject.getBoolean("cached"), jsonObject.getBoolean("success"),
jsonObject.has("score") ? jsonObject.getDouble("score") : -1,
jsonObject.getInt("queriesLeft"));
}
public static VPNResponse fromSet(StructureSet set) {
return new VPNResponse(set.getField("ip"), set.getField("countryName"),
set.containsKey("method") ? set.getField("method") : "N/A",
set.getField("countryCode"), set.getField("state"),
set.getField("city"), set.getField("isp"),
set.getField("timeZone"), set.getField("locationString"),
set.getField("proxy"), set.getField("usedAdvanced"),
set.getField("cached"), set.getField("success"),
set.containsKey("score") ? set.getDouble("score") : -1,
set.getField("queriesLeft"));
jsonObject.getDouble("latitude"), jsonObject.getDouble("longitude"),
jsonObject.getLong("lastAccess"), jsonObject.getInt("queriesLeft"));
}
}
Regular → Executable
+49
View File
@@ -5,6 +5,51 @@
#
license: ""
#
# Caching Database #
# Types: Mongo, Flatfile, MySQL.
#
# FLATFILE INFORMATION #
# Flatfile is enabled by default if nothing else is enabled.
# Make sure to only enable one at a time or plugin will default to Flatfile.
# The database will be stored in the root directory of the user that started the server.
# Each entry will be stored as a .json file in the directory with the ID of as its name.
# (user directory)/CarbonFFDBs/{database}
#
# MONGO INFORMATION #
# For Mongo, leave credentials empty if your mongo instance doesn't require it.
# The authDatabase is used if your mongo credentials are tied to a different database.
#
# MYSQL INFORMATION #
# It is recommended you turn SSL to true for secure transfer of information.
#
# GENERAL INFORMATION #
# It is highly recommended you leave the hashIp setting to true and the hash algorithm to SHA2.
# Hash Options: CRC16, SHA1, SHA2, Argon.
# This will hash the IPs into a SHA1 or SHA2 string to protect your users privacy.
# More information on SHA-2: https://en.wikipedia.org/wiki/SHA-2
database:
flatfile:
enabled: true
database: "kaurivpn"
mongo:
enabled: false
ip: "127.0.0.1"
port: 27017
username: ""
password: ""
database: "kaurivpn"
authDatabase: ""
mysql:
enabled: false
ip: "127.0.0.1"
port: 3306
username: "root"
password: "password"
database: "kaurivpn"
ssl: true
general:
hashIp: true
hashType: "SHA2"
# Enable kicking #
# You can disable the function of removing players from the server.
kick-players: true
@@ -21,6 +66,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
Regular → Executable
+4 -1
View File
@@ -6,4 +6,7 @@ version: ${project.version}
permissions:
antivpn.showip:
default: false
description: Permission node to show IPs.
description: Permission node to show IPs.
antivpn.bypass:
default: op
description: Permission node to bypass VPN kicks and alerts.