From 8ffbc7047f5f02b2b37610a1e9681f80c64cf911 Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Sat, 20 Jan 2024 10:26:56 +0100 Subject: [PATCH] Add cookie handling, TransferProvider for other platforms --- .../packets/EntityPackets1_17.java | 9 ++- .../Protocol1_20_3To1_20_5.java | 74 +++++++++++++------ .../provider/NoopTransferProvider.java | 27 +++++++ .../provider/TransferProvider.java | 29 ++++++++ .../storage/CookieStorage.java | 36 +++++++++ 5 files changed, 150 insertions(+), 25 deletions(-) create mode 100644 common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_20_3to1_20_5/provider/NoopTransferProvider.java create mode 100644 common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_20_3to1_20_5/provider/TransferProvider.java create mode 100644 common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_20_3to1_20_5/storage/CookieStorage.java diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_4to1_17/packets/EntityPackets1_17.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_4to1_17/packets/EntityPackets1_17.java index 1a22bfaf..7c8d166d 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_4to1_17/packets/EntityPackets1_17.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_4to1_17/packets/EntityPackets1_17.java @@ -29,7 +29,11 @@ import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.types.version.Types1_16; import com.viaversion.viaversion.api.type.types.version.Types1_17; -import com.viaversion.viaversion.libs.opennbt.tag.builtin.*; +import com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag; +import com.viaversion.viaversion.libs.opennbt.tag.builtin.IntTag; +import com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag; +import com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag; +import com.viaversion.viaversion.libs.opennbt.tag.builtin.Tag; import com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.ClientboundPackets1_16_2; import com.viaversion.viaversion.protocols.protocol1_17to1_16_4.ClientboundPackets1_17; @@ -210,8 +214,7 @@ public final class EntityPackets1_17 extends EntityRewriter 256 || logicalHeight.asInt() > 256) { if (warn && !warned) { - ViaBackwards.getPlatform().getLogger().warning("Custom worlds heights are NOT SUPPORTED for 1.16 players and older and may lead to errors!"); - ViaBackwards.getPlatform().getLogger().warning("You have min/max set to " + minY.asInt() + "/" + height.asInt()); + ViaBackwards.getPlatform().getLogger().warning("Increased world height is NOT SUPPORTED for 1.16 players and below. They will see a void below y 0 and above 256"); warned = true; } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_20_3to1_20_5/Protocol1_20_3To1_20_5.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_20_3to1_20_5/Protocol1_20_3To1_20_5.java index 70de7cc2..e3b7500b 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_20_3to1_20_5/Protocol1_20_3To1_20_5.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_20_3to1_20_5/Protocol1_20_3To1_20_5.java @@ -21,17 +21,24 @@ import com.viaversion.viabackwards.api.BackwardsProtocol; import com.viaversion.viabackwards.api.rewriters.SoundRewriter; import com.viaversion.viabackwards.api.rewriters.TranslatableRewriter; import com.viaversion.viabackwards.protocol.protocol1_20_3to1_20_5.data.BackwardsMappings; +import com.viaversion.viabackwards.protocol.protocol1_20_3to1_20_5.provider.TransferProvider; import com.viaversion.viabackwards.protocol.protocol1_20_3to1_20_5.rewriter.BlockItemPacketRewriter1_20_5; import com.viaversion.viabackwards.protocol.protocol1_20_3to1_20_5.rewriter.EntityPacketRewriter1_20_5; +import com.viaversion.viabackwards.protocol.protocol1_20_3to1_20_5.storage.CookieStorage; import com.viaversion.viabackwards.protocol.protocol1_20_3to1_20_5.storage.SecureChatStorage; +import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.minecraft.entities.EntityTypes1_20_5; +import com.viaversion.viaversion.api.platform.providers.ViaProviders; import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType; +import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; import com.viaversion.viaversion.api.protocol.packet.ServerboundPacketType; import com.viaversion.viaversion.api.protocol.packet.State; import com.viaversion.viaversion.api.type.Type; +import com.viaversion.viaversion.api.type.types.ByteArrayType; import com.viaversion.viaversion.data.entity.EntityTrackerBase; import com.viaversion.viaversion.protocols.base.ClientboundLoginPackets; +import com.viaversion.viaversion.protocols.base.ServerboundLoginPackets; import com.viaversion.viaversion.protocols.protocol1_20_2to1_20.packet.ServerboundConfigurationPackets1_20_2; import com.viaversion.viaversion.protocols.protocol1_20_3to1_20_2.packet.ClientboundConfigurationPackets1_20_3; import com.viaversion.viaversion.protocols.protocol1_20_3to1_20_2.packet.ClientboundPackets1_20_3; @@ -47,6 +54,7 @@ import com.viaversion.viaversion.rewriter.TagRewriter; public final class Protocol1_20_3To1_20_5 extends BackwardsProtocol { public static final BackwardsMappings MAPPINGS = new BackwardsMappings(); + private static final ByteArrayType COOKIE_DATA_TYPE = new ByteArrayType(5120); private final EntityPacketRewriter1_20_5 entityRewriter = new EntityPacketRewriter1_20_5(this); private final BlockItemPacketRewriter1_20_5 itemRewriter = new BlockItemPacketRewriter1_20_5(this); private final TranslatableRewriter translatableRewriter = new TranslatableRewriter<>(this, ReadType.NBT); @@ -95,37 +103,59 @@ public final class Protocol1_20_3To1_20_5 extends BackwardsProtocol { - wrapper.cancel(); - }); - registerClientbound(State.CONFIGURATION, ClientboundConfigurationPackets1_20_5.COOKIE_REQUEST.getId(), -1, wrapper -> { - wrapper.cancel(); - }); - registerClientbound(State.CONFIGURATION, ClientboundConfigurationPackets1_20_5.STORE_COOKIE.getId(), -1, wrapper -> { - wrapper.cancel(); - }); - registerClientbound(State.CONFIGURATION, ClientboundConfigurationPackets1_20_5.TRANSFER.getId(), -1, wrapper -> { - wrapper.cancel(); - }); - registerClientbound(ClientboundPackets1_20_5.COOKIE_REQUEST, null, wrapper -> { - wrapper.cancel(); - }); - registerClientbound(ClientboundPackets1_20_5.STORE_COOKIE, null, wrapper -> { - wrapper.cancel(); - }); - registerClientbound(ClientboundPackets1_20_5.TRANSFER, null, wrapper -> { - wrapper.cancel(); - }); + registerClientbound(State.LOGIN, ClientboundLoginPackets.COOKIE_REQUEST.getId(), -1, wrapper -> handleCookieRequest(wrapper, ServerboundLoginPackets.COOKIE_RESPONSE)); + registerClientbound(State.CONFIGURATION, ClientboundConfigurationPackets1_20_5.COOKIE_REQUEST.getId(), -1, wrapper -> handleCookieRequest(wrapper, ServerboundConfigurationPackets1_20_5.COOKIE_RESPONSE)); + registerClientbound(State.CONFIGURATION, ClientboundConfigurationPackets1_20_5.STORE_COOKIE.getId(), -1, this::handleStoreCookie); + registerClientbound(State.CONFIGURATION, ClientboundConfigurationPackets1_20_5.TRANSFER.getId(), -1, this::handleTransfer); + registerClientbound(ClientboundPackets1_20_5.COOKIE_REQUEST, null, wrapper -> handleCookieRequest(wrapper, ServerboundPackets1_20_5.COOKIE_RESPONSE)); + registerClientbound(ClientboundPackets1_20_5.STORE_COOKIE, null, this::handleStoreCookie); + registerClientbound(ClientboundPackets1_20_5.TRANSFER, null, this::handleTransfer); registerClientboundPacketIdChanges(State.CONFIGURATION, ClientboundConfigurationPackets1_20_5.class, ClientboundConfigurationPackets1_20_3.class); registerServerboundPacketIdChanges(State.CONFIGURATION, ServerboundConfigurationPackets1_20_2.class, ServerboundConfigurationPackets1_20_5.class); } + private void handleStoreCookie(final PacketWrapper wrapper) throws Exception { + wrapper.cancel(); + + final String resourceLocation = wrapper.read(Type.STRING); + final byte[] data = wrapper.read(COOKIE_DATA_TYPE); + wrapper.user().get(CookieStorage.class).cookies().put(resourceLocation, data); + } + + private void handleCookieRequest(final PacketWrapper wrapper, final ServerboundPacketType responseType) throws Exception { + wrapper.cancel(); + + final String resourceLocation = wrapper.read(Type.STRING); + final byte[] data = wrapper.user().get(CookieStorage.class).cookies().get(resourceLocation); + if (data == null) { + return; + } + + final PacketWrapper responsePacket = wrapper.create(responseType); + responsePacket.write(Type.STRING, resourceLocation); + responsePacket.write(COOKIE_DATA_TYPE, data); + responsePacket.scheduleSendToServer(Protocol1_20_3To1_20_5.class); + } + + private void handleTransfer(final PacketWrapper wrapper) throws Exception { + wrapper.cancel(); + + final String host = wrapper.read(Type.STRING); + final int port = wrapper.read(Type.VAR_INT); + Via.getManager().getProviders().get(TransferProvider.class).connectToServer(wrapper.user(), host, port); + } + @Override public void init(final UserConnection user) { addEntityTracker(user, new EntityTrackerBase(user, EntityTypes1_20_5.PLAYER)); user.put(new SecureChatStorage()); + user.put(new CookieStorage()); + } + + @Override + public void register(final ViaProviders providers) { + providers.register(TransferProvider.class, TransferProvider.NOOP); } @Override diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_20_3to1_20_5/provider/NoopTransferProvider.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_20_3to1_20_5/provider/NoopTransferProvider.java new file mode 100644 index 00000000..0e444a71 --- /dev/null +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_20_3to1_20_5/provider/NoopTransferProvider.java @@ -0,0 +1,27 @@ +/* + * This file is part of ViaBackwards - https://github.com/ViaVersion/ViaBackwards + * Copyright (C) 2016-2024 ViaVersion and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.viaversion.viabackwards.protocol.protocol1_20_3to1_20_5.provider; + +import com.viaversion.viaversion.api.connection.UserConnection; + +final class NoopTransferProvider implements TransferProvider { + + @Override + public void connectToServer(final UserConnection connection, final String host, final int port) { + } +} diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_20_3to1_20_5/provider/TransferProvider.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_20_3to1_20_5/provider/TransferProvider.java new file mode 100644 index 00000000..127b4279 --- /dev/null +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_20_3to1_20_5/provider/TransferProvider.java @@ -0,0 +1,29 @@ +/* + * This file is part of ViaBackwards - https://github.com/ViaVersion/ViaBackwards + * Copyright (C) 2016-2024 ViaVersion and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.viaversion.viabackwards.protocol.protocol1_20_3to1_20_5.provider; + +import com.viaversion.viaversion.api.connection.UserConnection; +import com.viaversion.viaversion.api.platform.providers.Provider; + +@FunctionalInterface +public interface TransferProvider extends Provider { + + TransferProvider NOOP = new NoopTransferProvider(); + + void connectToServer(UserConnection connection, String host, int port); +} diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_20_3to1_20_5/storage/CookieStorage.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_20_3to1_20_5/storage/CookieStorage.java new file mode 100644 index 00000000..be2fd0a5 --- /dev/null +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_20_3to1_20_5/storage/CookieStorage.java @@ -0,0 +1,36 @@ +/* + * This file is part of ViaBackwards - https://github.com/ViaVersion/ViaBackwards + * Copyright (C) 2016-2024 ViaVersion and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.viaversion.viabackwards.protocol.protocol1_20_3to1_20_5.storage; + +import com.viaversion.viaversion.api.connection.StorableObject; +import java.util.HashMap; +import java.util.Map; + +public final class CookieStorage implements StorableObject { + + private final Map cookies = new HashMap<>(); + + public Map cookies() { + return cookies; + } + + @Override + public boolean clearOnServerSwitch() { + return false; + } +}