From 8a4b86c9ef42a77cc623aa77bc335b3ef172437d Mon Sep 17 00:00:00 2001 From: Dawson Date: Wed, 14 Jan 2026 09:42:20 -0500 Subject: [PATCH] Allowlist functionality is now working, corrected sql errors --- Bukkit/Loader/pom.xml | 4 +- Bukkit/Plugin/pom.xml | 6 +- Bukkit/pom.xml | 2 +- Bungee/BungeeLoader/pom.xml | 4 +- Bungee/BungeePlugin/pom.xml | 6 +- Bungee/pom.xml | 2 +- Common/Source/pom.xml | 2 +- .../dev/brighten/antivpn/api/VPNExecutor.java | 2 +- .../antivpn/database/local/H2VPN.java | 125 ++++++++++-------- .../antivpn/database/local/version/First.java | 54 +++++--- .../database/local/version/Second.java | 89 ++++++++----- .../antivpn/database/mongo/MongoVPN.java | 2 +- .../sql/utils/ExecutableStatement.java | 30 ++--- .../database/sql/version/MySQLFirst.java | 14 +- Common/loader-utils/pom.xml | 2 +- Common/pom.xml | 2 +- Sponge/SpongeLoader/pom.xml | 4 +- Sponge/SpongePlugin/pom.xml | 6 +- Sponge/pom.xml | 2 +- Universal/pom.xml | 2 +- Velocity/VelocityLoader/pom.xml | 6 +- Velocity/VelocityPlugin/pom.xml | 6 +- Velocity/pom.xml | 2 +- pom.xml | 2 +- 24 files changed, 214 insertions(+), 162 deletions(-) diff --git a/Bukkit/Loader/pom.xml b/Bukkit/Loader/pom.xml index 621ec61..c697135 100644 --- a/Bukkit/Loader/pom.xml +++ b/Bukkit/Loader/pom.xml @@ -22,7 +22,7 @@ dev.brighten.antivpn Bukkit - 1.9.4 + 1.10.0 dev.brighten.antivpn.bukkit Loader @@ -99,7 +99,7 @@ dev.brighten.antivpn loader-utils - 1.9.4 + 1.10.0 compile diff --git a/Bukkit/Plugin/pom.xml b/Bukkit/Plugin/pom.xml index 106a8ea..182d091 100644 --- a/Bukkit/Plugin/pom.xml +++ b/Bukkit/Plugin/pom.xml @@ -22,7 +22,7 @@ dev.brighten.antivpn Bukkit - 1.9.4 + 1.10.0 Plugin @@ -98,7 +98,7 @@ dev.brighten.antivpn Source - 1.9.4 + 1.10.0 provided @@ -110,7 +110,7 @@ dev.brighten.antivpn loader-utils - 1.9.4 + 1.10.0 provided diff --git a/Bukkit/pom.xml b/Bukkit/pom.xml index 278e3b1..2cb1c28 100644 --- a/Bukkit/pom.xml +++ b/Bukkit/pom.xml @@ -21,7 +21,7 @@ AntiVPN dev.brighten.antivpn - 1.9.4 + 1.10.0 pom diff --git a/Bungee/BungeeLoader/pom.xml b/Bungee/BungeeLoader/pom.xml index f708039..9d19d14 100644 --- a/Bungee/BungeeLoader/pom.xml +++ b/Bungee/BungeeLoader/pom.xml @@ -22,7 +22,7 @@ dev.brighten.antivpn Bungee - 1.9.4 + 1.10.0 ../pom.xml @@ -95,7 +95,7 @@ dev.brighten.antivpn loader-utils - 1.9.4 + 1.10.0 compile diff --git a/Bungee/BungeePlugin/pom.xml b/Bungee/BungeePlugin/pom.xml index 81abf03..e6f1a46 100644 --- a/Bungee/BungeePlugin/pom.xml +++ b/Bungee/BungeePlugin/pom.xml @@ -22,7 +22,7 @@ dev.brighten.antivpn Bungee - 1.9.4 + 1.10.0 ../pom.xml @@ -85,7 +85,7 @@ dev.brighten.antivpn Source - 1.9.4 + 1.10.0 provided @@ -104,7 +104,7 @@ dev.brighten.antivpn loader-utils - 1.9.4 + 1.10.0 provided diff --git a/Bungee/pom.xml b/Bungee/pom.xml index 6da004f..bcc273a 100644 --- a/Bungee/pom.xml +++ b/Bungee/pom.xml @@ -21,7 +21,7 @@ AntiVPN dev.brighten.antivpn - 1.9.4 + 1.10.0 pom 4.0.0 diff --git a/Common/Source/pom.xml b/Common/Source/pom.xml index 7962e9e..6384f17 100644 --- a/Common/Source/pom.xml +++ b/Common/Source/pom.xml @@ -22,7 +22,7 @@ dev.brighten.antivpn Common - 1.9.4 + 1.10.0 Source diff --git a/Common/Source/src/main/java/dev/brighten/antivpn/api/VPNExecutor.java b/Common/Source/src/main/java/dev/brighten/antivpn/api/VPNExecutor.java index 0525d91..4749be3 100644 --- a/Common/Source/src/main/java/dev/brighten/antivpn/api/VPNExecutor.java +++ b/Common/Source/src/main/java/dev/brighten/antivpn/api/VPNExecutor.java @@ -133,7 +133,7 @@ public abstract class VPNExecutor { return AntiVPN.getInstance().getDatabase().isWhitelisted(ip); } try { - return whitelistedIps.contains(new CIDRUtils(ip + "/32")); + return whitelistedIps.contains(new CIDRUtils(ip)); } catch (UnknownHostException e) { throw new RuntimeException(e); } diff --git a/Common/Source/src/main/java/dev/brighten/antivpn/database/local/H2VPN.java b/Common/Source/src/main/java/dev/brighten/antivpn/database/local/H2VPN.java index 990cc49..100a378 100644 --- a/Common/Source/src/main/java/dev/brighten/antivpn/database/local/H2VPN.java +++ b/Common/Source/src/main/java/dev/brighten/antivpn/database/local/H2VPN.java @@ -20,6 +20,7 @@ import com.github.benmanes.caffeine.cache.Cache; import com.github.benmanes.caffeine.cache.Caffeine; import dev.brighten.antivpn.AntiVPN; import dev.brighten.antivpn.database.VPNDatabase; +import dev.brighten.antivpn.database.sql.utils.ExecutableStatement; import dev.brighten.antivpn.database.sql.utils.MySQL; import dev.brighten.antivpn.database.sql.utils.Query; import dev.brighten.antivpn.database.version.Version; @@ -67,20 +68,23 @@ public class H2VPN implements VPNDatabase { return Optional.empty(); VPNResponse response = cachedResponses.get(ip, ip2 -> { - try(ResultSet rs = Query.prepare("select * from `responses` where `ip` = ? limit 1").append(ip). - executeQuery()) { - if (rs != null && rs.next()) { - return new VPNResponse(rs.getString("asn"), rs.getString("ip"), - rs.getString("countryName"), rs.getString("countryCode"), - rs.getString("city"), rs.getString("timeZone"), - rs.getString("method"), rs.getString("isp"), "N/A", - rs.getBoolean("proxy"), rs.getBoolean("cached"), true, - rs.getDouble("latitude"), rs.getDouble("longitude"), - rs.getTimestamp("inserted").getTime(), -1); + try(ExecutableStatement statement = Query.prepare("select * from `responses` where `ip` = ? limit 1").append(ip)) { + try(ResultSet rs = statement.executeQuery()) { + if (rs != null && rs.next()) { + return new VPNResponse(rs.getString("asn"), rs.getString("ip"), + rs.getString("countryName"), rs.getString("countryCode"), + rs.getString("city"), rs.getString("timeZone"), + rs.getString("method"), rs.getString("isp"), "N/A", + rs.getBoolean("proxy"), rs.getBoolean("cached"), true, + rs.getDouble("latitude"), rs.getDouble("longitude"), + rs.getTimestamp("inserted").getTime(), -1); + } } } catch (SQLException e) { AntiVPN.getInstance().getExecutor().logException("There was a problem getting a response for " + ip, e); + } catch (Exception e) { + throw new RuntimeException(e); } return null; }); @@ -105,14 +109,14 @@ public class H2VPN implements VPNDatabase { if(AntiVPN.getInstance().getVpnConfig().cachedResults()) { cachedResponses.put(toCache.getIp(), toCache); - try { - Query.prepare("insert into `responses` (`ip`,`asn`,`countryName`,`countryCode`,`city`,`timeZone`," - + "`method`,`isp`,`proxy`,`cached`,`inserted`,`latitude`,`longitude`) values (?,?,?,?,?,?,?,?,?,?,?,?,?)") - .append(toCache.getIp()).append(toCache.getAsn()).append(toCache.getCountryName()) - .append(toCache.getCountryCode()).append(toCache.getCity()).append(toCache.getTimeZone()) - .append(toCache.getMethod()).append(toCache.getIsp()).append(toCache.isProxy()) - .append(toCache.isCached()).append(new Timestamp(System.currentTimeMillis())) - .append(toCache.getLatitude()).append(toCache.getLongitude()).execute(); + try(var statement = Query.prepare("insert into `responses` (`ip`,`asn`,`countryName`,`countryCode`,`city`,`timeZone`," + + "`method`,`isp`,`proxy`,`cached`,`inserted`,`latitude`,`longitude`) values (?,?,?,?,?,?,?,?,?,?,?,?,?)") + .append(toCache.getIp()).append(toCache.getAsn()).append(toCache.getCountryName()) + .append(toCache.getCountryCode()).append(toCache.getCity()).append(toCache.getTimeZone()) + .append(toCache.getMethod()).append(toCache.getIsp()).append(toCache.isProxy()) + .append(toCache.isCached()).append(new Timestamp(System.currentTimeMillis())) + .append(toCache.getLatitude()).append(toCache.getLongitude())) { + statement.execute(); } catch(SQLException e) { AntiVPN.getInstance().getExecutor().logException("Could not cache response for IP: " + toCache.getIp(), e); } @@ -124,8 +128,8 @@ public class H2VPN implements VPNDatabase { if(!AntiVPN.getInstance().getVpnConfig().isDatabaseEnabled() || MySQL.isClosed()) return; - try { - Query.prepare("delete from `responses` where `ip` = ?").append(ip).execute(); + try(var statement = Query.prepare("delete from `responses` where `ip` = ?").append(ip)) { + statement.execute(); } catch (SQLException e) { AntiVPN.getInstance().getExecutor().logException("Could not delete response from IP: " + ip, e); } @@ -135,9 +139,11 @@ public class H2VPN implements VPNDatabase { public boolean isWhitelisted(UUID uuid) { if (!AntiVPN.getInstance().getVpnConfig().isDatabaseEnabled() || MySQL.isClosed()) return false; - try(ResultSet set = Query.prepare("select uuid from `whitelisted` where `uuid` = ? limit 1") - .append(uuid.toString()).executeQuery()) { - return set != null && set.next() && set.getString("uuid") != null; + try(var statement = Query.prepare("select uuid from `whitelisted` where `uuid` = ? limit 1") + .append(uuid.toString())) { + try(var set = statement.executeQuery()) { + return set != null && set.next() && set.getString("uuid") != null; + } } catch (SQLException e) { AntiVPN.getInstance().getExecutor().logException("Could not check whitelist for uuid '" + uuid + "' due to SQL error.", e); return false; @@ -147,7 +153,7 @@ public class H2VPN implements VPNDatabase { @SneakyThrows @Override public boolean isWhitelisted(String ip) { - return isWhitelisted(new CIDRUtils(ip + "/32")); + return isWhitelisted(new CIDRUtils(ip)); } @Override @@ -158,10 +164,12 @@ public class H2VPN implements VPNDatabase { BigInteger start = cidr.getStartIpInt(); BigInteger end = cidr.getEndIpInt(); - try(var result = Query.prepare("SELECT * FROM `whitelisted-ranges` WHERE ip_start <= ? AND ip_end >= ?") - .append(start).append(end).executeQuery()) { + try(var statement = Query.prepare("SELECT * FROM `whitelisted-ranges` WHERE ip_start <= ? AND ip_end >= ?") + .append(start).append(end)) { - return result.next(); + try(var result = statement.executeQuery()) { + return result.next(); + } } catch (SQLException e) { AntiVPN.getInstance().getExecutor().logException("Could not check whitelist for cidr '" + cidr + "' due to SQL error.", e); } @@ -173,8 +181,8 @@ public class H2VPN implements VPNDatabase { if (!AntiVPN.getInstance().getVpnConfig().isDatabaseEnabled() || MySQL.isClosed()) return; - try { - Query.prepare("insert into `whitelisted` (`uuid`) values (?)").append(uuid.toString()).execute(); + try(var statement = Query.prepare("insert into `whitelisted` (`uuid`) values (?)").append(uuid.toString())) { + statement.execute(); AntiVPN.getInstance().getExecutor().getWhitelisted().add(uuid); } catch (SQLException e) { AntiVPN.getInstance().getExecutor().logException("Could not add uuid '" + uuid + "' to whitelist due to SQL error.", e); @@ -185,8 +193,8 @@ public class H2VPN implements VPNDatabase { public void removeWhitelist(UUID uuid) { if (!AntiVPN.getInstance().getVpnConfig().isDatabaseEnabled() || MySQL.isClosed()) return; - try { - Query.prepare("delete from `whitelisted` where `uuid` = ?").append(uuid.toString()).execute(); + try(var statement = Query.prepare("delete from `whitelisted` where `uuid` = ?").append(uuid.toString())) { + statement.execute(); AntiVPN.getInstance().getExecutor().getWhitelisted().remove(uuid); } catch (SQLException e) { AntiVPN.getInstance().getExecutor().logException("Could not remove uuid '" + uuid + "' from whitelist due to SQL error.", e); @@ -198,9 +206,9 @@ public class H2VPN implements VPNDatabase { if (!AntiVPN.getInstance().getVpnConfig().isDatabaseEnabled() || MySQL.isClosed()) return; - try { - Query.prepare("insert into `whitelisted-ranges` (`cidr_string`, `ip_start`, `ip_end`) values (?, ?, ?)") - .append(cidr.toString()).append(cidr.getStartIpInt()).append(cidr.getEndIpInt()).execute(); + try(var statement = Query.prepare("insert into `whitelisted-ranges` (`cidr_string`, `ip_start`, `ip_end`) values (?, ?, ?)") + .append(cidr.toString()).append(cidr.getStartIpInt()).append(cidr.getEndIpInt())) { + statement.execute(); } catch (SQLException e) { AntiVPN.getInstance().getExecutor().logException("Could not add cidr '" + cidr + "' to whitelist due to SQL error.", e); @@ -212,8 +220,8 @@ public class H2VPN implements VPNDatabase { if (!AntiVPN.getInstance().getVpnConfig().isDatabaseEnabled() || MySQL.isClosed()) return; - try { - Query.prepare("delete from `whitelisted-ranges` where `cidr_string` = ?").append(cidr.toString()).execute(); + try(var statement = Query.prepare("delete from `whitelisted-ranges` where `cidr_string` = ?").append(cidr.toString())) { + statement.execute(); } catch (SQLException e) { AntiVPN.getInstance().getExecutor().logException("Could not remove cidr '" + cidr + "' from whitelist due to SQL error.", e); @@ -227,9 +235,8 @@ public class H2VPN implements VPNDatabase { if (!AntiVPN.getInstance().getVpnConfig().isDatabaseEnabled() || MySQL.isClosed()) return uuids; - try { - if(!MySQL.isClosed()) Query.prepare("select uuid from `whitelisted`") - .execute(set -> uuids.add(UUID.fromString(set.getString("uuid")))); + try(var statement = Query.prepare("select uuid from `whitelisted`")) { + statement.execute(set -> uuids.add(UUID.fromString(set.getString("uuid")))); } catch (SQLException e) { AntiVPN.getInstance().getExecutor().logException("Could not get all whitelisted players due to SQL error.", e); } @@ -243,13 +250,14 @@ public class H2VPN implements VPNDatabase { if (!AntiVPN.getInstance().getVpnConfig().isDatabaseEnabled() || MySQL.isClosed()) return ips; - try { - if(!MySQL.isClosed()) Query.prepare("select `cidr_string`, `ip_start`, `ip_end` from `whitelisted-ranges`") - .execute(set -> { + try(var statement = Query.prepare("select `cidr_string`, `ip_start`, `ip_end` from `whitelisted-ranges`")) { + statement.execute(set -> { try { ips.add(new CIDRUtils(set.getString("cidr_string"))); } catch (UnknownHostException e) { - AntiVPN.getInstance().getExecutor().logException("Could not format ip " + set.getString("cidr_string") + " into a CIDR!", e); + AntiVPN.getInstance().getExecutor() + .logException("Could not format ip " + + set.getString("cidr_string") + " into a CIDR!", e); } }); } catch (SQLException e) { @@ -262,12 +270,13 @@ public class H2VPN implements VPNDatabase { @Override public void alertsState(UUID uuid, Consumer result) { if(MySQL.isClosed()) return; - AntiVPN.getInstance().getExecutor().getThreadExecutor().execute(() -> { - try(ResultSet set = Query.prepare("select * from `alerts` where `uuid` = ? limit 1") - .append(uuid.toString()).executeQuery()) { - result.accept(set != null && set.next() && set.getString("uuid") != null); + try(var statement = Query.prepare("select * from `alerts` where `uuid` = ? limit 1") + .append(uuid.toString())) { + try(var set = statement.executeQuery()) { + result.accept(set != null && set.next() && set.getString("uuid") != null); + } } catch (SQLException e) { AntiVPN.getInstance().getExecutor().logException("There was a problem getting alerts state for " + uuid, e); result.accept(false); @@ -283,22 +292,22 @@ public class H2VPN implements VPNDatabase { //We want to make sure there isn't already a uuid inserted to prevent double insertions alertsState(uuid, alreadyEnabled -> { //No need to make another thread execute, already async if(!alreadyEnabled) { - try { - Query.prepare("insert into `alerts` (`uuid`) values (?)").append(uuid.toString()) - .execute(); + try(var statement = Query.prepare("insert into `alerts` (`uuid`) values (?)") + .append(uuid.toString())) { + statement.execute(); } catch (SQLException e) { - AntiVPN.getInstance().getExecutor().logException("There was a problem updating alerts state for " + uuid, e); + AntiVPN.getInstance().getExecutor() + .logException("There was a problem updating alerts state for " + uuid, e); } } //No need to insert again of already enabled }); //Removing any uuid from the alerts table will disable alerts globally. } else { - try { - Query.prepare("delete from `alerts` where `uuid` = ?") - .append(uuid.toString()) - .execute(); + try(var statement = Query.prepare("delete from `alerts` where `uuid` = ?").append(uuid.toString())) { + statement.execute(); } catch (SQLException e) { - AntiVPN.getInstance().getExecutor().logException("There was a problem updating alerts state for " + uuid, e); + AntiVPN.getInstance().getExecutor().logException("There was a problem updating alerts state for " + + uuid, e); } } } @@ -307,8 +316,8 @@ public class H2VPN implements VPNDatabase { public void clearResponses() { if(MySQL.isClosed()) return; - try { - Query.prepare("delete from `responses`").execute(); + try(var statement = Query.prepare("delete from `responses`")) { + statement.execute(); } catch (SQLException e) { AntiVPN.getInstance().getExecutor().logException("There was a problem clearing responses.", e); } diff --git a/Common/Source/src/main/java/dev/brighten/antivpn/database/local/version/First.java b/Common/Source/src/main/java/dev/brighten/antivpn/database/local/version/First.java index 3b88541..4724ca2 100644 --- a/Common/Source/src/main/java/dev/brighten/antivpn/database/local/version/First.java +++ b/Common/Source/src/main/java/dev/brighten/antivpn/database/local/version/First.java @@ -19,41 +19,56 @@ package dev.brighten.antivpn.database.local.version; import dev.brighten.antivpn.AntiVPN; import dev.brighten.antivpn.database.DatabaseException; import dev.brighten.antivpn.database.VPNDatabase; -import dev.brighten.antivpn.database.local.H2VPN; +import dev.brighten.antivpn.database.sql.utils.ExecutableStatement; import dev.brighten.antivpn.database.sql.utils.Query; import dev.brighten.antivpn.database.version.Version; +import dev.brighten.antivpn.utils.MiscUtils; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; public class First implements Version { + + private final List toClose = new ArrayList<>(); @Override public void update(VPNDatabase database) throws DatabaseException { - if(database instanceof H2VPN h2VPN) { - h2VPN.backupDatabase(); - } try { - Query.prepare("create table if not exists `whitelisted` (`uuid` varchar(36) not null)").execute(); - Query.prepare("create table if not exists `whitelisted-ips` (`ip` varchar(45) not null)").execute(); - Query.prepare("create table if not exists `responses` (`ip` varchar(45) not null, `asn` varchar(12)," + closeOnEnd(Query.prepare("create table if not exists `whitelisted` (`uuid` varchar(36) not null)")) + .execute(); + closeOnEnd(Query.prepare("create table if not exists `whitelisted-ips` (`ip` varchar(45) not null)")) + .execute(); + closeOnEnd(Query + .prepare("create table if not exists `responses` (`ip` varchar(45) not null, `asn` varchar(12)," + "`countryName` text, `countryCode` varchar(10), `city` text, `timeZone` varchar(64), " + "`method` varchar(32), `isp` text, `proxy` boolean, `cached` boolean, `inserted` timestamp," - + "`latitude` double, `longitude` double)").execute(); - Query.prepare("create table if not exists `alerts` (`uuid` varchar(36) not null)").execute(); - Query.prepare("create table if not exists `database_version` (`version` int)").execute(); - Query.prepare("insert into `database_version` (`version`) values (?)").append(versionNumber()).execute(); + + "`latitude` double, `longitude` double)")).execute(); + closeOnEnd(Query.prepare("create table if not exists `alerts` (`uuid` varchar(36) not null)")) + .execute(); + closeOnEnd(Query.prepare("create table if not exists `database_version` (`version` int)")).execute(); + closeOnEnd(Query.prepare("insert into `database_version` (`version`) values (?)") + .append(versionNumber())).execute(); AntiVPN.getInstance().getExecutor().log("Creating indexes..."); - Query.prepare("create index if not exists `uuid_1` on `whitelisted` (`uuid`)").execute(); - Query.prepare("create index if not exists `ip_1` on `responses` (`ip`)").execute(); - Query.prepare("create index if not exists `proxy_1` on `responses` (`proxy`)").execute(); - Query.prepare("create index if not exists `inserted_1` on `responses` (`inserted`)").execute(); - Query.prepare("create index if not exists `ip_1` on `whitelisted-ips` (`ip`)").execute(); + closeOnEnd(Query.prepare("create index if not exists `uuid_1` on `whitelisted` (`uuid`)")).execute(); + closeOnEnd(Query.prepare("create index if not exists `ip_1` on `responses` (`ip`)")).execute(); + closeOnEnd(Query.prepare("create index if not exists `proxy_1` on `responses` (`proxy`)")).execute(); + closeOnEnd(Query.prepare("create index if not exists `inserted_1` on `responses` (`inserted`)")).execute(); + closeOnEnd(Query.prepare("create index if not exists `ip_1` on `whitelisted-ips` (`ip`)")).execute(); } catch (SQLException e) { throw new DatabaseException("Failed to update database", e); + } finally { + MiscUtils.close(toClose.toArray(AutoCloseable[]::new)); + toClose.clear(); } } + private ExecutableStatement closeOnEnd(ExecutableStatement statement) { + toClose.add(statement); + return statement; + } + @Override public int versionNumber() { return 0; @@ -61,10 +76,13 @@ public class First implements Version { @Override public boolean needsUpdate(VPNDatabase database) { - try(ResultSet set = Query.prepare("select * from `database_version` where version = 0").executeQuery()) { - return set.getFetchSize() == 0; + try(var statement = Query.prepare("select * from `database_version` where version = 0")) { + try(ResultSet set = statement.executeQuery()) { + return set.getFetchSize() == 0; + } } catch (SQLException e) { return true; } + } } diff --git a/Common/Source/src/main/java/dev/brighten/antivpn/database/local/version/Second.java b/Common/Source/src/main/java/dev/brighten/antivpn/database/local/version/Second.java index 2e2ddc4..cf151e8 100644 --- a/Common/Source/src/main/java/dev/brighten/antivpn/database/local/version/Second.java +++ b/Common/Source/src/main/java/dev/brighten/antivpn/database/local/version/Second.java @@ -20,17 +20,20 @@ import dev.brighten.antivpn.AntiVPN; import dev.brighten.antivpn.database.DatabaseException; import dev.brighten.antivpn.database.VPNDatabase; import dev.brighten.antivpn.database.local.H2VPN; +import dev.brighten.antivpn.database.sql.utils.ExecutableStatement; import dev.brighten.antivpn.database.sql.utils.Query; import dev.brighten.antivpn.database.version.Version; import dev.brighten.antivpn.utils.CIDRUtils; +import dev.brighten.antivpn.utils.MiscUtils; import java.net.UnknownHostException; -import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; public class Second implements Version { + private final List toClose = new ArrayList<>(); + @Override public void update(VPNDatabase database) throws DatabaseException { if(database instanceof H2VPN h2VPN) { @@ -38,22 +41,24 @@ public class Second implements Version { } List whitelistedIps = new ArrayList<>(); - try (var set = Query.prepare("SELECT * FROM `whitelisted-ips`").executeQuery()) { - while (set.next()) { - whitelistedIps.add(set.getString("ip")); + try (var statement = Query.prepare("SELECT * FROM `whitelisted-ips`")) { + try(var set = statement.executeQuery()) { + while (set.next()) { + whitelistedIps.add(set.getString("ip")); + } } } catch (SQLException e) { throw new DatabaseException("Could not get whitelisted ips from database!", e); } try { - Query.prepare("CREATE TABLE IF NOT EXISTS `whitelisted-ranges` " + - "(id INT AUTO_INCREMENT PRIMARY KEY, " + - "cidr_string VARCHAR(45), " + - "ip_start BIGINT NOT NULL, " + - "ip_end BIGINT NOT NULL") + closeOnEnd(Query.prepare("CREATE TABLE IF NOT EXISTS `whitelisted-ranges` " + + "(id INT AUTO_INCREMENT PRIMARY KEY, " + + "cidr_string VARCHAR(45), " + + "ip_start BIGINT NOT NULL, " + + "ip_end BIGINT NOT NULL)")) .execute(); - Query.prepare("CREATE INDEX idx_ip_range ON `whitelisted-ranges` (ip_start, ip_end)").execute(); + closeOnEnd(Query.prepare("CREATE INDEX idx_ip_range ON `whitelisted-ranges` (ip_start, ip_end)")).execute(); var cidrs = whitelistedIps.stream().map(ip -> { try { @@ -79,9 +84,9 @@ public class Second implements Version { } } - Query.prepare("DROP INDEX ip_1 on `whitelisted-ips`").execute(); - Query.prepare("DROP TABLE `whitelisted-ips`").execute(); - Query.prepare("INSERT INTO `database_version` (`version`) VALUES (?)").append(versionNumber()).execute(); + closeOnEnd(Query.prepare("DROP INDEX ip_1 on `whitelisted-ips`")).execute(); + closeOnEnd(Query.prepare("DROP TABLE `whitelisted-ips`")).execute(); + closeOnEnd(Query.prepare("INSERT INTO `database_version` (`version`) VALUES (?)").append(versionNumber())).execute(); } catch (Throwable e) { AntiVPN.getInstance().getExecutor().log("Failed to update database to version 1: " + e.getMessage()); try { @@ -90,29 +95,51 @@ public class Second implements Version { throw new DatabaseException("Failed to rollback database!", e); } throw new DatabaseException("Failed to update to version one, rolling back database!", e); + } finally { + MiscUtils.close(toClose.toArray(AutoCloseable[]::new)); + toClose.clear(); } } + private ExecutableStatement closeOnEnd(ExecutableStatement statement) { + toClose.add(statement); + return statement; + } + private void rollback(List ipAddresses) throws SQLException { AntiVPN.getInstance().getExecutor().log("Rolling back to version 0..."); - Query.prepare("DROP INDEX idx_ip_range ON `whitelisted-ranges`").execute(); - Query.prepare("DROP TABLE `whitelisted-ranges`").execute(); - Query.prepare("DELETE FROM `database_version` WHERE version = ?").append(versionNumber()).execute(); - - Query.prepare("CREATE TABLE IF NOT EXISTS `whitelisted-ips` (`ip` VARCHAR(45) NOT NULL)") - .execute(); - Query.prepare("create index if not exists `ip_1` on `whitelisted-ips` (`ip`)").execute(); - - Query.prepare("DELETE FROM `whitelisted-ips`").execute(); - - var statement = Query.prepare("INSERT INTO `whitelisted-ips` (`ip`) VALUES (?)"); - for (String ip : ipAddresses) { - statement.append(ip); - statement.addBatch(); + try(var statement = Query.prepare("DROP INDEX idx_ip_range ON `whitelisted-ranges`")) { + statement.execute(); + } + try(var statement = Query.prepare("DROP TABLE `whitelisted-ranges`")) { + statement.execute(); } - statement.executeBatch(); + try(var statement = Query.prepare("DELETE FROM `database_version` WHERE version = ?").append(versionNumber())) { + statement.execute(); + } + + try(var statement = Query.prepare("CREATE TABLE IF NOT EXISTS `whitelisted-ips` (`ip` VARCHAR(45) NOT NULL)")) { + statement.execute(); + } + + try(var statement = Query.prepare("create index if not exists `ip_1` on `whitelisted-ips` (`ip`)")) { + statement.execute(); + } + + try(var statement = Query.prepare("DELETE FROM `whitelisted-ips`")) { + statement.execute(); + } + + try(var statement = Query.prepare("INSERT INTO `whitelisted-ips` (`ip`) VALUES (?)")) { + for (String ip : ipAddresses) { + statement.append(ip); + statement.addBatch(); + } + + statement.executeBatch(); + } } @Override @@ -122,8 +149,10 @@ public class Second implements Version { @Override public boolean needsUpdate(VPNDatabase database) { - try (ResultSet set = Query.prepare("select * from `database_version` where version = 1").executeQuery()) { - return set.getFetchSize() == 0; + try (var statement = Query.prepare("select * from `database_version` where version = 1")) { + try(var set = statement.executeQuery()) { + return set.getFetchSize() == 0; + } } catch (SQLException e) { return true; } diff --git a/Common/Source/src/main/java/dev/brighten/antivpn/database/mongo/MongoVPN.java b/Common/Source/src/main/java/dev/brighten/antivpn/database/mongo/MongoVPN.java index 8506192..625c3aa 100644 --- a/Common/Source/src/main/java/dev/brighten/antivpn/database/mongo/MongoVPN.java +++ b/Common/Source/src/main/java/dev/brighten/antivpn/database/mongo/MongoVPN.java @@ -146,7 +146,7 @@ public class MongoVPN implements VPNDatabase { @Override public boolean isWhitelisted(String ip) { try { - return isWhitelisted(new CIDRUtils(ip + "/32")); + return isWhitelisted(new CIDRUtils(ip)); } catch (UnknownHostException e) { AntiVPN.getInstance().getExecutor().log("Failed to check whitelist for IP: " + ip, e); return false; diff --git a/Common/Source/src/main/java/dev/brighten/antivpn/database/sql/utils/ExecutableStatement.java b/Common/Source/src/main/java/dev/brighten/antivpn/database/sql/utils/ExecutableStatement.java index f69de4b..5b0a7e6 100644 --- a/Common/Source/src/main/java/dev/brighten/antivpn/database/sql/utils/ExecutableStatement.java +++ b/Common/Source/src/main/java/dev/brighten/antivpn/database/sql/utils/ExecutableStatement.java @@ -16,13 +16,14 @@ package dev.brighten.antivpn.database.sql.utils; -import dev.brighten.antivpn.utils.MiscUtils; +import lombok.Getter; import lombok.SneakyThrows; import java.sql.*; import java.util.UUID; -public class ExecutableStatement { +public class ExecutableStatement implements AutoCloseable { + @Getter private final PreparedStatement statement; private int pos = 1; @@ -31,35 +32,21 @@ public class ExecutableStatement { } public int execute() throws SQLException { - try { - return statement.executeUpdate(); - } finally { - MiscUtils.close(statement); - } + return statement.executeUpdate(); } public void execute(ResultSetIterator iterator) throws SQLException { try(var rs = statement.executeQuery()) { while (rs.next()) iterator.next(rs); - } finally { - MiscUtils.close(statement); } } public int[] executeBatch() throws SQLException { - try { - return statement.executeBatch(); - } finally { - MiscUtils.close(statement); - } + return statement.executeBatch(); } public ResultSet executeQuery() throws SQLException { - try { - return statement.executeQuery(); - } finally { - MiscUtils.close(statement); - } + return statement.executeQuery(); } @SneakyThrows @@ -152,4 +139,9 @@ public class ExecutableStatement { statement.addBatch(); return this; } + + @Override + public void close() throws SQLException { + statement.close(); + } } diff --git a/Common/Source/src/main/java/dev/brighten/antivpn/database/sql/version/MySQLFirst.java b/Common/Source/src/main/java/dev/brighten/antivpn/database/sql/version/MySQLFirst.java index 0a5a3c8..9f40d48 100644 --- a/Common/Source/src/main/java/dev/brighten/antivpn/database/sql/version/MySQLFirst.java +++ b/Common/Source/src/main/java/dev/brighten/antivpn/database/sql/version/MySQLFirst.java @@ -28,15 +28,19 @@ public class MySQLFirst extends First { @Override public void update(VPNDatabase database) throws DatabaseException { - try { - Query.prepare("select `DATA_TYPE` from INFORMATION_SCHEMA.COLUMNS " + - "WHERE table_name = 'responses' AND COLUMN_NAME = 'isp';").execute(set -> { + try(var statement = Query.prepare("select `DATA_TYPE` from INFORMATION_SCHEMA.COLUMNS " + + "WHERE table_name = 'responses' AND COLUMN_NAME = 'isp';")) { + statement.execute(set -> { if(set.getObject("DATA_TYPE").toString().contains("varchar")) { AntiVPN.getInstance().getExecutor().log("Using old database format for storing responses! " + "Dropping table and creating a new one..."); - if(Query.prepare("drop table `responses`").execute() > 0) { - AntiVPN.getInstance().getExecutor().log("Successfully dropped table!"); + try(var state = Query.prepare("drop table `responses`")) { + if(state.execute() > 0) { + AntiVPN.getInstance().getExecutor().log("Successfully dropped table!"); + } } + + } }); } catch (SQLException e) { diff --git a/Common/loader-utils/pom.xml b/Common/loader-utils/pom.xml index 2a6d1a7..2a97bec 100644 --- a/Common/loader-utils/pom.xml +++ b/Common/loader-utils/pom.xml @@ -22,7 +22,7 @@ dev.brighten.antivpn Common - 1.9.4 + 1.10.0 loader-utils diff --git a/Common/pom.xml b/Common/pom.xml index 05a1d9f..e2c9efc 100644 --- a/Common/pom.xml +++ b/Common/pom.xml @@ -21,7 +21,7 @@ AntiVPN dev.brighten.antivpn - 1.9.4 + 1.10.0 pom diff --git a/Sponge/SpongeLoader/pom.xml b/Sponge/SpongeLoader/pom.xml index 762575f..c4c6604 100644 --- a/Sponge/SpongeLoader/pom.xml +++ b/Sponge/SpongeLoader/pom.xml @@ -22,7 +22,7 @@ dev.brighten.antivpn Sponge - 1.9.4 + 1.10.0 ../pom.xml @@ -104,7 +104,7 @@ dev.brighten.antivpn loader-utils - 1.9.4 + 1.10.0 compile diff --git a/Sponge/SpongePlugin/pom.xml b/Sponge/SpongePlugin/pom.xml index d19f754..21fecf6 100644 --- a/Sponge/SpongePlugin/pom.xml +++ b/Sponge/SpongePlugin/pom.xml @@ -22,7 +22,7 @@ dev.brighten.antivpn Sponge - 1.9.4 + 1.10.0 ../pom.xml @@ -49,13 +49,13 @@ dev.brighten.antivpn loader-utils - 1.9.4 + 1.10.0 provided dev.brighten.antivpn Source - 1.9.4 + 1.10.0 provided diff --git a/Sponge/pom.xml b/Sponge/pom.xml index 4e6ed3d..2b0d1f2 100644 --- a/Sponge/pom.xml +++ b/Sponge/pom.xml @@ -22,7 +22,7 @@ dev.brighten.antivpn AntiVPN - 1.9.4 + 1.10.0 pom diff --git a/Universal/pom.xml b/Universal/pom.xml index c243aad..e092308 100644 --- a/Universal/pom.xml +++ b/Universal/pom.xml @@ -22,7 +22,7 @@ dev.brighten.antivpn AntiVPN - 1.9.4 + 1.10.0 Universal diff --git a/Velocity/VelocityLoader/pom.xml b/Velocity/VelocityLoader/pom.xml index 8083e35..1d9598b 100644 --- a/Velocity/VelocityLoader/pom.xml +++ b/Velocity/VelocityLoader/pom.xml @@ -22,7 +22,7 @@ dev.brighten.antivpn Velocity - 1.9.4 + 1.10.0 ../pom.xml @@ -57,13 +57,13 @@ dev.brighten.antivpn loader-utils - 1.9.4 + 1.10.0 compile dev.brighten.antivpn VelocityPlugin - 1.9.4 + 1.10.0 provided diff --git a/Velocity/VelocityPlugin/pom.xml b/Velocity/VelocityPlugin/pom.xml index 5b97faf..45317d0 100644 --- a/Velocity/VelocityPlugin/pom.xml +++ b/Velocity/VelocityPlugin/pom.xml @@ -22,7 +22,7 @@ dev.brighten.antivpn AntiVPN - 1.9.4 + 1.10.0 ../../pom.xml @@ -51,7 +51,7 @@ dev.brighten.antivpn Source - 1.9.4 + 1.10.0 provided @@ -63,7 +63,7 @@ dev.brighten.antivpn loader-utils - 1.9.4 + 1.10.0 provided diff --git a/Velocity/pom.xml b/Velocity/pom.xml index 3633749..d126240 100644 --- a/Velocity/pom.xml +++ b/Velocity/pom.xml @@ -21,7 +21,7 @@ AntiVPN dev.brighten.antivpn - 1.9.4 + 1.10.0 pom 4.0.0 diff --git a/pom.xml b/pom.xml index f597903..8916900 100644 --- a/pom.xml +++ b/pom.xml @@ -23,7 +23,7 @@ dev.brighten.antivpn AntiVPN pom - 1.9.4 + 1.10.0 Common