From 99e9c784cdb607f63bb2914f1e525bf237dbb10c Mon Sep 17 00:00:00 2001 From: mangorifo Date: Wed, 29 Mar 2023 16:52:22 +0800 Subject: [PATCH] better webhook validation ft. gpt4 --- src/main/java/cf/sobrooms/Main.java | 36 ++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/src/main/java/cf/sobrooms/Main.java b/src/main/java/cf/sobrooms/Main.java index 48a578a..b6b22a3 100644 --- a/src/main/java/cf/sobrooms/Main.java +++ b/src/main/java/cf/sobrooms/Main.java @@ -21,6 +21,8 @@ import org.bukkit.Bukkit; import org.bukkit.plugin.java.JavaPlugin; import java.io.*; +import java.net.HttpURLConnection; +import java.net.URL; import java.util.Objects; public class Main extends JavaPlugin { @@ -89,16 +91,38 @@ public class Main extends JavaPlugin { } public static boolean validateWebhook_Pub() { - if (getPublicMessageWebhook().startsWith("https://") || getPublicMessageWebhook().startsWith("http://")) - return true; - else + try { + URL url = new URL(getPublicMessageWebhook()); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("GET"); + int responseCode = connection.getResponseCode(); + if (responseCode == HttpURLConnection.HTTP_OK) { + return true; + } else { + System.out.println("Invalid webhook URL provided. Please create a new one."); + return false; + } + } catch (Exception e) { + System.out.println("Invalid webhook URL provided. Please create a new one.. (Caught exception)"); return false; + } } public static boolean validateWebhook_Ntfy() { - if (getNotifyWebhook().startsWith("https://") || getNotifyWebhook().startsWith("http://")) - return true; - else + try { + URL url = new URL(getNotifyWebhook()); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("GET"); + int responseCode = connection.getResponseCode(); + if (responseCode == HttpURLConnection.HTTP_OK) { + return true; + } else { + System.out.println("Invalid webhook URL provided. Please create a new one."); + return false; + } + } catch (Exception e) { + System.out.println("Invalid webhook URL provided. Please create a new one.. (Caught exception)"); return false; + } } public static String getPublicMessageWebhook() { try {