First commit.

- Almost everything.
This commit is contained in:
funkemunky
2020-01-10 13:16:12 -05:00
parent 35b0f78efe
commit be93dec2a4
15 changed files with 657 additions and 0 deletions
+60
View File
@@ -0,0 +1,60 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>dev.brighten.plugin</groupId>
<artifactId>AntiVPN</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>8</source>
<target>8</target>
<compilerArgument>-XDignore.symbol.file</compilerArgument>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<repositories>
<repository>
<id>funkemunky-releases</id>
<url>http://nexus.funkemunky.cc/content/repositories/releases/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>cc.funkemunky.utils</groupId>
<artifactId>lombok</artifactId>
<version>1.18.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.github.spigot</groupId>
<artifactId>1.8.8</artifactId>
<version>1.8.8</version>
</dependency>
<dependency>
<groupId>cc.funkemunky.plugins</groupId>
<artifactId>Atlas</artifactId>
<version>1.6.6</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,72 @@
package dev.brighten.pl;
import cc.funkemunky.api.Atlas;
import cc.funkemunky.api.utils.MiscUtils;
import dev.brighten.pl.handlers.AlertsHandler;
import dev.brighten.pl.handlers.VPNHandler;
import dev.brighten.pl.vpn.VPNAPI;
import org.bukkit.Bukkit;
import org.bukkit.event.HandlerList;
import org.bukkit.plugin.java.JavaPlugin;
public class AntiVPN extends JavaPlugin {
public static AntiVPN INSTANCE;
public VPNAPI vpnAPI;
public VPNHandler vpnHandler;
public AlertsHandler alertsHandler;
public void onEnable() {
INSTANCE = this;
enable();
}
public void onDisable() {
disable();
}
public void enable() {
print(true, "scanner");
Atlas.getInstance().initializeScanner(this, true, true);
print(true, "vpn api and handlers");
vpnAPI = new VPNAPI();
vpnHandler = new VPNHandler();
print(true, "alerts handler");
alertsHandler = new AlertsHandler();
MiscUtils.printToConsole("&aCompleted startup.");
}
public void disable() {
print(false, "listeners");
HandlerList.unregisterAll(this);
print(false, "tasks");
Bukkit.getScheduler().cancelTasks(this);
print("Save", "database");
AntiVPN.INSTANCE.vpnAPI.database.saveDatabase();
print(false, "threads");
AntiVPN.INSTANCE.vpnAPI.vpnThread.shutdown();
print(false, "handlers");
AntiVPN.INSTANCE.vpnAPI.vpnThread = null;
AntiVPN.INSTANCE.vpnHandler = null;
AntiVPN.INSTANCE.alertsHandler = null;
MiscUtils.printToConsole("&aCompleted shutdown.");
}
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 + "...");
}
}
@@ -0,0 +1,19 @@
package dev.brighten.pl.commands;
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 AlertsCommand {
@Command(name = "kaurivpn.alerts", description = "toggle vpn alerts",
display = "alerts", playerOnly = true, permission = {"kauri.alerts", "kauri.command.alerts"})
public void onCommand(CommandAdapter cmd) {
boolean toggled = AntiVPN.INSTANCE.alertsHandler.toggleAlerts(cmd.getPlayer());
cmd.getSender().sendMessage(toggled
? "&aYou are now viewing vpn detection alerts."
: "&cYou are no longer viewing vpn detection alerts.");
}
}
@@ -0,0 +1,17 @@
package dev.brighten.pl.commands;
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;
@Init(commands = true)
public class VpnCommand {
@Command(name = "kaurivpn", description = "The Kauri AntiVPN main command.",
aliases = {"antivpn", "avpn", "kvpn"}, permission = "kvpn.command")
public void onCommand(CommandAdapter cmd) {
Atlas.getInstance().getCommandManager().runHelpMessage(cmd,
cmd.getSender(), Atlas.getInstance().getCommandManager().getDefaultScheme());
}
}
@@ -0,0 +1,44 @@
package dev.brighten.pl.handlers;
import cc.funkemunky.api.utils.JsonMessage;
import dev.brighten.pl.utils.Config;
import dev.brighten.pl.utils.StringUtils;
import dev.brighten.pl.vpn.VPNResponse;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.craftbukkit.v1_8_R3.util.WeakCollection;
import org.bukkit.entity.Player;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import java.util.*;
public class AlertsHandler {
private Set<Player> hasAlerts = new HashSet<>();
public void sendAlert(UUID uuid, VPNResponse response) {
JsonMessage message = new JsonMessage();
OfflinePlayer player = Bukkit.getOfflinePlayer(uuid);
message.addText(StringUtils.formatString(Config.alertMessage, response)
.replace("%player%", player.getName()))
.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);
}
//TODO When updated Atlas releases, add this functionality.
public void sendBungeeAlert(UUID uuid, VPNResponse response) {
//Empty for now.
}
public boolean toggleAlerts(Player player) {
boolean contains;
if(contains = hasAlerts.contains(player)) hasAlerts.remove(player);
else hasAlerts.add(player);
return !contains;
}
}
@@ -0,0 +1,69 @@
package dev.brighten.pl.handlers;
import cc.funkemunky.api.Atlas;
import cc.funkemunky.api.utils.RunUtils;
import cc.funkemunky.api.utils.Tuple;
import dev.brighten.pl.AntiVPN;
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.val;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import java.util.Queue;
import java.util.UUID;
import java.util.concurrent.ConcurrentLinkedQueue;
public class VPNHandler {
private Queue<Tuple<UUID, String>> queue = new ConcurrentLinkedQueue<>();
public void run() {
}
private void runCheck() {
AntiVPN.INSTANCE.vpnAPI.vpnThread.execute(() -> {
Tuple<UUID, String> element;
while((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);
}
}
}
runCheck();
});
}
private void alert(VPNResponse response, UUID uuid) {
if(Config.alertBungee) {
AntiVPN.INSTANCE.alertsHandler.sendBungeeAlert(uuid, response); //Empty method until Atlas v1.7 releases.
} else {
AntiVPN.INSTANCE.alertsHandler.sendAlert(uuid, response);
}
}
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);
if(player != null)
player.kickPlayer(StringUtils.formatString(Config.kickMessage, response));
}
}
public void checkPlayer(Player player) {
queue.add(new Tuple<>(player.getUniqueId(), player.getAddress().getAddress().getHostAddress()));
}
}
@@ -0,0 +1,18 @@
package dev.brighten.pl.listeners;
import cc.funkemunky.api.utils.Init;
import dev.brighten.pl.AntiVPN;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
@Init
public class JoinListener implements Listener {
@EventHandler(priority = EventPriority.MONITOR)
public void onJoin(PlayerJoinEvent event) {
AntiVPN.INSTANCE.vpnHandler.checkPlayer(event.getPlayer());
}
}
@@ -0,0 +1,23 @@
package dev.brighten.pl.listeners.impl;
import dev.brighten.pl.vpn.VPNResponse;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import java.util.UUID;
@RequiredArgsConstructor
@Getter
public class VPNCheckEvent extends Event {
private UUID uuid;
private final VPNResponse response;
private static HandlerList handlerList = new HandlerList();
public HandlerList getHandlers() {
return handlerList;
}
}
@@ -0,0 +1,40 @@
package dev.brighten.pl.utils;
import cc.funkemunky.api.utils.ConfigSetting;
import cc.funkemunky.api.utils.Init;
import java.util.ArrayList;
import java.util.List;
@Init
public class Config {
@ConfigSetting(name = "license")
public static String license = "";
@ConfigSetting(name = "kick-players")
public static boolean kickPlayers = true;
@ConfigSetting(name = "kick-message")
public static String kickMessage = "";
@ConfigSetting(name = "kick-bungee")
public static boolean kickBungee = false;
@ConfigSetting(name = "alert-staff")
public static boolean alertStaff = true;
@ConfigSetting(name = "alert-message")
public static String alertMessage = "";
@ConfigSetting(name = "alert-hover")
public static List<String> alertHoverMessage = new ArrayList<>();
@ConfigSetting(name = "alert-bungee")
public static boolean alertBungee = false;
@ConfigSetting(name = "hide-ips")
public static boolean hideIps = false;
@ConfigSetting(name = "fire-event")
public static boolean fireEvent = true;
}
@@ -0,0 +1,32 @@
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();
}
}
}
@@ -0,0 +1,27 @@
package dev.brighten.pl.utils;
import cc.funkemunky.api.utils.Color;
import cc.funkemunky.carbon.utils.json.JSONException;
import dev.brighten.pl.vpn.VPNResponse;
import lombok.val;
public class StringUtils {
public static String formatString(String string, VPNResponse response) {
String message = Color.translate(string);
if (response.isSuccess()) {
try {
val json = response.toJson();
for (String key : json.keySet()) {
message = message.replace(key, String.valueOf(json.get(key)));
}
return message;
} catch (JSONException e) {
e.printStackTrace();
}
}
return message;
}
}
@@ -0,0 +1,109 @@
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 lombok.val;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import java.io.IOException;
import java.util.Objects;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class VPNAPI {
public Database database;
public ExecutorService 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.newFixedThreadPool(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());
}
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();
}
}
}
public VPNResponse getIfCached(String ipAddress) {
if(database.contains(ipAddress)) {
return VPNResponse.fromSet(database.get(ipAddress));
} else {
return null;
}
}
public VPNResponse getResponse(String ipAddress) {
try {
val response = getIfCached(ipAddress);
if(response != null) return response;
String url = "https://funkemunky.cc/vpn?license=" + Config.license + "&ip=" + ipAddress;
JSONObject object = JsonReader.readJsonFromUrl(url);
if (!object.has("ip")) {
return null;
}
val toCacheAndReturn = VPNResponse.fromJson(object.toString());
cacheReponse(toCacheAndReturn);
return toCacheAndReturn;
} catch (IOException | JSONException e) {
e.printStackTrace();
}
return null;
}
}
@@ -0,0 +1,67 @@
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 lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
@Getter
@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;
public JSONObject toJson() throws JSONException {
JSONObject json = new JSONObject();
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;
}
public static VPNResponse fromJson(String json) throws JSONException {
JSONObject jsonObject = new JSONObject(json);
return new VPNResponse(jsonObject.getString("ip"), jsonObject.getString("countryName"),
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.getBoolean("cached"), jsonObject.getBoolean("sucess"),
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("sucess"),
set.containsKey("score") ? set.getDouble("score") : -1,
set.getField("queriesLeft"));
}
}
+51
View File
@@ -0,0 +1,51 @@
#
# You will need a license from https://funkemunky.cc/shop to use the AntiVPN to its full potential.
# Your server will have 200 free queries per month. Once that ends, the plugin will no longer query.
# You can also use your Kauri Anticheat license here.
#
license: ""
#
# Enable kicking #
# You can disable the function of removing players from the server.
kick-players: true
# Kick Message #
# The message sent to the player when they are kicked.
kick-message: |-
&r
&6&lKauri AntiVPN
&7You have been kicked for using a VPN.
&r
&f&o%method%
#
# Run Command in Bungee #
# This will allow the command to be run in Bungee.
kick-bungee: false
#
# VPN Use Alerts #
# A great way to find nefarious players quickly (i.e. cheaters) without them knowing what is going on.
alert-staff: true
#
# Alert Message #
# This will be the message sent to staff vpn detection.
# Requires the permission antivpn.alert to view alerts.
alert-message: "&8[&c!&8] &7Found &f%player% &7to be using a VPN."
# Alert Hover #
# The lines here will be shown when hovering on the alert message
# Placeholders: %method%, %ip%, %countryCode%, %countryName%, %score%, %queriesLeft%, %proxy%, %isp%, %timeZone%
alert-hover:
- "&eMethod: &f%method%"
- "&eIP: &f%ip%"
- "&eCountry: &f%countryName%"
#
# Alert Bungee #
# Requires AntiVPN to be on all servers using it and will require Atlas bungee mode to be enabled.
# All configuration for alerts is local. No setup required on Bungee.
alert-bungee: false
#
# Hide IP #
# This will hide the IP from all users. If false, IPs are only shown to users with permission antivpn.showip.
hide-ips: false
#
# Fire Event #
# If disabled, the VPNCheckEvent will no longer be fired. Only disable this if you have no plugins depending on it.
fire-event: true
+9
View File
@@ -0,0 +1,9 @@
name: KauriVPN
main: dev.brighten.pl.AntiVPN
author: funkemunky
depend: [Atlas]
version: ${project.version}
permissions:
antivpn.showip:
default: false
description: Permission node to show IPs.