From 50fdbb0c285c67302212d06104378a6644418dd0 Mon Sep 17 00:00:00 2001 From: Colbster937 Date: Wed, 14 Jan 2026 16:44:44 -0600 Subject: [PATCH] add http blacklist sharing --- .gitignore | 6 +- build.gradle.kts | 76 +++---- gradle/wrapper/gradle-wrapper.properties | 7 + .../originblacklist/base/OriginBlacklist.java | 185 ++++++++++++------ .../base/command/OriginBlacklistCommand.java | 25 +-- .../base/config/OriginBlacklistConfig.java | 57 +++--- .../base/http/OriginBlacklistHTTPHandler.java | 39 ++++ .../base/http/OriginBlacklistHTTPServer.java | 56 ++++++ .../base/util/UpdateChecker.java | 2 +- .../bukkit/OriginBlacklistBukkit.java | 5 + .../bungee/OriginBlacklistBungee.java | 5 + .../velocity/OriginBlacklistVelocity.java | 6 + 12 files changed, 333 insertions(+), 136 deletions(-) create mode 100644 gradle/wrapper/gradle-wrapper.properties create mode 100644 src/main/java/xyz/webmc/originblacklist/base/http/OriginBlacklistHTTPHandler.java create mode 100644 src/main/java/xyz/webmc/originblacklist/base/http/OriginBlacklistHTTPServer.java diff --git a/.gitignore b/.gitignore index 72eba41..2eb830d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,10 @@ /.gradle/ /.vscode/ -/gradle/ +/gradle/** /build/ /bin/ /run/ /gradlew -/gradlew.bat \ No newline at end of file +/gradlew.bat +!/gradle/wrapper/ +!/gradle/wrapper/*.properties diff --git a/build.gradle.kts b/build.gradle.kts index 7cc6048..404596e 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,4 +1,5 @@ import groovy.lang.Closure +import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar import com.palantir.gradle.gitversion.VersionDetails import org.gradle.api.tasks.compile.JavaCompile import org.gradle.language.jvm.tasks.ProcessResources @@ -37,9 +38,11 @@ val PLUGIN_PROV_J = getBukkitBungeeDeps(PLUGIN_PROV) val PLUGIN_ATHR_J = getBukkitBungeeDeps(PLUGIN_ATHR) val PLUGIN_CTBR_J = getBukkitBungeeDeps(PLUGIN_CTBR) +val EAGXS_VER = "1.0.8" + plugins { id("java") - id("com.gradleup.shadow") version "9.3.0" + id("com.gradleup.shadow") version "9.3.1" id("com.palantir.git-version") version "4.2.0" id("xyz.jpenilla.run-paper") version "3.0.2" id("xyz.jpenilla.run-waterfall") version "3.0.2" @@ -113,7 +116,7 @@ tasks.withType().configureEach { options.release.set(17) } -tasks.withType() { +tasks.withType().configureEach { duplicatesStrategy = DuplicatesStrategy.EXCLUDE outputs.upToDateWhen { false } @@ -126,7 +129,6 @@ tasks.withType() { doLast { val file = destinationDir.resolve("build.properties") file.parentFile.mkdirs() - file.writeText( BUILD_PROPS.entries.joinToString("\n") { (k, v) -> "$k = $v" @@ -137,37 +139,15 @@ tasks.withType() { inputs.files(tasks.named("compileJava").map { it.outputs.files }) } -tasks.withType() { - minecraftVersion("1.12.2") - runDirectory.set(layout.projectDirectory.dir("run/paper")) - downloadPlugins { - github("lax1dude", "eaglerxserver", "v1.0.8", "EaglerXServer.jar") - modrinth("placeholderapi", "2.11.7") +tasks.withType().configureEach { + if (this !is ShadowJar) enabled = false +} + +tasks.withType().configureEach { + doFirst { + delete(layout.buildDirectory.dir("libs")) + mkdir(layout.buildDirectory.dir("libs")) } -} - -tasks.withType() { - waterfallVersion("1.21") - runDirectory.set(layout.projectDirectory.dir("run/waterfall")) - downloadPlugins { - github("lax1dude", "eaglerxserver", "v1.0.8", "EaglerXServer.jar") - } -} - -tasks.withType() { - velocityVersion("3.4.0-SNAPSHOT") - runDirectory.set(layout.projectDirectory.dir("run/velocity")) - downloadPlugins { - github("lax1dude", "eaglerxserver", "v1.0.8", "EaglerXServer.jar") - modrinth("miniplaceholders", "3.1.0") - } -} - -tasks.jar { - archiveFileName.set("$PLUGIN_NAME-$PLUGIN_VERS.jar") -} - -tasks.shadowJar { relocate("org.bstats", "$PLUGIN_DOMN.$PLUGIN_IDEN.shaded.bstats") relocate("de.marhali.json5", "$PLUGIN_DOMN.$PLUGIN_IDEN.shaded.json5") relocate("org.semver4j.semver4j", "$PLUGIN_DOMN.$PLUGIN_IDEN.shaded.semver4j") @@ -175,6 +155,10 @@ tasks.shadowJar { archiveFileName.set("$PLUGIN_NAME-$PLUGIN_VERS.jar") } +tasks.named("build") { + dependsOn(tasks.named("shadowJar")) +} + tasks.register("printVars") { group = "help" doLast { @@ -183,6 +167,32 @@ tasks.register("printVars") { } } +tasks.withType().configureEach { + minecraftVersion("1.12.2") + runDirectory.set(layout.projectDirectory.dir("run/paper")) + downloadPlugins { + github("lax1dude", "eaglerxserver", "v" + EAGXS_VER, "EaglerXServer.jar") + modrinth("placeholderapi", "2.11.7") + } +} + +tasks.withType().configureEach { + waterfallVersion("1.21") + runDirectory.set(layout.projectDirectory.dir("run/waterfall")) + downloadPlugins { + github("lax1dude", "eaglerxserver", "v" + EAGXS_VER, "EaglerXServer.jar") + } +} + +tasks.withType().configureEach { + velocityVersion("3.4.0-SNAPSHOT") + runDirectory.set(layout.projectDirectory.dir("run/velocity")) + downloadPlugins { + github("lax1dude", "eaglerxserver", "v" + EAGXS_VER, "EaglerXServer.jar") + modrinth("miniplaceholders", "3.1.0") + } +} + fun getBukkitBungeeDeps(list: List): String { return list.joinToString( prefix = "[", diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..23449a2 --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,7 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-9.2.1-bin.zip +networkTimeout=10000 +validateDistributionUrl=true +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/src/main/java/xyz/webmc/originblacklist/base/OriginBlacklist.java b/src/main/java/xyz/webmc/originblacklist/base/OriginBlacklist.java index 101793f..e2c0437 100644 --- a/src/main/java/xyz/webmc/originblacklist/base/OriginBlacklist.java +++ b/src/main/java/xyz/webmc/originblacklist/base/OriginBlacklist.java @@ -6,6 +6,7 @@ import xyz.webmc.originblacklist.base.enums.EnumConnectionType; import xyz.webmc.originblacklist.base.enums.EnumLogLevel; import xyz.webmc.originblacklist.base.events.OriginBlacklistLoginEvent; import xyz.webmc.originblacklist.base.events.OriginBlacklistMOTDEvent; +import xyz.webmc.originblacklist.base.http.OriginBlacklistHTTPServer; import xyz.webmc.originblacklist.base.util.BuildInfo; import xyz.webmc.originblacklist.base.util.IOriginBlacklistPlugin; import xyz.webmc.originblacklist.base.util.OPlayer; @@ -26,8 +27,10 @@ import java.util.Base64; import java.util.List; import java.util.concurrent.TimeUnit; +import de.marhali.json5.Json5; import de.marhali.json5.Json5Array; import de.marhali.json5.Json5Element; +import de.marhali.json5.Json5Object; import inet.ipaddr.AddressStringException; import inet.ipaddr.IPAddressString; import net.kyori.adventure.text.Component; @@ -47,11 +50,15 @@ public final class OriginBlacklist { private final IOriginBlacklistPlugin plugin; private final OriginBlacklistConfig config; + private final OriginBlacklistHTTPServer http; + private final Json5 json5; private String updateURL; public OriginBlacklist(final IOriginBlacklistPlugin plugin) { this.plugin = plugin; this.config = new OriginBlacklistConfig(plugin); + this.http = new OriginBlacklistHTTPServer(this); + this.json5 = Json5.builder(builder -> builder.prettyPrinting().indentFactor(0).build()); plugin.scheduleRepeat(() -> { this.checkForUpdates(); }, this.config.getInteger("update_checker.check_timer"), TimeUnit.SECONDS); @@ -60,6 +67,22 @@ public final class OriginBlacklist { public final void init() { this.plugin.log(EnumLogLevel.INFO, "Initialized Plugin"); this.plugin.log(EnumLogLevel.DEBUG, "Commit " + BuildInfo.get("git_cm_hash")); + if (this.isHTTPServerEnabled()) { + this.http.start(); + } + } + + public final void shutdown() { + this.plugin.log(EnumLogLevel.INFO, "Shutting down..."); + this.http.stop(); + } + + public final void handleReload() { + if (this.isHTTPServerEnabled()) { + this.http.start(); + } else { + this.http.stop(); + } } public final void handleLogin(final OriginBlacklistLoginEvent event) { @@ -115,6 +138,10 @@ public final class OriginBlacklist { return this.config.getBoolean("bStats"); } + public final boolean isHTTPServerEnabled() { + return this.config.getBoolean("blacklist_http_share.enabled"); + } + public final OriginBlacklistConfig getConfig() { return this.config; } @@ -155,14 +182,15 @@ public final class OriginBlacklist { final URL url = new URL(this.updateURL); final Path jar = this.plugin.getPluginJarPath(); final Path bak = jar.resolveSibling(jar.getFileName().toString() + ".bak"); - final Path upd = jar.resolveSibling(Paths.get(URLDecoder.decode(url.getPath(), StandardCharsets.UTF_8)).getFileName()); + final Path upd = jar + .resolveSibling(Paths.get(URLDecoder.decode(url.getPath(), StandardCharsets.UTF_8)).getFileName()); try { Files.copy(jar, bak, StandardCopyOption.REPLACE_EXISTING); } catch (final Throwable t) { t.printStackTrace(); } - + try { final HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); @@ -190,7 +218,9 @@ public final class OriginBlacklist { } public final void updatePlugin() { - this.updatePlugin(() -> {}, () -> {}); + this.updatePlugin(() -> { + }, () -> { + }); } public final EnumBlacklistType testBlacklist(final OPlayer player) { @@ -203,11 +233,14 @@ public final class OriginBlacklist { EnumBlacklistType type = EnumBlacklistType.NONE; if (isNonNull(origin)) { - if (whitelist && !type.isBlacklisted()) type = EnumBlacklistType.ORIGIN; + if (whitelist && !type.isBlacklisted()) + type = EnumBlacklistType.ORIGIN; for (final Json5Element element : this.config.getArray("blacklist.origins").getAsJson5Array()) { if (origin.matches(element.getAsString())) { - if (whitelist) type = EnumBlacklistType.NONE; - else if (!type.isBlacklisted()) type = EnumBlacklistType.ORIGIN; + if (whitelist) + type = EnumBlacklistType.NONE; + else if (!type.isBlacklisted()) + type = EnumBlacklistType.ORIGIN; break; } } @@ -216,39 +249,49 @@ public final class OriginBlacklist { } if (isNonNull(brand)) { - if (whitelist && !type.isBlacklisted()) type = EnumBlacklistType.BRAND; + if (whitelist && !type.isBlacklisted()) + type = EnumBlacklistType.BRAND; for (final Json5Element element : this.config.getArray("blacklist.brands")) { if (brand.matches(element.getAsString())) { - if (whitelist) type = EnumBlacklistType.NONE; - else if (!type.isBlacklisted()) type = EnumBlacklistType.BRAND; + if (whitelist) + type = EnumBlacklistType.NONE; + else if (!type.isBlacklisted()) + type = EnumBlacklistType.BRAND; break; } } } if (isNonNull(name)) { - if (whitelist && !type.isBlacklisted()) type = EnumBlacklistType.NAME; + if (whitelist && !type.isBlacklisted()) + type = EnumBlacklistType.NAME; for (final Json5Element element : this.config.getArray("blacklist.player_names")) { if (name.matches(element.getAsString())) { - if (whitelist) type = EnumBlacklistType.NONE; - else if (!type.isBlacklisted()) type = EnumBlacklistType.NAME; + if (whitelist) + type = EnumBlacklistType.NONE; + else if (!type.isBlacklisted()) + type = EnumBlacklistType.NAME; break; } } } if (isNonNull(addr)) { - if (whitelist && !type.isBlacklisted()) type = EnumBlacklistType.ADDR; + if (whitelist && !type.isBlacklisted()) + type = EnumBlacklistType.ADDR; for (final Json5Element element : this.config.getArray("blacklist.ip_addresses")) { try { if ((new IPAddressString(element.getAsString()).toAddress()) .contains((new IPAddressString(addr)).toAddress())) { - if (whitelist) type = EnumBlacklistType.NONE; - else if (!type.isBlacklisted()) type = EnumBlacklistType.ADDR; + if (whitelist) + type = EnumBlacklistType.NONE; + else if (!type.isBlacklisted()) + type = EnumBlacklistType.ADDR; break; } } catch (final AddressStringException exception) { - if (this.isDebugEnabled()) exception.printStackTrace(); + if (this.isDebugEnabled()) + exception.printStackTrace(); } } } @@ -256,6 +299,24 @@ public final class OriginBlacklist { return type; } + public final String getBlacklistShare() { + try { + final Json5Object obj = new Json5Object(); + obj.addProperty("plugin_version", this.plugin.getPluginVersion().getVersion()); + obj.addProperty("blacklist_to_whitelist", this.config.getBoolean("blacklist_to_whitelist")); + obj.addProperty("block_undefined_origin", this.config.getBoolean("block_undefined_origin")); + final Json5Object bObj = new Json5Object(); + final String[] types = new String[] { "origins", "brands", "player_names", "ip_addresses" }; + for (final String type : types) { + bObj.add(type, this.config.getArray("blacklist." + type)); + } + obj.add("blacklist", bObj); + return this.json5.serialize(obj); + } catch (final Throwable t) { + return null; + } + } + private final Component getBlacklistedComponent(final String type, final String id, final String blockType, final String blockTypeAlt, final String notAllowed, final String notAllowedAlt, final String blockValue, final String action) { @@ -288,52 +349,51 @@ public final class OriginBlacklist { userAgent = UNKNOWN_STR; } final byte[] payload = String.format( - """ - { - "content": "Blocked a blacklisted %s from joining", - "embeds": [ - { - "title": "-------- Player Information --------", - "description": "**→ Name:** %s\\n**→ Origin:** %s\\n**→ Brand:** %s\\n**→ IP Address:** %s\\n**→ Protocol Version:** %s\\n**→ User Agent:** %s\\n**→ Rewind:** %s\\n**→ Player Type:** %s", - "color": 15801922, - "fields": [], - "footer": { - "text": "OriginBlacklist v%s", - "icon_url": "https://raw.githubusercontent.com/%s/refs/heads/main/img/icon.png" - } - } - ], - "components": [ - { - "type": 1, - "components": [ - { - "type": 2, - "style": 5, - "label": "Get the Plugin", - "url": "https://github.com/%s", - "emoji": { - "name": "🌐" + """ + { + "content": "Blocked a blacklisted %s from joining", + "embeds": [ + { + "title": "-------- Player Information --------", + "description": "**→ Name:** %s\\n**→ Origin:** %s\\n**→ Brand:** %s\\n**→ IP Address:** %s\\n**→ Protocol Version:** %s\\n**→ User Agent:** %s\\n**→ Rewind:** %s\\n**→ Player Type:** %s", + "color": 15801922, + "fields": [], + "footer": { + "text": "OriginBlacklist v%s", + "icon_url": "https://raw.githubusercontent.com/%s/refs/heads/main/img/icon.png" + } } - } - ] - } - ] - } - """, - type.getAltString(), - player.getName().replaceAll("_", "\\_"), - player.getOrigin(), - player.getBrand(), - this.config.getBoolean("discord.send_ips") ? player.getAddr() : "*\\*CENSORED\\**", - player.getPVN(), - userAgent, - player.isRewind() ? "YES" : "NO", - connType.toString(), - this.plugin.getPluginVersion(), - PLUGIN_REPO, - PLUGIN_REPO - ).getBytes(); + ], + "components": [ + { + "type": 1, + "components": [ + { + "type": 2, + "style": 5, + "label": "Get the Plugin", + "url": "https://github.com/%s", + "emoji": { + "name": "🌐" + } + } + ] + } + ] + } + """, + type.getAltString(), + player.getName().replaceAll("_", "\\_"), + player.getOrigin(), + player.getBrand(), + this.config.getBoolean("discord.send_ips") ? player.getAddr() : "*\\*CENSORED\\**", + player.getPVN(), + userAgent, + player.isRewind() ? "YES" : "NO", + connType.toString(), + this.plugin.getPluginVersion(), + PLUGIN_REPO, + PLUGIN_REPO).getBytes(); final Json5Array arr = this.config.get("discord.webhook_urls").getAsJson5Array(); for (final Json5Element element : arr) { this.plugin.runAsync(() -> { @@ -371,7 +431,8 @@ public final class OriginBlacklist { } else { this.updatePlugin(); } - }, () -> {}); + }, () -> { + }); } public static final String getComponentString(final Component comp) { diff --git a/src/main/java/xyz/webmc/originblacklist/base/command/OriginBlacklistCommand.java b/src/main/java/xyz/webmc/originblacklist/base/command/OriginBlacklistCommand.java index ba3c9c8..2207f1c 100644 --- a/src/main/java/xyz/webmc/originblacklist/base/command/OriginBlacklistCommand.java +++ b/src/main/java/xyz/webmc/originblacklist/base/command/OriginBlacklistCommand.java @@ -24,20 +24,21 @@ public class OriginBlacklistCommand implements ICommand { if (ctx.hasPermission("originblacklist.command")) { if (args.length > 0) { final OriginBlacklistConfig config = this.plugin.getConfig(); - final String command = args[0].toLowerCase(); - final String argA = args.length > 1 ? args[1].toLowerCase() : null; + final String command = args[0]; + final String argA = args.length > 1 ? args[1] : null; ; final String argB = args.length > 2 ? args[2] : null; - final boolean add = "add".equals(command); - final boolean remove = "remove".equals(command); - if ("reload".equals(command)) { + final boolean add = "add".equalsIgnoreCase(command); + final boolean remove = "remove".equalsIgnoreCase(command); + if ("reload".equalsIgnoreCase(command)) { if (ctx.hasPermission("originblacklist.command.reload")) { config.reloadConfig(); + this.plugin.handleReload(); ctx.reply("Configuration Reloaded"); } else { ctx.reply(NO_PERMISSION); } - } else if ("update".equals(command)) { + } else if ("update".equalsIgnoreCase(command)) { if (ctx.hasPermission("originblacklist.command.update")) { ctx.reply("Checking for updates..."); this.plugin.checkForUpdates(() -> { @@ -57,13 +58,13 @@ public class OriginBlacklistCommand implements ICommand { if ((add && ctx.hasPermission("originblacklist.command.add")) || (remove && ctx.hasPermission("originblacklist.command.add"))) { final String arrName; - if ("origin".equals(argA)) { + if ("origin".equalsIgnoreCase(argA)) { arrName = "origins"; - } else if ("brand".equals(argA)) { + } else if ("brand".equalsIgnoreCase(argA)) { arrName = "brands"; - } else if ("name".equals(argA)) { + } else if ("name".equalsIgnoreCase(argA)) { arrName = "player_names"; - } else if ("ip".equals(argA)) { + } else if ("ip".equalsIgnoreCase(argA)) { arrName = "ip_addresses"; } else { arrName = null; @@ -94,7 +95,7 @@ public class OriginBlacklistCommand implements ICommand { } else { ctx.reply(NO_PERMISSION); } - } else if ("test".equals(command) && OriginBlacklist.isNonNull(argA)) { + } else if ("test".equalsIgnoreCase(command) && OriginBlacklist.isNonNull(argA)) { if (ctx.hasPermission("originblacklist.command.test")) { if (this.isBlacklisted(argA)) { ctx.reply("" + argA + " is on the blacklist."); @@ -104,7 +105,7 @@ public class OriginBlacklistCommand implements ICommand { } else { ctx.reply(NO_PERMISSION); } - } else if ("list".equals(command)) { + } else if ("list".equalsIgnoreCase(command)) { if (ctx.hasPermission("originblacklist.command.list")) { ctx.reply("Blacklist:"); ctx.reply(" - Origins:"); diff --git a/src/main/java/xyz/webmc/originblacklist/base/config/OriginBlacklistConfig.java b/src/main/java/xyz/webmc/originblacklist/base/config/OriginBlacklistConfig.java index 7c53eab..3eb8844 100644 --- a/src/main/java/xyz/webmc/originblacklist/base/config/OriginBlacklistConfig.java +++ b/src/main/java/xyz/webmc/originblacklist/base/config/OriginBlacklistConfig.java @@ -50,19 +50,19 @@ public final class OriginBlacklistConfig { private final void loadConfig() { try { this.reloadConfigUnsafe(); + this.reloadIconImage(); } catch (final IOException exception) { throw new RuntimeException("Failed to load config.", exception); } - this.reloadIconImage(); } public final void reloadConfig() { try { this.reloadConfigUnsafe(); + this.reloadIconImage(); } catch (final IOException exception) { exception.printStackTrace(); } - this.reloadIconImage(); } private final void reloadConfigUnsafe() throws IOException { @@ -95,10 +95,10 @@ public final class OriginBlacklistConfig { final BufferedImage img = ImageIO.read(iconFile); if (img.getWidth() == 64 && img.getHeight() == 64) { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); + final ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(img, "png", baos); this.icon64 = OriginBlacklist.getPNGBase64FromBytes(baos.toByteArray()); - byte[] bytes = new byte[64 * 64 * 4]; + final byte[] bytes = new byte[64 * 64 * 4]; for (int y = 0; y < 64; y++) { for (int x = 0; x < 64; x++) { int pixel = img.getRGB(x, y); @@ -255,7 +255,7 @@ public final class OriginBlacklistConfig { private static final Json5Object getDefaultConfig() { final Json5Object obj = new Json5Object(); addJSONObj(obj, "debug", Json5Primitive.fromBoolean(false), null); - final Json5Object mobj = new Json5Object(); + final Json5Object mObj = new Json5Object(); final Json5Array kick = new Json5Array(); kick.add("This %block_type% is %not_allowed_alt%!"); kick.add("» %blocked_value% «"); @@ -264,18 +264,18 @@ public final class OriginBlacklistConfig { kick.add(""); kick.add("Think this is a mistake? Join our discord:"); kick.add("discord.gg/changethisintheconfig"); - addJSONObj(mobj, "kick", kick, null); + addJSONObj(mObj, "kick", kick, null); final Json5Array motd = new Json5Array(); motd.add("This %block_type% is %not_allowed%!"); motd.add("» %blocked_value%"); - addJSONObj(mobj, "motd", motd, null); + addJSONObj(mObj, "motd", motd, null); final Json5Object actions = new Json5Object(); actions.add("generic", Json5Primitive.fromString("Please switch to a different %block_type%.")); actions.add("player_name", Json5Primitive.fromString("Please change your %block_type%.")); actions.add("ip_address", Json5Primitive.fromString("Please contact staff for assistance.")); - addJSONObj(mobj, "actions", actions, null); - addJSONObj(obj, "messages", mobj, null); - final Json5Object bobj = new Json5Object(); + addJSONObj(mObj, "actions", actions, null); + addJSONObj(obj, "messages", mObj, null); + final Json5Object bObj = new Json5Object(); final Json5Array origins = new Json5Array(); origins.add(".*eaglerhackedclients\\.vercel\\.app.*"); origins.add(".*eaglerhacks\\.github\\.io.*"); @@ -287,32 +287,37 @@ public final class OriginBlacklistConfig { origins.add(".*uec\\.vercel\\.app.*"); origins.add(".*valux-game\\.github\\.io.*"); origins.add(".*project516\\.dev.*"); - addJSONObj(bobj, "origins", origins, null); + addJSONObj(bObj, "origins", origins, null); final Json5Array brands = new Json5Array(); brands.add(".*dragonx.*"); brands.add(".*piclient.*"); brands.add(".*justin.*"); brands.add(".*wurstx.*"); brands.add(".*moonlight.*"); - addJSONObj(bobj, "brands", brands, null); + addJSONObj(bObj, "brands", brands, null); final Json5Array players = new Json5Array(); players.add("Admin"); - addJSONObj(bobj, "player_names", players, null); + addJSONObj(bObj, "player_names", players, null); final Json5Array ips = new Json5Array(); ips.add("192.0.2.0/24"); - addJSONObj(bobj, "ip_addresses", ips, null); - addJSONObj(obj, "blacklist", bobj, null); - final Json5Object dobj = new Json5Object(); - addJSONObj(dobj, "enabled", Json5Primitive.fromBoolean(false), null); - addJSONObj(dobj, "webhook_urls", new Json5Array(), null); - addJSONObj(dobj, "send_ips", Json5Primitive.fromBoolean(true), null); - addJSONObj(obj, "discord", dobj, null); - final Json5Object uobj = new Json5Object(); - addJSONObj(uobj, "enabled", Json5Primitive.fromBoolean(true), null); - addJSONObj(uobj, "allow_snapshots", Json5Primitive.fromBoolean(false), null); - addJSONObj(uobj, "check_timer", Json5Primitive.fromNumber(3600), null); - addJSONObj(uobj, "auto_update", Json5Primitive.fromBoolean(true), null); - addJSONObj(obj, "update_checker", uobj, null); + addJSONObj(bObj, "ip_addresses", ips, null); + addJSONObj(obj, "blacklist", bObj, null); + final Json5Object dObj = new Json5Object(); + addJSONObj(dObj, "enabled", Json5Primitive.fromBoolean(false), null); + addJSONObj(dObj, "webhook_urls", new Json5Array(), null); + addJSONObj(dObj, "send_ips", Json5Primitive.fromBoolean(true), null); + addJSONObj(obj, "discord", dObj, null); + final Json5Object uObj = new Json5Object(); + addJSONObj(uObj, "enabled", Json5Primitive.fromBoolean(true), null); + addJSONObj(uObj, "allow_snapshots", Json5Primitive.fromBoolean(false), null); + addJSONObj(uObj, "check_timer", Json5Primitive.fromNumber(3600), null); + addJSONObj(uObj, "auto_update", Json5Primitive.fromBoolean(true), null); + addJSONObj(obj, "update_checker", uObj, null); + final Json5Object hObj = new Json5Object(); + addJSONObj(hObj, "enabled", Json5Primitive.fromBoolean(false), null); + addJSONObj(hObj, "http_port", Json5Primitive.fromNumber(8080), null); + addJSONObj(hObj, "listen_addr", Json5Primitive.fromString("0.0.0.0"), null); + addJSONObj(obj, "blacklist_http_share", hObj, null); addJSONObj(obj, "blacklist_to_whitelist", Json5Primitive.fromBoolean(false), null); addJSONObj(obj, "block_undefined_origin", Json5Primitive.fromBoolean(false), null); addJSONObj(obj, "bStats", Json5Primitive.fromBoolean(true), null); diff --git a/src/main/java/xyz/webmc/originblacklist/base/http/OriginBlacklistHTTPHandler.java b/src/main/java/xyz/webmc/originblacklist/base/http/OriginBlacklistHTTPHandler.java new file mode 100644 index 0000000..423a5d6 --- /dev/null +++ b/src/main/java/xyz/webmc/originblacklist/base/http/OriginBlacklistHTTPHandler.java @@ -0,0 +1,39 @@ +package xyz.webmc.originblacklist.base.http; + +import xyz.webmc.originblacklist.base.OriginBlacklist; + +import java.io.IOException; +import java.io.OutputStream; +import java.nio.charset.StandardCharsets; + +import com.sun.net.httpserver.HttpExchange; +import com.sun.net.httpserver.HttpHandler; + +public class OriginBlacklistHTTPHandler implements HttpHandler { + private final OriginBlacklist plugin; + + public OriginBlacklistHTTPHandler(final OriginBlacklist plugin) { + this.plugin = plugin; + } + + @Override + public final void handle(final HttpExchange exchange) throws IOException { + try { + final String path = exchange.getRequestURI().getPath(); + if ("/".equals(path)) { + final byte[] bytes = this.plugin.getBlacklistShare().getBytes(StandardCharsets.UTF_8); + + exchange.getResponseHeaders().set("Content-Type", "application/json; charset=utf-8"); + exchange.sendResponseHeaders(200, bytes.length); + + try (final OutputStream os = exchange.getResponseBody()) { + os.write(bytes); + } + } else { + exchange.sendResponseHeaders(404, -1); + } + } finally { + exchange.close(); + } + } +} diff --git a/src/main/java/xyz/webmc/originblacklist/base/http/OriginBlacklistHTTPServer.java b/src/main/java/xyz/webmc/originblacklist/base/http/OriginBlacklistHTTPServer.java new file mode 100644 index 0000000..a238249 --- /dev/null +++ b/src/main/java/xyz/webmc/originblacklist/base/http/OriginBlacklistHTTPServer.java @@ -0,0 +1,56 @@ +package xyz.webmc.originblacklist.base.http; + +import xyz.webmc.originblacklist.base.OriginBlacklist; +import xyz.webmc.originblacklist.base.config.OriginBlacklistConfig; + +import java.net.InetSocketAddress; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +import com.sun.net.httpserver.HttpServer; + +public class OriginBlacklistHTTPServer { + private final OriginBlacklist plugin; + private final ExecutorService executor; + private HttpServer server; + private boolean running; + + public OriginBlacklistHTTPServer(final OriginBlacklist plugin) { + this.plugin = plugin; + this.executor = Executors.newFixedThreadPool(4); + this.running = false; + } + + public final void start() { + if (!this.running) { + try { + this.server = this.createServer(); + this.server.start(); + this.running = true; + } catch (final Throwable t) { + } + } + } + + public final void stop() { + if (this.running && this.server != null) { + this.server.stop(0); + this.running = false; + } + } + + public final void shutdown() { + this.stop(); + this.executor.shutdownNow(); + } + + private final HttpServer createServer() throws Throwable { + final OriginBlacklistConfig config = this.plugin.getConfig(); + final HttpServer server = HttpServer + .create(new InetSocketAddress(config.getString("blacklist_http_share.listen_addr"), + config.getInteger("blacklist_http_share.http_port")), 0); + server.createContext("/", new OriginBlacklistHTTPHandler(this.plugin)); + server.setExecutor(executor); + return server; + } +} diff --git a/src/main/java/xyz/webmc/originblacklist/base/util/UpdateChecker.java b/src/main/java/xyz/webmc/originblacklist/base/util/UpdateChecker.java index e131450..390dc27 100644 --- a/src/main/java/xyz/webmc/originblacklist/base/util/UpdateChecker.java +++ b/src/main/java/xyz/webmc/originblacklist/base/util/UpdateChecker.java @@ -71,7 +71,7 @@ public final class UpdateChecker { String comm; try { comm = ver.getBuild().get(0).trim(); - } catch (Throwable t) { + } catch (final Throwable t) { comm = ""; } if (ver.isGreaterThan(currentVersion) || (allowSnapshots && currentVersion.diff(ver) == VersionDiff.BUILD diff --git a/src/main/java/xyz/webmc/originblacklist/bukkit/OriginBlacklistBukkit.java b/src/main/java/xyz/webmc/originblacklist/bukkit/OriginBlacklistBukkit.java index 7e6fb62..f73481f 100644 --- a/src/main/java/xyz/webmc/originblacklist/bukkit/OriginBlacklistBukkit.java +++ b/src/main/java/xyz/webmc/originblacklist/bukkit/OriginBlacklistBukkit.java @@ -93,6 +93,11 @@ public final class OriginBlacklistBukkit extends JavaPlugin implements Listener, } } + @Override + public void onDisable() { + this.blacklist.shutdown(); + } + @EventHandler(priority = EventPriority.NORMAL) public final void onEaglerLogin(final EaglercraftLoginEvent event) { final OPlayer player = new OPlayer(event.getLoginConnection(), event.getProfileUsername(), event.getProfileUUID()); diff --git a/src/main/java/xyz/webmc/originblacklist/bungee/OriginBlacklistBungee.java b/src/main/java/xyz/webmc/originblacklist/bungee/OriginBlacklistBungee.java index 0acd53c..00c8522 100644 --- a/src/main/java/xyz/webmc/originblacklist/bungee/OriginBlacklistBungee.java +++ b/src/main/java/xyz/webmc/originblacklist/bungee/OriginBlacklistBungee.java @@ -89,6 +89,11 @@ public final class OriginBlacklistBungee extends Plugin implements Listener, IOr } } + @Override + public void onDisable() { + this.blacklist.shutdown(); + } + @EventHandler(priority = EventPriority.HIGHEST) public final void onEaglerLogin(final EaglercraftLoginEvent event) { final OPlayer player = new OPlayer(event.getLoginConnection(), event.getProfileUsername(), event.getProfileUUID()); diff --git a/src/main/java/xyz/webmc/originblacklist/velocity/OriginBlacklistVelocity.java b/src/main/java/xyz/webmc/originblacklist/velocity/OriginBlacklistVelocity.java index 2118fee..78d469d 100644 --- a/src/main/java/xyz/webmc/originblacklist/velocity/OriginBlacklistVelocity.java +++ b/src/main/java/xyz/webmc/originblacklist/velocity/OriginBlacklistVelocity.java @@ -24,6 +24,7 @@ import com.velocitypowered.api.event.connection.PreLoginEvent; import com.velocitypowered.api.event.player.PlayerClientBrandEvent; import com.velocitypowered.api.event.proxy.ProxyInitializeEvent; import com.velocitypowered.api.event.proxy.ProxyPingEvent; +import com.velocitypowered.api.event.proxy.ProxyShutdownEvent; import com.velocitypowered.api.plugin.PluginContainer; import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.ProxyServer; @@ -105,6 +106,11 @@ public final class OriginBlacklistVelocity implements IOriginBlacklistPlugin { } } + @Subscribe + public final void onProxyShutdown(final ProxyShutdownEvent e) { + this.blacklist.shutdown(); + } + @Subscribe(order = PostOrder.FIRST) public final void onEaglerLogin(final EaglercraftLoginEvent event) { final OPlayer player = new OPlayer(event.getLoginConnection(), event.getProfileUsername(), event.getProfileUUID());