Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add simple login payload api #3736

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,17 @@ public interface PendingConnection extends Connection
*/
@ApiStatus.Experimental
CompletableFuture<byte[]> retrieveCookie(String cookie);

/**
* Sends a login payload request to the client.
*
* @param channel the channel to send this data via
* @param data the data to send
* @return a {@link CompletableFuture} that will be completed when the
* Login Payload response is received. If the client doesn't know the channel,
* the {@link CompletableFuture} will complete with a null value
* @throws IllegalStateException if the player's version is not at least
* 1.13
*/
CompletableFuture<byte[]> sendData(String channel, byte[] data);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
import java.security.GeneralSecurityException;
import java.security.MessageDigest;
import java.time.Instant;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.UUID;
Expand Down Expand Up @@ -67,6 +69,7 @@
import net.md_5.bungee.protocol.packet.LegacyHandshake;
import net.md_5.bungee.protocol.packet.LegacyPing;
import net.md_5.bungee.protocol.packet.LoginAcknowledged;
import net.md_5.bungee.protocol.packet.LoginPayloadRequest;
import net.md_5.bungee.protocol.packet.LoginPayloadResponse;
import net.md_5.bungee.protocol.packet.LoginRequest;
import net.md_5.bungee.protocol.packet.LoginSuccess;
Expand Down Expand Up @@ -96,6 +99,8 @@ public class InitialHandler extends PacketHandler implements PendingConnection
@Getter
private final Set<String> registeredChannels = new HashSet<>();
private State thisState = State.HANDSHAKE;
private int loginPayloadId;
private final Map<Integer, CompletableFuture<byte[]>> requestedLoginPayloads = new HashMap<>();
private final Queue<CookieFuture> requestedCookies = new LinkedList<>();

@Data
Expand Down Expand Up @@ -688,7 +693,13 @@ public void done(PostLoginEvent result, Throwable error)
@Override
public void handle(LoginPayloadResponse response) throws Exception
{
disconnect( "Unexpected custom LoginPayloadResponse" );
CompletableFuture<byte[]> future;
synchronized ( requestedLoginPayloads )
{
future = requestedLoginPayloads.remove( response.getId() );
}
Preconditions.checkState( future != null, "Unexpected custom LoginPayloadResponse" );
future.complete( response.getData() );
}

@Override
Expand Down Expand Up @@ -884,4 +895,22 @@ public CompletableFuture<byte[]> retrieveCookie(String cookie)

return future;
}

@Override
public CompletableFuture<byte[]> sendData(String channel, byte[] data)
{
Preconditions.checkState( getVersion() >= ProtocolConstants.MINECRAFT_1_13, "LoginPayloads are only supported in 1.13 and above" );
Preconditions.checkState( loginRequest != null, "Cannot send login data for status or legacy connections" );

CompletableFuture<byte[]> future = new CompletableFuture<>();
final int id;
synchronized ( requestedLoginPayloads )
{
// thread safe loginPayloadId
id = loginPayloadId++;
requestedLoginPayloads.put( id, future );
}
unsafe.sendPacket( new LoginPayloadRequest( id, channel, data ) );
return future;
}
}