From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Dreeam <61569423+Dreeam-qwq@users.noreply.github.com> Date: Thu, 28 Mar 2024 13:36:09 -0400 Subject: [PATCH] Cache player profileResult This patch includes code that references the Caffeine caching library, which is licensed under the Apache License, Version 2.0. Caffeine (https://github.com/ben-manes/caffeine) Copyright (c) Ben Manes Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. diff --git a/net/minecraft/server/network/ServerLoginPacketListenerImpl.java b/net/minecraft/server/network/ServerLoginPacketListenerImpl.java index 069477e524a28b20a0289221858bdc802704a890..114b25f933c6a1b011523581a5a02a5a2c1e827e 100644 --- a/net/minecraft/server/network/ServerLoginPacketListenerImpl.java +++ b/net/minecraft/server/network/ServerLoginPacketListenerImpl.java @@ -71,6 +71,14 @@ 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 + // Leaf start - Cache player profileResult + 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(); + private static final com.github.benmanes.caffeine.cache.Cache playerSession = com.github.benmanes.caffeine.cache.Caffeine.newBuilder() + .expireAfterWrite(org.dreeam.leaf.config.modules.misc.Cache.cachePlayerProfileResultTimeout, java.util.concurrent.TimeUnit.MINUTES) + .build(); + // Leaf end - Cache player profileResult public ServerLoginPacketListenerImpl(MinecraftServer server, Connection connection, boolean transferred) { this.server = server; @@ -304,9 +312,30 @@ public class ServerLoginPacketListenerImpl implements ServerLoginPacketListener, String string1 = Objects.requireNonNull(ServerLoginPacketListenerImpl.this.requestedUsername, "Player name not initialized"); try { - ProfileResult profileResult = ServerLoginPacketListenerImpl.this.server - .getSessionService() - .hasJoinedServer(string1, string, this.getAddress()); + // Leaf start - Cache player profileResult + ProfileResult profileResult; + if (org.dreeam.leaf.config.modules.misc.Cache.cachePlayerProfileResult) { + profileResult = playerProfileResultCache.getIfPresent(string1); + + InetAddress address = this.getAddress(); + InetAddress lastAddress = playerSession.getIfPresent(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, address); + if (profileResult != null && address != null) { + playerProfileResultCache.put(string1, profileResult); + playerSession.put(string1, address); + } + } + } else { + profileResult = ServerLoginPacketListenerImpl.this.server + .getSessionService() + .hasJoinedServer(string1, string, this.getAddress()); + } + // Leaf end - Cache player profileResult if (profileResult != null) { GameProfile gameProfile = profileResult.profile(); // CraftBukkit start - fire PlayerPreLoginEvent @@ -351,6 +380,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