Search in sources :

Example 11 with EngineConnection

use of org.spongepowered.api.network.EngineConnection in project Sponge by SpongePowered.

the class ServerLoginPacketListenerImplMixin_Vanilla method onResponsePayload.

@Inject(method = "handleCustomQueryPacket", at = @At("HEAD"), cancellable = true)
private void onResponsePayload(final ServerboundCustomQueryPacket packet, final CallbackInfo ci) {
    ci.cancel();
    final SpongeChannelManager channelRegistry = (SpongeChannelManager) Sponge.channelManager();
    this.server.execute(() -> channelRegistry.handleLoginResponsePayload((EngineConnection) this, packet));
}
Also used : SpongeChannelManager(org.spongepowered.common.network.channel.SpongeChannelManager) EngineConnection(org.spongepowered.api.network.EngineConnection) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 12 with EngineConnection

use of org.spongepowered.api.network.EngineConnection in project Sponge by SpongePowered.

the class SpongeBasicPacketChannel method handleLoginRequestPayload.

@Override
protected void handleLoginRequestPayload(final EngineConnection connection, final int transactionId, final ChannelBuf payload) {
    // Is currently always executed on the client,
    // the server always expects a response
    final int opcode = this.readOpcode(payload);
    final SpongePacketBinding<Packet> binding = this.requireBinding(opcode);
    final Packet packet = this.decodePayload(() -> binding.getPacketConstructor().get(), payload);
    // A normal packet binding
    if (binding instanceof SpongeHandlerPacketBinding) {
        final SpongeHandlerPacketBinding<Packet> handlerBinding = (SpongeHandlerPacketBinding<Packet>) binding;
        this.handle(connection, handlerBinding, packet);
        // The server always expects a response
        PacketSender.sendTo(connection, PacketUtil.createLoginPayloadResponse(null, transactionId));
    } else {
        // A transactional packet binding
        final SpongeTransactionalPacketBinding<RequestPacket<Packet>, Packet> transactionalBinding = (SpongeTransactionalPacketBinding) binding;
        final RequestPacketHandler<Packet, Packet, EngineConnection> handler = (RequestPacketHandler<Packet, Packet, EngineConnection>) transactionalBinding.getRequestHandler(connection);
        boolean success = false;
        if (handler != null) {
            final SpongeRequestPacketResponse<Packet> response = new SpongeRequestPacketResponse<Packet>() {

                @Override
                protected void fail0(final ChannelException exception) {
                    final net.minecraft.network.protocol.Packet<?> mcPacket = PacketUtil.createLoginPayloadResponse(null, transactionId);
                    PacketSender.sendTo(connection, mcPacket);
                }

                @Override
                protected void success0(final Packet response) {
                    try {
                        final ChannelBuf responsePayload = SpongeBasicPacketChannel.this.encodeLoginPayload(transactionalBinding.opcode(), response);
                        final net.minecraft.network.protocol.Packet<?> mcPacket = PacketUtil.createLoginPayloadResponse(responsePayload, transactionId);
                        PacketSender.sendTo(connection, mcPacket);
                    } catch (final Throwable ex) {
                        SpongeBasicPacketChannel.this.handleException(connection, new ChannelIOException("Failed to encode response packet", ex), null);
                    }
                }
            };
            try {
                handler.handleRequest(packet, connection, response);
                success = true;
            } catch (final Throwable ex) {
                this.handleException(connection, new ChannelIOException("Failed to handle request packet", ex), null);
            }
        }
        if (!success) {
            final net.minecraft.network.protocol.Packet<?> mcPacket = PacketUtil.createLoginPayloadResponse(null, transactionId);
            PacketSender.sendTo(connection, mcPacket);
        }
    }
}
Also used : RequestPacket(org.spongepowered.api.network.channel.packet.RequestPacket) Packet(org.spongepowered.api.network.channel.packet.Packet) RequestPacket(org.spongepowered.api.network.channel.packet.RequestPacket) ChannelIOException(org.spongepowered.api.network.channel.ChannelIOException) ChannelBuf(org.spongepowered.api.network.channel.ChannelBuf) RequestPacketHandler(org.spongepowered.api.network.channel.packet.RequestPacketHandler) EngineConnection(org.spongepowered.api.network.EngineConnection) ChannelException(org.spongepowered.api.network.channel.ChannelException)

