Catcher that catches anything sent to the client in reset

This commit is contained in:
Adrian Bergqvist 2022-10-18 23:25:10 +02:00
parent cc589a17d8
commit 81d22900fa
No known key found for this signature in database
GPG Key ID: FAE7D8EDE225E686
3 changed files with 46 additions and 4 deletions

View File

@ -10,6 +10,7 @@ import com.velocitypowered.proxy.config.VelocityConfiguration;
import com.velocitypowered.proxy.connection.MinecraftConnection;
import com.velocitypowered.proxy.connection.backend.VelocityServerConnection;
import com.velocitypowered.proxy.connection.client.ConnectedPlayer;
import com.velocitypowered.proxy.network.Connections;
import com.velocitypowered.proxy.protocol.StateRegistry;
import com.velocitypowered.proxy.protocol.packet.LoginPluginMessage;
import com.velocitypowered.proxy.protocol.packet.LoginPluginResponse;
@ -18,16 +19,19 @@ import com.velocitypowered.proxy.protocol.packet.ServerLoginSuccess;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import net.kyori.adventure.identity.Identity;
import net.kyori.adventure.text.Component;
import org.adde0109.ambassador.velocity.VelocityForgeClientConnectionPhase;
import org.adde0109.ambassador.velocity.VelocityForgeHandshakeSessionHandler;
import org.adde0109.ambassador.velocity.VelocityLoginPayloadManager;
import java.util.ArrayList;
import java.util.NoSuchElementException;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
public class FML2CRPMClientConnectionPhase implements VelocityForgeClientConnectionPhase {
private boolean isResettable;
private static String OUTBOUND_CATCHER_NAME = "ambassador-catcher";
//TODO: Use modData inside ConnectedPlayer instead
public byte[] modListData;
@ -64,6 +68,7 @@ public class FML2CRPMClientConnectionPhase implements VelocityForgeClientConnect
MinecraftConnection connection = player.getConnection();
connection.setSessionHandler(new VelocityForgeHandshakeSessionHandler(connection.getSessionHandler(),player));
if (connection.getState() == StateRegistry.LOGIN) {
payloadManager.sendPayload("fml:loginwrapper", Unpooled.wrappedBuffer(ForgeHandshakeUtils.generateResetPacket()));
} else {
@ -76,6 +81,7 @@ public class FML2CRPMClientConnectionPhase implements VelocityForgeClientConnect
});
this.clientPhase = null;
connection.getChannel().pipeline().addBefore(Connections.HANDLER,OUTBOUND_CATCHER_NAME,new FML2CRPMOutgoingCatcher());
}
public void complete(VelocityServer server, ConnectedPlayer player, MinecraftConnection connection) {
VelocityConfiguration configuration = (VelocityConfiguration) server.getConfiguration();
@ -92,6 +98,10 @@ public class FML2CRPMClientConnectionPhase implements VelocityForgeClientConnect
connection.setState(StateRegistry.PLAY);
connection.setSessionHandler(((VelocityForgeHandshakeSessionHandler) connection.getSessionHandler()).getOriginal());
try {
connection.getChannel().pipeline().remove(OUTBOUND_CATCHER_NAME);
} catch (NoSuchElementException ignored) {
}
}
public void handleKick(KickedFromServerEvent event) {

View File

@ -0,0 +1,35 @@
package org.adde0109.ambassador.forge;
import com.velocitypowered.proxy.protocol.packet.LoginPluginMessage;
import com.velocitypowered.proxy.protocol.packet.ServerLoginSuccess;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOutboundHandlerAdapter;
import io.netty.channel.ChannelPromise;
import java.util.*;
public class FML2CRPMOutgoingCatcher extends ChannelOutboundHandlerAdapter {
private final Map<ChannelPromise, Object> catchedPackets = Collections.synchronizedMap(new LinkedHashMap<>());
@Override
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
final Set<Map.Entry<ChannelPromise, Object>> s = catchedPackets.entrySet();
Iterator<Map.Entry<ChannelPromise, Object>> i = s.iterator();
while (catchedPackets.entrySet().iterator().hasNext()) {
final Map.Entry<ChannelPromise, Object> entry = i.next();
ctx.write(entry.getValue(),entry.getKey());
i.remove();
}
ctx.flush();
}
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
if (msg instanceof LoginPluginMessage || msg instanceof ServerLoginSuccess) {
ctx.write(msg, promise);
} else {
catchedPackets.put(promise,msg);
}
}
}

View File

@ -5,12 +5,9 @@ import com.velocitypowered.proxy.protocol.packet.LoginPluginMessage;
import com.velocitypowered.proxy.protocol.packet.LoginPluginResponse;
import io.netty.buffer.ByteBuf;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
public class VelocityLoginPayloadManager {
private final HashMap<Integer, CompletableFuture<ByteBuf>> listenerList = new HashMap<>();