Add Cookie API (#1313)

This commit is contained in:
Luccboy
2024-05-30 17:52:30 +02:00
committed by GitHub
parent deacdb6228
commit 07f1f9e7bd
16 changed files with 917 additions and 0 deletions

View File

@@ -0,0 +1,155 @@
/*
* Copyright (C) 2024 Velocity Contributors
*
* The Velocity API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the api top-level directory.
*/
package com.velocitypowered.api.event.player;
import com.google.common.base.Preconditions;
import com.velocitypowered.api.event.ResultedEvent;
import com.velocitypowered.api.event.annotation.AwaitingEvent;
import com.velocitypowered.api.proxy.Player;
import java.util.Arrays;
import net.kyori.adventure.key.Key;
import org.jetbrains.annotations.Nullable;
/**
* This event is fired when a cookie response from a client is received by the proxy.
* This usually happens after either a proxy plugin or a backend server requested a cookie.
* Velocity will wait on this event to finish firing before discarding the
* received cookie (if handled) or forwarding it to the backend server.
*/
@AwaitingEvent
public final class CookieReceiveEvent implements ResultedEvent<CookieReceiveEvent.ForwardResult> {
private final Player player;
private final Key originalKey;
private final byte @Nullable [] originalData;
private ForwardResult result;
/**
* Creates a new instance.
*
* @param player the player who sent the cookie response
* @param key the identifier of the cookie
* @param data the data of the cookie
*/
public CookieReceiveEvent(final Player player, final Key key, final byte @Nullable [] data) {
this.player = Preconditions.checkNotNull(player, "player");
this.originalKey = Preconditions.checkNotNull(key, "key");
this.originalData = data;
this.result = ForwardResult.forward();
}
@Override
public ForwardResult getResult() {
return result;
}
@Override
public void setResult(ForwardResult result) {
this.result = Preconditions.checkNotNull(result, "result");
}
public Player getPlayer() {
return player;
}
public Key getOriginalKey() {
return originalKey;
}
public byte @Nullable [] getOriginalData() {
return originalData;
}
@Override
public String toString() {
return "CookieReceiveEvent{"
+ ", originalKey=" + originalKey
+ ", originalData=" + Arrays.toString(originalData)
+ ", result=" + result
+ '}';
}
/**
* A result determining whether or not to forward the cookie response on.
*/
public static final class ForwardResult implements ResultedEvent.Result {
private static final ForwardResult ALLOWED = new ForwardResult(true, null, null);
private static final ForwardResult DENIED = new ForwardResult(false, null, null);
private final boolean status;
private final Key key;
private final byte[] data;
private ForwardResult(final boolean status, final Key key, final byte[] data) {
this.status = status;
this.key = key;
this.data = data;
}
@Override
public boolean isAllowed() {
return status;
}
public Key getKey() {
return key;
}
public byte[] getData() {
return data;
}
@Override
public String toString() {
return status ? "forward to backend server" : "handled by proxy";
}
/**
* Allows the cookie response to be forwarded to the backend server.
*
* @return the forward result
*/
public static ForwardResult forward() {
return ALLOWED;
}
/**
* Prevents the cookie response from being forwarded to the backend server, the cookie response
* is handled by the proxy.
*
* @return the handled result
*/
public static ForwardResult handled() {
return DENIED;
}
/**
* Allows the cookie response to be forwarded to the backend server, but silently replaces the
* identifier of the cookie with another.
*
* @param key the identifier to use instead
* @return a result with a new key
*/
public static ForwardResult key(final Key key) {
Preconditions.checkNotNull(key, "key");
return new ForwardResult(true, key, null);
}
/**
* Allows the cookie response to be forwarded to the backend server, but silently replaces the
* data of the cookie with another.
*
* @param data the data of the cookie to use instead
* @return a result with new data
*/
public static ForwardResult data(final byte[] data) {
return new ForwardResult(true, null, data);
}
}
}

View File

@@ -0,0 +1,127 @@
/*
* Copyright (C) 2024 Velocity Contributors
*
* The Velocity API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the api top-level directory.
*/
package com.velocitypowered.api.event.player;
import com.google.common.base.Preconditions;
import com.velocitypowered.api.event.ResultedEvent;
import com.velocitypowered.api.event.annotation.AwaitingEvent;
import com.velocitypowered.api.proxy.Player;
import net.kyori.adventure.key.Key;
/**
* This event is fired when a cookie is requested from a client either by a proxy plugin or
* by a backend server. Velocity will wait on this event to finish firing before discarding the
* received cookie (if handled) or forwarding it to the backend server.
*/
@AwaitingEvent
public final class CookieRequestEvent implements ResultedEvent<CookieRequestEvent.ForwardResult> {
private final Player player;
private final Key originalKey;
private ForwardResult result;
/**
* Creates a new instance.
*
* @param player the player from whom the cookies is requested
* @param key the identifier of the cookie
*/
public CookieRequestEvent(final Player player, final Key key) {
this.player = Preconditions.checkNotNull(player, "player");
this.originalKey = Preconditions.checkNotNull(key, "key");
this.result = ForwardResult.forward();
}
@Override
public ForwardResult getResult() {
return result;
}
@Override
public void setResult(ForwardResult result) {
this.result = Preconditions.checkNotNull(result, "result");
}
public Player getPlayer() {
return player;
}
public Key getOriginalKey() {
return originalKey;
}
@Override
public String toString() {
return "CookieRequestEvent{"
+ ", originalKey=" + originalKey
+ ", result=" + result
+ '}';
}
/**
* A result determining whether or not to forward the cookie request on.
*/
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 final boolean status;
private final Key key;
private ForwardResult(final boolean status, final Key key) {
this.status = status;
this.key = key;
}
@Override
public boolean isAllowed() {
return status;
}
public Key getKey() {
return key;
}
@Override
public String toString() {
return status ? "forward to client" : "handled by proxy";
}
/**
* Allows the cookie request to be forwarded to the client.
*
* @return the forward result
*/
public static ForwardResult forward() {
return ALLOWED;
}
/**
* Prevents the cookie request from being forwarded to the client, the cookie request is
* handled by the proxy.
*
* @return the handled result
*/
public static ForwardResult handled() {
return DENIED;
}
/**
* Allows the cookie response to be forwarded to the client, but silently replaces the
* identifier of the cookie with another.
*
* @param key the identifier to use instead
* @return a result with a new key
*/
public static ForwardResult key(final Key key) {
Preconditions.checkNotNull(key, "key");
return new ForwardResult(true, key);
}
}
}

