CIDR Support setup and implemeneted for adding/removing via command, and checking updates for CIDR implemented. Also fixed problem with SHA-algor not being present

This commit is contained in:
2025-05-30 18:10:08 -04:00
parent 83f06b29c8
commit bb3a31e100
13 changed files with 133 additions and 201 deletions
@@ -30,21 +30,8 @@ import java.util.List;
@Getter
@Setter(AccessLevel.PRIVATE)
@MavenLibrary(groupId = "com.h2database", artifactId ="h2", version = "2.2.220", relocations = {
@Relocate(from ="org" + ".\\h2", to ="dev.brighten.antivpn.shaded.org.h2")})
@MavenLibrary(groupId = "org.mongodb", artifactId = "mongo-java-driver", version = "3.12.14", relocations = {
@Relocate(from = "com." + "\\mongodb", to = "dev.brighten.antivpn.shaded.com.mongodb"),
@Relocate(from = "org" + "\\.bson", to = "dev.brighten.antivpn.shaded.org.bson")
})
@MavenLibrary(
groupId = "com.mysql",
artifactId = "mysql-connector-j",
version = "9.1.0",
relocations = {
@Relocate(from = "com.my\\" + "sql.cj", to = "dev.brighten.antivpn.shaded.com.mysql.cj"),
@Relocate(from = "com.my\\" + "sql.jdbc", to = "dev.brighten.antivpn.shaded.com.mysql.jdbc")
}
)
@MavenLibrary(groupId = "org\\.sqlite", artifactId ="sqlite-jdbc", version = "3.48.0.0", relocations = {
@Relocate(from ="org" + ".\\sqlite", to ="dev.brighten.antivpn.shaded.org.sqlite")})
@MavenLibrary(groupId = "com.\\github\\.ben-manes\\.caffeine", artifactId = "caffeine", version = "3.1.8",
relocations = {
@Relocate(from = "com\\.github\\.benmanes\\.caffeine", to = "dev.brighten.antivpn.shaded.com.github.benmanes.caffeine"),
@@ -142,21 +129,7 @@ public class AntiVPN {
}
public void stop() {
if (database instanceof H2VPN) {
database.shutdown();
// Try to deregister driver
try {
java.sql.Driver driver = java.sql.DriverManager.getDriver("jdbc:h2:");
if (driver != null) {
java.sql.DriverManager.deregisterDriver(driver);
}
} catch (Exception e) {
// Log but don't throw
executor.log("Failed to deregister H2 driver: " + e.getMessage());
}
}
VPNExecutor.threadExecutor.shutdown();
executor.shutdown();
if(database != null) database.shutdown();
}
@@ -47,7 +47,7 @@ public abstract class APIPlayer {
//Is exempt
|| (uuid != null && AntiVPN.getInstance().getExecutor().isWhitelisted(uuid))
//Or has a name that starts with a certain prefix. This is for Bedrock exempting.
|| AntiVPN.getInstance().getExecutor().isWhitelisted(ip.getHostAddress())
|| AntiVPN.getInstance().getExecutor().isWhitelisted(ip.getHostAddress() + "/32")
|| AntiVPN.getInstance().getVpnConfig().getPrefixWhitelists().stream()
.anyMatch(name::startsWith)) return new CheckResult(null, ResultType.WHITELISTED);
@@ -74,7 +74,7 @@ public abstract class APIPlayer {
&& !((uuid != null && AntiVPN.getInstance().getExecutor()
.isWhitelisted(uuid))
//Or has a name that starts with a certain prefix. This is for Bedrock exempting.
|| AntiVPN.getInstance().getExecutor().isWhitelisted(ip.getHostAddress()))
|| AntiVPN.getInstance().getExecutor().isWhitelisted(ip.getHostAddress() + "/32"))
// This bit of code will decide whether or not to kick the player
// If it contains the code and it is set to whitelist, it will not kick
// as they are equal and vise versa. However, if the contains does not match
@@ -4,8 +4,10 @@ import dev.brighten.antivpn.AntiVPN;
import dev.brighten.antivpn.api.APIPlayer;
import dev.brighten.antivpn.command.Command;
import dev.brighten.antivpn.command.CommandExecutor;
import dev.brighten.antivpn.utils.CIDRUtils;
import dev.brighten.antivpn.utils.MiscUtils;
import java.net.UnknownHostException;
import java.util.*;
import java.util.stream.Collectors;
@@ -62,50 +64,75 @@ public class AllowlistCommand extends Command {
if(!databaseEnabled) executor.sendMessage("&cThe database is currently not setup, " +
"so any changes here will disappear after a restart.");
CIDRUtils cidrUtils;
try {
cidrUtils = new CIDRUtils(args[1]);
} catch(IllegalArgumentException | UnknownHostException e) {
cidrUtils = null;
}
if(cidrUtils != null) {
if(!databaseEnabled) {
return switch (args[0].toLowerCase()) {
case "add", "insert" -> {
AntiVPN.getInstance().getExecutor().getWhitelistedIps().add(cidrUtils.getCidr());
yield String.format("&aAdded &6%s &ato exemption allowlist.", cidrUtils.getCidr());
}
case "remove", "delete" -> {
AntiVPN.getInstance().getExecutor().getWhitelistedIps().remove(cidrUtils.getCidr());
yield String.format("&cRemoved &%s &cfrom the exemption allowlist.", cidrUtils.getCidr());
}
default -> "&c\"" + args[0] + "\" is not a valid argument";
};
} else return switch (args[0].toLowerCase()) {
case "add", "insert" -> {
AntiVPN.getInstance().getExecutor().getWhitelistedIps().add(cidrUtils.getCidr());
AntiVPN.getInstance().getDatabase().addWhitelist(cidrUtils.getCidr());
yield String.format("&aAdded &6%s &ato exemption allowlist.", cidrUtils.getCidr());
}
case "remove", "delete" -> {
AntiVPN.getInstance().getExecutor().getWhitelistedIps().remove(cidrUtils.getCidr());
AntiVPN.getInstance().getDatabase().removeWhitelist(cidrUtils.getCidr());
yield String.format("&cRemoved &6%s &cfrom the exemption allowlist.", cidrUtils.getCidr());
}
default -> "&c\"" + args[0] + "\" is not a valid argument";
};
}
if(MiscUtils.isIpv4(args[1])) {
if(!databaseEnabled) {
switch(args[0].toLowerCase()) {
case "add":
case "insert": {
AntiVPN.getInstance().getExecutor().getWhitelistedIps().add(args[1]);
AntiVPN.getInstance().getDatabase().removeWhitelist(args[1]);
return String.format("&aAdded &6%s &ato the exemption allowlist.", args[1]);
return switch(args[0].toLowerCase()) {
case "add", "insert" -> {
AntiVPN.getInstance().getExecutor().getWhitelistedIps().add(args[1] + "/32");
AntiVPN.getInstance().getDatabase().addWhitelist(args[1] + "/32");
yield String.format("&aAdded &6%s &ato the exemption allowlist.", args[1] + "/32");
}
case "remove":
case "delete": {
AntiVPN.getInstance().getExecutor().getWhitelistedIps().remove(args[1]);
AntiVPN.getInstance().getDatabase().removeWhitelist(args[1]);
return String.format("&cRemoved &6%s &cfrom the exemption allowlist.", args[1]);
case "remove", "delete" -> {
AntiVPN.getInstance().getExecutor().getWhitelistedIps().remove(args[1] + "/32");
AntiVPN.getInstance().getDatabase().removeWhitelist(args[1] + "/32");
yield String.format("&cRemoved &6%s &cfrom the exemption allowlist.", args[1] + "/32");
}
default: {
return "&c\"" + args[0] + "\" is not a valid argument";
default -> "&c\"" + args[0] + "\" is not a valid argument";
};
} else return switch (args[0].toLowerCase()) {
case "add", "insert" -> {
AntiVPN.getInstance().getDatabase().addWhitelist(args[1] + "/32");
yield String.format("&aAdded &6%s &a to the exemption allowlist.", args[1] + "/32");
}
}
} else {
switch(args[0].toLowerCase()) {
case "add":
case "insert": {
AntiVPN.getInstance().getDatabase().addWhitelist(args[1]);
return String.format("&aAdded &6%s &a to the exemption allowlist.", args[1]);
case "remove", "delete" -> {
AntiVPN.getInstance().getDatabase().removeWhitelist(args[1] + "/32");
yield String.format("&cRemoved &6%s &c from the exemption allowlist.", args[1] + "/32");
}
case "remove":
case "delete": {
AntiVPN.getInstance().getDatabase().removeWhitelist(args[1]);
return String.format("&cRemoved &6%s &c from the exemption allowlist.", args[1]);
}
default: {
return "&c\"" + args[0] + "\" is not a valid argument";
}
}
}
default -> "&c\"" + args[0] + "\" is not a valid argument";
};
} else {
UUID uuid = null;
UUID uuid;
try {
uuid = UUID.fromString(args[1]);
} catch(IllegalArgumentException e) {
Optional<APIPlayer> player = AntiVPN.getInstance().getPlayerExecutor().getPlayer(args[1]);
if(!player.isPresent()) {
if(player.isEmpty()) {
return "&cThe player \"" + args[1] + "\" is not online, so please provide a UUID.";
}
@@ -113,54 +140,44 @@ public class AllowlistCommand extends Command {
}
if(!databaseEnabled) {
switch(args[0].toLowerCase()) {
case "add": {
return switch (args[0].toLowerCase()) {
case "add" -> {
AntiVPN.getInstance().getExecutor().getWhitelisted().add(uuid);
return String.format("&aAdded &6%s &auuid to the exemption allowlist.", uuid.toString());
yield String.format("&aAdded &6%s &auuid to the exemption allowlist.", uuid.toString());
}
case "remove":
case "delete": {
case "remove", "delete" -> {
AntiVPN.getInstance().getExecutor().getWhitelisted().remove(uuid);
return String.format("&cRemoved &6%s &cuuid from the exemption allowlist.", uuid.toString());
yield String.format("&cRemoved &6%s &cuuid from the exemption allowlist.", uuid.toString());
}
default: {
return "&c\"" + args[0] + "\" is not a valid argument";
}
}
default -> "&c\"" + args[0] + "\" is not a valid argument";
};
} else {
switch(args[0].toLowerCase()) {
case "add": {
return switch (args[0].toLowerCase()) {
case "add" -> {
AntiVPN.getInstance().getDatabase().addWhitelist(uuid);
return String.format("&aAdded &6%s &auuid to the exemption allowlist.", uuid.toString());
yield String.format("&aAdded &6%s &auuid to the exemption allowlist.", uuid.toString());
}
case "remove":
case "delete": {
case "remove", "delete" -> {
AntiVPN.getInstance().getDatabase().removeWhitelist(uuid);
return String.format("&cRemoved &6%s &cuuid from the exemption allowlist.", uuid.toString());
yield String.format("&cRemoved &6%s &cuuid from the exemption allowlist.", uuid.toString());
}
default: {
return "&c\"" + args[0] + "\" is not a valid argument";
}
}
default -> "&c\"" + args[0] + "\" is not a valid argument";
};
}
}
}
@Override
public List<String> tabComplete(CommandExecutor executor, String alias, String[] args) {
switch(args.length) {
case 1: {
return Arrays.stream(secondArgs)
.filter(narg -> narg.toLowerCase().startsWith(args[0].toLowerCase()))
.collect(Collectors.toList());
}
case 2: {
return AntiVPN.getInstance().getPlayerExecutor().getOnlinePlayers().stream()
.map(APIPlayer::getName)
.filter(name -> name.toLowerCase().startsWith(args[1].toLowerCase()))
.collect(Collectors.toList());
}
}
return Collections.emptyList();
return switch (args.length) {
case 1 -> Arrays.stream(secondArgs)
.filter(narg -> narg.toLowerCase().startsWith(args[0].toLowerCase()))
.collect(Collectors.toList());
case 2 -> AntiVPN.getInstance().getPlayerExecutor().getOnlinePlayers().stream()
.map(APIPlayer::getName)
.filter(name -> name.toLowerCase().startsWith(args[1].toLowerCase()))
.collect(Collectors.toList());
default -> Collections.emptyList();
};
}
}
@@ -51,7 +51,7 @@ public class PlanCommand extends Command {
@Override
public String execute(CommandExecutor executor, String[] args) {
VPNExecutor.threadExecutor.execute(() -> {
AntiVPN.getInstance().getExecutor().getThreadExecutor().execute(() -> {
QueryResponse result;
try {
if(AntiVPN.getInstance().getVpnConfig().getLicense().isEmpty()) {
@@ -179,15 +179,16 @@ public class LiteDatabase implements VPNDatabase {
@Override
public void init() {
String url = "jdbc:sqlite:./plugins/AntiVPN/database.db";
String url = "jdbc:sqlite:" + AntiVPN.getInstance().getPluginFolder().toPath() + "/database.db";
try {
Class.forName("org.sqlite.JDBC");
Connection connection = DriverManager.getConnection(url);
Statement statement = connection.createStatement();
statement.execute("CREATE TABLE IF NOT EXISTS vpn_responses (ip TEXT, response TEXT)");
statement.execute("CREATE TABLE IF NOT EXISTS whitelist (uuid TEXT, NUMBER minimum, NUMBER maximum)");
statement.execute("CREATE TABLE IF NOT EXISTS whitelist (uuid TEXT, minimum NUMERIC, maximum NUMERIC)");
statement.execute("CREATE TABLE IF NOT EXISTS alerts (uuid TEXT)");
statement.execute("CREATE TABLE IF NOT EXISTS version (PRIMARY KEY(version) NUMBER, updated STATE)");
statement.execute("CREATE TABLE IF NOT EXISTS version (version INTEGER PRIMARY KEY, updated BOOLEAN)");
this.connection = connection;
@@ -204,7 +205,7 @@ public class LiteDatabase implements VPNDatabase {
AntiVPN.getInstance().getExecutor().logException(e);
}
}
} catch (SQLException e) {
} catch (SQLException | ClassNotFoundException e) {
AntiVPN.getInstance().getExecutor().logException(e);
}
}
@@ -20,7 +20,7 @@ public class StringUtil {
public static String getHash(String input) {
try {
MessageDigest digest = MessageDigest.getInstance("SHA-128");
MessageDigest digest = MessageDigest.getInstance("SHA1");
byte[] hash = digest.digest(input.getBytes(StandardCharsets.UTF_8));
StringBuilder hexString = new StringBuilder(2 * hash.length);
for (byte b : hash) {