From a6aac8fce782562f034f92578fbc640e1ace89bd Mon Sep 17 00:00:00 2001 From: funkemunky Date: Mon, 21 Feb 2022 08:19:46 -0500 Subject: [PATCH] Using regex to check ipv4 and fixing allowlist ips --- .../antivpn/command/impl/AllowlistCommand.java | 8 ++++++-- .../java/dev/brighten/antivpn/utils/MiscUtils.java | 12 ++++-------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Common/src/main/java/dev/brighten/antivpn/command/impl/AllowlistCommand.java b/Common/src/main/java/dev/brighten/antivpn/command/impl/AllowlistCommand.java index efe07c1..304b9e2 100644 --- a/Common/src/main/java/dev/brighten/antivpn/command/impl/AllowlistCommand.java +++ b/Common/src/main/java/dev/brighten/antivpn/command/impl/AllowlistCommand.java @@ -65,13 +65,16 @@ public class AllowlistCommand extends Command { if(MiscUtils.isIpv4(args[1])) { if(!databaseEnabled) { switch(args[0].toLowerCase()) { - case "add": { + case "add": + case "insert": { AntiVPN.getInstance().getExecutor().getWhitelistedIps().add(args[1]); + AntiVPN.getInstance().getDatabase().setWhitelisted(args[1], true); return String.format("&aAdded &6%s &ato the exemption allowlist.", args[1]); } case "remove": case "delete": { AntiVPN.getInstance().getExecutor().getWhitelistedIps().remove(args[1]); + AntiVPN.getInstance().getDatabase().setWhitelisted(args[1], false); return String.format("&cRemoved &6%s &cfrom the exemption allowlist.", args[1]); } default: { @@ -80,7 +83,8 @@ public class AllowlistCommand extends Command { } } else { switch(args[0].toLowerCase()) { - case "add": { + case "add": + case "insert": { AntiVPN.getInstance().getDatabase().setWhitelisted(args[1], true); return String.format("&aAdded &6%s &a to the exemption allowlist.", args[1]); } diff --git a/Common/src/main/java/dev/brighten/antivpn/utils/MiscUtils.java b/Common/src/main/java/dev/brighten/antivpn/utils/MiscUtils.java index 6941b88..802d907 100644 --- a/Common/src/main/java/dev/brighten/antivpn/utils/MiscUtils.java +++ b/Common/src/main/java/dev/brighten/antivpn/utils/MiscUtils.java @@ -11,9 +11,12 @@ import java.net.URL; import java.net.URLClassLoader; import java.nio.channels.Channels; import java.nio.channels.ReadableByteChannel; +import java.util.regex.Pattern; public class MiscUtils { + private static final Pattern ipv4 = Pattern.compile("[1-9]{1,3}\\.[1-9]{1,3}\\.[1-9]{1,3}.[1-9]{1,3}"); + public static void close(Closeable... closeables) { try { for (Closeable closeable : closeables) if (closeable != null) closeable.close(); @@ -32,13 +35,6 @@ public class MiscUtils { public static boolean isIpv4(String ip) { - - try { - InetAddress address = InetAddress.getByName(ip); - - return address instanceof Inet4Address; - } catch(Exception e) { - return false; - } + return ipv4.matcher(ip).matches(); } }