fix webhook validation and change readme
This commit is contained in:
@@ -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());
|
||||
|
||||
@@ -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());
|
||||
|
||||
Reference in New Issue
Block a user