0

Added IP banning support

This commit is contained in:
David Panić 2019-04-07 22:23:38 +02:00
parent ecc4b6d999
commit 7ae8d6908a
No known key found for this signature in database
GPG Key ID: 0FDC88FEC4D8A110
8 changed files with 284 additions and 35 deletions

View File

@ -1,8 +1,7 @@
package com.redstoner.bungeeBans;
import com.redstoner.bungeeBans.commands.BanCommand;
import com.redstoner.bungeeBans.commands.GetBanCommand;
import com.redstoner.bungeeBans.commands.UnbanCommand;
import com.redstoner.bungeeBans.commands.*;
import com.redstoner.bungeeBans.json.IPBan;
import com.redstoner.bungeeBans.json.PlayerBan;
import com.redstoner.bungeeBans.listeners.BanJoinListener;
import com.redstoner.bungeeBans.listeners.DisableJoinListener;
@ -16,15 +15,18 @@ import java.nio.file.NoSuchFileException;
public class Main extends Plugin {
private File playerBanFile = new File("banned-players.json");
private File ipBanFile = new File("banned-ips.json");
private BanManager<PlayerBan> playerBanManager = new BanManager<>(playerBanFile, PlayerBan.class);
private BanManager<IPBan> ipBanManager = new BanManager<>(ipBanFile, IPBan.class);
private boolean shouldSave = true;
@Override
public void onEnable() {
if (
loadBans("player", playerBanManager, playerBanFile)
loadBans("player", playerBanManager, playerBanFile) ||
loadBans("IP", ipBanManager, ipBanFile)
) {
return;
}
@ -35,19 +37,19 @@ public class Main extends Plugin {
pm.registerCommand(this, new UnbanCommand(playerBanManager));
pm.registerCommand(this, new GetBanCommand(playerBanManager));
pm.registerListener(this, new BanJoinListener<>(playerBanManager));
pm.registerCommand(this, new BanIPCommand(ipBanManager, this));
pm.registerCommand(this, new UnbanIPCommand(ipBanManager));
pm.registerCommand(this, new GetIPBanCommand(ipBanManager));
pm.registerListener(this, new BanJoinListener<>("player", playerBanManager));
pm.registerListener(this, new BanJoinListener<>("IP", ipBanManager));
}
@Override
public void onDisable() {
if (shouldSave) {
try {
playerBanManager.saveBans();
getLogger().info("Saved bans to file!");
} catch (IOException e) {
getLogger().severe("Failed to save bans: " + e.getMessage());
e.printStackTrace();
}
saveBans("player", playerBanManager);
saveBans("IP", ipBanManager);
}
}
@ -91,4 +93,14 @@ public class Main extends Plugin {
return true;
}
}
private void saveBans(String name, BanManager banManager) {
try {
playerBanManager.saveBans();
getLogger().info("Saved " + name + " bans to file!");
} catch (IOException e) {
getLogger().severe("Failed to save " + name + " bans: " + e.getMessage());
e.printStackTrace();
}
}
}

View File

