From 04cfc31c3f6661a0942e608919602e2e4bac8084 Mon Sep 17 00:00:00 2001 From: masmc05 Date: Mon, 28 Oct 2024 14:21:03 +0200 Subject: [PATCH 1/3] Allow responding to cookie requests --- .../api/event/player/CookieRequestEvent.java | 30 ++++++++++++++++--- .../backend/BackendPlaySessionHandler.java | 3 ++ .../backend/ConfigSessionHandler.java | 3 ++ .../backend/LoginSessionHandler.java | 3 ++ .../connection/client/ConnectedPlayer.java | 3 ++ 5 files changed, 38 insertions(+), 4 deletions(-) diff --git a/api/src/main/java/com/velocitypowered/api/event/player/CookieRequestEvent.java b/api/src/main/java/com/velocitypowered/api/event/player/CookieRequestEvent.java index 2f3db4e4c1..0cbe18a984 100644 --- a/api/src/main/java/com/velocitypowered/api/event/player/CookieRequestEvent.java +++ b/api/src/main/java/com/velocitypowered/api/event/player/CookieRequestEvent.java @@ -68,15 +68,19 @@ public String toString() { */ public static final class ForwardResult implements Result { - private static final ForwardResult ALLOWED = new ForwardResult(true, null); - private static final ForwardResult DENIED = new ForwardResult(false, null); + private static final ForwardResult ALLOWED = new ForwardResult(true, null, false, null); + private static final ForwardResult DENIED = new ForwardResult(false, null, false, null); private final boolean status; private final Key key; + private final boolean respond; + private final byte[] data; - private ForwardResult(final boolean status, final Key key) { + private ForwardResult(final boolean status, final Key key, boolean respond, byte[] data) { this.status = status; this.key = key; + this.respond = respond; + this.data = data; } @Override @@ -88,6 +92,14 @@ public Key getKey() { return key; } + public boolean shouldRespond() { + return respond; + } + + public byte[] getData() { + return data; + } + @Override public String toString() { return status ? "forward to client" : "handled by proxy"; @@ -112,6 +124,16 @@ public static ForwardResult handled() { return DENIED; } + /** + * Sends this response to the request to the server + * + * @param data the data to send, null will mean missing data + * @return a result with the data + */ + public static ForwardResult respond(final byte [] data) { + return new ForwardResult(false, null, true, data); + } + /** * Allows the cookie request to be forwarded to the client, but silently replaces the * identifier of the cookie with another. @@ -121,7 +143,7 @@ public static ForwardResult handled() { */ public static ForwardResult key(final Key key) { Preconditions.checkNotNull(key, "key"); - return new ForwardResult(true, key); + return new ForwardResult(true, key, false, null); } } } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BackendPlaySessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BackendPlaySessionHandler.java index 128d5c370a..de89d04219 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BackendPlaySessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BackendPlaySessionHandler.java @@ -62,6 +62,7 @@ import com.velocitypowered.proxy.protocol.packet.ResourcePackRequestPacket; import com.velocitypowered.proxy.protocol.packet.ResourcePackResponsePacket; import com.velocitypowered.proxy.protocol.packet.ServerDataPacket; +import com.velocitypowered.proxy.protocol.packet.ServerboundCookieResponsePacket; import com.velocitypowered.proxy.protocol.packet.TabCompleteResponsePacket; import com.velocitypowered.proxy.protocol.packet.TransferPacket; import com.velocitypowered.proxy.protocol.packet.UpsertPlayerInfoPacket; @@ -435,6 +436,8 @@ public boolean handle(ClientboundCookieRequestPacket packet) { ? event.getOriginalKey() : event.getResult().getKey(); playerConnection.write(new ClientboundCookieRequestPacket(resultedKey)); + } else if (event.getResult().shouldRespond()) { + serverConn.ensureConnected().write(new ServerboundCookieResponsePacket(packet.getKey(), event.getResult().getData())); } }, playerConnection.eventLoop()); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/ConfigSessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/ConfigSessionHandler.java index 74f0576c17..c0571af8a1 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/ConfigSessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/ConfigSessionHandler.java @@ -46,6 +46,7 @@ import com.velocitypowered.proxy.protocol.packet.RemoveResourcePackPacket; import com.velocitypowered.proxy.protocol.packet.ResourcePackRequestPacket; import com.velocitypowered.proxy.protocol.packet.ResourcePackResponsePacket; +import com.velocitypowered.proxy.protocol.packet.ServerboundCookieResponsePacket; import com.velocitypowered.proxy.protocol.packet.TransferPacket; import com.velocitypowered.proxy.protocol.packet.config.ClientboundCustomReportDetailsPacket; import com.velocitypowered.proxy.protocol.packet.config.ClientboundServerLinksPacket; @@ -324,6 +325,8 @@ public boolean handle(ClientboundCookieRequestPacket packet) { ? event.getOriginalKey() : event.getResult().getKey(); serverConn.getPlayer().getConnection().write(new ClientboundCookieRequestPacket(resultedKey)); + } else if (event.getResult().shouldRespond()) { + serverConn.ensureConnected().write(new ServerboundCookieResponsePacket(event.getOriginalKey(), event.getResult().getData())); } }, serverConn.ensureConnected().eventLoop()); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/LoginSessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/LoginSessionHandler.java index 612e9c25a0..6d80b96a05 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/LoginSessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/LoginSessionHandler.java @@ -41,6 +41,7 @@ import com.velocitypowered.proxy.protocol.packet.LoginPluginMessagePacket; import com.velocitypowered.proxy.protocol.packet.LoginPluginResponsePacket; import com.velocitypowered.proxy.protocol.packet.ServerLoginSuccessPacket; +import com.velocitypowered.proxy.protocol.packet.ServerboundCookieResponsePacket; import com.velocitypowered.proxy.protocol.packet.SetCompressionPacket; import com.velocitypowered.proxy.util.except.QuietRuntimeException; import io.netty.buffer.ByteBuf; @@ -189,6 +190,8 @@ public boolean handle(ClientboundCookieRequestPacket packet) { ? event.getOriginalKey() : event.getResult().getKey(); serverConn.getPlayer().getConnection().write(new ClientboundCookieRequestPacket(resultedKey)); + } else if (event.getResult().shouldRespond()) { + serverConn.ensureConnected().write(new ServerboundCookieResponsePacket(event.getOriginalKey(), event.getResult().getData())); } }, serverConn.ensureConnected().eventLoop()); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java index 46c3d63d26..635a6a4ef4 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java @@ -78,6 +78,7 @@ import com.velocitypowered.proxy.protocol.packet.KeepAlivePacket; import com.velocitypowered.proxy.protocol.packet.PluginMessagePacket; import com.velocitypowered.proxy.protocol.packet.RemoveResourcePackPacket; +import com.velocitypowered.proxy.protocol.packet.ServerboundCookieResponsePacket; import com.velocitypowered.proxy.protocol.packet.TransferPacket; import com.velocitypowered.proxy.protocol.packet.chat.ChatQueue; import com.velocitypowered.proxy.protocol.packet.chat.ChatType; @@ -1071,6 +1072,8 @@ public void requestCookie(final Key key) { ? event.getOriginalKey() : event.getResult().getKey(); connection.write(new ClientboundCookieRequestPacket(resultedKey)); + } else if (event.getResult().shouldRespond()) { + this.ensureBackendConnection().write(new ServerboundCookieResponsePacket(event.getOriginalKey(), event.getResult().getData())); } }, connection.eventLoop()); } From 61423d38780a52b99155dcf08756a457e5bf471d Mon Sep 17 00:00:00 2001 From: masmc05 Date: Mon, 28 Oct 2024 14:38:21 +0200 Subject: [PATCH 2/3] Add the missing period --- .../velocitypowered/api/event/player/CookieRequestEvent.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/src/main/java/com/velocitypowered/api/event/player/CookieRequestEvent.java b/api/src/main/java/com/velocitypowered/api/event/player/CookieRequestEvent.java index 0cbe18a984..fc319dadd4 100644 --- a/api/src/main/java/com/velocitypowered/api/event/player/CookieRequestEvent.java +++ b/api/src/main/java/com/velocitypowered/api/event/player/CookieRequestEvent.java @@ -125,7 +125,7 @@ public static ForwardResult handled() { } /** - * Sends this response to the request to the server + * Sends this response to the request to the server. * * @param data the data to send, null will mean missing data * @return a result with the data From 1250d3a68d057110ab35db6c50eb844acca4ac06 Mon Sep 17 00:00:00 2001 From: masmc05 Date: Sun, 24 Nov 2024 15:43:26 +0200 Subject: [PATCH 3/3] Update the toString --- .../api/event/player/CookieRequestEvent.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/api/src/main/java/com/velocitypowered/api/event/player/CookieRequestEvent.java b/api/src/main/java/com/velocitypowered/api/event/player/CookieRequestEvent.java index fc319dadd4..e54b37a6b1 100644 --- a/api/src/main/java/com/velocitypowered/api/event/player/CookieRequestEvent.java +++ b/api/src/main/java/com/velocitypowered/api/event/player/CookieRequestEvent.java @@ -11,6 +11,7 @@ import com.velocitypowered.api.event.ResultedEvent; import com.velocitypowered.api.event.annotation.AwaitingEvent; import com.velocitypowered.api.proxy.Player; +import java.util.Base64; import net.kyori.adventure.key.Key; /** @@ -102,7 +103,13 @@ public byte[] getData() { @Override public String toString() { - return status ? "forward to client" : "handled by proxy"; + if (status) { + return "Forward to client"; + } + if (respond) { + return "Respond with " + Base64.getEncoder().encodeToString(data); + } + return "Handled by proxy"; } /**