Example 13 with EngineConnection

use of org.spongepowered.api.network.EngineConnection in project Sponge by SpongePowered.

the class SpongeRawLoginDataChannel method sendTo.

@Override
public CompletableFuture<ChannelBuf> sendTo(final EngineConnection connection, final Consumer<ChannelBuf> payload) {
    ConnectionUtil.checkHandshakePhase(connection);
    final CompletableFuture<ChannelBuf> future = new CompletableFuture<>();
    final ChannelBuf buf;
    try {
        buf = this.parent.encodePayload(payload);
    } catch (final Throwable t) {
        this.parent.handleException(connection, t, future);
        return future;
    }
    final TransactionStore transactionStore = ConnectionUtil.getTransactionStore(connection);
    final int transactionId = transactionStore.nextId();
    final Consumer<TransactionResult> resultConsumer = result -> {
        if (result.isSuccess()) {
            future.complete(result.getPayload());
        } else {
            this.parent.handleException(connection, result.getCause(), future);
        }
    };
    final Packet<?> mcPacket = PacketUtil.createLoginPayloadRequest(this.parent.key(), buf, transactionId);
    PacketSender.sendTo(connection, mcPacket, sendFuture -> {
        if (sendFuture.isSuccess()) {
            transactionStore.put(transactionId, this.parent, resultConsumer);
        } else {
            // The packet already failed before it could reach the client
            future.completeExceptionally(sendFuture.cause());
        }
    });
    return future;
}
Also used : PacketUtil(org.spongepowered.common.network.channel.PacketUtil) SpongeChannel(org.spongepowered.common.network.channel.SpongeChannel) TransactionStore(org.spongepowered.common.network.channel.TransactionStore) EngineConnection(org.spongepowered.api.network.EngineConnection) ChannelException(org.spongepowered.api.network.channel.ChannelException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) EngineConnectionSide(org.spongepowered.api.network.EngineConnectionSide) CompletableFuture(java.util.concurrent.CompletableFuture) PacketSender(org.spongepowered.common.network.channel.PacketSender) ChannelBuf(org.spongepowered.api.network.channel.ChannelBuf) Objects(java.util.Objects) Consumer(java.util.function.Consumer) RawDataChannel(org.spongepowered.api.network.channel.raw.RawDataChannel) ConnectionUtil(org.spongepowered.common.network.channel.ConnectionUtil) Map(java.util.Map) Packet(net.minecraft.network.protocol.Packet) RawHandshakeDataRequestResponse(org.spongepowered.api.network.channel.raw.handshake.RawHandshakeDataRequestResponse) TransactionResult(org.spongepowered.common.network.channel.TransactionResult) RawHandshakeDataRequestHandler(org.spongepowered.api.network.channel.raw.handshake.RawHandshakeDataRequestHandler) RawHandshakeDataChannel(org.spongepowered.api.network.channel.raw.handshake.RawHandshakeDataChannel) TransactionStore(org.spongepowered.common.network.channel.TransactionStore) CompletableFuture(java.util.concurrent.CompletableFuture) TransactionResult(org.spongepowered.common.network.channel.TransactionResult) ChannelBuf(org.spongepowered.api.network.channel.ChannelBuf)

Example 14 with EngineConnection

use of org.spongepowered.api.network.EngineConnection in project Sponge by SpongePowered.

the class VelocityForwardingInfo method sendQuery.

