| 1 | package net.minecraft.server.jsonrpc.methods;
|
| 2 |
|
| 3 | import com.google.common.net.InetAddresses;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import com.mojang.serialization.MapCodec;
|
| 6 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 7 | import java.time.Instant;
|
| 8 | import java.util.Collection;
|
| 9 | import java.util.Date;
|
| 10 | import java.util.List;
|
| 11 | import java.util.Objects;
|
| 12 | import java.util.Optional;
|
| 13 | import java.util.Set;
|
| 14 | import java.util.stream.Collectors;
|
| 15 | import net.minecraft.network.chat.Component;
|
| 16 | import net.minecraft.server.jsonrpc.api.PlayerDto;
|
| 17 | import net.minecraft.server.jsonrpc.internalapi.MinecraftApi;
|
| 18 | import net.minecraft.server.level.ServerPlayer;
|
| 19 | import net.minecraft.server.players.IpBanListEntry;
|
| 20 | import net.minecraft.util.ExtraCodecs;
|
| 21 | import org.jspecify.annotations.Nullable;
|
| 22 |
|
| 23 | public class IpBanlistService {
|
| 24 | private static final String BAN_SOURCE = "Management server";
|
| 25 |
|
| 26 | public static List<IpBanlistService.IpBanDto> get(final MinecraftApi minecraftApi) {
|
| 27 | return minecraftApi.banListService().getIpBanEntries().stream().map(IpBanlistService.IpBan::from).map(IpBanlistService.IpBanDto::from).toList();
|
| 28 | }
|
| 29 |
|
| 30 | public static List<IpBanlistService.IpBanDto> add(
|
| 31 | final MinecraftApi minecraftApi, final List<IpBanlistService.IncomingIpBanDto> bans, final ClientInfo clientInfo
|
| 32 | ) {
|
| 33 | bans.stream()
|
| 34 | .map(ban -> banIp(minecraftApi, ban, clientInfo))
|
| 35 | .flatMap(Collection::stream)
|
| 36 | .forEach(player -> player.connection.disconnect(Component.translatable("multiplayer.disconnect.ip_banned")));
|
| 37 | return get(minecraftApi);
|
| 38 | }
|
| 39 |
|
| 40 | private static List<ServerPlayer> banIp(final MinecraftApi minecraftApi, final IpBanlistService.IncomingIpBanDto ban, final ClientInfo clientInfo) {
|
| 41 | IpBanlistService.IpBan ipBan = ban.toIpBan();
|
| 42 | if (ipBan != null) {
|
| 43 | return banIp(minecraftApi, ipBan, clientInfo);
|
| 44 | }
|
| 45 |
|
| 46 | if (ban.player().isPresent()) {
|
| 47 | Optional<ServerPlayer> player = minecraftApi.playerListService().getPlayer(ban.player().get().id(), ban.player().get().name());
|
| 48 | if (player.isPresent()) {
|
| 49 | return banIp(minecraftApi, ban.toIpBan(player.get()), clientInfo);
|
| 50 | }
|
| 51 | }
|
| 52 |
|
| 53 | return List.of();
|
| 54 | }
|
| 55 |
|
| 56 | private static List<ServerPlayer> banIp(final MinecraftApi minecraftApi, final IpBanlistService.IpBan ban, final ClientInfo clientInfo) {
|
| 57 | minecraftApi.banListService().addIpBan(ban.toIpBanEntry(), clientInfo);
|
| 58 | return minecraftApi.playerListService().getPlayersWithAddress(ban.ip());
|
| 59 | }
|
| 60 |
|
| 61 | public static List<IpBanlistService.IpBanDto> clear(final MinecraftApi minecraftApi, final ClientInfo clientInfo) {
|
| 62 | minecraftApi.banListService().clearIpBans(clientInfo);
|
| 63 | return get(minecraftApi);
|
| 64 | }
|
| 65 |
|
| 66 | public static List<IpBanlistService.IpBanDto> remove(final MinecraftApi minecraftApi, final List<String> ban, final ClientInfo clientInfo) {
|
| 67 | ban.forEach(ip -> minecraftApi.banListService().removeIpBan(ip, clientInfo));
|
| 68 | return get(minecraftApi);
|
| 69 | }
|
| 70 |
|
| 71 | public static List<IpBanlistService.IpBanDto> set(final MinecraftApi minecraftApi, final List<IpBanlistService.IpBanDto> ips, final ClientInfo clientInfo) {
|
| 72 | Set<IpBanlistService.IpBan> finalBanlist = ips.stream()
|
| 73 | .filter(ban -> InetAddresses.isInetAddress(ban.ip()))
|
| 74 | .map(IpBanlistService.IpBanDto::toIpBan)
|
| 75 | .collect(Collectors.toSet());
|
| 76 | Set<IpBanlistService.IpBan> currentBans = minecraftApi.banListService()
|
| 77 | .getIpBanEntries()
|
| 78 | .stream()
|
| 79 | .map(IpBanlistService.IpBan::from)
|
| 80 | .collect(Collectors.toSet());
|
| 81 | currentBans.stream().filter(ban -> !finalBanlist.contains(ban)).forEach(ban -> minecraftApi.banListService().removeIpBan(ban.ip(), clientInfo));
|
| 82 | finalBanlist.stream().filter(ban -> !currentBans.contains(ban)).forEach(ban -> minecraftApi.banListService().addIpBan(ban.toIpBanEntry(), clientInfo));
|
| 83 | finalBanlist.stream()
|
| 84 | .filter(ban -> !currentBans.contains(ban))
|
| 85 | .flatMap(ban -> minecraftApi.playerListService().getPlayersWithAddress(ban.ip()).stream())
|
| 86 | .forEach(player -> player.connection.disconnect(Component.translatable("multiplayer.disconnect.ip_banned")));
|
| 87 | return get(minecraftApi);
|
| 88 | }
|
| 89 |
|
| 90 | public record IncomingIpBanDto(Optional<PlayerDto> player, Optional<String> ip, Optional<String> reason, Optional<String> source, Optional<Instant> expires) {
|
| 91 | public static final MapCodec<IpBanlistService.IncomingIpBanDto> CODEC = RecordCodecBuilder.mapCodec(
|
| 92 | i -> i.group(
|
| 93 | PlayerDto.CODEC.codec().optionalFieldOf("player").forGetter(IpBanlistService.IncomingIpBanDto::player),
|
| 94 | Codec.STRING.optionalFieldOf("ip").forGetter(IpBanlistService.IncomingIpBanDto::ip),
|
| 95 | Codec.STRING.optionalFieldOf("reason").forGetter(IpBanlistService.IncomingIpBanDto::reason),
|
| 96 | Codec.STRING.optionalFieldOf("source").forGetter(IpBanlistService.IncomingIpBanDto::source),
|
| 97 | ExtraCodecs.INSTANT_ISO8601.optionalFieldOf("expires").forGetter(IpBanlistService.IncomingIpBanDto::expires)
|
| 98 | )
|
| 99 | .apply(i, IpBanlistService.IncomingIpBanDto::new)
|
| 100 | );
|
| 101 |
|
| 102 | private IpBanlistService.IpBan toIpBan(final ServerPlayer player) {
|
| 103 | return new IpBanlistService.IpBan(player.getIpAddress(), this.reason().orElse(null), this.source().orElse("Management server"), this.expires());
|
| 104 | }
|
| 105 |
|
| 106 | private IpBanlistService.@Nullable IpBan toIpBan() {
|
| 107 | return !this.ip().isEmpty() && InetAddresses.isInetAddress(this.ip().get())
|
| 108 | ? new IpBanlistService.IpBan(this.ip().get(), this.reason().orElse(null), this.source().orElse("Management server"), this.expires())
|
| 109 | : null;
|
| 110 | }
|
| 111 | }
|
| 112 |
|
| 113 | private record IpBan(String ip, @Nullable String reason, String source, Optional<Instant> expires) {
|
| 114 | private static IpBanlistService.IpBan from(final IpBanListEntry entry) {
|
| 115 | return new IpBanlistService.IpBan(
|
| 116 | Objects.requireNonNull(entry.getUser()), entry.getReason(), entry.getSource(), Optional.ofNullable(entry.getExpires()).map(Date::toInstant)
|
| 117 | );
|
| 118 | }
|
| 119 |
|
| 120 | private IpBanListEntry toIpBanEntry() {
|
| 121 | return new IpBanListEntry(this.ip(), null, this.source(), this.expires().map(Date::from).orElse(null), this.reason());
|
| 122 | }
|
| 123 | }
|
| 124 |
|
| 125 | public record IpBanDto(String ip, Optional<String> reason, Optional<String> source, Optional<Instant> expires) {
|
| 126 | public static final MapCodec<IpBanlistService.IpBanDto> CODEC = RecordCodecBuilder.mapCodec(
|
| 127 | i -> i.group(
|
| 128 | Codec.STRING.fieldOf("ip").forGetter(IpBanlistService.IpBanDto::ip),
|
| 129 | Codec.STRING.optionalFieldOf("reason").forGetter(IpBanlistService.IpBanDto::reason),
|
| 130 | Codec.STRING.optionalFieldOf("source").forGetter(IpBanlistService.IpBanDto::source),
|
| 131 | ExtraCodecs.INSTANT_ISO8601.optionalFieldOf("expires").forGetter(IpBanlistService.IpBanDto::expires)
|
| 132 | )
|
| 133 | .apply(i, IpBanlistService.IpBanDto::new)
|
| 134 | );
|
| 135 |
|
| 136 | private static IpBanlistService.IpBanDto from(final IpBanlistService.IpBan ban) {
|
| 137 | return new IpBanlistService.IpBanDto(ban.ip(), Optional.ofNullable(ban.reason()), Optional.of(ban.source()), ban.expires());
|
| 138 | }
|
| 139 |
|
| 140 | public static IpBanlistService.IpBanDto from(final IpBanListEntry ban) {
|
| 141 | return from(IpBanlistService.IpBan.from(ban));
|
| 142 | }
|
| 143 |
|
| 144 | private IpBanlistService.IpBan toIpBan() {
|
| 145 | return new IpBanlistService.IpBan(this.ip(), this.reason().orElse(null), this.source().orElse("Management server"), this.expires());
|
| 146 | }
|
| 147 | }
|
| 148 | }
|
| 149 |
|