#1227: Refinements to new ban API for improved compatibility and correctness

By: Doc <nachito94@msn.com>
This commit is contained in:
CraftBukkit/Spigot 2023-07-04 06:50:47 +10:00
parent 7f5ff95fe4
commit 3c49f90bdc
5 changed files with 46 additions and 21 deletions

View File

@ -23,6 +23,7 @@ import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.Collections;
@ -1721,14 +1722,14 @@ public final class CraftServer implements Server {
}
@Override
public void banIP(InetSocketAddress address) {
public void banIP(InetAddress address) {
Preconditions.checkArgument(address != null, "Address cannot be null.");
((CraftIpBanList) this.getBanList(BanList.Type.IP)).addBan(address, null, null, null);
}
@Override
public void unbanIP(InetSocketAddress address) {
public void unbanIP(InetAddress address) {
Preconditions.checkArgument(address != null, "Address cannot be null.");
((CraftIpBanList) this.getBanList(BanList.Type.IP)).pardon(address);

View File

@ -1,13 +1,14 @@
package org.bukkit.craftbukkit.ban;
import java.net.InetSocketAddress;
import com.google.common.net.InetAddresses;
import java.net.InetAddress;
import java.time.Instant;
import java.util.Date;
import net.minecraft.server.players.IpBanEntry;
import net.minecraft.server.players.IpBanList;
import org.bukkit.BanEntry;
public final class CraftIpBanEntry implements BanEntry<InetSocketAddress> {
public final class CraftIpBanEntry implements BanEntry<InetAddress> {
private static final Date minorDate = Date.from(Instant.parse("1899-12-31T04:00:00Z"));
private final IpBanList list;
private final String target;
@ -31,8 +32,8 @@ public final class CraftIpBanEntry implements BanEntry<InetSocketAddress> {
}
@Override
public InetSocketAddress getBanTarget() {
return new InetSocketAddress(this.target, 0);
public InetAddress getBanTarget() {
return InetAddresses.forString(this.target);
}
@Override

View File

@ -3,7 +3,7 @@ package org.bukkit.craftbukkit.ban;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableSet;
import com.google.common.net.InetAddresses;
import java.net.InetSocketAddress;
import java.net.InetAddress;
import java.util.Date;
import java.util.Set;
import net.minecraft.server.players.IpBanEntry;
@ -18,7 +18,7 @@ public class CraftIpBanList implements org.bukkit.ban.IpBanList {
}
@Override
public BanEntry<InetSocketAddress> getBanEntry(String target) {
public BanEntry<InetAddress> getBanEntry(String target) {
Preconditions.checkArgument(target != null, "Target cannot be null");
IpBanEntry entry = this.list.get(target);
@ -30,12 +30,12 @@ public class CraftIpBanList implements org.bukkit.ban.IpBanList {
}
@Override
public BanEntry<InetSocketAddress> getBanEntry(InetSocketAddress target) {
public BanEntry<InetAddress> getBanEntry(InetAddress target) {
return this.getBanEntry(this.getIpFromAddress(target));
}
@Override
public BanEntry<InetSocketAddress> addBan(String target, String reason, Date expires, String source) {
public BanEntry<InetAddress> addBan(String target, String reason, Date expires, String source) {
Preconditions.checkArgument(target != null, "Ban target cannot be null");
IpBanEntry entry = new IpBanEntry(target, new Date(),
@ -48,13 +48,25 @@ public class CraftIpBanList implements org.bukkit.ban.IpBanList {
}
@Override
public BanEntry<InetSocketAddress> addBan(InetSocketAddress target, String reason, Date expires, String source) {
public BanEntry<InetAddress> addBan(InetAddress target, String reason, Date expires, String source) {
return this.addBan(this.getIpFromAddress(target), reason, expires, source);
}
@Override
public Set<BanEntry<InetSocketAddress>> getBanEntries() {
ImmutableSet.Builder<BanEntry<InetSocketAddress>> builder = ImmutableSet.builder();
public Set<BanEntry> getBanEntries() {
ImmutableSet.Builder<BanEntry> builder = ImmutableSet.builder();
for (String target : list.getUserList()) {
IpBanEntry ipBanEntry = list.get(target);
if (ipBanEntry != null) {
builder.add(new CraftIpBanEntry(target, ipBanEntry, list));
}
}
return builder.build();
}
@Override
public Set<BanEntry<InetAddress>> getEntries() {
ImmutableSet.Builder<BanEntry<InetAddress>> builder = ImmutableSet.builder();
for (String target : list.getUserList()) {
IpBanEntry ipBanEntry = list.get(target);
if (ipBanEntry != null) {
@ -71,7 +83,7 @@ public class CraftIpBanList implements org.bukkit.ban.IpBanList {
}
@Override
public boolean isBanned(InetSocketAddress target) {
public boolean isBanned(InetAddress target) {
return this.isBanned(getIpFromAddress(target));
}
@ -82,15 +94,14 @@ public class CraftIpBanList implements org.bukkit.ban.IpBanList {
}
@Override
public void pardon(InetSocketAddress target) {
public void pardon(InetAddress target) {
this.pardon(getIpFromAddress(target));
}
private String getIpFromAddress(InetSocketAddress address) {
private String getIpFromAddress(InetAddress address) {
if (address == null) {
return null;
}
Preconditions.checkArgument(!address.isUnresolved(), "%s its not a valid address", address);
return InetAddresses.toAddrString(address.getAddress());
return InetAddresses.toAddrString(address);
}
}

View File

@ -51,7 +51,18 @@ public class CraftProfileBanList implements ProfileBanList {
}
@Override
public Set<BanEntry<PlayerProfile>> getBanEntries() {
public Set<BanEntry> getBanEntries() {
ImmutableSet.Builder<BanEntry> builder = ImmutableSet.builder();
for (GameProfileBanEntry entry : list.getEntries()) {
GameProfile profile = entry.getUser();
builder.add(new CraftProfileBanEntry(profile, entry, list));
}
return builder.build();
}
@Override
public Set<BanEntry<PlayerProfile>> getEntries() {
ImmutableSet.Builder<BanEntry<PlayerProfile>> builder = ImmutableSet.builder();
for (GameProfileBanEntry entry : list.getEntries()) {
GameProfile profile = entry.getUser();

View File

@ -11,6 +11,7 @@ import it.unimi.dsi.fastutil.shorts.ShortSet;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.ArrayList;
@ -1208,9 +1209,9 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
}
@Override
public BanEntry<InetSocketAddress> banIp(String reason, Date expires, String source, boolean kickPlayer) {
public BanEntry<InetAddress> banIp(String reason, Date expires, String source, boolean kickPlayer) {
Preconditions.checkArgument(getAddress() != null, "The Address of this Player is null");
BanEntry<InetSocketAddress> banEntry = ((IpBanList) server.getBanList(BanList.Type.IP)).addBan(getAddress(), reason, expires, source);
BanEntry<InetAddress> banEntry = ((IpBanList) server.getBanList(BanList.Type.IP)).addBan(getAddress().getAddress(), reason, expires, source);
if (kickPlayer) {
this.kickPlayer(reason);
}