From 01bf48b350afae70af0e244a5f67c2057143544f Mon Sep 17 00:00:00 2001 From: Dreeam <61569423+Dreeam-qwq@users.noreply.github.com> Date: Thu, 5 Jun 2025 02:29:54 +0800 Subject: [PATCH] Valid session if current session is invalid --- .../0048-Cache-player-profileResult.patch | 51 ++++++++++++++++--- ...0054-Configurable-connection-message.patch | 6 +-- ...t-place-player-if-the-server-is-full.patch | 4 +- .../0100-Smooth-teleport-config.patch | 4 +- ...-SparklyPaper-Parallel-world-ticking.patch | 6 +-- .../0165-Async-switch-connection-state.patch | 4 +- 6 files changed, 57 insertions(+), 18 deletions(-) diff --git a/leaf-server/minecraft-patches/features/0048-Cache-player-profileResult.patch b/leaf-server/minecraft-patches/features/0048-Cache-player-profileResult.patch index 9c7603ff..aa59e724 100644 --- a/leaf-server/minecraft-patches/features/0048-Cache-player-profileResult.patch +++ b/leaf-server/minecraft-patches/features/0048-Cache-player-profileResult.patch @@ -5,10 +5,10 @@ Subject: [PATCH] Cache player profileResult diff --git a/net/minecraft/server/network/ServerLoginPacketListenerImpl.java b/net/minecraft/server/network/ServerLoginPacketListenerImpl.java -index 069477e524a28b20a0289221858bdc802704a890..49f1743db193be1f10bfe6419231eb682e1068f7 100644 +index 069477e524a28b20a0289221858bdc802704a890..c25f5b92b16f45b960d9b405b2fe1c40ec4e1124 100644 --- a/net/minecraft/server/network/ServerLoginPacketListenerImpl.java +++ b/net/minecraft/server/network/ServerLoginPacketListenerImpl.java -@@ -71,6 +71,11 @@ public class ServerLoginPacketListenerImpl implements ServerLoginPacketListener, +@@ -71,6 +71,12 @@ public class ServerLoginPacketListenerImpl implements ServerLoginPacketListener, private net.minecraft.server.level.ServerPlayer player; // CraftBukkit public boolean iKnowThisMayNotBeTheBestIdeaButPleaseDisableUsernameValidation = false; // Paper - username validation overriding private int velocityLoginMessageId = -1; // Paper - Add Velocity IP Forwarding Support @@ -16,11 +16,12 @@ index 069477e524a28b20a0289221858bdc802704a890..49f1743db193be1f10bfe6419231eb68 + private static final com.github.benmanes.caffeine.cache.Cache playerProfileResultCache = com.github.benmanes.caffeine.cache.Caffeine.newBuilder() + .expireAfterWrite(org.dreeam.leaf.config.modules.misc.Cache.cachePlayerProfileResultTimeout, java.util.concurrent.TimeUnit.MINUTES) + .build(); ++ public static final java.util.Map playerSession = new java.util.concurrent.ConcurrentHashMap<>(); + // Leaf end - Cache player profileResult public ServerLoginPacketListenerImpl(MinecraftServer server, Connection connection, boolean transferred) { this.server = server; -@@ -304,9 +309,25 @@ public class ServerLoginPacketListenerImpl implements ServerLoginPacketListener, +@@ -304,9 +310,30 @@ public class ServerLoginPacketListenerImpl implements ServerLoginPacketListener, String string1 = Objects.requireNonNull(ServerLoginPacketListenerImpl.this.requestedUsername, "Player name not initialized"); try { @@ -29,15 +30,20 @@ index 069477e524a28b20a0289221858bdc802704a890..49f1743db193be1f10bfe6419231eb68 - .hasJoinedServer(string1, string, this.getAddress()); + // Leaf start - Cache player profileResult + ProfileResult profileResult; -+ if (false) { // TODO ++ if (org.dreeam.leaf.config.modules.misc.Cache.cachePlayerProfileResult) { + profileResult = playerProfileResultCache.getIfPresent(string1); + -+ if (profileResult == null) { ++ InetAddress address = this.getAddress(); ++ InetAddress lastAddress = playerSession.get(string1); ++ if (isInvalidSession(address, lastAddress)) { ++ // Send request to mojang server to verify session ++ // Result will be null if is invalid and will do disconnect logic below + profileResult = ServerLoginPacketListenerImpl.this.server + .getSessionService() -+ .hasJoinedServer(string1, string, this.getAddress()); ++ .hasJoinedServer(string1, string, address); + if (profileResult != null) { + playerProfileResultCache.put(string1, profileResult); ++ playerSession.put(string1, address); + } + } + } else { @@ -49,3 +55,36 @@ index 069477e524a28b20a0289221858bdc802704a890..49f1743db193be1f10bfe6419231eb68 if (profileResult != null) { GameProfile gameProfile = profileResult.profile(); // CraftBukkit start - fire PlayerPreLoginEvent +@@ -351,6 +378,20 @@ public class ServerLoginPacketListenerImpl implements ServerLoginPacketListener, + // Paper end - Cache authenticator threads + } + ++ // Leaf start - Cache player profileResult ++ private static boolean isInvalidSession(@org.jetbrains.annotations.Nullable InetAddress currAddress, @org.jetbrains.annotations.Nullable InetAddress lastAddress) { ++ // Invalid address or non-public IP address ++ if (currAddress == null || ++ currAddress.isAnyLocalAddress() || ++ currAddress.isLinkLocalAddress() || ++ currAddress.isLoopbackAddress() || ++ currAddress.isSiteLocalAddress()) { ++ return true; ++ } ++ return !currAddress.equals(lastAddress); ++ } ++ // Leaf end - Cache player profileResult ++ + // CraftBukkit start + private GameProfile callPlayerPreLoginEvents(GameProfile gameprofile) throws Exception { // Paper - Add more fields to AsyncPlayerPreLoginEvent + // Paper start - Add Velocity IP Forwarding Support +diff --git a/net/minecraft/server/players/PlayerList.java b/net/minecraft/server/players/PlayerList.java +index e8683f45823cac55e3e68ccc500f10f0632e72fd..f71debc88c1d16c09546c026817aa192faac8fc6 100644 +--- a/net/minecraft/server/players/PlayerList.java ++++ b/net/minecraft/server/players/PlayerList.java +@@ -669,6 +669,7 @@ public abstract class PlayerList { + // Paper end - Fix kick event leave message not being sent + org.purpurmc.purpur.task.BossBarTask.removeFromAll(player.getBukkitEntity()); // Purpur - Implement TPSBar + net.minecraft.server.network.ServerGamePacketListenerImpl.afkCooldown.remove(player.getBukkitEntity().getUniqueId()); // Leaf - Improve Purpur AFK system ++ net.minecraft.server.network.ServerLoginPacketListenerImpl.playerSession.remove(player.getBukkitEntity().getName()); // Leaf - Cache player profileResult + ServerLevel serverLevel = player.serverLevel(); + player.awardStat(Stats.LEAVE_GAME); + // CraftBukkit start - Quitting must be before we do final save of data, in case plugins need to modify it diff --git a/leaf-server/minecraft-patches/features/0054-Configurable-connection-message.patch b/leaf-server/minecraft-patches/features/0054-Configurable-connection-message.patch index 529a69d4..e16f7d4a 100644 --- a/leaf-server/minecraft-patches/features/0054-Configurable-connection-message.patch +++ b/leaf-server/minecraft-patches/features/0054-Configurable-connection-message.patch @@ -5,7 +5,7 @@ Subject: [PATCH] Configurable connection message diff --git a/net/minecraft/server/players/PlayerList.java b/net/minecraft/server/players/PlayerList.java -index e8683f45823cac55e3e68ccc500f10f0632e72fd..5c88d51920481235145bb7fd8cf148607b2dbed0 100644 +index f71debc88c1d16c09546c026817aa192faac8fc6..4fa7952c30408e898cc2e3ce41d5b0f44f19ae31 100644 --- a/net/minecraft/server/players/PlayerList.java +++ b/net/minecraft/server/players/PlayerList.java @@ -434,7 +434,7 @@ public abstract class PlayerList { @@ -26,7 +26,7 @@ index e8683f45823cac55e3e68ccc500f10f0632e72fd..5c88d51920481235145bb7fd8cf14860 joinMessage = io.papermc.paper.adventure.PaperAdventure.asVanilla(jm); // Paper - Adventure this.server.getPlayerList().broadcastSystemMessage(joinMessage, false); // Paper - Adventure } -@@ -677,7 +677,7 @@ public abstract class PlayerList { +@@ -678,7 +678,7 @@ public abstract class PlayerList { player.closeContainer(org.bukkit.event.inventory.InventoryCloseEvent.Reason.DISCONNECT); // Paper - Inventory close reason } @@ -35,7 +35,7 @@ index e8683f45823cac55e3e68ccc500f10f0632e72fd..5c88d51920481235145bb7fd8cf14860 this.cserver.getPluginManager().callEvent(playerQuitEvent); player.getBukkitEntity().disconnect(playerQuitEvent.getQuitMessage()); -@@ -1673,4 +1673,38 @@ public abstract class PlayerList { +@@ -1674,4 +1674,38 @@ public abstract class PlayerList { public boolean isAllowCommandsForAllPlayers() { return this.allowCommandsForAllPlayers; } diff --git a/leaf-server/minecraft-patches/features/0073-Do-not-place-player-if-the-server-is-full.patch b/leaf-server/minecraft-patches/features/0073-Do-not-place-player-if-the-server-is-full.patch index 698b73a2..ea9b96af 100644 --- a/leaf-server/minecraft-patches/features/0073-Do-not-place-player-if-the-server-is-full.patch +++ b/leaf-server/minecraft-patches/features/0073-Do-not-place-player-if-the-server-is-full.patch @@ -6,7 +6,7 @@ Subject: [PATCH] Do not place player if the server is full Fix https://github.com/PaperMC/Paper/issues/10668 diff --git a/net/minecraft/server/players/PlayerList.java b/net/minecraft/server/players/PlayerList.java -index c247b1dd9504b10ea73ec3bd96d2bf9e48fabf3e..119ffa8f10bdcc27ff4b7dc4e1ef18212607c4bf 100644 +index 4fa7952c30408e898cc2e3ce41d5b0f44f19ae31..6be59ddf2584455de0955b34d8ba1a053103f684 100644 --- a/net/minecraft/server/players/PlayerList.java +++ b/net/minecraft/server/players/PlayerList.java @@ -341,6 +341,13 @@ public abstract class PlayerList { @@ -23,7 +23,7 @@ index c247b1dd9504b10ea73ec3bd96d2bf9e48fabf3e..119ffa8f10bdcc27ff4b7dc4e1ef1821 org.bukkit.Location loc = ev.getSpawnLocation(); serverLevel = ((org.bukkit.craftbukkit.CraftWorld) loc.getWorld()).getHandle(); -@@ -835,7 +842,7 @@ public abstract class PlayerList { +@@ -836,7 +843,7 @@ public abstract class PlayerList { // return this.players.size() >= this.maxPlayers && !this.canBypassPlayerLimit(gameProfile) // ? Component.translatable("multiplayer.disconnect.server_full") // : null; diff --git a/leaf-server/minecraft-patches/features/0100-Smooth-teleport-config.patch b/leaf-server/minecraft-patches/features/0100-Smooth-teleport-config.patch index aef1c4e6..7e86b89e 100644 --- a/leaf-server/minecraft-patches/features/0100-Smooth-teleport-config.patch +++ b/leaf-server/minecraft-patches/features/0100-Smooth-teleport-config.patch @@ -30,10 +30,10 @@ index 4f01b53bf801f99253efd27df6216912705d18af..82a1715fea41e6a41c4ff441ea89f424 level.addDuringTeleport(this); this.triggerDimensionChangeTriggers(serverLevel); diff --git a/net/minecraft/server/players/PlayerList.java b/net/minecraft/server/players/PlayerList.java -index 75fb49f1596f475278d12c8c7aea9ad4952b6056..b17c8a2f5294ac28cc05fb05c84a041b2c6c8721 100644 +index 6be59ddf2584455de0955b34d8ba1a053103f684..66cb015de63949ca5add9dc3456db9c638b78048 100644 --- a/net/minecraft/server/players/PlayerList.java +++ b/net/minecraft/server/players/PlayerList.java -@@ -955,11 +955,11 @@ public abstract class PlayerList { +@@ -956,11 +956,11 @@ public abstract class PlayerList { byte b = (byte)(keepInventory ? 1 : 0); ServerLevel serverLevel = serverPlayer.serverLevel(); LevelData levelData = serverLevel.getLevelData(); diff --git a/leaf-server/minecraft-patches/features/0134-SparklyPaper-Parallel-world-ticking.patch b/leaf-server/minecraft-patches/features/0134-SparklyPaper-Parallel-world-ticking.patch index 2b087735..5e1160e4 100644 --- a/leaf-server/minecraft-patches/features/0134-SparklyPaper-Parallel-world-ticking.patch +++ b/leaf-server/minecraft-patches/features/0134-SparklyPaper-Parallel-world-ticking.patch @@ -867,7 +867,7 @@ index 8362def0dc61496a087bd859052bd80ebba83185..09f517059aa47ca67329bc913243d4fd // Paper end - Inventory close reason this.connection.send(new ClientboundContainerClosePacket(this.containerMenu.containerId)); diff --git a/net/minecraft/server/players/PlayerList.java b/net/minecraft/server/players/PlayerList.java -index b17c8a2f5294ac28cc05fb05c84a041b2c6c8721..3591de34443069f3f163f8d17df6372c3068611d 100644 +index 66cb015de63949ca5add9dc3456db9c638b78048..1009421bfd289734b50a528bfca8d957e4610159 100644 --- a/net/minecraft/server/players/PlayerList.java +++ b/net/minecraft/server/players/PlayerList.java @@ -252,6 +252,8 @@ public abstract class PlayerList { @@ -879,7 +879,7 @@ index b17c8a2f5294ac28cc05fb05c84a041b2c6c8721..3591de34443069f3f163f8d17df6372c player.isRealPlayer = true; // Paper player.loginTime = System.currentTimeMillis(); // Paper - Replace OfflinePlayer#getLastPlayed GameProfile gameProfile = player.getGameProfile(); -@@ -891,6 +893,15 @@ public abstract class PlayerList { +@@ -892,6 +894,15 @@ public abstract class PlayerList { return this.respawn(player, keepInventory, reason, eventReason, null); } public ServerPlayer respawn(ServerPlayer player, boolean keepInventory, Entity.RemovalReason reason, org.bukkit.event.player.PlayerRespawnEvent.RespawnReason eventReason, org.bukkit.Location location) { @@ -895,7 +895,7 @@ index b17c8a2f5294ac28cc05fb05c84a041b2c6c8721..3591de34443069f3f163f8d17df6372c player.stopRiding(); // CraftBukkit this.players.remove(player); this.playersByName.remove(player.getScoreboardName().toLowerCase(java.util.Locale.ROOT)); // Spigot -@@ -902,6 +913,7 @@ public abstract class PlayerList { +@@ -903,6 +914,7 @@ public abstract class PlayerList { ServerPlayer serverPlayer = player; Level fromWorld = player.level(); player.wonGame = false; diff --git a/leaf-server/minecraft-patches/features/0165-Async-switch-connection-state.patch b/leaf-server/minecraft-patches/features/0165-Async-switch-connection-state.patch index d02cdc9f..80676c50 100644 --- a/leaf-server/minecraft-patches/features/0165-Async-switch-connection-state.patch +++ b/leaf-server/minecraft-patches/features/0165-Async-switch-connection-state.patch @@ -118,10 +118,10 @@ index 2e9eb04c7c4342393c05339906c267bca9ff29b1..53b9daa909c2b89046d5af515e17afe0 try { PlayerList playerList = this.server.getPlayerList(); diff --git a/net/minecraft/server/network/ServerLoginPacketListenerImpl.java b/net/minecraft/server/network/ServerLoginPacketListenerImpl.java -index 49f1743db193be1f10bfe6419231eb682e1068f7..8cbfe4abca6a3962376f723b94de86b4cab9d9e2 100644 +index c25f5b92b16f45b960d9b405b2fe1c40ec4e1124..005fee891cdabc7105033be051389557631fb6d2 100644 --- a/net/minecraft/server/network/ServerLoginPacketListenerImpl.java +++ b/net/minecraft/server/network/ServerLoginPacketListenerImpl.java -@@ -472,11 +472,31 @@ public class ServerLoginPacketListenerImpl implements ServerLoginPacketListener, +@@ -492,11 +492,31 @@ public class ServerLoginPacketListenerImpl implements ServerLoginPacketListener, this.disconnect(ServerCommonPacketListenerImpl.DISCONNECT_UNEXPECTED_QUERY); }