Skip to content

Commit

Permalink
Updated to NetMinecraft 3.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
RaphiMC committed Sep 12, 2024
1 parent 36b276f commit 7807407
Show file tree
Hide file tree
Showing 22 changed files with 213 additions and 509 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ dependencies {
exclude group: "com.google.code.gson", module: "gson"
}
include "net.lenni0451.commons:swing:1.6.0"
include("net.raphimc.netminecraft:all:2.5.1-SNAPSHOT") {
include("net.raphimc.netminecraft:all:3.0.0-SNAPSHOT") {
exclude group: "com.google.code.gson", module: "gson"
}
include("net.raphimc:MinecraftAuth:4.1.0") {
include("net.raphimc:MinecraftAuth:4.1.1-SNAPSHOT") {
exclude group: "com.google.code.gson", module: "gson"
exclude group: "org.slf4j", module: "slf4j-api"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import io.netty.handler.codec.haproxy.HAProxyMessageDecoder;
import net.raphimc.netminecraft.constants.MCPipeline;
import net.raphimc.netminecraft.netty.connection.MinecraftChannelInitializer;
import net.raphimc.netminecraft.packet.registry.PacketRegistryUtil;
import net.raphimc.netminecraft.packet.registry.DefaultPacketRegistry;
import net.raphimc.viaproxy.ViaProxy;
import net.raphimc.viaproxy.plugins.events.Client2ProxyChannelInitializeEvent;
import net.raphimc.viaproxy.plugins.events.types.ITyped;
Expand Down Expand Up @@ -56,7 +56,7 @@ protected void initChannel(Channel channel) {
}

super.initChannel(channel);
channel.attr(MCPipeline.PACKET_REGISTRY_ATTRIBUTE_KEY).set(PacketRegistryUtil.getHandshakingRegistry(false));
channel.attr(MCPipeline.PACKET_REGISTRY_ATTRIBUTE_KEY).set(new DefaultPacketRegistry(false, -1));

if (ViaProxy.EVENT_MANAGER.call(new Client2ProxyChannelInitializeEvent(ITyped.Type.POST, channel, false)).isCancelled()) {
channel.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import io.netty.channel.SimpleChannelInboundHandler;
import net.raphimc.netminecraft.constants.ConnectionState;
import net.raphimc.netminecraft.constants.IntendedState;
import net.raphimc.netminecraft.packet.IPacket;
import net.raphimc.netminecraft.packet.Packet;
import net.raphimc.netminecraft.packet.impl.handshaking.C2SHandshakingClientIntentionPacket;
import net.raphimc.viabedrock.api.BedrockProtocolVersion;
import net.raphimc.vialegacy.api.LegacyProtocolVersion;
Expand Down Expand Up @@ -63,7 +63,7 @@
import java.util.function.Supplier;
import java.util.regex.Pattern;

public class Client2ProxyHandler extends SimpleChannelInboundHandler<IPacket> {
public class Client2ProxyHandler extends SimpleChannelInboundHandler<Packet> {

private ProxyConnection proxyConnection;

Expand All @@ -87,7 +87,7 @@ public void channelInactive(ChannelHandlerContext ctx) throws Exception {
}

@Override
protected void channelRead0(ChannelHandlerContext ctx, IPacket packet) throws Exception {
protected void channelRead0(ChannelHandlerContext ctx, Packet packet) throws Exception {
if (this.proxyConnection.isClosed()) return;

if (this.proxyConnection.getC2pConnectionState() == ConnectionState.HANDSHAKING) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@
import net.raphimc.minecraftauth.step.bedrock.StepMCChain;
import net.raphimc.minecraftauth.step.java.StepPlayerCertificates;
import net.raphimc.netminecraft.packet.PacketTypes;
import net.raphimc.netminecraft.packet.impl.login.C2SLoginHelloPacket1_19_3;
import net.raphimc.netminecraft.packet.impl.login.C2SLoginHelloPacket1_20_2;
import net.raphimc.netminecraft.packet.impl.login.C2SLoginKeyPacket1_19;
import net.raphimc.netminecraft.packet.impl.login.C2SLoginHelloPacket;
import net.raphimc.netminecraft.packet.impl.login.C2SLoginKeyPacket;
import net.raphimc.viabedrock.api.BedrockProtocolVersion;
import net.raphimc.viabedrock.protocol.storage.AuthChainData;
import net.raphimc.viaproxy.ViaProxy;
Expand Down Expand Up @@ -80,7 +79,7 @@ public static void fillPlayerData(final ProxyConnection proxyConnection) {
if (proxyConnection.getClientVersion().equals(ProtocolVersion.v1_19)) {
loginHelloKeySignature = playerCertificates.getLegacyPublicKeySignature();
}
proxyConnection.setLoginHelloPacket(new C2SLoginHelloPacket1_20_2(proxyConnection.getGameProfile().getName(), expiresAt, publicKey, loginHelloKeySignature, proxyConnection.getGameProfile().getId()));
proxyConnection.setLoginHelloPacket(new C2SLoginHelloPacket(proxyConnection.getGameProfile().getName(), expiresAt, publicKey, loginHelloKeySignature, proxyConnection.getGameProfile().getId()));

user.put(new ChatSession1_19_0(uuid, privateKey, new ProfileKey(expiresAtMillis, publicKeyBytes, playerCertificates.getLegacyPublicKeySignature())));
user.put(new ChatSession1_19_1(uuid, privateKey, new ProfileKey(expiresAtMillis, publicKeyBytes, keySignature)));
Expand All @@ -101,9 +100,7 @@ public static void fillPlayerData(final ProxyConnection proxyConnection) {
}

proxyConnection.getLoginHelloPacket().name = proxyConnection.getGameProfile().getName();
if (proxyConnection.getLoginHelloPacket() instanceof C2SLoginHelloPacket1_19_3) {
((C2SLoginHelloPacket1_19_3) proxyConnection.getLoginHelloPacket()).uuid = proxyConnection.getGameProfile().getId();
}
proxyConnection.getLoginHelloPacket().uuid = proxyConnection.getGameProfile().getId();
}

public static void joinServer(final String serverIdHash, final ProxyConnection proxyConnection) throws InterruptedException, ExecutionException {
Expand All @@ -127,7 +124,7 @@ public static void joinServer(final String serverIdHash, final ProxyConnection p
}
}

public static void signNonce(final byte[] nonce, final C2SLoginKeyPacket1_19 packet, final ProxyConnection proxyConnection) throws InterruptedException, ExecutionException, SignatureException {
public static void signNonce(final byte[] nonce, final C2SLoginKeyPacket packet, final ProxyConnection proxyConnection) throws InterruptedException, ExecutionException, SignatureException {
Logger.u_info("auth", proxyConnection, "Requesting nonce signature");
final UserConnection user = proxyConnection.getUserConnection();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
import com.viaversion.viaversion.util.Key;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFutureListener;
import net.raphimc.netminecraft.packet.Packet;
import net.raphimc.netminecraft.packet.PacketTypes;
import net.raphimc.netminecraft.packet.UnknownPacket;
import net.raphimc.netminecraft.packet.impl.common.S2CCustomPayloadPacket;
import net.raphimc.viaproxy.proxy.session.ProxyConnection;

import java.util.List;

public class BrandCustomPayloadPacketHandler extends CustomPayloadPacketHandler {
public class BrandCustomPayloadPacketHandler extends PacketHandler {

private static final String BRAND_CHANNEL = "minecraft:brand";
private static final String LEGACY_BRAND_CHANNEL = "MC|Brand";
Expand All @@ -38,26 +40,29 @@ public BrandCustomPayloadPacketHandler(ProxyConnection proxyConnection) {
}

@Override
public ByteBuf handleP2S(UnknownPacket packet, String channel, ByteBuf data, List<ChannelFutureListener> listeners) throws Exception {
if (Key.namespaced(channel).equals(BRAND_CHANNEL) || channel.equals(LEGACY_BRAND_CHANNEL)) {
String brand;
try {
brand = PacketTypes.readString(data, Short.MAX_VALUE);
} catch (Exception e) {
if (this.proxyConnection.getServerVersion().newerThan(ProtocolVersion.v1_20)) {
throw e;
} else { // <=1.20 clients ignore errors
brand = "Unknown";
public boolean handleP2S(Packet packet, List<ChannelFutureListener> listeners) throws Exception {
if (packet instanceof S2CCustomPayloadPacket customPayloadPacket) {
if (Key.namespaced(customPayloadPacket.channel).equals(BRAND_CHANNEL) || customPayloadPacket.channel.equals(LEGACY_BRAND_CHANNEL)) {
final ByteBuf data = Unpooled.wrappedBuffer(customPayloadPacket.data);
String brand;
try {
brand = PacketTypes.readString(data, Short.MAX_VALUE);
} catch (Exception e) {
if (this.proxyConnection.getServerVersion().newerThan(ProtocolVersion.v1_20)) {
throw e;
} else { // <=1.20 clients ignore errors
brand = "Unknown";
}
}
}
final String newBrand = "ViaProxy (" + this.proxyConnection.getClientVersion().getName() + ") -> " + brand + " §r(" + this.proxyConnection.getServerVersion().getName() + ")";
final String newBrand = "ViaProxy (" + this.proxyConnection.getClientVersion().getName() + ") -> " + brand + " §r(" + this.proxyConnection.getServerVersion().getName() + ")";

final ByteBuf newData = Unpooled.buffer();
PacketTypes.writeString(newData, newBrand);
return newData;
final ByteBuf newData = Unpooled.buffer();
PacketTypes.writeString(newData, newBrand);
customPayloadPacket.data = ByteBufUtil.getBytes(newData);
}
}

return super.handleP2S(packet, channel, data, listeners);
return super.handleP2S(packet, listeners);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import net.raphimc.netminecraft.constants.ConnectionState;
import net.raphimc.netminecraft.constants.MCPackets;
import net.raphimc.netminecraft.constants.MCPipeline;
import net.raphimc.netminecraft.packet.IPacket;
import net.raphimc.netminecraft.packet.Packet;
import net.raphimc.netminecraft.packet.PacketTypes;
import net.raphimc.netminecraft.packet.UnknownPacket;
import net.raphimc.viaproxy.proxy.session.ProxyConnection;
Expand All @@ -51,7 +51,7 @@ public ChatSignaturePacketHandler(ProxyConnection proxyConnection) {
}

@Override
public boolean handleC2P(IPacket packet, List<ChannelFutureListener> listeners) throws Exception {
public boolean handleC2P(Packet packet, List<ChannelFutureListener> listeners) throws Exception {
if (packet instanceof UnknownPacket unknownPacket && this.proxyConnection.getC2pConnectionState() == ConnectionState.PLAY) {
final UserConnection user = this.proxyConnection.getUserConnection();

Expand Down Expand Up @@ -86,7 +86,7 @@ public boolean handleC2P(IPacket packet, List<ChannelFutureListener> listeners)
}

@Override
public boolean handleP2S(IPacket packet, List<ChannelFutureListener> listeners) {
public boolean handleP2S(Packet packet, List<ChannelFutureListener> listeners) {
if (packet instanceof UnknownPacket unknownPacket && this.proxyConnection.getC2pConnectionState() == ConnectionState.PLAY) {
final UserConnection user = this.proxyConnection.getUserConnection();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,12 @@
package net.raphimc.viaproxy.proxy.packethandler;

import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFutureListener;
import net.raphimc.netminecraft.constants.ConnectionState;
import net.raphimc.netminecraft.constants.MCPackets;
import net.raphimc.netminecraft.constants.MCPipeline;
import net.raphimc.netminecraft.packet.IPacket;
import net.raphimc.netminecraft.packet.PacketTypes;
import net.raphimc.netminecraft.packet.UnknownPacket;
import net.raphimc.netminecraft.packet.Packet;
import net.raphimc.netminecraft.packet.impl.login.S2CLoginCompressionPacket;
import net.raphimc.netminecraft.packet.impl.login.S2CLoginGameProfilePacket1_7;
import net.raphimc.netminecraft.packet.impl.login.S2CLoginGameProfilePacket;
import net.raphimc.netminecraft.packet.impl.play.S2CPlaySetCompressionPacket;
import net.raphimc.viaproxy.ViaProxy;
import net.raphimc.viaproxy.proxy.session.ProxyConnection;
import net.raphimc.viaproxy.proxy.util.ChannelUtil;
Expand All @@ -37,26 +32,16 @@

public class CompressionPacketHandler extends PacketHandler {

private final int setCompressionId;

public CompressionPacketHandler(ProxyConnection proxyConnection) {
super(proxyConnection);

this.setCompressionId = MCPackets.S2C_SET_COMPRESSION.getId(proxyConnection.getClientVersion().getVersion());
}

@Override
public boolean handleP2S(IPacket packet, List<ChannelFutureListener> listeners) {
if (packet instanceof UnknownPacket unknownPacket && this.proxyConnection.getC2pConnectionState() == ConnectionState.PLAY) {
if (unknownPacket.packetId == this.setCompressionId) {
final ByteBuf data = Unpooled.wrappedBuffer(unknownPacket.data);
final int compressionThreshold = PacketTypes.readVarInt(data); // compression threshold
this.proxyConnection.getChannel().attr(MCPipeline.COMPRESSION_THRESHOLD_ATTRIBUTE_KEY).set(compressionThreshold);
data.release();

return false;
}
} else if (packet instanceof S2CLoginGameProfilePacket1_7) {
public boolean handleP2S(Packet packet, List<ChannelFutureListener> listeners) {
if (packet instanceof S2CPlaySetCompressionPacket setCompressionPacket) {
this.proxyConnection.getChannel().attr(MCPipeline.COMPRESSION_THRESHOLD_ATTRIBUTE_KEY).set(setCompressionPacket.compressionThreshold);
return false;
} else if (packet instanceof S2CLoginGameProfilePacket) {
if (this.proxyConnection.getClientVersion().newerThanOrEqualTo(ProtocolVersion.v1_8)) {
if (ViaProxy.getConfig().getCompressionThreshold() > -1 && this.proxyConnection.getC2P().attr(MCPipeline.COMPRESSION_THRESHOLD_ATTRIBUTE_KEY).get() == -1) {
ChannelUtil.disableAutoRead(this.proxyConnection.getChannel());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@

import io.netty.channel.ChannelFutureListener;
import net.raphimc.netminecraft.constants.ConnectionState;
import net.raphimc.netminecraft.constants.MCPackets;
import net.raphimc.netminecraft.packet.IPacket;
import net.raphimc.netminecraft.packet.UnknownPacket;
import net.raphimc.netminecraft.packet.Packet;
import net.raphimc.netminecraft.packet.impl.configuration.C2SConfigFinishConfigurationPacket;
import net.raphimc.netminecraft.packet.impl.configuration.S2CConfigFinishConfigurationPacket;
import net.raphimc.netminecraft.packet.impl.login.C2SLoginAcknowledgedPacket;
import net.raphimc.netminecraft.packet.impl.play.C2SPlayConfigurationAcknowledgedPacket;
import net.raphimc.netminecraft.packet.impl.play.S2CPlayStartConfigurationPacket;
import net.raphimc.viaproxy.proxy.session.ProxyConnection;
import net.raphimc.viaproxy.proxy.util.ChannelUtil;
import net.raphimc.viaproxy.util.logging.Logger;
Expand All @@ -33,30 +33,13 @@

public class ConfigurationPacketHandler extends PacketHandler {

private final int configurationAcknowledgedId;
private final int startConfigurationId;

public ConfigurationPacketHandler(ProxyConnection proxyConnection) {
super(proxyConnection);

this.configurationAcknowledgedId = MCPackets.C2S_CONFIGURATION_ACKNOWLEDGED.getId(proxyConnection.getClientVersion().getVersion());
this.startConfigurationId = MCPackets.S2C_START_CONFIGURATION.getId(proxyConnection.getClientVersion().getVersion());
}

@Override
public boolean handleC2P(IPacket packet, List<ChannelFutureListener> listeners) {
if (packet instanceof UnknownPacket unknownPacket && this.proxyConnection.getC2pConnectionState() == ConnectionState.PLAY) {
if (unknownPacket.packetId == this.configurationAcknowledgedId) {
this.proxyConnection.setC2pConnectionState(ConnectionState.CONFIGURATION);
listeners.add(f -> {
if (f.isSuccess()) {
Logger.u_info("session", this.proxyConnection, "Switching to CONFIGURATION state");
this.proxyConnection.setP2sConnectionState(ConnectionState.CONFIGURATION);
ChannelUtil.restoreAutoRead(this.proxyConnection.getChannel());
}
});
}
} else if (packet instanceof C2SLoginAcknowledgedPacket) {
public boolean handleC2P(Packet packet, List<ChannelFutureListener> listeners) {
if (packet instanceof C2SLoginAcknowledgedPacket) {
this.proxyConnection.setC2pConnectionState(ConnectionState.CONFIGURATION);
listeners.add(f -> {
if (f.isSuccess()) {
Expand All @@ -73,18 +56,25 @@ public boolean handleC2P(IPacket packet, List<ChannelFutureListener> listeners)
ChannelUtil.restoreAutoRead(this.proxyConnection.getChannel());
}
});
} else if (packet instanceof C2SPlayConfigurationAcknowledgedPacket) {
this.proxyConnection.setC2pConnectionState(ConnectionState.CONFIGURATION);
listeners.add(f -> {
if (f.isSuccess()) {
Logger.u_info("session", this.proxyConnection, "Switching to CONFIGURATION state");
this.proxyConnection.setP2sConnectionState(ConnectionState.CONFIGURATION);
ChannelUtil.restoreAutoRead(this.proxyConnection.getChannel());
}
});
}

return true;
}

@Override
public boolean handleP2S(IPacket packet, List<ChannelFutureListener> listeners) {
if (packet instanceof UnknownPacket unknownPacket && this.proxyConnection.getP2sConnectionState() == ConnectionState.PLAY) {
if (unknownPacket.packetId == this.startConfigurationId) {
ChannelUtil.disableAutoRead(this.proxyConnection.getChannel());
}
} else if (packet instanceof S2CConfigFinishConfigurationPacket) {
public boolean handleP2S(Packet packet, List<ChannelFutureListener> listeners) {
if (packet instanceof S2CConfigFinishConfigurationPacket) {
ChannelUtil.disableAutoRead(this.proxyConnection.getChannel());
} else if (packet instanceof S2CPlayStartConfigurationPacket) {
ChannelUtil.disableAutoRead(this.proxyConnection.getChannel());
}

Expand Down
Loading

0 comments on commit 7807407

Please sign in to comment.