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

Fix login plugin messages & IP forwarding #3928

Merged
merged 7 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -386,8 +386,14 @@ public void handleLoginResponsePayload(final EngineConnection connection, final
// Client -> Server response

final int transactionId = packet.transactionId();
final ChannelBuf payload = this.bufferAllocator.buffer();
packet.payload().write((FriendlyByteBuf) payload);

final ChannelBuf payload;
if (packet.payload() != null) {
payload = this.bufferAllocator.buffer();
packet.payload().write((FriendlyByteBuf) payload);
} else {
payload = null;
}

try {
this.handleLoginResponsePayload(connection, transactionId, payload);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public ResourceLocation id() {

@Override
public void write(FriendlyByteBuf var1) {
var1.writeBytes((FriendlyByteBuf) payload);
var1.writeBytes((FriendlyByteBuf) buf);
}
}, transactionId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,22 @@
package org.spongepowered.common.mixin.api.minecraft.server.network;


import static java.util.Objects.requireNonNull;

import net.kyori.adventure.text.Component;
import net.minecraft.network.Connection;
import net.minecraft.server.network.ServerLoginPacketListenerImpl;
import org.spongepowered.api.network.ServerSideConnection;
import org.spongepowered.api.profile.GameProfile;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.common.adventure.SpongeAdventure;
import org.spongepowered.common.bridge.network.ConnectionBridge;
import org.spongepowered.common.profile.SpongeGameProfile;

import java.net.InetSocketAddress;


@Mixin(ServerLoginPacketListenerImpl.class)
public abstract class ServerLoginPacketListenerImplMixin_API implements ServerSideConnection {
Expand All @@ -44,6 +51,27 @@ public abstract class ServerLoginPacketListenerImplMixin_API implements ServerSi
@Shadow public abstract void shadow$disconnect(net.minecraft.network.chat.Component reason);
// @formatter:on

@Override
public void close() {
this.shadow$disconnect(net.minecraft.network.chat.Component.translatable("disconnect.disconnected"));
}

@Override
public void close(final Component reason) {
requireNonNull(reason, "reason");
this.shadow$disconnect(SpongeAdventure.asVanilla(reason));
}

@Override
public InetSocketAddress address() {
return ((ConnectionBridge) this.connection).bridge$getAddress();
}

@Override
public InetSocketAddress virtualHost() {
return ((ConnectionBridge) this.connection).bridge$getVirtualHost();
}

@Override
public GameProfile profile() {
return SpongeGameProfile.of(this.authenticatedProfile);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* This file is part of Sponge, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* Copyright (c) contributors
*
* 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.
*/
package org.spongepowered.common.mixin.core.network.protocol.login;

import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.protocol.login.ServerboundCustomQueryAnswerPacket;
import net.minecraft.network.protocol.login.custom.CustomQueryAnswerPayload;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(ServerboundCustomQueryAnswerPacket.class)
public abstract class ServerboundCustomQueryAnswerPacketMixin {

@Inject(method = "readUnknownPayload", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/FriendlyByteBuf;skipBytes(I)Lnet/minecraft/network/FriendlyByteBuf;"), cancellable = true)
private static void impl$onReadUnknownPayload(final FriendlyByteBuf $$0, final CallbackInfoReturnable<CustomQueryAnswerPayload> cir) {
final var payload = $$0.readNullable(buf -> new FriendlyByteBuf(buf.readBytes(buf.readableBytes())));

cir.setReturnValue(payload == null ? null : buf -> buf.writeBytes(payload.copy()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public abstract class ClientIntentionPacketMixin_IpForward {

@Redirect(method = "<init>(Lnet/minecraft/network/FriendlyByteBuf;)V",
at = @At(value = "INVOKE", target = "Lnet/minecraft/network/FriendlyByteBuf;readUtf(I)Ljava/lang/String;"))
private String bungee$patchReadStringForPortForwarding(final FriendlyByteBuf buf, final int value) {
private static String bungee$patchReadStringForPortForwarding(final FriendlyByteBuf buf, final int value) {
if (SpongeConfigs.getCommon().get().ipForwarding.mode != IpForwardingCategory.Mode.LEGACY) {
return buf.readUtf(255);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@
import net.minecraft.network.Connection;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerLoginPacketListenerImpl;
import org.objectweb.asm.Opcodes;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyArg;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.common.applaunch.config.common.IpForwardingCategory;
import org.spongepowered.common.applaunch.config.core.SpongeConfigs;
Expand All @@ -52,7 +52,6 @@ public abstract class ServerLoginPacketListenerImplMixin_IpForward {
// @formatter:off
@Shadow @Final private MinecraftServer server;
@Shadow @Final public Connection connection;
@Shadow private GameProfile gameProfile;
// @formatter:on

private boolean ipForward$sentVelocityForwardingRequest;
Expand All @@ -69,29 +68,30 @@ public abstract class ServerLoginPacketListenerImplMixin_IpForward {
}

// Bungee
@Inject(method = "handleHello",
@ModifyArg(method = "handleHello",
at = @At(
value = "FIELD",
target = "Lnet/minecraft/server/network/ServerLoginPacketListenerImpl;gameProfile:Lcom/mojang/authlib/GameProfile;",
opcode = Opcodes.PUTFIELD,
ordinal = 1,
shift = At.Shift.AFTER))
private void bungee$initUuid(final CallbackInfo ci) {
value = "INVOKE",
target = "Lnet/minecraft/server/network/ServerLoginPacketListenerImpl;startClientVerification(Lcom/mojang/authlib/GameProfile;)V",
ordinal = 1))
private GameProfile bungee$initUuid(GameProfile $$0) {
if (!this.server.usesAuthentication() && SpongeConfigs.getCommon().get().ipForwarding.mode == IpForwardingCategory.Mode.LEGACY) {
final UUID uuid;
if (((ConnectionBridge_IpForward) this.connection).bungeeBridge$getSpoofedUUID() != null) {
uuid = ((ConnectionBridge_IpForward) this.connection).bungeeBridge$getSpoofedUUID();
} else {
uuid = UUID.nameUUIDFromBytes(("OfflinePlayer:" + this.gameProfile.getName()).getBytes(Charsets.UTF_8));
uuid = UUID.nameUUIDFromBytes(("OfflinePlayer:" + $$0.getName()).getBytes(Charsets.UTF_8));
}

this.gameProfile = new GameProfile(uuid, this.gameProfile.getName());
$$0 = new GameProfile(uuid, $$0.getName());

if (((ConnectionBridge_IpForward) this.connection).bungeeBridge$getSpoofedProfile() != null) {
for (final Property property : ((ConnectionBridge_IpForward) this.connection).bungeeBridge$getSpoofedProfile()) {
this.gameProfile.getProperties().put(property.name(), property);
$$0.getProperties().put(property.name(), property);
}
}
}

return $$0;
}

}
1 change: 1 addition & 0 deletions src/mixins/resources/mixins.sponge.core.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"network.chat.StyleMixin",
"network.chat.TranslatableContentsMixin",
"network.protocol.game.ClientboundResourcePackPacketMixin",
"network.protocol.login.ServerboundCustomQueryAnswerPacketMixin",
"network.syncher.EntityDataAccessorMixin",
"network.syncher.SynchedEntityDataMixin",
"registries.BuiltInRegistriesMixin",
Expand Down
Loading