Skip to content

Commit

Permalink
added cookie store and retrieve API
Browse files Browse the repository at this point in the history
  • Loading branch information
Outfluencer committed Jan 20, 2024
1 parent 0e51f06 commit cc979ab
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Locale;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import net.md_5.bungee.api.Callback;
import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.CommandSender;
Expand Down Expand Up @@ -339,4 +340,26 @@ public enum MainHand
*/
@Deprecated
Scoreboard getScoreboard();

/**
* Retrieves a cookie from this player.
*
* @param cookie the resource location of the cookie, for example "bungeecord:my_cookie"
*
* @return a {@link CompletableFuture} that will be completed when the Cookie response is received
* @apiNote Only useable for 1.20.5 clients or newer,
* if the cookie is not set in the client, the {@link CompletableFuture} will complete with a null value
*/
CompletableFuture<byte[]> retrieveCookie(String cookie);


/**
* Stores a cookie in this player's client.
*
* @param cookie the resource location of the cookie, for example "bungeecord:my_cookie"
* @param data the data to store in the cookie
*
* @apiNote Only useable for 1.20.5 clients or newer
*/
void storeCookie(String cookie, byte[] data);
}
13 changes: 13 additions & 0 deletions proxy/src/main/java/net/md_5/bungee/UserConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Objects;
import java.util.Queue;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.logging.Level;
import lombok.Getter;
Expand Down Expand Up @@ -771,4 +772,16 @@ public Scoreboard getScoreboard()
{
return serverSentScoreboard;
}

@Override
public CompletableFuture<byte[]> retrieveCookie(String cookie)
{
return pendingConnection.retrieveCookie( cookie );
}

@Override
public void storeCookie(String cookie, byte[] data)
{
pendingConnection.storeCookie( cookie, data );
}
}
57 changes: 57 additions & 0 deletions proxy/src/main/java/net/md_5/bungee/connection/InitialHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,19 @@
import java.security.MessageDigest;
import java.time.Instant;
import java.util.HashSet;
import java.util.Queue;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.logging.Level;
import javax.crypto.SecretKey;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
import net.md_5.bungee.BungeeCord;
import net.md_5.bungee.BungeeServerInfo;
import net.md_5.bungee.EncryptionUtil;
Expand Down Expand Up @@ -51,6 +58,8 @@
import net.md_5.bungee.protocol.PlayerPublicKey;
import net.md_5.bungee.protocol.Protocol;
import net.md_5.bungee.protocol.ProtocolConstants;
import net.md_5.bungee.protocol.packet.CookieRequest;
import net.md_5.bungee.protocol.packet.CookieResponse;
import net.md_5.bungee.protocol.packet.EncryptionRequest;
import net.md_5.bungee.protocol.packet.EncryptionResponse;
import net.md_5.bungee.protocol.packet.Handshake;
Expand All @@ -64,6 +73,7 @@
import net.md_5.bungee.protocol.packet.PluginMessage;
import net.md_5.bungee.protocol.packet.StatusRequest;
import net.md_5.bungee.protocol.packet.StatusResponse;
import net.md_5.bungee.protocol.packet.StoreCookie;
import net.md_5.bungee.util.AllowedCharacters;
import net.md_5.bungee.util.BufUtil;
import net.md_5.bungee.util.QuietException;
Expand All @@ -86,6 +96,18 @@ public class InitialHandler extends PacketHandler implements PendingConnection
@Getter
private final Set<String> registeredChannels = new HashSet<>();
private State thisState = State.HANDSHAKE;
private final Queue<CookieFuture> requestedCookies = new ConcurrentLinkedQueue<>();

@Data
@ToString
@EqualsAndHashCode
@AllArgsConstructor
public static class CookieFuture
{
private String cookie;
private CompletableFuture<byte[]> future;
}

private final Unsafe unsafe = new Unsafe()
{
@Override
Expand Down Expand Up @@ -653,6 +675,19 @@ public void handle(LoginPayloadResponse response) throws Exception
disconnect( "Unexpected custom LoginPayloadResponse" );
}

@Override
public void handle(CookieResponse cookieResponse)
{
// be careful spigot could also make the client send a cookie response
CookieFuture future = requestedCookies.peek();
if ( future != null && future.cookie.equals( cookieResponse.getCookie() ) )
{
requestedCookies.remove();
future.getFuture().complete( cookieResponse.getData() );
throw CancelSendSignal.INSTANCE;
}
}

@Override
public void disconnect(String reason)
{
Expand Down Expand Up @@ -785,4 +820,26 @@ public void relayMessage(PluginMessage input) throws Exception
brandMessage = input;
}
}

public CompletableFuture<byte[]> retrieveCookie(String cookie)
{
Preconditions.checkState( getVersion() >= ProtocolConstants.MINECRAFT_1_20_5, "Cookies are only supported in 1.20.5 and above" );
Preconditions.checkState( loginRequest != null, "Cannot retrieve cookies for Status or legacy connections" );
if ( cookie.indexOf( ':' ) == -1 )
{
// if we request an invalid resource location (no prefix) the client will respond with minecraft: prefix
cookie = "minecraft:" + cookie;
}
CompletableFuture<byte[]> future = new CompletableFuture<>();
requestedCookies.add( new CookieFuture( cookie, future ) );
unsafe.sendPacket( new CookieRequest( cookie ) );
return future;
}

public void storeCookie(String cookie, byte[] data)
{
Preconditions.checkState( getVersion() >= ProtocolConstants.MINECRAFT_1_20_5, "Cookies are only supported in 1.20.5 and above" );
Preconditions.checkState( loginRequest != null, "Cannot retrieve cookies for Status or legacy connections" );
unsafe().sendPacket( new StoreCookie( cookie, data ) );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import net.md_5.bungee.protocol.packet.ClientChat;
import net.md_5.bungee.protocol.packet.ClientCommand;
import net.md_5.bungee.protocol.packet.ClientSettings;
import net.md_5.bungee.protocol.packet.CookieResponse;
import net.md_5.bungee.protocol.packet.FinishConfiguration;
import net.md_5.bungee.protocol.packet.KeepAlive;
import net.md_5.bungee.protocol.packet.LoginAcknowledged;
Expand Down Expand Up @@ -363,6 +364,12 @@ public void handle(FinishConfiguration finishConfiguration) throws Exception
con.sendQueuedPackets();
}

@Override
public void handle(CookieResponse cookieResponse) throws Exception
{
con.getPendingConnection().handle( cookieResponse );
}

@Override
public String toString()
{
Expand Down

0 comments on commit cc979ab

Please sign in to comment.