Skip to content

Commit 6e1d5d1

Browse files
committed
add ip blacklisting & blacklist cmd
1 parent 1a22788 commit 6e1d5d1

File tree

13 files changed

+394
-80
lines changed

13 files changed

+394
-80
lines changed

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,15 @@ basically just a reimplementation of originblacklist but for eaglerxserver
99
- [x] Origin Blacklisting
1010
- [x] Brand Blacklisting
1111
- [x] Username blacklisting
12+
- [x] IP blacklisting
1213
- [x] Custom kick message
1314
- [x] Custom blacklist MOTD
1415
- [x] MiniMessage formatting for messages
15-
- [x] Velocity, *Bungee, and *Bukkit support
16-
<br>_<sub><span style="color:gray">Bungee and Bukkit are should work, but have bugs.</span></sub>_
16+
- [x] Velocity, Bungee, and Bukkit support
1717
- [x] Send blacklists to a discord webhook
18+
- [x] Simple blacklist command
1819
- [ ] Blacklist subscription URLs
19-
- [ ] Simple blacklist command
2020
- [ ] Blacklist -> Whitelist
21-
- [ ] IP blacklisting
2221
- [ ] Update system
2322

2423
### Download

build.gradle

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ plugins {
88

99

1010
group = 'dev.colbster937'
11-
version = '1.0.6'
11+
version = '1.1.0'
1212
description = 'A reimplementation of OriginBlacklist for EaglerXServer'
1313
def targetJavaVersion = 17
1414

@@ -51,6 +51,7 @@ dependencies {
5151
implementation("org.yaml:snakeyaml:2.2")
5252
implementation("net.kyori:adventure-text-serializer-legacy:4.20.0")
5353
implementation("net.kyori:adventure-text-minimessage:4.20.0")
54+
implementation("com.github.seancfoley:ipaddress:5.3.4")
5455
}
5556

5657
tasks {
@@ -74,6 +75,7 @@ processResources {
7475

7576
shadowJar {
7677
relocate 'org.yaml.snakeyaml', 'dev.colbster937.shaded.snakeyaml'
78+
relocate 'inet.ipaddr', 'dev.colbster937.shaded.ipaddr'
7779
archiveVersion.set('')
7880
archiveClassifier.set('')
7981
}

src/main/java/dev/colbster937/originblacklist/base/Base.java

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
public class Base {
2323
private static LoggerAdapter adapter;
2424
private static IEaglerXServerAPI api;
25-
private static ConfigManager config;
25+
private static IPBlacklist ipblacklist;
2626

2727
public static void setLogger(LoggerAdapter log) {
2828
adapter = log;
@@ -32,7 +32,9 @@ public static void setApi(IEaglerXServerAPI api1) {
3232
api = api1;
3333
}
3434

35-
public static String apiVer = "1.0.2";
35+
public static ConfigManager config;
36+
37+
public static String pluginVer = "1.0.2";
3638

3739
public static boolean checkVer(String v1, String v2) {
3840
String[] c = v1.split("\\.");
@@ -67,11 +69,13 @@ public static void handleConnection(IEaglercraftLoginEvent e) {
6769
String origin = conn.getWebSocketHeader(EnumWebSocketHeader.HEADER_ORIGIN);
6870
String brand = conn.getEaglerBrandString();
6971
String name = conn.getUsername();
72+
String notAllowed1 = "not allowed on the server";
73+
String notAllowed2 = "not allowed";
7074

7175
if (origin != null && !origin.equals("null")) {
7276
for (String origin1 : config.blacklist.origins) {
7377
if (matches(origin, origin1)) {
74-
setKick(e, formatKickMessage("origin", "website", origin, conn.getWebSocketHost()));
78+
setKick(e, formatKickMessage("origin", "website", notAllowed1, notAllowed2, origin, conn.getWebSocketHost()));
7579
webhook(conn, origin, brand, "origin");
7680
return;
7781
}
@@ -81,7 +85,7 @@ public static void handleConnection(IEaglercraftLoginEvent e) {
8185
if (brand != null && !brand.equals("null")) {
8286
for (String brand1 : config.blacklist.brands) {
8387
if (matches(brand, brand1)) {
84-
setKick(e, formatKickMessage("brand", "client", brand, conn.getWebSocketHost()));
88+
setKick(e, formatKickMessage("brand", "client", notAllowed1, notAllowed2, brand, conn.getWebSocketHost()));
8589
webhook(conn, origin, brand, "brand");
8690
return;
8791
}
@@ -91,7 +95,7 @@ public static void handleConnection(IEaglercraftLoginEvent e) {
9195
if (name != null && !name.equals("null")) {
9296
for (String name1 : config.blacklist.players) {
9397
if (matches(name, name1) || (name.length() > 16 || name.length() < 3)) {
94-
setKick(e, formatKickMessage("player", "username", name, conn.getWebSocketHost()));
98+
setKick(e, formatKickMessage("player", "username", notAllowed1, notAllowed2, name, conn.getWebSocketHost()));
9599
webhook(conn, origin, name, "player");
96100
return;
97101
}
@@ -117,6 +121,8 @@ public static void handleMOTD(IEaglercraftMOTDEvent e) {
117121
.map(line -> line
118122
.replaceAll("%blocktype%", "origin")
119123
.replaceAll("%easyblocktype%", "website")
124+
.replaceAll("%notallowed1%", "blacklisted")
125+
.replaceAll("%notallowed2%", "blacklisted")
120126
.replaceAll("%blocked%", origin)
121127
.replaceAll("%host%", conn.getWebSocketHost()))
122128
.map(line -> LegacyComponentSerializer.legacySection().serialize(
@@ -130,8 +136,6 @@ public static void handleMOTD(IEaglercraftMOTDEvent e) {
130136
return;
131137
}
132138
}
133-
} else if (origin != null && !origin.equals("null")) {
134-
setMOTD(conn, m);
135139
}
136140
}
137141
}
@@ -168,14 +172,28 @@ public static void setMOTD(IMOTDConnection conn, List<String> m) {
168172
}
169173
}
170174

175+
public static String handlePre(String ip, String name) {
176+
if (ip != null && !ip.equalsIgnoreCase("null")) {
177+
for (String ip1 : Base.config.blacklist.ips) {
178+
if (ipblacklist.check(ip)) {
179+
Component kick = formatKickMessage("ip address", "ip", "blacklisted", "blacklisted", ip, "");
180+
return LegacyComponentSerializer.legacySection().serialize(kick);
181+
}
182+
}
183+
}
184+
return "false";
185+
}
186+
171187
public static boolean matches(String text1, String text2) {
172188
return text1.toLowerCase().matches(text2.replace(".", "\\.").replaceAll("\\*", ".*").toLowerCase());
173189
}
174190

175-
public static Component formatKickMessage(String type, String easytype, String value, String host) {
191+
public static Component formatKickMessage(String type, String easytype, String notAllowed1, String notAllowed2, String value, String host) {
176192
String help = "";
177193
if (type != "player") {
178194
help = config.messages.help.generic;
195+
} else if (type == "ip") {
196+
help = config.messages.help.ip;
179197
} else {
180198
help = config.messages.help.player;
181199
}
@@ -184,6 +202,8 @@ public static Component formatKickMessage(String type, String easytype, String v
184202
.replaceAll("%help%", help)
185203
.replaceAll("%blocktype%", type)
186204
.replaceAll("%easyblocktype%", easytype)
205+
.replaceAll("%notallowed1%", notAllowed1)
206+
.replaceAll("%notallowed2%", notAllowed2)
187207
.replaceAll("%blocked%", value)
188208
.replaceAll("%host%", host));
189209
}
@@ -194,7 +214,7 @@ public static void webhook(IEaglerLoginConnection plr, String origin, String bra
194214
return;
195215

196216
CompletableFuture.runAsync(() -> {
197-
String addr = (plr.getPlayerAddress() != null ? plr.getPlayerAddress().toString().substring(1) : "undefined:undefined").split(":")[0];
217+
String addr = getAddr(plr);
198218
int protocol = !plr.isEaglerXRewindPlayer() ? plr.getMinecraftProtocol() : plr.getRewindProtocolVersion();
199219
String host = plr.getWebSocketHost();
200220
String userAgent = plr.getWebSocketHeader(EnumWebSocketHeader.HEADER_USER_AGENT);
@@ -235,6 +255,10 @@ public static void webhook(IEaglerLoginConnection plr, String origin, String bra
235255
});
236256
}
237257

258+
public static String getAddr(IEaglerLoginConnection plr) {
259+
return (plr.getPlayerAddress() != null ? plr.getPlayerAddress().toString().substring(1) : "undefined:undefined").split(":")[0];
260+
}
261+
238262
public static void init() {
239263
File motdIcon = new File(config.messages.motd.icon);
240264
if (!motdIcon.exists()) {
@@ -246,6 +270,7 @@ public static void init() {
246270
getLogger().warn(e.toString());
247271
}
248272
}
273+
ipblacklist = new IPBlacklist();
249274
}
250275

251276
public static void reloadConfig() {
Lines changed: 138 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
package dev.colbster937.originblacklist.base;
22

3+
import org.yaml.snakeyaml.Yaml;
4+
5+
import java.io.File;
6+
import java.io.IOException;
7+
import java.util.List;
8+
import java.util.Map;
9+
10+
import static dev.colbster937.originblacklist.base.Base.config;
11+
312
public class Command {
13+
private static final String permission = "<red>You do not have permission to use this command.</red>";
414

515
public interface CommandContext {
616
String getName();
@@ -9,31 +19,155 @@ public interface CommandContext {
919
String[] getArgs();
1020
}
1121

12-
1322
public static void usage(CommandContext ctx) {
1423
ctx.reply("<aqua>Commands:</aqua>");
1524
ctx.reply("<gray> - /originblacklist reload</gray>");
25+
ctx.reply("<gray> - /originblacklist add <brand/origin/name/ip> <value></gray>");
26+
ctx.reply("<gray> - /originblacklist remove <brand/origin/name/ip> <value></gray>");
27+
ctx.reply("<gray> - /originblacklist list</gray>");
1628
}
1729

1830
public static void handle(CommandContext ctx) {
1931
String[] args = ctx.getArgs();
20-
if (!ctx.hasPermission("originblacklist.reload")) {
21-
ctx.reply("<red>You do not have permission to use this command.</red>");
32+
if (!ctx.hasPermission("originblacklist.command")) {
33+
ctx.reply(permission);
2234
return;
2335
} else if (args.length == 0) {
2436
usage(ctx);
2537
return;
2638
}
2739

2840
String sub = args[0].toLowerCase();
41+
String sub1 = args.length > 1 ? args[1].toLowerCase() : "";
42+
String sub2 = args.length > 2 ? args[2].toLowerCase() : "";
2943

3044
switch (sub) {
3145
case "reload" -> {
46+
if (ctx.hasPermission("originblacklist.reload")) {
47+
Base.reloadConfig();
48+
ctx.reply("<green>Reloaded.</green>");
49+
} else {
50+
ctx.reply(permission);
51+
return;
52+
}
53+
}
54+
55+
case "add" -> {
56+
if (!ctx.hasPermission("originblacklist.add")) {
57+
ctx.reply(permission);
58+
return;
59+
}
60+
61+
if (sub1.isEmpty() || sub2.isEmpty()) {
62+
usage(ctx);
63+
return;
64+
}
65+
66+
List<String> list = switch (sub1) {
67+
case "brand" -> Base.config.blacklist.brands;
68+
case "origin" -> Base.config.blacklist.origins;
69+
case "player" -> Base.config.blacklist.players;
70+
case "ip" -> Base.config.blacklist.ips;
71+
default -> null;
72+
};
73+
74+
if (list == null) {
75+
usage(ctx);
76+
return;
77+
}
78+
79+
if (!list.contains(sub2)) {
80+
list.add(sub2);
81+
ctx.reply("<green>Added " + sub2 + " to " + sub1 + " blacklist.</green>");
82+
} else {
83+
ctx.reply("<yellow>Already blacklisted.</yellow>");
84+
}
85+
try {
86+
config.saveConfig(Base.config.toMap(), new File("plugins/originblacklist/config.yml"));
87+
} catch (IOException e) {
88+
throw new RuntimeException(e);
89+
}
90+
Base.reloadConfig();
91+
}
92+
93+
case "remove" -> {
94+
if (!ctx.hasPermission("originblacklist.remove")) {
95+
ctx.reply(permission);
96+
return;
97+
}
98+
99+
if (sub1.isEmpty() || sub2.isEmpty()) {
100+
usage(ctx);
101+
return;
102+
}
103+
104+
List<String> list = switch (sub1) {
105+
case "brand" -> Base.config.blacklist.brands;
106+
case "origin" -> Base.config.blacklist.origins;
107+
case "player" -> Base.config.blacklist.players;
108+
case "ip" -> Base.config.blacklist.ips;
109+
default -> null;
110+
};
111+
112+
if (list == null) {
113+
usage(ctx);
114+
return;
115+
}
116+
117+
if (list.remove(sub2)) {
118+
ctx.reply("<green>Removed " + sub2 + " from " + sub1 + " blacklist.</green>");
119+
} else {
120+
ctx.reply("<yellow>Entry not found in " + sub1 + ".</yellow>");
121+
}
122+
try {
123+
config.saveConfig(Base.config.toMap(), new File("plugins/originblacklist/config.yml"));
124+
} catch (IOException e) {
125+
throw new RuntimeException(e);
126+
}
32127
Base.reloadConfig();
33-
ctx.reply("<green>Reloaded.</green>");
128+
}
129+
130+
case "list" -> {
131+
if (!ctx.hasPermission("originblacklist.view")) {
132+
ctx.reply(permission);
133+
return;
134+
}
135+
136+
ctx.reply("<aqua>Blacklist:</aqua>");
137+
138+
ctx.reply("<gray> Brands:</gray>");
139+
for (String s : Base.config.blacklist.brands) ctx.reply("<gray> - " + s + "</gray>");
140+
141+
ctx.reply("<gray> Origins:</gray>");
142+
for (String s : Base.config.blacklist.origins) ctx.reply("<gray> - " + s + "</gray>");
143+
144+
ctx.reply("<gray> Players:</gray>");
145+
for (String s : Base.config.blacklist.players) ctx.reply("<gray> - " + s + "</gray>");
146+
147+
ctx.reply("<gray> IPs:</gray>");
148+
for (String s : Base.config.blacklist.ips) ctx.reply("<gray> - " + s + "</gray>");
34149
}
35150

36151
default -> usage(ctx);
37152
}
38153
}
154+
155+
public static List<String> suggest(CommandContext ctx) {
156+
String[] args = ctx.getArgs();
157+
158+
if (args.length == 1) {
159+
return List.of("reload", "add", "remove", "list");
160+
}
161+
162+
if (args.length == 2 && args[0].equalsIgnoreCase("add")) {
163+
return List.of("brand", "origin", "player", "ip");
164+
}
165+
166+
return List.of();
167+
}
168+
169+
public Map<String, Object> toMap() {
170+
Yaml yaml = new Yaml();
171+
return yaml.load(yaml.dump(this));
172+
}
39173
}

0 commit comments

Comments
 (0)