Updated NetMinecraft API usage

This commit is contained in:
RaphiMC 2024-05-22 21:08:57 +02:00
parent 0a74bbec23
commit 0e5e960afa
No known key found for this signature in database
GPG Key ID: 0F6BB0657A03AC94
11 changed files with 41 additions and 41 deletions

View File

@ -17,13 +17,13 @@
*/
package net.raphimc.viaproxy.injection.mixins;
import net.raphimc.netminecraft.packet.impl.handshake.C2SHandshakePacket;
import net.raphimc.netminecraft.packet.impl.handshaking.C2SHandshakingClientIntentionPacket;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.Constant;
import org.spongepowered.asm.mixin.injection.ModifyConstant;
@Mixin(C2SHandshakePacket.class)
public abstract class MixinC2SHandshakePacket {
@Mixin(C2SHandshakingClientIntentionPacket.class)
public abstract class MixinC2SHandshakingClientIntentionPacket {
@ModifyConstant(method = "read", constant = @Constant(intValue = 255))
private int allowLargerHostnames(int constant) {

View File

@ -56,7 +56,7 @@ public class Client2ProxyChannelInitializer extends MinecraftChannelInitializer
}
super.initChannel(channel);
channel.attr(MCPipeline.PACKET_REGISTRY_ATTRIBUTE_KEY).set(PacketRegistryUtil.getHandshakeRegistry(false));
channel.attr(MCPipeline.PACKET_REGISTRY_ATTRIBUTE_KEY).set(PacketRegistryUtil.getHandshakingRegistry(false));
if (ViaProxy.EVENT_MANAGER.call(new Client2ProxyChannelInitializeEvent(ITyped.Type.POST, channel, false)).isCancelled()) {
channel.close();

View File

@ -29,7 +29,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.impl.handshake.C2SHandshakePacket;
import net.raphimc.netminecraft.packet.impl.handshaking.C2SHandshakingClientIntentionPacket;
import net.raphimc.viabedrock.api.BedrockProtocolVersion;
import net.raphimc.vialegacy.api.LegacyProtocolVersion;
import net.raphimc.viaproxy.ViaProxy;
@ -91,7 +91,7 @@ public class Client2ProxyHandler extends SimpleChannelInboundHandler<IPacket> {
if (this.proxyConnection.isClosed()) return;
if (this.proxyConnection.getC2pConnectionState() == ConnectionState.HANDSHAKING) {
if (packet instanceof C2SHandshakePacket) this.handleHandshake((C2SHandshakePacket) packet);
if (packet instanceof C2SHandshakingClientIntentionPacket) this.handleHandshake((C2SHandshakingClientIntentionPacket) packet);
else throw new IllegalStateException("Unexpected packet in HANDSHAKING state");
return;
}
@ -110,7 +110,7 @@ public class Client2ProxyHandler extends SimpleChannelInboundHandler<IPacket> {
ExceptionUtil.handleNettyException(ctx, cause, this.proxyConnection);
}
private void handleHandshake(final C2SHandshakePacket packet) {
private void handleHandshake(final C2SHandshakingClientIntentionPacket packet) {
final ProtocolVersion clientVersion = ProtocolVersion.getProtocol(packet.protocolVersion);
if (packet.intendedState == null) {
@ -267,7 +267,7 @@ public class Client2ProxyHandler extends SimpleChannelInboundHandler<IPacket> {
}
handshakeParts[0] = address;
final C2SHandshakePacket newHandshakePacket = new C2SHandshakePacket(clientVersion.getOriginalVersion(), String.join("\0", handshakeParts), port, intendedState);
final C2SHandshakingClientIntentionPacket newHandshakePacket = new C2SHandshakingClientIntentionPacket(clientVersion.getOriginalVersion(), String.join("\0", handshakeParts), port, intendedState);
this.proxyConnection.getChannel().writeAndFlush(newHandshakePacket).addListeners(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE, (ChannelFutureListener) f2 -> {
if (f2.isSuccess()) {

View File

@ -28,7 +28,7 @@ import net.raphimc.netminecraft.packet.IPacket;
import net.raphimc.netminecraft.packet.PacketTypes;
import net.raphimc.netminecraft.packet.UnknownPacket;
import net.raphimc.netminecraft.packet.impl.login.S2CLoginCompressionPacket;
import net.raphimc.netminecraft.packet.impl.login.S2CLoginSuccessPacket1_7;
import net.raphimc.netminecraft.packet.impl.login.S2CLoginGameProfilePacket1_7;
import net.raphimc.viaproxy.ViaProxy;
import net.raphimc.viaproxy.proxy.session.ProxyConnection;
import net.raphimc.viaproxy.proxy.util.ChannelUtil;
@ -56,7 +56,7 @@ public class CompressionPacketHandler extends PacketHandler {
return false;
}
} else if (packet instanceof S2CLoginSuccessPacket1_7) {
} else if (packet instanceof S2CLoginGameProfilePacket1_7) {
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());

View File

@ -22,9 +22,9 @@ 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.impl.configuration.C2SConfigFinishConfiguration1_20_2;
import net.raphimc.netminecraft.packet.impl.configuration.S2CConfigFinishConfiguration1_20_2;
import net.raphimc.netminecraft.packet.impl.login.C2SLoginStartConfiguration1_20_2;
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.viaproxy.proxy.session.ProxyConnection;
import net.raphimc.viaproxy.proxy.util.ChannelUtil;
import net.raphimc.viaproxy.util.logging.Logger;
@ -56,7 +56,7 @@ public class ConfigurationPacketHandler extends PacketHandler {
}
});
}
} else if (packet instanceof C2SLoginStartConfiguration1_20_2) {
} else if (packet instanceof C2SLoginAcknowledgedPacket) {
this.proxyConnection.setC2pConnectionState(ConnectionState.CONFIGURATION);
listeners.add(f -> {
if (f.isSuccess()) {
@ -64,7 +64,7 @@ public class ConfigurationPacketHandler extends PacketHandler {
ChannelUtil.restoreAutoRead(this.proxyConnection.getChannel());
}
});
} else if (packet instanceof C2SConfigFinishConfiguration1_20_2) {
} else if (packet instanceof C2SConfigFinishConfigurationPacket) {
this.proxyConnection.setC2pConnectionState(ConnectionState.PLAY);
listeners.add(f -> {
if (f.isSuccess()) {
@ -84,7 +84,7 @@ public class ConfigurationPacketHandler extends PacketHandler {
if (unknownPacket.packetId == this.startConfigurationId) {
ChannelUtil.disableAutoRead(this.proxyConnection.getChannel());
}
} else if (packet instanceof S2CConfigFinishConfiguration1_20_2) {
} else if (packet instanceof S2CConfigFinishConfigurationPacket) {
ChannelUtil.disableAutoRead(this.proxyConnection.getChannel());
}

View File

@ -85,7 +85,7 @@ public class LoginPacketHandler extends PacketHandler {
}
if (ViaProxy.getConfig().isProxyOnlineMode() && !ViaProxy.EVENT_MANAGER.call(new ShouldVerifyOnlineModeEvent(this.proxyConnection)).isCancelled()) {
this.proxyConnection.getC2P().writeAndFlush(new S2CLoginKeyPacket1_8("", KEY_PAIR.getPublic().getEncoded(), this.verifyToken)).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
this.proxyConnection.getC2P().writeAndFlush(new S2CLoginHelloPacket1_20_5("", KEY_PAIR.getPublic().getEncoded(), this.verifyToken, true)).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
} else {
ViaProxy.EVENT_MANAGER.call(new ClientLoggedInEvent(proxyConnection));
ExternalInterface.fillPlayerData(this.proxyConnection);
@ -143,10 +143,10 @@ public class LoginPacketHandler extends PacketHandler {
public boolean handleP2S(IPacket packet, List<ChannelFutureListener> listeners) throws GeneralSecurityException, ExecutionException, InterruptedException {
if (packet instanceof S2CLoginDisconnectPacket1_7 loginDisconnectPacket) {
Logger.u_info("server kick", this.proxyConnection, ConsoleFormatter.convert(loginDisconnectPacket.reason.asLegacyFormatString()));
} else if (packet instanceof S2CLoginKeyPacket1_7 loginKeyPacket) {
final PublicKey publicKey = CryptUtil.decodeRsaPublicKey(loginKeyPacket.publicKey);
} else if (packet instanceof S2CLoginHelloPacket1_7 loginHelloPacket) {
final PublicKey publicKey = CryptUtil.decodeRsaPublicKey(loginHelloPacket.publicKey);
final SecretKey secretKey = CryptUtil.generateSecretKey();
final String serverHash = new BigInteger(CryptUtil.computeServerIdHash(loginKeyPacket.serverId, publicKey, secretKey)).toString(16);
final String serverHash = new BigInteger(CryptUtil.computeServerIdHash(loginHelloPacket.serverId, publicKey, secretKey)).toString(16);
boolean auth = true;
if (this.proxyConnection.getServerVersion().olderThanOrEqualTo(LegacyProtocolVersion.r1_6_4)) {
@ -157,11 +157,11 @@ public class LoginPacketHandler extends PacketHandler {
}
final byte[] encryptedSecretKey = CryptUtil.encryptData(publicKey, secretKey.getEncoded());
final byte[] encryptedNonce = CryptUtil.encryptData(publicKey, loginKeyPacket.nonce);
final byte[] encryptedNonce = CryptUtil.encryptData(publicKey, loginHelloPacket.nonce);
final C2SLoginKeyPacket1_19_3 loginKey = new C2SLoginKeyPacket1_19_3(encryptedSecretKey, encryptedNonce);
if (this.proxyConnection.getServerVersion().newerThanOrEqualTo(ProtocolVersion.v1_19) && this.proxyConnection.getLoginHelloPacket() instanceof C2SLoginHelloPacket1_19 && ((C2SLoginHelloPacket1_19) this.proxyConnection.getLoginHelloPacket()).key != null) {
ExternalInterface.signNonce(loginKeyPacket.nonce, loginKey, this.proxyConnection);
ExternalInterface.signNonce(loginHelloPacket.nonce, loginKey, this.proxyConnection);
}
this.proxyConnection.getChannel().writeAndFlush(loginKey).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
@ -172,10 +172,10 @@ public class LoginPacketHandler extends PacketHandler {
}
return false;
} else if (packet instanceof S2CLoginSuccessPacket1_7 loginSuccessPacket) {
} else if (packet instanceof S2CLoginGameProfilePacket1_7 loginGameProfilePacket) {
final ConnectionState nextState = this.proxyConnection.getClientVersion().newerThanOrEqualTo(ProtocolVersion.v1_20_2) ? ConnectionState.CONFIGURATION : ConnectionState.PLAY;
this.proxyConnection.setGameProfile(new GameProfile(loginSuccessPacket.uuid, loginSuccessPacket.name));
this.proxyConnection.setGameProfile(new GameProfile(loginGameProfilePacket.uuid, loginGameProfilePacket.name));
Logger.u_info("session", this.proxyConnection, "Connected successfully! Switching to " + nextState + " state");
ChannelUtil.disableAutoRead(this.proxyConnection.getChannel());

View File

@ -28,9 +28,9 @@ import net.raphimc.netminecraft.constants.MCPackets;
import net.raphimc.netminecraft.packet.IPacket;
import net.raphimc.netminecraft.packet.PacketTypes;
import net.raphimc.netminecraft.packet.UnknownPacket;
import net.raphimc.netminecraft.packet.impl.login.C2SLoginCustomPayloadPacket;
import net.raphimc.netminecraft.packet.impl.login.C2SLoginCustomQueryAnswerPacket;
import net.raphimc.netminecraft.packet.impl.login.C2SLoginKeyPacket1_7;
import net.raphimc.netminecraft.packet.impl.login.S2CLoginCustomPayloadPacket;
import net.raphimc.netminecraft.packet.impl.login.S2CLoginCustomQueryPacket;
import net.raphimc.netminecraft.packet.impl.login.S2CLoginDisconnectPacket1_20_3;
import net.raphimc.viaproxy.proxy.external_interface.OpenAuthModConstants;
import net.raphimc.viaproxy.proxy.session.ProxyConnection;
@ -67,8 +67,8 @@ public class OpenAuthModPacketHandler extends PacketHandler {
return false;
}
}
} else if (packet instanceof C2SLoginCustomPayloadPacket loginCustomPayload) {
if (loginCustomPayload.response != null && this.handleCustomPayload(loginCustomPayload.queryId, Unpooled.wrappedBuffer(loginCustomPayload.response))) {
} else if (packet instanceof C2SLoginCustomQueryAnswerPacket loginCustomQueryAnswer) {
if (loginCustomQueryAnswer.response != null && this.handleCustomPayload(loginCustomQueryAnswer.queryId, Unpooled.wrappedBuffer(loginCustomQueryAnswer.response))) {
return false;
}
} else if (packet instanceof C2SLoginKeyPacket1_7 loginKeyPacket) {
@ -90,7 +90,7 @@ public class OpenAuthModPacketHandler extends PacketHandler {
switch (this.proxyConnection.getC2pConnectionState()) {
case LOGIN:
if (this.proxyConnection.getClientVersion().newerThanOrEqualTo(ProtocolVersion.v1_13)) {
this.proxyConnection.getC2P().writeAndFlush(new S2CLoginCustomPayloadPacket(id, channel, PacketTypes.readReadableBytes(data))).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
this.proxyConnection.getC2P().writeAndFlush(new S2CLoginCustomQueryPacket(id, channel, PacketTypes.readReadableBytes(data))).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
} else {
final ByteBuf disconnectPacketData = Unpooled.buffer();
PacketTypes.writeString(disconnectPacketData, channel);

View File

@ -21,7 +21,7 @@ import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import io.netty.channel.ChannelFutureListener;
import net.raphimc.netminecraft.packet.IPacket;
import net.raphimc.netminecraft.packet.impl.status.S2CStatusPongPacket;
import net.raphimc.netminecraft.packet.impl.status.S2CStatusPongResponsePacket;
import net.raphimc.netminecraft.packet.impl.status.S2CStatusResponsePacket;
import net.raphimc.viaproxy.ViaProxy;
import net.raphimc.viaproxy.proxy.session.ProxyConnection;
@ -36,7 +36,7 @@ public class StatusPacketHandler extends PacketHandler {
@Override
public boolean handleP2S(IPacket packet, List<ChannelFutureListener> listeners) {
if (packet instanceof S2CStatusPongPacket) {
if (packet instanceof S2CStatusPongResponsePacket) {
listeners.add(ChannelFutureListener.CLOSE);
} else if (packet instanceof S2CStatusResponsePacket statusResponsePacket && !ViaProxy.getConfig().getCustomMotd().isBlank()) {
try {

View File

@ -25,7 +25,7 @@ import net.raphimc.netminecraft.constants.MCPackets;
import net.raphimc.netminecraft.packet.IPacket;
import net.raphimc.netminecraft.packet.PacketTypes;
import net.raphimc.netminecraft.packet.UnknownPacket;
import net.raphimc.netminecraft.packet.impl.configuration.S2CConfigTransfer1_20_5;
import net.raphimc.netminecraft.packet.impl.common.S2CTransferPacket;
import net.raphimc.viaproxy.ViaProxy;
import net.raphimc.viaproxy.proxy.session.ProxyConnection;
import net.raphimc.viaproxy.proxy.util.TransferDataHolder;
@ -48,7 +48,7 @@ public class TransferPacketHandler extends PacketHandler {
public boolean handleP2S(IPacket packet, List<ChannelFutureListener> listeners) {
if (packet instanceof UnknownPacket unknownPacket && this.proxyConnection.getP2sConnectionState() == ConnectionState.PLAY) {
if (unknownPacket.packetId == this.transferId) {
final S2CConfigTransfer1_20_5 transfer = new S2CConfigTransfer1_20_5();
final S2CTransferPacket transfer = new S2CTransferPacket();
transfer.read(Unpooled.wrappedBuffer(unknownPacket.data));
this.handleTransfer(transfer);
@ -58,7 +58,7 @@ public class TransferPacketHandler extends PacketHandler {
this.proxyConnection.getC2P().writeAndFlush(transferToViaProxy).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
return false;
}
} else if (packet instanceof S2CConfigTransfer1_20_5 transfer) {
} else if (packet instanceof S2CTransferPacket transfer) {
this.handleTransfer(transfer);
this.proxyConnection.getC2P().writeAndFlush(this.createTransferPacket()).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
return false;
@ -67,14 +67,14 @@ public class TransferPacketHandler extends PacketHandler {
return true;
}
private void handleTransfer(final S2CConfigTransfer1_20_5 transfer) {
private void handleTransfer(final S2CTransferPacket transfer) {
final InetSocketAddress newAddress = new InetSocketAddress(transfer.host, transfer.port);
TransferDataHolder.addTempRedirect(this.proxyConnection.getC2P(), newAddress);
}
private S2CConfigTransfer1_20_5 createTransferPacket() {
private S2CTransferPacket createTransferPacket() {
if (this.proxyConnection.getClientHandshakeAddress() != null) {
return new S2CConfigTransfer1_20_5(this.proxyConnection.getClientHandshakeAddress().getHost(), this.proxyConnection.getClientHandshakeAddress().getPort());
return new S2CTransferPacket(this.proxyConnection.getClientHandshakeAddress().getHost(), this.proxyConnection.getClientHandshakeAddress().getPort());
} else {
Logger.u_warn("transfer", this.proxyConnection, "Client handshake address is invalid, using ViaProxy bind address instead");
if (!(ViaProxy.getCurrentProxyServer().getChannel().localAddress() instanceof InetSocketAddress bindAddress)) {
@ -83,7 +83,7 @@ public class TransferPacketHandler extends PacketHandler {
if (!(this.proxyConnection.getC2P().localAddress() instanceof InetSocketAddress clientAddress)) {
throw new IllegalArgumentException("Client address must be an InetSocketAddress");
}
return new S2CConfigTransfer1_20_5(clientAddress.getHostString(), bindAddress.getPort());
return new S2CTransferPacket(clientAddress.getHostString(), bindAddress.getPort());
}
}

View File

@ -74,7 +74,7 @@ public class Proxy2ServerChannelInitializer extends MinecraftChannelInitializer
}
super.initChannel(channel);
channel.attr(MCPipeline.PACKET_REGISTRY_ATTRIBUTE_KEY).set(PacketRegistryUtil.getHandshakeRegistry(true));
channel.attr(MCPipeline.PACKET_REGISTRY_ATTRIBUTE_KEY).set(PacketRegistryUtil.getHandshakingRegistry(true));
channel.pipeline().addLast(new ViaProxyVLPipeline(user, proxyConnection.getServerVersion()));
channel.pipeline().addAfter(VLPipeline.VIA_CODEC_NAME, "via-" + MCPipeline.FLOW_CONTROL_HANDLER_NAME, new NoReadFlowControlHandler());

View File

@ -204,7 +204,7 @@ public class ProxyConnection extends NetClient {
this.c2pConnectionState = connectionState;
switch (connectionState) {
case HANDSHAKING:
this.c2p.attr(MCPipeline.PACKET_REGISTRY_ATTRIBUTE_KEY).set(PacketRegistryUtil.getHandshakeRegistry(false));
this.c2p.attr(MCPipeline.PACKET_REGISTRY_ATTRIBUTE_KEY).set(PacketRegistryUtil.getHandshakingRegistry(false));
break;
case STATUS:
this.c2p.attr(MCPipeline.PACKET_REGISTRY_ATTRIBUTE_KEY).set(PacketRegistryUtil.getStatusRegistry(false));
@ -226,7 +226,7 @@ public class ProxyConnection extends NetClient {
switch (connectionState) {
case HANDSHAKING:
if (this.getChannel() != null)
this.getChannel().attr(MCPipeline.PACKET_REGISTRY_ATTRIBUTE_KEY).set(PacketRegistryUtil.getHandshakeRegistry(true));
this.getChannel().attr(MCPipeline.PACKET_REGISTRY_ATTRIBUTE_KEY).set(PacketRegistryUtil.getHandshakingRegistry(true));
break;
case STATUS:
if (this.getChannel() != null)