Search in sources :

Example 1 with EngineConnection

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

the class ClientHandshakePacketListenerImplMixin_Vanilla method vanilla$handleRequestPayload.

// @formatter:on
@Inject(method = "handleCustomQuery", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/Connection;send(Lnet/minecraft/network/protocol/Packet;)V"), cancellable = true)
private void vanilla$handleRequestPayload(final ClientboundCustomQueryPacket packet, final CallbackInfo ci) {
    ci.cancel();
    final SpongeChannelManager channelRegistry = (SpongeChannelManager) Sponge.channelManager();
    this.minecraft.execute(() -> {
        final EngineConnection connection = (EngineConnection) this;
        if (!channelRegistry.handleLoginRequestPayload(connection, packet)) {
            PacketSender.sendTo(connection, PacketUtil.createLoginPayloadResponse(null, packet.getTransactionId()));
        }
    });
}
Also used : SpongeChannelManager(org.spongepowered.common.network.channel.SpongeChannelManager) EngineConnection(org.spongepowered.api.network.EngineConnection) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 2 with EngineConnection

use of org.spongepowered.api.network.EngineConnection in project SpongeCommon 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)

Example 3 with EngineConnection

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

the class PacketSender method sendTo.

public static void sendTo(final EngineConnection connection, final Packet<?> packet, @Nullable final Consumer<Future<? super Void>> listener) {
    final Connection networkManager = ((ConnectionHolderBridge) connection).bridge$getConnection();
    GenericFutureListener<? extends Future<? super Void>> asyncListener = null;
    if (listener != null) {
        final EngineConnectionSide<?> side = connection.side();
        // Complete the netty callback on the sync thread
        asyncListener = future -> {
            final BlockableEventLoop<?> executor;
            if (side == EngineConnectionSide.CLIENT) {
                executor = (BlockableEventLoop<?>) Sponge.client();
            } else {
                executor = (BlockableEventLoop<?>) Sponge.server();
            }
            executor.execute(() -> listener.accept(future));
        };
    }
    networkManager.send(packet, asyncListener);
}
Also used : EngineConnection(org.spongepowered.api.network.EngineConnection) Connection(net.minecraft.network.Connection) ConnectionHolderBridge(org.spongepowered.common.bridge.network.ConnectionHolderBridge)

Example 4 with EngineConnection

use of org.spongepowered.api.network.EngineConnection in project SpongeCommon 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 5 with EngineConnection

use of org.spongepowered.api.network.EngineConnection in project SpongeCommon 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)

Aggregations

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