fix webhook validation and change readme
This commit is contained in:
@@ -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)
|
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
|
# making webhooks
|
||||||
If you already know how, you can skip this step.
|
If you already know how, you can skip this step.
|
||||||
|
|
||||||
@@ -22,7 +26,8 @@ To make a webhook:
|
|||||||
|
|
||||||
|
|
||||||
# using this thing
|
# 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.\
|
On first run, it will make a directory where Spigot is.\
|
||||||
These files will be `NoS/notifyWebhook.json` and `NoS/pubMessageWebhook.json`.
|
These files will be `NoS/notifyWebhook.json` and `NoS/pubMessageWebhook.json`.
|
||||||
|
|
||||||
|
|||||||
+2
-1
@@ -1 +1,2 @@
|
|||||||
call ./gradlew jar
|
@call ./gradlew jar
|
||||||
|
@pause
|
||||||
@@ -32,7 +32,6 @@ public class Main extends JavaPlugin {
|
|||||||
public static void main(String... args) {
|
public static void main(String... args) {
|
||||||
System.out.printf("Initializing using server address: %s...%n", ServerUtils.address);
|
System.out.printf("Initializing using server address: %s...%n", ServerUtils.address);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
String loggingDir = "./NoS";
|
String loggingDir = "./NoS";
|
||||||
@@ -73,7 +72,8 @@ public class Main extends JavaPlugin {
|
|||||||
} catch (NullPointerException e) {
|
} catch (NullPointerException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
webhooksAreValidUrls = validateWebhook_Ntfy();
|
||||||
|
webhooksAreValidUrls = validateWebhook_Pub();
|
||||||
// register events
|
// register events
|
||||||
if (getLoggingConfig().equals("true") && webhooksAreValidUrls)
|
if (getLoggingConfig().equals("true") && webhooksAreValidUrls)
|
||||||
registerEvents();
|
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() {
|
public static String getPublicMessageWebhook() {
|
||||||
try {
|
try {
|
||||||
BufferedReader brfmsg = new BufferedReader(new FileReader(pubMessageWebhookConfig));
|
BufferedReader brfmsg = new BufferedReader(new FileReader(pubMessageWebhookConfig));
|
||||||
@@ -101,6 +114,7 @@ public class Main extends JavaPlugin {
|
|||||||
webhooksAreValidUrls = false;
|
webhooksAreValidUrls = false;
|
||||||
return lineMsg;
|
return lineMsg;
|
||||||
} else if (lineMsg.startsWith("https://") || lineMsg.startsWith("http://")) {
|
} else if (lineMsg.startsWith("https://") || lineMsg.startsWith("http://")) {
|
||||||
|
webhooksAreValidUrls = true;
|
||||||
return lineMsg;
|
return lineMsg;
|
||||||
} else {
|
} else {
|
||||||
System.out.printf("Please set the webhook URL in %s and restart the server", pubMessageWebhookConfig.getAbsolutePath());
|
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;
|
webhooksAreValidUrls = false;
|
||||||
return lineNtfy;
|
return lineNtfy;
|
||||||
} else if (lineNtfy.startsWith("https://") || lineNtfy.startsWith("http://")) {
|
} else if (lineNtfy.startsWith("https://") || lineNtfy.startsWith("http://")) {
|
||||||
|
webhooksAreValidUrls = true;
|
||||||
return lineNtfy;
|
return lineNtfy;
|
||||||
} else {
|
} else {
|
||||||
System.out.printf("Please set the webhook URL in %s and restart the server", notifyWebhookConfig.getAbsolutePath());
|
System.out.printf("Please set the webhook URL in %s and restart the server", notifyWebhookConfig.getAbsolutePath());
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ public class ServerUtils {
|
|||||||
public static String address_fallback = Utils.getServerHostPublicIP();
|
public static String address_fallback = Utils.getServerHostPublicIP();
|
||||||
public static String webhook_notify = Main.getNotifyWebhook();
|
public static String webhook_notify = Main.getNotifyWebhook();
|
||||||
public static String webhook_messages = Main.getPublicMessageWebhook();
|
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) {
|
public static boolean serverIsUp(String serverAddress, Integer serverPort) {
|
||||||
/*Socket socket;
|
/*Socket socket;
|
||||||
@@ -33,9 +33,9 @@ public class ServerUtils {
|
|||||||
try {
|
try {
|
||||||
Server server = Bukkit.getServer();
|
Server server = Bukkit.getServer();
|
||||||
Socket s = new Socket();
|
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();
|
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;
|
return true;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
System.out.println("Server is inactive. Pinging fallback address...");
|
System.out.println("Server is inactive. Pinging fallback address...");
|
||||||
@@ -44,6 +44,8 @@ public class ServerUtils {
|
|||||||
Socket s = new Socket();
|
Socket s = new Socket();
|
||||||
s.connect(new InetSocketAddress(address_fallback, Integer.parseInt(serverPort.toString())), 15);
|
s.connect(new InetSocketAddress(address_fallback, Integer.parseInt(serverPort.toString())), 15);
|
||||||
s.close();
|
s.close();
|
||||||
|
System.out.println("Server is online. Sending message to webhook_notify... (Pinged " + Utils.getServerHostPublicIP() + ":" + server.getPort() + ")");
|
||||||
|
return true;
|
||||||
} catch (Exception err) {
|
} catch (Exception err) {
|
||||||
System.out.println("Fallback address is inactive. No longer pinging...");
|
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.");
|
Bukkit.getConsoleSender().sendMessage("Triggered ServerLoad event.");
|
||||||
if (timesCalled < 1) {
|
if (timesCalled < 1) {
|
||||||
Bukkit.getConsoleSender().sendMessage("Checking if server is up on " + ServerUtils.address + " with port " + ServerUtils.port + "...");
|
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_messages);
|
||||||
System.out.println(ServerUtils.webhook_notify);
|
System.out.println(ServerUtils.webhook_notify);
|
||||||
DiscordWebhook notify = new DiscordWebhook(Main.getNotifyWebhook());
|
DiscordWebhook notify = new DiscordWebhook(Main.getNotifyWebhook());
|
||||||
|
|||||||
Reference in New Issue
Block a user