From 814c67ace841e0b163100af7102012d9870e47bd Mon Sep 17 00:00:00 2001 From: CraftBukkit/Spigot Date: Fri, 28 Oct 2022 20:19:11 +1100 Subject: [PATCH] SPIGOT-7171: Ability to get the IP/hostname players are requesting status of By: md_5 --- .../minecraft/network/NetworkManager.patch | 12 ++++++++-- .../server/network/HandshakeListener.patch | 14 +++++------- .../server/network/LoginListener.patch | 22 ++++++------------- .../server/network/PacketStatusListener.patch | 2 +- .../minecraft/server/players/PlayerList.patch | 4 ++-- 5 files changed, 25 insertions(+), 29 deletions(-) diff --git a/paper-server/nms-patches/net/minecraft/network/NetworkManager.patch b/paper-server/nms-patches/net/minecraft/network/NetworkManager.patch index 6d83f64b15..b017f56bfb 100644 --- a/paper-server/nms-patches/net/minecraft/network/NetworkManager.patch +++ b/paper-server/nms-patches/net/minecraft/network/NetworkManager.patch @@ -1,6 +1,14 @@ --- a/net/minecraft/network/NetworkManager.java +++ b/net/minecraft/network/NetworkManager.java -@@ -164,7 +164,7 @@ +@@ -84,6 +84,7 @@ + private float averageSentPackets; + private int tickCount; + private boolean handlingFault; ++ public String hostname = ""; // CraftBukkit - add field + + public NetworkManager(EnumProtocolDirection enumprotocoldirection) { + this.receiving = enumprotocoldirection; +@@ -164,7 +165,7 @@ } private static void genericsFtw(Packet packet, PacketListener packetlistener) { @@ -9,7 +17,7 @@ } public void setListener(PacketListener packetlistener) { -@@ -289,7 +289,7 @@ +@@ -289,7 +290,7 @@ public void disconnect(IChatBaseComponent ichatbasecomponent) { if (this.channel.isOpen()) { diff --git a/paper-server/nms-patches/net/minecraft/server/network/HandshakeListener.patch b/paper-server/nms-patches/net/minecraft/server/network/HandshakeListener.patch index 5230effd77..60422bf368 100644 --- a/paper-server/nms-patches/net/minecraft/server/network/HandshakeListener.patch +++ b/paper-server/nms-patches/net/minecraft/server/network/HandshakeListener.patch @@ -18,7 +18,11 @@ private static final IChatBaseComponent IGNORE_STATUS_REASON = IChatBaseComponent.literal("Ignoring status request"); private final MinecraftServer server; private final NetworkManager connection; -@@ -26,6 +35,40 @@ +@@ -23,9 +32,44 @@ + + @Override + public void handleIntention(PacketHandshakingInSetProtocol packethandshakinginsetprotocol) { ++ this.connection.hostname = packethandshakinginsetprotocol.hostName + ":" + packethandshakinginsetprotocol.port; // CraftBukkit - set hostname switch (packethandshakinginsetprotocol.getIntention()) { case LOGIN: this.connection.setProtocol(EnumProtocol.LOGIN); @@ -59,11 +63,3 @@ if (packethandshakinginsetprotocol.getProtocolVersion() != SharedConstants.getCurrentVersion().getProtocolVersion()) { IChatMutableComponent ichatmutablecomponent; -@@ -39,6 +82,7 @@ - this.connection.disconnect(ichatmutablecomponent); - } else { - this.connection.setListener(new LoginListener(this.server, this.connection)); -+ ((LoginListener) this.connection.getPacketListener()).hostname = packethandshakinginsetprotocol.hostName + ":" + packethandshakinginsetprotocol.port; // CraftBukkit - set hostname - } - break; - case STATUS: diff --git a/paper-server/nms-patches/net/minecraft/server/network/LoginListener.patch b/paper-server/nms-patches/net/minecraft/server/network/LoginListener.patch index 1f89ff92bd..2eac6cf154 100644 --- a/paper-server/nms-patches/net/minecraft/server/network/LoginListener.patch +++ b/paper-server/nms-patches/net/minecraft/server/network/LoginListener.patch @@ -13,15 +13,7 @@ public class LoginListener implements TickablePacketListener, PacketLoginInListener { private static final AtomicInteger UNIQUE_THREAD_ID = new AtomicInteger(0); -@@ -59,6 +65,7 @@ - private EntityPlayer delayedAcceptPlayer; - @Nullable - private ProfilePublicKey.a profilePublicKeyData; -+ public String hostname = ""; // CraftBukkit - add field - - public LoginListener(MinecraftServer minecraftserver, NetworkManager networkmanager) { - this.state = LoginListener.EnumProtocolState.HELLO; -@@ -88,6 +95,13 @@ +@@ -88,6 +94,13 @@ } @@ -35,13 +27,13 @@ @Override public NetworkManager getConnection() { return this.connection; -@@ -123,10 +137,12 @@ +@@ -123,10 +136,12 @@ } } - IChatBaseComponent ichatbasecomponent = this.server.getPlayerList().canPlayerLogin(this.connection.getRemoteAddress(), this.gameProfile); + // CraftBukkit start - fire PlayerLoginEvent -+ EntityPlayer s = this.server.getPlayerList().canPlayerLogin(this, this.gameProfile, profilepublickey, hostname); ++ EntityPlayer s = this.server.getPlayerList().canPlayerLogin(this, this.gameProfile, profilepublickey); - if (ichatbasecomponent != null) { - this.disconnect(ichatbasecomponent); @@ -51,7 +43,7 @@ } else { this.state = LoginListener.EnumProtocolState.ACCEPTED; if (this.server.getCompressionThreshold() >= 0 && !this.connection.isMemoryConnection()) { -@@ -139,7 +155,7 @@ +@@ -139,7 +154,7 @@ EntityPlayer entityplayer = this.server.getPlayerList().getPlayer(this.gameProfile.getId()); try { @@ -60,7 +52,7 @@ if (entityplayer != null) { this.state = LoginListener.EnumProtocolState.DELAY_ACCEPT; -@@ -188,7 +204,7 @@ +@@ -188,7 +203,7 @@ public void handleHello(PacketLoginInStart packetlogininstart) { Validate.validState(this.state == LoginListener.EnumProtocolState.HELLO, "Unexpected hello packet", new Object[0]); Validate.validState(isValidUsername(packetlogininstart.name()), "Invalid characters in username", new Object[0]); @@ -69,7 +61,7 @@ GameProfile gameprofile = this.server.getSingleplayerProfile(); if (gameprofile != null && packetlogininstart.name().equalsIgnoreCase(gameprofile.getName())) { -@@ -249,6 +265,43 @@ +@@ -249,6 +264,43 @@ try { LoginListener.this.gameProfile = LoginListener.this.server.getSessionService().hasJoinedServer(new GameProfile((UUID) null, gameprofile.getName()), s, this.getAddress()); if (LoginListener.this.gameProfile != null) { @@ -113,7 +105,7 @@ LoginListener.LOGGER.info("UUID of player {} is {}", LoginListener.this.gameProfile.getName(), LoginListener.this.gameProfile.getId()); LoginListener.this.state = LoginListener.EnumProtocolState.READY_TO_ACCEPT; } else if (LoginListener.this.server.isSingleplayer()) { -@@ -268,6 +321,11 @@ +@@ -268,6 +320,11 @@ LoginListener.this.disconnect(IChatBaseComponent.translatable("multiplayer.disconnect.authservers_down")); LoginListener.LOGGER.error("Couldn't verify username because servers are unavailable"); } diff --git a/paper-server/nms-patches/net/minecraft/server/network/PacketStatusListener.patch b/paper-server/nms-patches/net/minecraft/server/network/PacketStatusListener.patch index 1e4f173157..3db9536e70 100644 --- a/paper-server/nms-patches/net/minecraft/server/network/PacketStatusListener.patch +++ b/paper-server/nms-patches/net/minecraft/server/network/PacketStatusListener.patch @@ -32,7 +32,7 @@ + CraftIconCache icon = server.server.getServerIcon(); + + ServerListPingEvent() { -+ super(((InetSocketAddress) connection.getRemoteAddress()).getAddress(), server.getMotd(), server.previewsChat(), server.getPlayerList().getMaxPlayers()); ++ super(connection.hostname, ((InetSocketAddress) connection.getRemoteAddress()).getAddress(), server.getMotd(), server.previewsChat(), server.getPlayerList().getMaxPlayers()); + } + + @Override diff --git a/paper-server/nms-patches/net/minecraft/server/players/PlayerList.patch b/paper-server/nms-patches/net/minecraft/server/players/PlayerList.patch index 163b5a2c53..c2489e9745 100644 --- a/paper-server/nms-patches/net/minecraft/server/players/PlayerList.patch +++ b/paper-server/nms-patches/net/minecraft/server/players/PlayerList.patch @@ -317,7 +317,7 @@ - @Nullable - public IChatBaseComponent canPlayerLogin(SocketAddress socketaddress, GameProfile gameprofile) { + // CraftBukkit start - Whole method, SocketAddress to LoginListener, added hostname to signature, return EntityPlayer -+ public EntityPlayer canPlayerLogin(LoginListener loginlistener, GameProfile gameprofile, ProfilePublicKey profilepublickey, String hostname) { ++ public EntityPlayer canPlayerLogin(LoginListener loginlistener, GameProfile gameprofile, ProfilePublicKey profilepublickey) { IChatMutableComponent ichatmutablecomponent; - if (this.bans.isBanned(gameprofile)) { @@ -349,7 +349,7 @@ + + EntityPlayer entity = new EntityPlayer(this.server, this.server.getLevel(World.OVERWORLD), gameprofile, profilepublickey); + Player player = entity.getBukkitEntity(); -+ PlayerLoginEvent event = new PlayerLoginEvent(player, hostname, ((java.net.InetSocketAddress) socketaddress).getAddress()); ++ PlayerLoginEvent event = new PlayerLoginEvent(player, loginlistener.connection.hostname, ((java.net.InetSocketAddress) socketaddress).getAddress()); + + if (getBans().isBanned(gameprofile) && !getBans().get(gameprofile).hasExpired()) { GameProfileBanEntry gameprofilebanentry = (GameProfileBanEntry) this.bans.get(gameprofile);