1
0
mirror of https://github.com/GeyserMC/Floodgate.git synced 2026-01-06 15:42:03 +00:00

Merge branch 'dev/2.0' of https://github.com/GeyserMC/Floodgate into bungee-java-16

This commit is contained in:
Camotoy
2021-05-31 09:48:04 -04:00
9 changed files with 140 additions and 38 deletions

View File

@@ -47,6 +47,7 @@ import org.geysermc.floodgate.link.PlayerLinkLoader;
import org.geysermc.floodgate.module.ConfigLoadedModule;
import org.geysermc.floodgate.module.PostInitializeModule;
import org.geysermc.floodgate.util.PrefixCheckTask;
import org.geysermc.floodgate.util.TimeSyncerHolder;
public class FloodgatePlatform {
private static final UUID KEY = UUID.randomUUID();
@@ -97,6 +98,8 @@ public class FloodgatePlatform {
guice = guice.createChildInjector(new ConfigLoadedModule(config));
PlayerLink link = guice.getInstance(PlayerLinkLoader.class).load();
TimeSyncerHolder.init();
InstanceHolder.set(api, link, this.injector, packetHandlers, handshakeHandlers, KEY);
}

View File

@@ -51,9 +51,12 @@ import org.geysermc.floodgate.api.player.PropertyKey;
import org.geysermc.floodgate.config.FloodgateConfigHolder;
import org.geysermc.floodgate.crypto.FloodgateCipher;
import org.geysermc.floodgate.skin.SkinUploadManager;
import org.geysermc.floodgate.time.TimeSyncer;
import org.geysermc.floodgate.util.BedrockData;
import org.geysermc.floodgate.util.Constants;
import org.geysermc.floodgate.util.InvalidFormatException;
import org.geysermc.floodgate.util.LinkedPlayer;
import org.geysermc.floodgate.util.TimeSyncerHolder;
import org.geysermc.floodgate.util.Utils;
@RequiredArgsConstructor
@@ -61,7 +64,7 @@ public final class FloodgateHandshakeHandler {
private final Cache<String, Long> handleCache =
CacheBuilder.newBuilder()
.maximumSize(500)
.expireAfterWrite(1, TimeUnit.MINUTES)
.expireAfterWrite(10, TimeUnit.SECONDS)
.build();
private final HandshakeHandlersImpl handshakeHandlers;
@@ -108,8 +111,23 @@ public final class FloodgateHandshakeHandler {
// timestamp checks
long timeDifference = System.currentTimeMillis() - bedrockData.getTimestamp();
if (timeDifference > 6000 || timeDifference < 0) {
TimeSyncer timeSyncer = TimeSyncerHolder.get();
if (!timeSyncer.hasUsefulOffset()) {
logger.warn("We couldn't make sure that your system clock is accurate. " +
"This can cause issues with logging in.");
}
// the time syncer is accurate, but we have to account for some minor differences
final int errorMargin = 150; // 150ms
long timeDifference = timeSyncer.getRealMillis() - bedrockData.getTimestamp();
if (timeDifference > 6000 + errorMargin || timeDifference < -errorMargin) {
if (Constants.DEBUG_MODE || logger.isDebug()) {
logger.info("Current time: " + System.currentTimeMillis());
logger.info("Stored time: " + bedrockData.getTimestamp());
logger.info("Time offset: " + timeSyncer.getTimeOffset());
}
return callHandlerAndReturnResult(
ResultType.TIMESTAMP_DENIED,
channel, bedrockData, hostname);
@@ -117,9 +135,10 @@ public final class FloodgateHandshakeHandler {
Long cachedTimestamp = handleCache.getIfPresent(bedrockData.getXuid());
if (cachedTimestamp != null) {
// the cached timestamp is newer than the gotten timestamp
// you also can't reuse the data (the timestamp is there to prevent that as well)
if (cachedTimestamp >= bedrockData.getTimestamp()) {
// the cached timestamp should be older than the received timestamp
// and it should also not be possible to reuse the handshake
long diff = bedrockData.getTimestamp() - cachedTimestamp;
if (diff == 0 || diff < 0 && -diff > errorMargin) {
return callHandlerAndReturnResult(
ResultType.TIMESTAMP_DENIED,
channel, bedrockData, hostname);

View File

@@ -39,10 +39,12 @@ import org.geysermc.floodgate.api.handshake.HandshakeData;
import org.geysermc.floodgate.api.player.FloodgatePlayer;
import org.geysermc.floodgate.api.player.PropertyKey;
import org.geysermc.floodgate.api.player.PropertyKey.Result;
import org.geysermc.floodgate.time.TimeSyncer;
import org.geysermc.floodgate.util.BedrockData;
import org.geysermc.floodgate.util.DeviceOs;
import org.geysermc.floodgate.util.InputMode;
import org.geysermc.floodgate.util.LinkedPlayer;
import org.geysermc.floodgate.util.TimeSyncerHolder;
import org.geysermc.floodgate.util.UiProfile;
import org.geysermc.floodgate.util.Utils;
@@ -77,10 +79,7 @@ public final class FloodgatePlayerImpl implements FloodgatePlayer {
*/
@Setter private boolean login = true;
protected static FloodgatePlayerImpl from(
BedrockData data,
HandshakeData handshakeData) {
protected static FloodgatePlayerImpl from(BedrockData data, HandshakeData handshakeData) {
FloodgateApi api = InstanceHolder.getApi();
UUID javaUniqueId = Utils.getJavaUuid(data.getXuid());
@@ -109,9 +108,11 @@ public final class FloodgatePlayerImpl implements FloodgatePlayer {
}
public BedrockData toBedrockData() {
TimeSyncer timeSyncer = TimeSyncerHolder.get();
return BedrockData.of(version, username, xuid, deviceOs.ordinal(), languageCode,
uiProfile.ordinal(), inputMode.ordinal(), ip, linkedPlayer, proxy, subscribeId,
verifyCode);
verifyCode, timeSyncer);
}
@Override

View File

@@ -38,6 +38,7 @@ public final class Constants {
public static final boolean DEBUG_MODE = true;
public static final String NTP_SERVER = "time.cloudflare.com";
public static final String TIMESTAMP_DENIED_MESSAGE =
"Something isn't right with this data." +
" Try logging in again or contact a server administrator if the issue persists.";

View File

@@ -0,0 +1,44 @@
/*
* Copyright (c) 2019-2021 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 org.geysermc.floodgate.time.TimeSyncer;
public final class TimeSyncerHolder {
private static TimeSyncer timeSyncer;
public static void init() {
timeSyncer = new TimeSyncer(Constants.NTP_SERVER);
}
public static void set(TimeSyncer syncer) {
timeSyncer = syncer;
}
public static TimeSyncer get() {
return timeSyncer;
}
}

View File

@@ -33,6 +33,7 @@ import static org.geysermc.floodgate.util.ReflectionUtils.getPrefixedClass;
import static org.geysermc.floodgate.util.ReflectionUtils.makeAccessible;
import static org.geysermc.floodgate.util.ReflectionUtils.setValue;
import com.mojang.authlib.GameProfile;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.ReferenceCountUtil;
@@ -41,7 +42,6 @@ import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import org.geysermc.floodgate.api.handshake.HandshakeData;
import org.geysermc.floodgate.api.logger.FloodgateLogger;
@@ -51,8 +51,7 @@ import org.geysermc.floodgate.player.FloodgateHandshakeHandler;
import org.geysermc.floodgate.player.FloodgateHandshakeHandler.HandshakeResult;
import org.geysermc.floodgate.util.BedrockData;
import org.geysermc.floodgate.util.Constants;
import org.geysermc.floodgate.util.ReflectionUtils;
import org.geysermc.floodgate.util.SpigotUtils;
import org.geysermc.floodgate.util.ProxyUtils;
@RequiredArgsConstructor
public final class SpigotDataHandler extends ChannelInboundHandlerAdapter {
@@ -60,8 +59,6 @@ public final class SpigotDataHandler extends ChannelInboundHandlerAdapter {
private static final Class<?> HANDSHAKE_PACKET;
private static final Field HANDSHAKE_HOST;
private static final Class<?> GAME_PROFILE;
private static final Constructor<?> GAME_PROFILE_CONSTRUCTOR;
private static final Field LOGIN_PROFILE;
private static final Class<?> LOGIN_START_PACKET;
@@ -91,21 +88,9 @@ public final class SpigotDataHandler extends ChannelInboundHandlerAdapter {
LOGIN_START_PACKET = getPrefixedClass("PacketLoginInStart");
checkNotNull(LOGIN_START_PACKET, "PacketLoginInStart cannot be null");
GAME_PROFILE = ReflectionUtils.getClass("com.mojang.authlib.GameProfile");
checkNotNull(GAME_PROFILE, "GameProfile class cannot be null");
Constructor<?> gameProfileConstructor = null;
try {
gameProfileConstructor = GAME_PROFILE.getConstructor(UUID.class, String.class);
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
GAME_PROFILE_CONSTRUCTOR = gameProfileConstructor;
checkNotNull(GAME_PROFILE_CONSTRUCTOR, "GameProfileConstructor cannot be null");
LOGIN_LISTENER = getPrefixedClass("LoginListener");
checkNotNull(LOGIN_LISTENER, "LoginListener cannot be null");
LOGIN_PROFILE = getFieldOfType(LOGIN_LISTENER, GAME_PROFILE);
LOGIN_PROFILE = getFieldOfType(LOGIN_LISTENER, GameProfile.class);
checkNotNull(LOGIN_PROFILE, "Profile from LoginListener cannot be null");
INIT_UUID = getMethod(LOGIN_LISTENER, "initUUID");
checkNotNull(INIT_UUID, "initUUID from LoginListener cannot be null");
@@ -219,7 +204,7 @@ public final class SpigotDataHandler extends ChannelInboundHandlerAdapter {
}
player = result.getFloodgatePlayer();
bungeeData = SpigotUtils.isBungeeData();
bungeeData = ProxyUtils.isProxyData();
if (!bungeeData) {
// Use a spoofedUUID for initUUID (just like Bungeecord)
@@ -237,7 +222,7 @@ public final class SpigotDataHandler extends ChannelInboundHandlerAdapter {
}
// set the player his GameProfile, we can't change the username without this
Object gameProfile = GAME_PROFILE_CONSTRUCTOR.newInstance(
GameProfile gameProfile = new GameProfile(
player.getCorrectUniqueId(), player.getCorrectUsername()
);
setValue(loginListener, LOGIN_PROFILE, gameProfile);

View File

@@ -31,16 +31,39 @@ import static org.geysermc.floodgate.util.ReflectionUtils.getField;
import java.lang.reflect.Field;
@SuppressWarnings("ConstantConditions")
public final class SpigotUtils {
public final class ProxyUtils {
private static final Field IS_BUNGEE_DATA;
private static final Field IS_MODERN_FORWARDING;
static {
Class<?> spigotConfig = ReflectionUtils.getClass("org.spigotmc.SpigotConfig");
IS_BUNGEE_DATA = getField(spigotConfig, "bungee");
checkNotNull(IS_BUNGEE_DATA, "bungee field cannot be null. Are you using CraftBukkit?");
Field velocitySupport;
try {
Class<?> paperConfig = Class.forName("com.destroystokyo.paper.PaperConfig");
velocitySupport = getField(paperConfig, "velocitySupport");
} catch (ClassNotFoundException e) {
// We're not on a platform that has modern forwarding
velocitySupport = null; // NOPMD - there's really not a better way around this unless you want to use an optional
}
IS_MODERN_FORWARDING = velocitySupport;
}
public static boolean isBungeeData() {
public static boolean isProxyData() {
return isBungeeData() || isVelocitySupport();
}
private static boolean isBungeeData() {
return ReflectionUtils.getCastedValue(null, IS_BUNGEE_DATA);
}
private static boolean isVelocitySupport() {
if (IS_MODERN_FORWARDING == null) {
return false;
}
return ReflectionUtils.getCastedValue(null, IS_MODERN_FORWARDING);
}
}

View File

@@ -27,6 +27,7 @@ package org.geysermc.floodgate.addon.data;
import com.google.inject.Inject;
import com.google.inject.name.Named;
import com.velocitypowered.api.proxy.ProxyServer;
import io.netty.channel.Channel;
import io.netty.util.AttributeKey;
import org.geysermc.floodgate.api.ProxyFloodgateApi;
@@ -41,6 +42,7 @@ public final class VelocityDataAddon implements InjectorAddon {
@Inject private FloodgateHandshakeHandler handshakeHandler;
@Inject private ProxyFloodgateConfig config;
@Inject private ProxyFloodgateApi api;
@Inject private ProxyServer proxy;
@Inject private FloodgateLogger logger;
@Inject
@@ -64,7 +66,7 @@ public final class VelocityDataAddon implements InjectorAddon {
if (toServer) {
channel.pipeline().addAfter(
packetEncoder, "floodgate_data_handler",
new VelocityServerDataHandler(config, api)
new VelocityServerDataHandler(config, api, proxy)
);
return;
}

View File

@@ -35,13 +35,13 @@ import static org.geysermc.floodgate.util.ReflectionUtils.invoke;
import static org.geysermc.floodgate.util.ReflectionUtils.setValue;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ProxyServer;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageEncoder;
import io.netty.util.ReferenceCountUtil;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.geysermc.floodgate.api.ProxyFloodgateApi;
import org.geysermc.floodgate.api.player.FloodgatePlayer;
import org.geysermc.floodgate.config.ProxyFloodgateConfig;
@@ -49,11 +49,11 @@ import org.geysermc.floodgate.player.FloodgatePlayerImpl;
import org.geysermc.floodgate.util.BedrockData;
@SuppressWarnings("ConstantConditions")
@RequiredArgsConstructor
public final class VelocityServerDataHandler extends MessageToMessageEncoder<Object> {
private static final Class<?> HANDSHAKE_PACKET;
private static final Field HANDSHAKE_ADDRESS;
private static final Method GET_ASSOCIATION;
private static final Method GET_FORWARDING_MODE;
private static final Method GET_PLAYER;
static {
@@ -68,6 +68,11 @@ public final class VelocityServerDataHandler extends MessageToMessageEncoder<Obj
GET_ASSOCIATION = getMethod(minecraftConnection, "getAssociation");
checkNotNull(GET_ASSOCIATION, "getAssociation in MinecraftConnection cannot be null");
Class<?> configClass = getPrefixedClass("config.VelocityConfiguration");
GET_FORWARDING_MODE = getMethod(configClass, "getPlayerInfoForwardingMode");
checkNotNull(GET_FORWARDING_MODE, "getPlayerInfoForwardingMode in VelocityConfiguration cannot be null");
Class<?> serverConnection = getPrefixedClass("connection.backend.VelocityServerConnection");
GET_PLAYER = getMethod(serverConnection, "getPlayer");
@@ -76,9 +81,20 @@ public final class VelocityServerDataHandler extends MessageToMessageEncoder<Obj
private final ProxyFloodgateConfig config;
private final ProxyFloodgateApi api;
private final boolean isModernForwarding;
//private final AttributeKey<FloodgatePlayer> playerAttribute;
private boolean done;
public VelocityServerDataHandler(ProxyFloodgateConfig config,
ProxyFloodgateApi api,
ProxyServer proxy) {
this.config = config;
this.api = api;
Enum<?> forwardingMode = castedInvoke(proxy.getConfiguration(), GET_FORWARDING_MODE);
this.isModernForwarding = "MODERN".equals(forwardingMode.name());
}
@Override
protected void encode(ChannelHandlerContext ctx, Object packet, List<Object> out) {
ReferenceCountUtil.retain(packet);
@@ -122,8 +138,16 @@ public final class VelocityServerDataHandler extends MessageToMessageEncoder<Obj
// use the same system that we use on bungee, our data goes before all the other data
int addressFinished = address.indexOf('\0');
String originalAddress = address.substring(0, addressFinished);
String remaining = address.substring(addressFinished);
String originalAddress;
String remaining;
if (isModernForwarding && addressFinished == -1) {
// There is no additional data to hook onto
originalAddress = address;
remaining = "";
} else {
originalAddress = address.substring(0, addressFinished);
remaining = address.substring(addressFinished);
}
setValue(packet, HANDSHAKE_ADDRESS, originalAddress + '\0' + encryptedData + remaining);