idea: deal with cookies and transfer

This commit is contained in:
onebeastchris 2024-04-26 14:50:48 +02:00
parent 8e3a3ea453
commit 91a74603c7
9 changed files with 251 additions and 7 deletions

View file

@ -70,6 +70,5 @@ public class WrittenBookItem extends Item {
builder.putString("title", bookContent.getTitle().getRaw())
.putString("author", bookContent.getAuthor())
.putInt("generation", bookContent.getGeneration());
// TODO isResolved
}
}

View file

@ -54,11 +54,14 @@ public final class LocalSession extends TcpSession {
private final String clientIp;
private final PacketCodecHelper codecHelper;
public LocalSession(String host, int port, SocketAddress targetAddress, String clientIp, PacketProtocol protocol, MinecraftCodecHelper codecHelper) {
private final boolean transferring;
public LocalSession(String host, int port, SocketAddress targetAddress, String clientIp, PacketProtocol protocol, MinecraftCodecHelper codecHelper, boolean transferring) {
super(host, port, protocol);
this.targetAddress = targetAddress;
this.clientIp = clientIp;
this.codecHelper = codecHelper;
this.transferring = transferring;
}
@Override
@ -79,7 +82,7 @@ public final class LocalSession extends TcpSession {
public void initChannel(@NonNull LocalChannelWithRemoteAddress channel) {
channel.spoofedRemoteAddress(new InetSocketAddress(clientIp, 0));
PacketProtocol protocol = getPacketProtocol();
protocol.newClientSession(LocalSession.this, false);
protocol.newClientSession(LocalSession.this, transferring);
refreshReadTimeoutHandler(channel);
refreshWriteTimeoutHandler(channel);

View file

@ -569,6 +569,12 @@ public class GeyserSession implements GeyserConnection, GeyserCommandSource {
@Setter
private @Nullable ItemData currentBook = null;
/**
* Stores cookies sent by the Java server.
*/
@Setter @Getter
private Map<String, byte[]> cookies = new Object2ObjectOpenHashMap<>();
private final GeyserCameraData cameraData;
private final GeyserEntityData entityData;
@ -853,7 +859,7 @@ public class GeyserSession implements GeyserConnection, GeyserCommandSource {
* After getting whatever credentials needed, we attempt to join the Java server.
*/
private void connectDownstream() {
SessionLoginEvent loginEvent = new SessionLoginEvent(this, remoteServer);
SessionLoginEvent loginEvent = new SessionLoginEvent(this, remoteServer, new Object2ObjectOpenHashMap<>());
GeyserImpl.getInstance().eventBus().fire(loginEvent);
if (loginEvent.isCancelled()) {
String disconnectReason = loginEvent.disconnectReason() == null ?
@ -862,6 +868,7 @@ public class GeyserSession implements GeyserConnection, GeyserCommandSource {
return;
}
this.cookies = loginEvent.cookies();
this.remoteServer = loginEvent.remoteServer();
boolean floodgate = this.remoteServer.authType() == AuthType.FLOODGATE;
@ -873,7 +880,7 @@ public class GeyserSession implements GeyserConnection, GeyserCommandSource {
// We're going to connect through the JVM and not through TCP
downstream = new LocalSession(this.remoteServer.address(), this.remoteServer.port(),
geyser.getBootstrap().getSocketAddress(), upstream.getAddress().getAddress().getHostAddress(),
this.protocol, this.protocol.createHelper());
this.protocol, this.protocol.createHelper(), loginEvent.transferring());
this.downstream = new DownstreamSession(downstream);
} else {
downstream = new TcpClientSession(this.remoteServer.address(), this.remoteServer.port(), this.protocol);

View file

@ -0,0 +1,42 @@
/*
* Copyright (c) 2019-2024 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.geyser.translator.protocol.java.entity.player;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.translator.protocol.PacketTranslator;
import org.geysermc.mcprotocollib.protocol.packet.common.clientbound.ClientboundCookieRequestPacket;
import org.geysermc.mcprotocollib.protocol.packet.common.clientbound.ServerboundCookieResponsePacket;
public class JavaCookieRequestTranslator extends PacketTranslator<ClientboundCookieRequestPacket> {
@Override
public void translate(GeyserSession session, ClientboundCookieRequestPacket packet) {
ServerboundCookieResponsePacket responsePacket = new ServerboundCookieResponsePacket(
packet.getKey(),
session.getCookies().get(packet.getKey())
);
session.sendDownstreamPacket(responsePacket);
}
}

View file

@ -0,0 +1,37 @@
/*
* Copyright (c) 2019-2024 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.geyser.translator.protocol.java.entity.player;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.translator.protocol.PacketTranslator;
import org.geysermc.mcprotocollib.protocol.packet.common.clientbound.ClientboundStoreCookiePacket;
public class JavaStoreCookieTranslator extends PacketTranslator<ClientboundStoreCookiePacket> {
@Override
public void translate(GeyserSession session, ClientboundStoreCookiePacket packet) {
session.getCookies().put(packet.getKey(), packet.getPayload());
}
}

View file

@ -0,0 +1,43 @@
/*
* Copyright (c) 2019-2024 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.geyser.translator.protocol.java.entity.player;
import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.api.event.java.ServerTransferEvent;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.translator.protocol.PacketTranslator;
import org.geysermc.mcprotocollib.protocol.packet.common.clientbound.ClientboundTransferPacket;
public class JavaTransferPacketTranslator extends PacketTranslator<ClientboundTransferPacket> {
@Override
public void translate(GeyserSession session, ClientboundTransferPacket packet) {
GeyserImpl.getInstance().eventBus().fire(new ServerTransferEvent(
session,
packet.getHost(),
packet.getPort(),
session.getCookies()));
}
}