public static void sendQuery(final ServerLoginPacketListenerImpl mcConn) {
    final EngineConnection conn = (EngineConnection) mcConn;
    VelocityChannel.CHANNEL.sendTo(conn, cbuf -> {
    }).whenComplete((response, error) -> {
        if (error != null) {
            if (error instanceof NoResponseException) {
                conn.close(Component.text("This server requires you to connect with Velocity."));
            }
            return;
        }
        if (!VelocityForwardingInfo.checkIntegrity(response)) {
            conn.close(Component.text("Unable to verify player details. Is your forwarding secret correct?"));
            return;
        }
        final ConnectionAccessor connectionAccessor = (ConnectionAccessor) mcConn.getConnection();
        connectionAccessor.accessor$address(new InetSocketAddress(VelocityForwardingInfo.readAddress(response), ((InetSocketAddress) mcConn.getConnection().getRemoteAddress()).getPort()));
        ((ServerLoginPacketListenerImplAccessor) mcConn).accessor$gameProfile(VelocityForwardingInfo.createProfile(response));
    }).exceptionally(err -> {
        if (!(err instanceof NoResponseException)) {
            // Handled above
            VelocityForwardingInfo.LOGGER.error("Failed to process velocity forwarding info", err);
            conn.close(Component.text("Invalid forwarding information received!"));
        }
        return null;
    });
}
Also used : MessageDigest(java.security.MessageDigest) GameProfile(com.mojang.authlib.GameProfile) SecretKeySpec(javax.crypto.spec.SecretKeySpec) FriendlyByteBuf(net.minecraft.network.FriendlyByteBuf) InetAddress(java.net.InetAddress) RawDataChannel(org.spongepowered.api.network.channel.raw.RawDataChannel) ByteBuf(io.netty.buffer.ByteBuf) Component(net.kyori.adventure.text.Component) Property(com.mojang.authlib.properties.Property) ResourceKey(org.spongepowered.api.ResourceKey) ServerLoginPacketListenerImpl(net.minecraft.server.network.ServerLoginPacketListenerImpl) EngineConnection(org.spongepowered.api.network.EngineConnection) NoResponseException(org.spongepowered.api.network.channel.NoResponseException) ServerLoginPacketListenerImplAccessor(org.spongepowered.common.accessor.server.network.ServerLoginPacketListenerImplAccessor) Sponge(org.spongepowered.api.Sponge) Mac(javax.crypto.Mac) InetSocketAddress(java.net.InetSocketAddress) StandardCharsets(java.nio.charset.StandardCharsets) ChannelBuf(org.spongepowered.api.network.channel.ChannelBuf) Logger(org.apache.logging.log4j.Logger) ChannelExceptionHandler(org.spongepowered.api.network.channel.ChannelExceptionHandler) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SpongeConfigs(org.spongepowered.common.applaunch.config.core.SpongeConfigs) ConnectionAccessor(org.spongepowered.common.accessor.network.ConnectionAccessor) InvalidKeyException(java.security.InvalidKeyException) InetAddresses(com.google.common.net.InetAddresses) LogManager(org.apache.logging.log4j.LogManager) RawHandshakeDataChannel(org.spongepowered.api.network.channel.raw.handshake.RawHandshakeDataChannel) InetSocketAddress(java.net.InetSocketAddress) EngineConnection(org.spongepowered.api.network.EngineConnection) ConnectionAccessor(org.spongepowered.common.accessor.network.ConnectionAccessor) NoResponseException(org.spongepowered.api.network.channel.NoResponseException)

Aggregations

EngineConnection (org.spongepowered.api.network.EngineConnection)14 ChannelBuf (org.spongepowered.api.network.channel.ChannelBuf)6 Inject (org.spongepowered.asm.mixin.injection.Inject)6 SpongeChannelManager (org.spongepowered.common.network.channel.SpongeChannelManager)6 ChannelException (org.spongepowered.api.network.channel.ChannelException)4 RawDataChannel (org.spongepowered.api.network.channel.raw.RawDataChannel)4 RawHandshakeDataChannel (org.spongepowered.api.network.channel.raw.handshake.RawHandshakeDataChannel)4 InetAddresses (com.google.common.net.InetAddresses)2 GameProfile (com.mojang.authlib.GameProfile)2 Property (com.mojang.authlib.properties.Property)2 ByteBuf (io.netty.buffer.ByteBuf)2 InetAddress (java.net.InetAddress)2 InetSocketAddress (java.net.InetSocketAddress)2 StandardCharsets (java.nio.charset.StandardCharsets)2 InvalidKeyException (java.security.InvalidKeyException)2 MessageDigest (java.security.MessageDigest)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 Map (java.util.Map)2 Objects (java.util.Objects)2 CompletableFuture (java.util.concurrent.CompletableFuture)2