Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce session server lookups #509

Merged
merged 12 commits into from
May 18, 2024
Prev Previous commit
Next Next commit
fix(velocity): linked player textures
bridge committed May 10, 2024
commit b31791af2225b18ce1c6457e5e9a2d81a7fba73b
Original file line number Diff line number Diff line change
@@ -29,6 +29,7 @@
import java.util.Objects;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.geysermc.floodgate.api.event.skin.SkinApplyEvent.SkinData;
import org.geysermc.floodgate.util.Constants;

public class SkinDataImpl implements SkinData {
private final String value;
@@ -55,4 +56,9 @@ public static SkinData from(JsonObject data) {
public @NonNull String signature() {
return signature;
}

public static final SkinData DEFAULT_SKIN = new SkinDataImpl(
Constants.DEFAULT_MINECRAFT_JAVA_SKIN_TEXTURE,
Constants.DEFAULT_MINECRAFT_JAVA_SKIN_SIGNATURE
);
}
99 changes: 99 additions & 0 deletions core/src/main/java/org/geysermc/floodgate/util/MojangUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Copyright (c) 2019-2024 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Floodgate
*/

package org.geysermc.floodgate.util;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import lombok.NonNull;
import org.geysermc.floodgate.api.event.skin.SkinApplyEvent.SkinData;
import org.geysermc.floodgate.skin.SkinDataImpl;
import org.geysermc.floodgate.util.HttpClient.HttpResponse;

public class MojangUtils {

private static final String SESSION_SERVER =
"https://sessionserver.mojang.com/session/minecraft/profile/%s?unsigned=false";

private static final Cache<UUID, SkinData> SKIN_CACHE = CacheBuilder.newBuilder()
.expireAfterWrite(5, TimeUnit.MINUTES)
.maximumSize(500)
.build();

public static @NonNull SkinData getSkinCached(
HttpClient httpClient,
UUID playerId
) throws ExecutionException {
return SKIN_CACHE.get(playerId, () -> getSkin(httpClient, playerId));
}

public static @NonNull SkinData getSkin(HttpClient httpClient, UUID playerId) {
final HttpResponse<JsonObject> httpResponse = httpClient.get(String.format(SESSION_SERVER, playerId.toString()));

if (httpResponse.getHttpCode() != 200) {
return SkinDataImpl.DEFAULT_SKIN;
}

JsonObject response = httpResponse.getResponse();

if (response == null) {
return SkinDataImpl.DEFAULT_SKIN;
}

JsonArray properties = response.getAsJsonArray("properties");

if (properties.size() == 0) {
return SkinDataImpl.DEFAULT_SKIN;
}

for (JsonElement property : properties) {
if (!property.isJsonObject()) {
continue;
}

JsonObject propertyObject = property.getAsJsonObject();

if (!propertyObject.has("name")
|| !propertyObject.has("value")
|| !propertyObject.has("signature")
|| !propertyObject.get("name").getAsString().equals("textures")) {
continue;
}

return new SkinDataImpl(
propertyObject.get("value").getAsString(),
propertyObject.get("signature").getAsString()
);
}

return SkinDataImpl.DEFAULT_SKIN;
}
}
Original file line number Diff line number Diff line change
@@ -48,16 +48,21 @@
import io.netty.channel.Channel;
import io.netty.util.AttributeKey;
import java.lang.reflect.Field;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import net.kyori.adventure.text.Component;
import org.geysermc.floodgate.api.ProxyFloodgateApi;
import org.geysermc.floodgate.api.event.skin.SkinApplyEvent.SkinData;
import org.geysermc.floodgate.api.logger.FloodgateLogger;
import org.geysermc.floodgate.api.player.FloodgatePlayer;
import org.geysermc.floodgate.config.ProxyFloodgateConfig;
import org.geysermc.floodgate.util.Constants;
import org.geysermc.floodgate.util.HttpClient;
import org.geysermc.floodgate.util.LanguageManager;
import org.geysermc.floodgate.util.MojangUtils;

public final class VelocityListener {
private static final Field INITIAL_MINECRAFT_CONNECTION;
@@ -110,6 +115,9 @@ public final class VelocityListener {
@Named("kickMessageAttribute")
private AttributeKey<String> kickMessageAttribute;

@Inject
private HttpClient httpClient;

@Subscribe(order = PostOrder.EARLY)
public void onPreLogin(PreLoginEvent event) {
FloodgatePlayer player = null;
@@ -154,9 +162,22 @@ public void onGameProfileRequest(GameProfileRequestEvent event) {
GameProfile profile = new GameProfile(
player.getCorrectUniqueId(),
player.getCorrectUsername(),
List.of(DEFAULT_TEXTURE_PROPERTY) // Otherwise game server will try to fetch the skin from Mojang
player.isLinked() ? Collections.emptyList() : List.of(DEFAULT_TEXTURE_PROPERTY) // Otherwise game server will try to fetch the skin from Mojang
);

if (player.isLinked()) {
// Do texture lookup to session server
try {
SkinData skin = MojangUtils.getSkinCached(httpClient,
player.getJavaUniqueId());

profile.addProperty(new Property("textures", skin.value(), skin.signature()));
} catch (ExecutionException e) {
logger.error("Failed to get skin for player " + player.getJavaUniqueId() + ", applying default.", e);
profile.addProperty(DEFAULT_TEXTURE_PROPERTY);
}
}

event.setGameProfile(profile);
}
}