Search in sources :

Example 1 with NoResponseException

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

the class SpongePacketChannel method handleResponsePacket.

private void handleResponsePacket(final EngineConnection connection, final int transactionId, @Nullable final ChannelBuf payload, final int dynamicOpcode) {
    final TransactionStore store = ConnectionUtil.getTransactionStore(connection);
    final TransactionStore.Entry stored = store.remove(transactionId);
    if (stored == null) {
        return;
    }
    final TransactionData<RequestPacket<Packet>, Packet> transactionData = (TransactionData<RequestPacket<Packet>, Packet>) stored.getData();
    final TransactionResult result = payload == null ? TransactionResult.failure(new NoResponseException()) : TransactionResult.success(payload);
    this.handleTransactionResponse(connection, transactionData, result, dynamicOpcode);
}
Also used : TransactionStore(org.spongepowered.common.network.channel.TransactionStore) RequestPacket(org.spongepowered.api.network.channel.packet.RequestPacket) Packet(org.spongepowered.api.network.channel.packet.Packet) TransactionResult(org.spongepowered.common.network.channel.TransactionResult) RequestPacket(org.spongepowered.api.network.channel.packet.RequestPacket) NoResponseException(org.spongepowered.api.network.channel.NoResponseException)

Example 2 with NoResponseException

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

the class SpongePacketChannel method handleTransactionResponse.

@Override
protected void handleTransactionResponse(final EngineConnection connection, final Object stored, final TransactionResult result) {
    if (result.isSuccess()) {
        final ChannelBuf payload = result.getPayload();
        final long typeAndValue = payload.readVarLong();
        final int type = SpongePacketChannel.extractType(typeAndValue);
        final int value = SpongePacketChannel.extractValue(typeAndValue);
        if (type == SpongePacketChannel.TYPE_RESPONSE || type == SpongePacketChannel.TYPE_NO_RESPONSE || type == SpongePacketChannel.TYPE_DYNAMIC_RESPONSE) {
            final TransactionData<RequestPacket<Packet>, Packet> transactionData = (TransactionData<RequestPacket<Packet>, Packet>) stored;
            if (type == SpongePacketChannel.TYPE_RESPONSE) {
                this.handleTransactionResponse(connection, transactionData, result, SpongePacketChannel.NO_DYNAMIC_OPCODE);
            } else if (type == SpongePacketChannel.TYPE_NO_RESPONSE) {
                this.handleTransactionResponse(connection, transactionData, TransactionResult.failure(new NoResponseException()), SpongePacketChannel.NO_DYNAMIC_OPCODE);
            } else {
                this.handleTransactionResponse(connection, transactionData, result, value);
            }
        } else {
            this.handleException(connection, new ChannelIOException("Unknown packet type: " + type), null);
        }
    }
}
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) NoResponseException(org.spongepowered.api.network.channel.NoResponseException)

Example 3 with NoResponseException

use of org.spongepowered.api.network.channel.NoResponseException 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 4 with NoResponseException

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

the class SpongeChannelManager method handleLoginResponsePayload.

private void handleLoginResponsePayload(final EngineConnection connection, final int transactionId, @Nullable final ChannelBuf payload) {
    // Sponge magic... Allows normal packets to be send during the login phase from the client to server
    if (transactionId == Constants.Channels.LOGIN_PAYLOAD_IGNORED_TRANSACTION_ID) {
        return;
    }
    if (transactionId == Constants.Channels.LOGIN_PAYLOAD_TRANSACTION_ID) {
        if (payload != null) {
            final ResourceKey channelKey = ResourceKey.resolve(payload.readString());
            this.handlePlayPayload(connection, channelKey, payload);
        }
        return;
    }
    // Normal handling
    final TransactionStore transactionStore = ConnectionUtil.getTransactionStore(connection);
    final TransactionStore.Entry entry = transactionStore.remove(transactionId);
    if (entry == null) {
        return;
    }
    if (entry.getData() instanceof ClientTypeSyncFuture) {
        if (payload != null) {
            this.handleClientType(connection, payload);
        }
        ((ClientTypeSyncFuture) entry.getData()).future.complete(null);
        return;
    }
    if (entry.getData() instanceof ChannelRegistrySyncFuture) {
        if (payload != null) {
            this.handleChannelRegistry(connection, payload);
        }
        ((ChannelRegistrySyncFuture) entry.getData()).future.complete(null);
        return;
    }
    final TransactionResult result = payload == null ? TransactionResult.failure(new NoResponseException()) : TransactionResult.success(payload);
    entry.getChannel().handleTransactionResponse(connection, entry.getData(), result);
}
Also used : NoResponseException(org.spongepowered.api.network.channel.NoResponseException) ResourceKey(org.spongepowered.api.ResourceKey)

