fix webhook validation and change readme

This commit is contained in:
mangorifo
2023-03-29 16:36:55 +08:00
parent 4a5fb14339
commit ef7609dccf
5 changed files with 31 additions and 8 deletions
+6 -1
View File
@@ -6,6 +6,10 @@ this was just made to remember whatever goofy things we did in a minecraft serve
or, you know, notify us when the server starts (but not ends)
# Prerequisites
Before using this plugin, you must:
* Get Spigot
* Get a JAR file from [the releases](https://git.sob.moe/sobrooms/notify-on-start/releases) or [Google Drive](https://drive.google.com/drive/folders/11wMPjOh2b8oRzpeJOrOI4ZAkImOlr93u?usp=sharing) (the drive folder is frequently updated)
# making webhooks
If you already know how, you can skip this step.
@@ -22,7 +26,8 @@ To make a webhook:
# using this thing
You must get Spigot.\
Of course, you must get a release first, as said in the prerequisites. After you get a jar, put it in the `plugins` folder
On first run, it will make a directory where Spigot is.\
These files will be `NoS/notifyWebhook.json` and `NoS/pubMessageWebhook.json`.
+2 -1
View File
@@ -1 +1,2 @@
call ./gradlew jar
@call ./gradlew jar
@pause
+17 -2
View File
@@ -32,7 +32,6 @@ public class Main extends JavaPlugin {
public static void main(String... args) {
System.out.printf("Initializing using server address: %s...%n", ServerUtils.address);
}
@Override
public void onEnable() {
String loggingDir = "./NoS";
@@ -73,7 +72,8 @@ public class Main extends JavaPlugin {
} catch (NullPointerException e) {
throw new RuntimeException(e);
}
webhooksAreValidUrls = validateWebhook_Ntfy();
webhooksAreValidUrls = validateWebhook_Pub();
// register events
if (getLoggingConfig().equals("true") && webhooksAreValidUrls)
registerEvents();
@@ -87,6 +87,19 @@ public class Main extends JavaPlugin {
}
}
public static boolean validateWebhook_Pub() {
if (getPublicMessageWebhook().startsWith("https://") || getPublicMessageWebhook().startsWith("http://"))
return true;
else
return false;
}
public static boolean validateWebhook_Ntfy() {
if (getNotifyWebhook().startsWith("https://") || getNotifyWebhook().startsWith("http://"))
return true;
else
return false;
}
public static String getPublicMessageWebhook() {
try {
BufferedReader brfmsg = new BufferedReader(new FileReader(pubMessageWebhookConfig));
@@ -101,6 +114,7 @@ public class Main extends JavaPlugin {
webhooksAreValidUrls = false;
return lineMsg;
} else if (lineMsg.startsWith("https://") || lineMsg.startsWith("http://")) {
webhooksAreValidUrls = true;
return lineMsg;
} else {
System.out.printf("Please set the webhook URL in %s and restart the server", pubMessageWebhookConfig.getAbsolutePath());
@@ -125,6 +139,7 @@ public class Main extends JavaPlugin {
webhooksAreValidUrls = false;
return lineNtfy;
} else if (lineNtfy.startsWith("https://") || lineNtfy.startsWith("http://")) {
webhooksAreValidUrls = true;
return lineNtfy;
} else {
System.out.printf("Please set the webhook URL in %s and restart the server", notifyWebhookConfig.getAbsolutePath());
+5 -3
View File
@@ -11,7 +11,7 @@ public class ServerUtils {
public static String address_fallback = Utils.getServerHostPublicIP();
public static String webhook_notify = Main.getNotifyWebhook();
public static String webhook_messages = Main.getPublicMessageWebhook();
public static String address = "http://mc-srv2-singapore.rrryfoo.cf";
public static String address = "mc-srv2-singapore.rrryfoo.cf";
public static boolean serverIsUp(String serverAddress, Integer serverPort) {
/*Socket socket;
@@ -33,9 +33,9 @@ public class ServerUtils {
try {
Server server = Bukkit.getServer();
Socket s = new Socket();
s.connect(new InetSocketAddress(serverAddress.toString(), Integer.parseInt(serverPort.toString())), 15);
s.connect(new InetSocketAddress(serverAddress, Integer.parseInt(serverPort.toString())), 15);
s.close();
System.out.println("Server is online. Sending message to webhook_notify... (Pinged " + Utils.getServerHostPublicIP() + ":" + server.getPort() + ")");
System.out.println("Server is online. Sending message to webhook_notify... (Pinged " + serverAddress + ":" + server.getPort() + ")");
return true;
} catch (Exception e) {
System.out.println("Server is inactive. Pinging fallback address...");
@@ -44,6 +44,8 @@ public class ServerUtils {
Socket s = new Socket();
s.connect(new InetSocketAddress(address_fallback, Integer.parseInt(serverPort.toString())), 15);
s.close();
System.out.println("Server is online. Sending message to webhook_notify... (Pinged " + Utils.getServerHostPublicIP() + ":" + server.getPort() + ")");
return true;
} catch (Exception err) {
System.out.println("Fallback address is inactive. No longer pinging...");
}
@@ -24,7 +24,7 @@ public class ServerLoad implements Listener {
Bukkit.getConsoleSender().sendMessage("Triggered ServerLoad event.");
if (timesCalled < 1) {
Bukkit.getConsoleSender().sendMessage("Checking if server is up on " + ServerUtils.address + " with port " + ServerUtils.port + "...");
if (ServerUtils.serverIsUp(address, 25565)) {
if (ServerUtils.serverIsUp(address, Bukkit.getServer().getPort())) {
System.out.println(ServerUtils.webhook_messages);
System.out.println(ServerUtils.webhook_notify);
DiscordWebhook notify = new DiscordWebhook(Main.getNotifyWebhook());