Skip to content

Commit 12f9d66

Browse files
committed
make it register an eaglerxserver route
1 parent c43de8a commit 12f9d66

File tree

11 files changed

+248
-147
lines changed

11 files changed

+248
-147
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
- [x] Plugin update checker
1616
- [x] Send blacklist logs to a webhook
1717
- [x] Ingame blacklist management commands
18+
- [x] Blacklist sharing via EaglerXServer routes
1819
- [x] Reverse blacklist (whitelist)
1920
- [ ] Subscribe to an auto-updating blacklist
2021

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ val PLUGIN_NAME = "OriginBlacklist"
1414
val PLUGIN_IDEN = "originblacklist"
1515
val PLUGIN_DOMN = "xyz.webmc.$PLUGIN_IDEN"
1616
val PLUGIN_DESC = "An eaglercraft client blacklist plugin."
17-
val PLUGIN_VERS = "2.0.6"
17+
val PLUGIN_VERS = "2.0.7"
1818
val PLUGIN_SITE = "https://github.com/WebMCDevelopment/$PLUGIN_IDEN"
1919
val PLUGIN_DEPA = listOf("EaglercraftXServer")
2020
val PLUGIN_DEPB = listOf("EaglercraftXServer")

src/main/java/xyz/webmc/originblacklist/base/OriginBlacklist.java

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import xyz.webmc.originblacklist.base.enums.EnumLogLevel;
77
import xyz.webmc.originblacklist.base.events.OriginBlacklistLoginEvent;
88
import xyz.webmc.originblacklist.base.events.OriginBlacklistMOTDEvent;
9-
import xyz.webmc.originblacklist.base.http.OriginBlacklistHTTPServer;
9+
import xyz.webmc.originblacklist.base.http.OriginBlacklistRequestHandler;
1010
import xyz.webmc.originblacklist.base.util.BuildInfo;
1111
import xyz.webmc.originblacklist.base.util.IOriginBlacklistPlugin;
1212
import xyz.webmc.originblacklist.base.util.OPlayer;
@@ -38,9 +38,11 @@
3838
import net.kyori.adventure.text.Component;
3939
import net.kyori.adventure.text.minimessage.MiniMessage;
4040
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
41+
import net.lax1dude.eaglercraft.backend.server.api.IEaglerXServerAPI;
4142
import net.lax1dude.eaglercraft.backend.server.api.query.IMOTDConnection;
4243
import org.semver4j.Semver;
4344

