Allowlist functionality is now working, corrected sql errors

This commit is contained in:
2026-01-14 09:42:20 -05:00
parent ac57a540c2
commit 8a4b86c9ef
24 changed files with 214 additions and 162 deletions
+2 -2
View File
@@ -22,7 +22,7 @@
<parent>
<groupId>dev.brighten.antivpn</groupId>
<artifactId>Bukkit</artifactId>
<version>1.9.4</version>
<version>1.10.0</version>
</parent>
<groupId>dev.brighten.antivpn.bukkit</groupId>
<artifactId>Loader</artifactId>
@@ -99,7 +99,7 @@
<dependency>
<groupId>dev.brighten.antivpn</groupId>
<artifactId>loader-utils</artifactId>
<version>1.9.4</version>
<version>1.10.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
+3 -3
View File
@@ -22,7 +22,7 @@
<parent>
<groupId>dev.brighten.antivpn</groupId>
<artifactId>Bukkit</artifactId>
<version>1.9.4</version>
<version>1.10.0</version>
</parent>
<artifactId>Plugin</artifactId>
@@ -98,7 +98,7 @@
<dependency>
<groupId>dev.brighten.antivpn</groupId>
<artifactId>Source</artifactId>
<version>1.9.4</version>
<version>1.10.0</version>
<scope>provided</scope>
</dependency>
<dependency>
@@ -110,7 +110,7 @@
<dependency>
<groupId>dev.brighten.antivpn</groupId>
<artifactId>loader-utils</artifactId>
<version>1.9.4</version>
<version>1.10.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
+1 -1
View File
@@ -21,7 +21,7 @@
<parent>
<artifactId>AntiVPN</artifactId>
<groupId>dev.brighten.antivpn</groupId>
<version>1.9.4</version>
<version>1.10.0</version>
</parent>
<packaging>pom</packaging>
<modules>
+2 -2
View File
@@ -22,7 +22,7 @@
<parent>
<groupId>dev.brighten.antivpn</groupId>
<artifactId>Bungee</artifactId>
<version>1.9.4</version>
<version>1.10.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
@@ -95,7 +95,7 @@
<dependency>
<groupId>dev.brighten.antivpn</groupId>
<artifactId>loader-utils</artifactId>
<version>1.9.4</version>
<version>1.10.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
+3 -3
View File
@@ -22,7 +22,7 @@
<parent>
<groupId>dev.brighten.antivpn</groupId>
<artifactId>Bungee</artifactId>
<version>1.9.4</version>
<version>1.10.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
@@ -85,7 +85,7 @@
<dependency>
<groupId>dev.brighten.antivpn</groupId>
<artifactId>Source</artifactId>
<version>1.9.4</version>
<version>1.10.0</version>
<scope>provided</scope>
</dependency>
<dependency>
@@ -104,7 +104,7 @@
<dependency>
<groupId>dev.brighten.antivpn</groupId>
<artifactId>loader-utils</artifactId>
<version>1.9.4</version>
<version>1.10.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
+1 -1
View File
@@ -21,7 +21,7 @@
<parent>
<artifactId>AntiVPN</artifactId>
<groupId>dev.brighten.antivpn</groupId>
<version>1.9.4</version>
<version>1.10.0</version>
</parent>
<packaging>pom</packaging>
<modelVersion>4.0.0</modelVersion>
+1 -1
View File
@@ -22,7 +22,7 @@
<parent>
<groupId>dev.brighten.antivpn</groupId>
<artifactId>Common</artifactId>
<version>1.9.4</version>
<version>1.10.0</version>
</parent>
<artifactId>Source</artifactId>
@@ -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);
}
@@ -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<Boolean> 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);
}
@@ -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<VPNDatabase> {
private final List<AutoCloseable> 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<VPNDatabase> {
@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;
}
}
}
@@ -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<VPNDatabase> {
private final List<AutoCloseable> 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<VPNDatabase> {
}
List<String> 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<VPNDatabase> {
}
}
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<VPNDatabase> {
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<String> 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<VPNDatabase> {
@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;
}
@@ -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;
@@ -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();
}
}
@@ -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) {
+1 -1
View File
@@ -22,7 +22,7 @@
<parent>
<groupId>dev.brighten.antivpn</groupId>
<artifactId>Common</artifactId>
<version>1.9.4</version>
<version>1.10.0</version>
</parent>
<artifactId>loader-utils</artifactId>
+1 -1
View File
@@ -21,7 +21,7 @@
<parent>
<artifactId>AntiVPN</artifactId>
<groupId>dev.brighten.antivpn</groupId>
<version>1.9.4</version>
<version>1.10.0</version>
</parent>
<packaging>pom</packaging>
<modules>
+2 -2
View File
@@ -22,7 +22,7 @@
<parent>
<groupId>dev.brighten.antivpn</groupId>
<artifactId>Sponge</artifactId>
<version>1.9.4</version>
<version>1.10.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
@@ -104,7 +104,7 @@
<dependency>
<groupId>dev.brighten.antivpn</groupId>
<artifactId>loader-utils</artifactId>
<version>1.9.4</version>
<version>1.10.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
+3 -3
View File
@@ -22,7 +22,7 @@
<parent>
<groupId>dev.brighten.antivpn</groupId>
<artifactId>Sponge</artifactId>
<version>1.9.4</version>
<version>1.10.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
@@ -49,13 +49,13 @@
<dependency>
<groupId>dev.brighten.antivpn</groupId>
<artifactId>loader-utils</artifactId>
<version>1.9.4</version>
<version>1.10.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>dev.brighten.antivpn</groupId>
<artifactId>Source</artifactId>
<version>1.9.4</version>
<version>1.10.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
+1 -1
View File
@@ -22,7 +22,7 @@
<parent>
<groupId>dev.brighten.antivpn</groupId>
<artifactId>AntiVPN</artifactId>
<version>1.9.4</version>
<version>1.10.0</version>
</parent>
<packaging>pom</packaging>
<modules>
+1 -1
View File
@@ -22,7 +22,7 @@
<parent>
<groupId>dev.brighten.antivpn</groupId>
<artifactId>AntiVPN</artifactId>
<version>1.9.4</version>
<version>1.10.0</version>
</parent>
<artifactId>Universal</artifactId>
+3 -3
View File
@@ -22,7 +22,7 @@
<parent>
<groupId>dev.brighten.antivpn</groupId>
<artifactId>Velocity</artifactId>
<version>1.9.4</version>
<version>1.10.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
@@ -57,13 +57,13 @@
<dependency>
<groupId>dev.brighten.antivpn</groupId>
<artifactId>loader-utils</artifactId>
<version>1.9.4</version>
<version>1.10.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>dev.brighten.antivpn</groupId>
<artifactId>VelocityPlugin</artifactId>
<version>1.9.4</version>
<version>1.10.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
+3 -3
View File
@@ -22,7 +22,7 @@
<parent>
<groupId>dev.brighten.antivpn</groupId>
<artifactId>AntiVPN</artifactId>
<version>1.9.4</version>
<version>1.10.0</version>
<relativePath>../../pom.xml</relativePath>
</parent>
@@ -51,7 +51,7 @@
<dependency>
<groupId>dev.brighten.antivpn</groupId>
<artifactId>Source</artifactId>
<version>1.9.4</version>
<version>1.10.0</version>
<scope>provided</scope>
</dependency>
<dependency>
@@ -63,7 +63,7 @@
<dependency>
<groupId>dev.brighten.antivpn</groupId>
<artifactId>loader-utils</artifactId>
<version>1.9.4</version>
<version>1.10.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
+1 -1
View File
@@ -21,7 +21,7 @@
<parent>
<artifactId>AntiVPN</artifactId>
<groupId>dev.brighten.antivpn</groupId>
<version>1.9.4</version>
<version>1.10.0</version>
</parent>
<packaging>pom</packaging>
<modelVersion>4.0.0</modelVersion>
+1 -1
View File
@@ -23,7 +23,7 @@
<groupId>dev.brighten.antivpn</groupId>
<artifactId>AntiVPN</artifactId>
<packaging>pom</packaging>
<version>1.9.4</version>
<version>1.10.0</version>
<modules>
<module>Common</module>