Example 5 with NoResponseException

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

the class ChannelTest method onRegisterChannel.

@Listener
public void onRegisterChannel(final RegisterChannelEvent event) {
    this.channel = event.register(ResourceKey.of("channeltest", "default"), PacketChannel.class);
    this.channel.register(PrintTextPacket.class, 0).addHandler((packet, connection) -> {
        this.logReceived(this.channel, packet, connection);
        this.plugin.logger().info(packet.getText());
    });
    this.channel.registerTransactional(PingPacket.class, PongPacket.class, 1).setRequestHandler((requestPacket, connection, response) -> {
        this.logReceived(this.channel, requestPacket, connection);
        response.success(new PongPacket(requestPacket.getId()));
    });
    this.basicChannel = event.register(ResourceKey.of("channeltest", "basic"), BasicPacketChannel.class);
    this.basicChannel.register(PrintTextPacket.class, 0).addHandler((packet, connection) -> {
        this.logReceived(this.basicChannel, packet, connection);
        this.plugin.logger().info(packet.getText());
    });
    this.basicChannel.registerTransactional(PingPacket.class, PongPacket.class, 1).setRequestHandler((requestPacket, connection, response) -> {
        this.logReceived(this.basicChannel, requestPacket, connection);
        response.success(new PongPacket(requestPacket.getId()));
    });
    this.rawChannel = event.register(ResourceKey.of("channeltest", "raw"), RawDataChannel.class);
    this.rawChannel.handshake().setRequestHandler((request, connection, response) -> {
        final int value = request.readVarInt();
        this.logReceived(this.rawChannel, value, connection);
        if (value == 0) {
            response.fail(new NoResponseException());
        } else {
            response.success(buf -> buf.writeVarInt(value * 2));
        }
    });
}
Also used : BasicPacketChannel(org.spongepowered.api.network.channel.packet.basic.BasicPacketChannel) BasicPacketChannel(org.spongepowered.api.network.channel.packet.basic.BasicPacketChannel) PacketChannel(org.spongepowered.api.network.channel.packet.PacketChannel) NoResponseException(org.spongepowered.api.network.channel.NoResponseException) RawDataChannel(org.spongepowered.api.network.channel.raw.RawDataChannel) Listener(org.spongepowered.api.event.Listener)

Aggregations

NoResponseException (org.spongepowered.api.network.channel.NoResponseException)6 ResourceKey (org.spongepowered.api.ResourceKey)3 RawDataChannel (org.spongepowered.api.network.channel.raw.RawDataChannel)3 Listener (org.spongepowered.api.event.Listener)2 EngineConnection (org.spongepowered.api.network.EngineConnection)2 ChannelBuf (org.spongepowered.api.network.channel.ChannelBuf)2 Packet (org.spongepowered.api.network.channel.packet.Packet)2 PacketChannel (org.spongepowered.api.network.channel.packet.PacketChannel)2 RequestPacket (org.spongepowered.api.network.channel.packet.RequestPacket)2 BasicPacketChannel (org.spongepowered.api.network.channel.packet.basic.BasicPacketChannel)2 InetAddresses (com.google.common.net.InetAddresses)1 Inject (com.google.inject.Inject)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