45+
@SuppressWarnings({ "rawtypes" })
4446
public final class OriginBlacklist {
4547
private static final String COMMIT_L = BuildInfo.get("git_cm_hash");
4648
private static final String COMMIT_S = COMMIT_L.substring(0, 8);
@@ -54,15 +56,13 @@ public final class OriginBlacklist {
5456

5557
private final IOriginBlacklistPlugin plugin;
5658
private final OriginBlacklistConfig config;
57-
private final OriginBlacklistHTTPServer http;
5859
private final Json5 json5;
5960
private String updateURL;
6061
private Path jarFile;
6162

6263
public OriginBlacklist(final IOriginBlacklistPlugin plugin) {
6364
this.plugin = plugin;
6465
this.config = new OriginBlacklistConfig(this);
65-
this.http = new OriginBlacklistHTTPServer(this);
6666
this.json5 = Json5.builder(builder -> builder.prettyPrinting().indentFactor(0).build());
6767
}
6868

@@ -71,24 +71,27 @@ public final void init() {
7171
this.plugin.scheduleRepeat(() -> {
7272
this.checkForUpdates();
7373
}, this.config.getInteger("update_checker.check_timer"), TimeUnit.SECONDS);
74-
if (this.isHTTPServerEnabled()) {
75-
this.http.start();
74+
if (this.isBlacklistAPIEnabled()) {
75+
OriginBlacklistRequestHandler.register(this);
7676
}
7777
this.plugin.log(EnumLogLevel.INFO, "Initialized Plugin");
7878
this.plugin.log(EnumLogLevel.DEBUG, "Commit " + COMMIT_L);
7979
}
8080

8181
public final void shutdown() {
8282
this.plugin.log(EnumLogLevel.INFO, "Shutting down...");
83-
this.http.stop();
83+
OriginBlacklistRequestHandler.unRegister(this);
8484
this.plugin.shutdown();
8585
}
8686

8787
public final void handleReload() {
88-
if (this.isHTTPServerEnabled()) {
89-
this.http.start();
90-
} else {
91-
this.http.stop();
88+
try {
89+
if (this.isBlacklistAPIEnabled()) {
90+
OriginBlacklistRequestHandler.register(this);
91+
} else {
92+
OriginBlacklistRequestHandler.unRegister(this);
93+
}
94+
} catch (final Throwable t) {
9295
}
9396
}
9497

@@ -150,8 +153,8 @@ public final boolean isLogFileEnabled() {
150153
return this.config.getBoolean("logFile");
151154
}
152155

153-
public final boolean isHTTPServerEnabled() {
154-
return this.config.getBoolean("blacklist_http_share.enabled");
156+
public final boolean isBlacklistAPIEnabled() {
157+
return this.config.getBoolean("blacklist_http_api");
155158
}
156159

157160
public final OriginBlacklistConfig getConfig() {
@@ -334,6 +337,10 @@ public final String getDataDir() {
334337
return "plugins/" + plugin.getPluginId();
335338
}
336339

340+
public final IEaglerXServerAPI getEaglerAPI() {
341+
return this.plugin.getEaglerAPI();
342+
}
343+
337344
private final Component getBlacklistedComponent(final String type, final String id, final String blockType,
338345
final String blockTypeAlt, final String notAllowed, final String notAllowedAlt, final String blockValue,
339346
final String action) {

src/main/java/xyz/webmc/originblacklist/base/config/OriginBlacklistConfig.java

Lines changed: 152 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.nio.file.Files;
1212
import java.nio.file.Path;
1313
import java.nio.file.StandardCopyOption;
14+
import java.util.ArrayList;
1415

1516
import javax.imageio.ImageIO;
1617

@@ -21,6 +22,8 @@
2122
import de.marhali.json5.Json5Primitive;
2223

2324
public final class OriginBlacklistConfig {
25+
private static final Json5Object DEFAULT_CONFIG = getDefaultConfig();
26+
2427
private final Json5 json5;
2528
private final File file;
2629
private final Path filePath;
@@ -70,16 +73,14 @@ private final void reloadConfigUnsafe() throws IOException {
7073
Json5Element parsed = this.json5.parse(text);
7174
if (parsed instanceof Json5Object) {
7275
this.config = (Json5Object) parsed;
73-
if (merge(this.config, getDefaultConfig())) {
74-
this.saveConfig();
75-
}
76+
merge(this.config, DEFAULT_CONFIG);
7677
} else {
7778
throw new IOException("Config must be an object!");
7879
}
7980
} else {
80-
this.config = getDefaultConfig();
81-
this.saveConfig();
81+
this.config = DEFAULT_CONFIG;
8282
}
83+
this.saveConfig();
8384
}
8485

8586
private final void reloadIconImage() {
@@ -206,7 +207,6 @@ public final boolean remove(final String key) {
206207
} else {
207208
element = null;
208209
}
209-
210210
if (element == null) {
211211
break;
212212
}
@@ -312,11 +312,7 @@ private static final Json5Object getDefaultConfig() {
312312
addJSONObj(uObj, "check_timer", Json5Primitive.fromNumber(3600), null);
313313
addJSONObj(uObj, "auto_update", Json5Primitive.fromBoolean(true), null);
314314
addJSONObj(obj, "update_checker", uObj, null);
315-
final Json5Object hObj = new Json5Object();
316-
addJSONObj(hObj, "enabled", Json5Primitive.fromBoolean(false), null);
317-
addJSONObj(hObj, "http_port", Json5Primitive.fromNumber(8080), null);
318-
addJSONObj(hObj, "listen_addr", Json5Primitive.fromString("0.0.0.0"), null);
319-
addJSONObj(obj, "blacklist_http_share", hObj, null);
315+
addJSONObj(obj, "blacklist_http_api", Json5Primitive.fromBoolean(false), null);
320316
addJSONObj(obj, "blacklist_to_whitelist", Json5Primitive.fromBoolean(false), null);
321317
addJSONObj(obj, "block_undefined_origin", Json5Primitive.fromBoolean(false), null);
322318
addJSONObj(obj, "bStats", Json5Primitive.fromBoolean(true), null);
@@ -325,24 +321,157 @@ private static final Json5Object getDefaultConfig() {
325321
return obj;
326322
}
327323

328-
private static final boolean merge(final Json5Object a, final Json5Object b) {
324+
private static final boolean merge(final Json5Object aObj, final Json5Object bObj) {
325+
final Json5Object ret = new Json5Object();
329326
boolean changed = false;
330-
331-
for (String key : b.keySet()) {
332-
Json5Element element = b.get(key);
333-
if (!a.has(key)) {
334-
a.add(key, element.deepCopy());
335-
changed = true;
336-
} else {
337-
final Json5Element _element = a.get(key);
338-
if (_element instanceof Json5Object objA && element instanceof Json5Object objB) {
339-
if (merge(objA, objB)) {
327+
for (final String key : bObj.keySet()) {
328+
final Json5Element dv = bObj.get(key);
329+
if (aObj.has(key)) {
330+
final Json5Element v = aObj.get(key);
331+
if (dv instanceof Json5Object) {
332+
if (v instanceof Json5Object) {
333+
final boolean c = merge((Json5Object) v, (Json5Object) dv);
334+
ret.add(key, (Json5Object) v);
335+
if (c) {
336+
changed = true;
337+
}
338+
} else {
339+
ret.add(key, dv.deepCopy());
340+
changed = true;
341+
}
342+
} else if (dv instanceof Json5Array) {
343+
if (v instanceof Json5Array) {
344+
final Json5Array vArr = (Json5Array) v;
345+
final Json5Array dArr = (Json5Array) dv;
346+
if (dArr.size() == 0) {
347+
ret.add(key, vArr.deepCopy());
348+
} else {
349+
final Json5Element d0 = dArr.get(0);
350+
if (d0 instanceof Json5Primitive && ((Json5Primitive) d0).isString()) {
351+
final Json5Array out = new Json5Array();
352+
for (final Json5Element e : vArr) {
353+
if (e instanceof Json5Primitive && ((Json5Primitive) e).isString()) {
354+
out.add(e.deepCopy());
355+
}
356+
}
357+
if (out.size() > 0) {
358+
if (out.size() != vArr.size()) {
359+
changed = true;
360+
}
361+
ret.add(key, out);
362+
} else {
363+
ret.add(key, dArr.deepCopy());
364+
changed = true;
365+
}
366+
} else {
367+
boolean a = true;
368+
for (final Json5Element e : vArr) {
369+
if (e == null) {
370+
a = false;
371+
} else if (d0 instanceof Json5Object) {
372+
if (!(e instanceof Json5Object)) {
373+
a = false;
374+
}
375+
} else if (d0 instanceof Json5Array) {
376+
if (!(e instanceof Json5Array)) {
377+
a = false;
378+
}
379+
} else if (d0 instanceof Json5Primitive && e instanceof Json5Primitive) {
380+
final Json5Primitive bp = (Json5Primitive) d0;
381+
final Json5Primitive ap = (Json5Primitive) e;
382+
if (bp.isBoolean()) {
383+
if (!ap.isBoolean()) {
384+
a = false;
385+
}
386+
} else if (bp.isNumber()) {
387+
if (!ap.isNumber()) {
388+
a = false;
389+
}
390+
} else if (bp.isString()) {
391+
if (!ap.isString()) {
392+
a = false;
393+
}
394+
}
395+
} else {
396+
a = false;
397+
}
398+
if (!a) {
399+
break;
400+
}
401+
}
402+
if (a) {
403+
ret.add(key, vArr.deepCopy());
404+
} else {
405+
ret.add(key, dArr.deepCopy());
406+
changed = true;
407+
}
408+
}
409+
}
410+
} else {
411+
ret.add(key, dv.deepCopy());
412+
changed = true;
413+
}
414+
} else if (dv instanceof Json5Primitive) {
415+
if (v instanceof Json5Primitive) {
416+
final Json5Primitive dp = (Json5Primitive) dv;
417+
final Json5Primitive vp = (Json5Primitive) v;
418+
if (dp.isBoolean()) {
419+
if (vp.isBoolean()) {
420+
ret.add(key, vp.deepCopy());
421+
} else {
422+
ret.add(key, dv.deepCopy());
423+
changed = true;
424+
}
425+
} else if (dp.isNumber()) {
426+
if (vp.isNumber()) {
427+
ret.add(key, vp.deepCopy());
428+
} else {
429+
ret.add(key, dv.deepCopy());
430+
changed = true;
431+
}
432+
} else if (dp.isString()) {
433+
if (vp.isString()) {
434+
ret.add(key, vp.deepCopy());
435+
} else {
436+
ret.add(key, dv.deepCopy());
437+
changed = true;
438+
}
439+
} else {
440+
ret.add(key, dv.deepCopy());
441+
changed = true;
442+
}
443+
} else {
444+
ret.add(key, dv.deepCopy());
340445
changed = true;
341446
}
447+
} else {
448+
ret.add(key, dv.deepCopy());
449+
changed = true;
342450
}
451+
} else {
452+
ret.add(key, dv.deepCopy());
453+
changed = true;
343454
}
344455
}
345-
456+
for (final String key : aObj.keySet()) {
457+
if (!bObj.has(key)) {
458+
ret.add(key, aObj.get(key).deepCopy());
459+
}
460+
}
461+
for (final String key : aObj.keySet()) {
462+
if (!bObj.has(key)) {
463+
ret.add(key, aObj.get(key).deepCopy());
464+
}
465+
}
466+
for (final String k : new ArrayList<>(aObj.keySet())) {
467+
aObj.remove(k);
468+
}
469+
for (final String k : ret.keySet()) {
470+
aObj.add(k, ret.get(k));
471+
}
472+
for (final String key : ret.keySet()) {
473+
aObj.add(key, ret.get(key));
474+
}
346475
return changed;
347476
}
348477

src/main/java/xyz/webmc/originblacklist/base/http/OriginBlacklistHTTPHandler.java

Lines changed: 0 additions & 39 deletions
This file was deleted.

0 commit comments

Comments
 (0)