@ -5,9 +5,12 @@ import com.mojang.api.profiles.Profile;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.regex.Pattern;
public class Util {
private static final HttpProfileRepository profileRepo = new HttpProfileRepository("minecraft");
private static final Pattern ipValidity = Pattern.compile(
"^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])$");
public static String dashUUID(String uuid) {
return uuid.replaceFirst("([0-9a-fA-F]{8})([0-9a-fA-F]{4})([0-9a-fA-F]{4})([0-9a-fA-F]{4})([0-9a-fA-F]+)", "$1-$2-$3-$4-$5");
@ -20,4 +23,8 @@ public class Util {
public static Profile[] findProfilesByNames(String... names) {
return profileRepo.findProfilesByNames(names);
}
public static boolean validateIP(String ip) {
return ipValidity.matcher(ip).matches();
}
}

View File

@ -33,7 +33,7 @@ public class BanCommand extends Command {
switch (args.length) {
case 0:
sender.sendMessage(
new ComponentBuilder("Usage: ")
new ComponentBuilder(ChatColor.RED + "Usage: ")
.append(ChatColor.AQUA + "/ban ")
.append(ChatColor.GOLD + "<username> ")
.append(ChatColor.YELLOW + "[reason]")
@ -67,9 +67,8 @@ public class BanCommand extends Command {
try {
bm.saveBans();
sender.sendMessage(new TextComponent(ChatColor.GREEN + "Saved bans to file!"));
} catch (IOException e) {
sender.sendMessage(new TextComponent(ChatColor.RED + "Failed to save bans to file!"));
sender.sendMessage(new TextComponent(ChatColor.RED + "Failed to save player bans to file! (nothing was changed)"));
e.printStackTrace();
bm.removeBan(ban);

View File

@ -0,0 +1,95 @@
package com.redstoner.bungeeBans.commands;
import com.redstoner.bungeeBans.BanManager;
import com.redstoner.bungeeBans.Main;
import com.redstoner.bungeeBans.Util;
import com.redstoner.bungeeBans.json.IPBan;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.plugin.Command;
import java.io.IOException;
import java.util.Arrays;
public class BanIPCommand extends Command {
private BanManager<IPBan> bm;
private Main plugin;
public BanIPCommand(BanManager<IPBan> bm, Main plugin) {
super("banip", "bungeeBans.banip");
this.bm = bm;
this.plugin = plugin;
}
@Override
public void execute(CommandSender sender, String[] args) {
String reason = "Banned by an operator.";
switch (args.length) {
case 0:
sender.sendMessage(
new ComponentBuilder(ChatColor.RED + "Usage: ")
.append(ChatColor.AQUA + "/banip ")
.append(ChatColor.GOLD + "<ip> ")
.append(ChatColor.YELLOW + "[reason]")
.create()
);
case 1:
break;
default:
reason = String.join(" ", Arrays.copyOfRange(args, 1, args.length));
break;
}
String ip = args[0];
String expires = "forever"; //TODO: expiry option
if (!Util.validateIP(ip)) {
sender.sendMessage(new TextComponent(ChatColor.RED + "Invalid IP!"));
return;
}
if (bm.getBan(ip) != null) {
sender.sendMessage(new TextComponent(ChatColor.RED + "That IP is already banned!"));
return;
}
IPBan ban = new IPBan(ip, Util.getNow(), sender.getName(), expires, reason);
bm.addBan(ban);
try {
bm.saveBans();
} catch (IOException e) {
sender.sendMessage(new TextComponent(ChatColor.RED + "Failed to save IP bans to file! (nothing was changed)"));
e.printStackTrace();
bm.removeBan(ban);
return;
}
for (ProxiedPlayer player : plugin.getProxy().getPlayers()) {
if (player.getAddress().getAddress().toString().equals(ip)) {
player.disconnect(
new ComponentBuilder(ChatColor.RED + "Your IP was banned by ")
.append(ChatColor.AQUA + sender.getName())
.append(ChatColor.RED + " for ")
.append(ChatColor.YELLOW + reason)
.create()
);
}
}
sender.sendMessage(
new ComponentBuilder(ChatColor.GREEN + "Banned IP ")
.append(ChatColor.AQUA + ip)
.append(ChatColor.GREEN + " for ")
.append(ChatColor.AQUA + reason)
.create()
);
}
}

View File

@ -0,0 +1,59 @@
package com.redstoner.bungeeBans.commands;
import com.redstoner.bungeeBans.BanManager;
import com.redstoner.bungeeBans.Util;
import com.redstoner.bungeeBans.json.IPBan;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.plugin.Command;
public class GetIPBanCommand extends Command {
private BanManager<IPBan> bm;
public GetIPBanCommand(BanManager<IPBan> bm) {
super("getipban", "bungeebans.getipban");
this.bm = bm;
}
@Override
public void execute(CommandSender sender, String[] args) {
if (args.length != 1) {
sender.sendMessage(
new ComponentBuilder(ChatColor.RED + "Usage: ")
.append(ChatColor.AQUA + "/getipban ")
.append(ChatColor.GOLD + "<ip> ")
.create()
);
return;
}
String ip = args[0];
if (!Util.validateIP(ip)) {
sender.sendMessage(new TextComponent(ChatColor.RED + "Invalid IP!"));
return;
}
IPBan ban = bm.getBan(ip);
if (ban == null) {
sender.sendMessage(new TextComponent(ChatColor.RED + "That IP is not banned!"));
return;
}
sender.sendMessage(
new ComponentBuilder(ChatColor.GREEN + "IP ")
.append(ChatColor.AQUA + ip)
.append(ChatColor.GREEN + " is banned for ")
.append(ChatColor.AQUA + ban.getReason())
.append(ChatColor.GREEN + " until ")
.append(ChatColor.AQUA + ban.getExpires())
.create()
);
}
}

View File

@ -1,6 +1,5 @@
package com.redstoner.bungeeBans.commands;
import com.mojang.api.profiles.HttpProfileRepository;
import com.mojang.api.profiles.Profile;
import com.redstoner.bungeeBans.BanManager;
import com.redstoner.bungeeBans.Util;
@ -14,8 +13,6 @@ import net.md_5.bungee.api.plugin.Command;
import java.io.IOException;
public class UnbanCommand extends Command {
HttpProfileRepository profileRepo = new HttpProfileRepository("minecraft");
private BanManager<PlayerBan> bm;
public UnbanCommand(BanManager<PlayerBan> bm) {
@ -28,8 +25,7 @@ public class UnbanCommand extends Command {
public void execute(CommandSender sender, String[] args) {
if (args.length != 1) {
sender.sendMessage(
new ComponentBuilder(ChatColor.RED + "Invalid command! ")
.append("Usage: ")
new ComponentBuilder(ChatColor.RED + "Usage: ")
.append(ChatColor.AQUA + "/unban ")
.append(ChatColor.GOLD + "<username> ")
.create()
@ -38,7 +34,7 @@ public class UnbanCommand extends Command {
return;
}
Profile[] profiles = profileRepo.findProfilesByNames(args[0]);
Profile[] profiles = Util.findProfilesByNames(args[0]);
if (profiles.length != 1) {
sender.sendMessage(new TextComponent(ChatColor.RED + "Invalid name!"));
@ -59,9 +55,8 @@ public class UnbanCommand extends Command {
try {
bm.saveBans();
sender.sendMessage(new TextComponent(ChatColor.GREEN + "Saved bans to file!"));
} catch (IOException e) {
sender.sendMessage(new TextComponent(ChatColor.RED + "Failed to save bans to file!"));
sender.sendMessage(new TextComponent(ChatColor.RED + "Failed to save player bans to file! (nothing was changed)"));
e.printStackTrace();
bm.addBan(ban);

View File

@ -0,0 +1,68 @@
package com.redstoner.bungeeBans.commands;
import com.redstoner.bungeeBans.BanManager;
import com.redstoner.bungeeBans.Util;
import com.redstoner.bungeeBans.json.IPBan;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.plugin.Command;
import java.io.IOException;
public class UnbanIPCommand extends Command {
private BanManager<IPBan> bm;
public UnbanIPCommand(BanManager<IPBan> bm) {
super("unbanip", "bungeebans.unbanip", "pardonip");
this.bm = bm;
}
@Override
public void execute(CommandSender sender, String[] args) {
if (args.length != 1) {
sender.sendMessage(
new ComponentBuilder(ChatColor.RED + "Usage: ")
.append(ChatColor.AQUA + "/unbanip ")
.append(ChatColor.GOLD + "<ip> ")
.create()
);
return;
}
String ip = args[0];
if (!Util.validateIP(ip)) {
sender.sendMessage(new TextComponent(ChatColor.RED + "Invalid IP!"));
return;
}
IPBan ban = bm.getBan(ip);
if (ban == null) {
sender.sendMessage(new TextComponent(ChatColor.RED + "That IP is not banned!"));
return;
}
bm.removeBan(ban);
try {
bm.saveBans();
} catch (IOException e) {
sender.sendMessage(new TextComponent(ChatColor.RED + "Failed to save IP bans to file! (nothing was changed)"));
e.printStackTrace();
bm.addBan(ban);
return;
}
sender.sendMessage(
new ComponentBuilder(ChatColor.GREEN + "Unbanned IP ")
.append(ChatColor.AQUA + ip)
.create()
);
}
}

View File

@ -25,26 +25,40 @@ public class BanJoinListener<T extends Ban> implements Listener {
public void onJoin(PreLoginEvent event) {
event.setCancelled(true);
T ban;
PendingConnection conn = event.getConnection();
String name = conn.getName();
Profile[] profiles = Util.findProfilesByNames(name);
switch (this.name) {
case "player":
String name = conn.getName();
if (profiles.length != 1) {
event.setCancelReason(
new ComponentBuilder(ChatColor.RED + "Server error occured while joining: ")
.append(ChatColor.AQUA + "The mojang API does not know your UUID!")
.create()
);
Profile[] profiles = Util.findProfilesByNames(name);
return;
if (profiles.length != 1) {
event.setCancelReason(
new ComponentBuilder(ChatColor.RED + "Server error occured while joining: ")
.append(ChatColor.AQUA + "The mojang API does not know your UUID!")
.create()
);
return;
}
ban = bm.getBan(Util.dashUUID(profiles[0].getId()));
break;
case "IP":
String address = conn.getAddress().getAddress().getHostAddress();
System.out.println("IP connection: " + address);
ban = bm.getBan(address);
break;
default:
throw new UnsupportedOperationException();
}
T ban = bm.getBan(Util.dashUUID(profiles[0].getId()));
if (ban != null) {
event.setCancelReason(
new ComponentBuilder(ChatColor.RED + "You were " + name + " banned by ")
new ComponentBuilder(ChatColor.RED + "You were " + this.name + " banned by ")
.append(ChatColor.AQUA + ban.getSource())
.append(ChatColor.RED + " for ")
.append(ChatColor.AQUA + ban.getReason())