View File

@@ -0,0 +1,154 @@
/*
* Copyright (C) 2024 Velocity Contributors
*
* The Velocity API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the api top-level directory.
*/
package com.velocitypowered.api.event.player;
import com.google.common.base.Preconditions;
import com.velocitypowered.api.event.ResultedEvent;
import com.velocitypowered.api.event.annotation.AwaitingEvent;
import com.velocitypowered.api.proxy.Player;
import java.util.Arrays;
import net.kyori.adventure.key.Key;
/**
* This event is fired when a cookie should be stored on a player's client. This process can be
* initiated either by a proxy plugin or by a backend server. Velocity will wait on this event
* to finish firing before discarding the cookie (if handled) or forwarding it to the client so
* that it can store the cookie.
*/
@AwaitingEvent
public final class CookieStoreEvent implements ResultedEvent<CookieStoreEvent.ForwardResult> {
private final Player player;
private final Key originalKey;
private final byte[] originalData;
private ForwardResult result;
/**
* Creates a new instance.
*
* @param player the player who should store the cookie
* @param key the identifier of the cookie
* @param data the data of the cookie
*/
public CookieStoreEvent(final Player player, final Key key, final byte[] data) {
this.player = Preconditions.checkNotNull(player, "player");
this.originalKey = Preconditions.checkNotNull(key, "key");
this.originalData = Preconditions.checkNotNull(data, "data");
this.result = ForwardResult.forward();
}
@Override
public ForwardResult getResult() {
return result;
}
@Override
public void setResult(ForwardResult result) {
this.result = Preconditions.checkNotNull(result, "result");
}
public Player getPlayer() {
return player;
}
public Key getOriginalKey() {
return originalKey;
}
public byte[] getOriginalData() {
return originalData;
}
@Override
public String toString() {
return "CookieStoreEvent{"
+ ", originalKey=" + originalKey
+ ", originalData=" + Arrays.toString(originalData)
+ ", result=" + result
+ '}';
}
/**
* A result determining whether or not to forward the cookie on.
*/
public static final class ForwardResult implements Result {
private static final ForwardResult ALLOWED = new ForwardResult(true, null, null);
private static final ForwardResult DENIED = new ForwardResult(false, null, null);
private final boolean status;
private final Key key;
private final byte[] data;
private ForwardResult(final boolean status, final Key key, final byte[] data) {
this.status = status;
this.key = key;
this.data = data;
}
@Override
public boolean isAllowed() {
return status;
}
public Key getKey() {
return key;
}
public byte[] getData() {
return data;
}
@Override
public String toString() {
return status ? "forward to client" : "handled by proxy";
}
/**
* Allows the cookie to be forwarded to the client so that it can store it.
*
* @return the forward result
*/
public static ForwardResult forward() {
return ALLOWED;
}
/**
* Prevents the cookie from being forwarded to the client, the cookie is handled by the proxy.
*
* @return the handled result
*/
public static ForwardResult handled() {
return DENIED;
}
/**
* Allows the cookie to be forwarded to the client so that it can store it, but silently
* replaces the identifier of the cookie with another.
*
* @param key the identifier to use instead
* @return a result with a new key
*/
public static ForwardResult key(final Key key) {
Preconditions.checkNotNull(key, "key");
return new ForwardResult(true, key, null);
}
/**
* Allows the cookie to be forwarded to the client so that it can store it, but silently
* replaces the data of the cookie with another.
*
* @param data the data of the cookie to use instead
* @return a result with new data
*/
public static ForwardResult data(final byte[] data) {
Preconditions.checkNotNull(data, "data");
return new ForwardResult(true, null, data);
}
}
}

View File

@@ -8,6 +8,7 @@
package com.velocitypowered.api.proxy;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.event.player.CookieReceiveEvent;
import com.velocitypowered.api.event.player.PlayerResourcePackStatusEvent;
import com.velocitypowered.api.proxy.crypto.KeyIdentifiable;
import com.velocitypowered.api.proxy.messages.ChannelIdentifier;
@@ -436,4 +437,28 @@ public interface Player extends
* @since 3.3.0
*/
void transferToHost(@NotNull InetSocketAddress address);
/**
* Stores a cookie with arbitrary data on the player's client.
*
* @param key the identifier of the cookie
* @param data the data of the cookie
* @throws IllegalArgumentException if the player is from a version lower than 1.20.5
* @since 3.3.0
* @sinceMinecraft 1.20.5
*/
void storeCookie(Key key, byte[] data);
/**
* Requests a previously stored cookie from the player's client.
* Calling this method causes the client to send the cookie to the proxy.
* To retrieve the actual data of the requested cookie, you have to use the
* {@link CookieReceiveEvent}.
*
* @param key the identifier of the cookie
* @throws IllegalArgumentException if the player is from a version lower than 1.20.5
* @since 3.3.0
* @sinceMinecraft 1.20.5
*/
void requestCookie(Key key);
}