diff --git a/src/main/java/uranium/Main.java b/src/main/java/uranium/Main.java index a183ea8..b482a0c 100644 --- a/src/main/java/uranium/Main.java +++ b/src/main/java/uranium/Main.java @@ -7,6 +7,7 @@ import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.messaging.PluginMessageListener; import uranium.listeners.*; +import uranium.tasks.IPCheck; import uranium.user.*; public class Main extends JavaPlugin implements PluginMessageListener { @@ -34,11 +35,12 @@ public class Main extends JavaPlugin implements PluginMessageListener { } private void setupCommands() { - + } private void setupTasks() { - + getLogger().info("Initializing tasks..."); + getServer().getScheduler().scheduleSyncRepeatingTask(this, new IPCheck(this), 30, 30); // 1.5 seconds } private void setupPluginChannels() { diff --git a/src/main/java/uranium/tasks/IPCheck.java b/src/main/java/uranium/tasks/IPCheck.java index bb7c9b3..b5be74f 100644 --- a/src/main/java/uranium/tasks/IPCheck.java +++ b/src/main/java/uranium/tasks/IPCheck.java @@ -4,6 +4,7 @@ import org.bukkit.entity.Player; import uranium.Main; import uranium.user.UserManager; import uranium.util.Util; +import uranium.util.WebhookUtil; import java.util.List; @@ -37,6 +38,14 @@ public class IPCheck implements Runnable { Util.sendStaff(prefix + "&c" + user.getName() + "&7 joined using the debug runtime."); else Util.sendStaff(prefix + "&c" + user.getName() + "&7 joined using a strange domain: &c" + user.getDomain()); + + if (plugin.getConfig().getBoolean("suspicious-domains.enable-webhook")) + WebhookUtil.sendSusDomain(user.getName(), + user.getDomain().equalsIgnoreCase("null") + ? "Offline Download" + : user.getDomain().equalsIgnoreCase("") + ? "Debug Runtime" + : user.getDomain()); }); } }); diff --git a/src/main/java/uranium/util/WebhookUtil.java b/src/main/java/uranium/util/WebhookUtil.java new file mode 100644 index 0000000..523f414 --- /dev/null +++ b/src/main/java/uranium/util/WebhookUtil.java @@ -0,0 +1,60 @@ +package uranium.util; + +import club.minnced.discord.webhook.WebhookClient; +import club.minnced.discord.webhook.WebhookClientBuilder; +import club.minnced.discord.webhook.send.WebhookEmbed; +import club.minnced.discord.webhook.send.WebhookEmbedBuilder; +import org.bukkit.plugin.java.JavaPlugin; +import uranium.Main; + +public class WebhookUtil { + + private static final Main plugin; + + static { + plugin = JavaPlugin.getPlugin(Main.class); + } + + private static String filterForDiscord(String message) { + return message + .replace("_", "\\_") + .replace("*", "\\*") + .replace("~", "\\~") + .replace("`", "\\`") + .replace("|", "\\|") + .replace("> ", "\\> ") + .replace("@everyone", "") + .replace("@here", ""); + } + + public static void sendSusDomain(String username, String domain) { + String webhookURI = plugin.getConfig().getString("suspicious-domains.webhook-url"); + if (webhookURI.equalsIgnoreCase("")) { + plugin.getLogger().warning("The webhook URL for suspicious domains has been incorrectly configured."); + return; + } + WebhookClientBuilder builder = new WebhookClientBuilder(webhookURI); + builder.setThreadFactory((job) -> { + Thread thread = new Thread(job); + thread.setName("Webhook"); + thread.setDaemon(true); + return thread; + }); + builder.setWait(true); + try (WebhookClient client = builder.build()) { + WebhookEmbed embed = new WebhookEmbedBuilder() + .setColor(0xFF0000) + .setDescription("A user has logged onto the server using a suspicious domain.") + .addField(new WebhookEmbed.EmbedField(true, "Username", filterForDiscord(username))) + .addField(new WebhookEmbed.EmbedField(true, "Domain", "http://" + filterForDiscord(domain))) + .build(); + + client.send(embed); + } catch(Exception ex) { + plugin.getLogger().severe("An error occurred while attempting to deliver the webhook. Please report the following stacktrace to Cold."); + plugin.getLogger().severe("Webhook URL: " + webhookURI); + ex.printStackTrace(); + } + } + +} \ No newline at end of file