Skip to content

feat: add cookies#507

Closed
BorisP1234 wants to merge 8 commits into
minekube:masterfrom
BorisP1234:master
Closed

feat: add cookies#507
BorisP1234 wants to merge 8 commits into
minekube:masterfrom
BorisP1234:master

Conversation

@BorisP1234

@BorisP1234 BorisP1234 commented Feb 20, 2025

Copy link
Copy Markdown
Contributor

Hello, cookie packets were introduced in Minecraft 1.20.5 along with transfer packets, an underrated feature that can be particularly useful in multi-proxy scenarios. That’s why I found it unfortunate that this functionality wasn’t included in the Gate API, so I decided to add it myself!

I added three functions to the player variable:

  • player.CookieStore(key key.Key, payload []byte) error
    This function sends a cookie to the player, storing it even between transfers until the player disconnects. It returns an error if:

    • The key is empty.
    • The payload is too large.
    • The player's state is not "play" or "config."
    • The player is using a version lower than 1.20.5.
    • An error occurs while sending the packet.
  • player.CookieRequest(key key.Key) error
    This function requests a cookie from the player. The player then sends a response packet, which can be handled with the PlayerCookieResponseEvent (explained later). It returns an error if:

    • The key is empty.
    • The player is using a version lower than 1.20.5.
    • An error occurs while sending the packet.
  • player.CookieRequestWithResult(key key.Key) ([]byte, error)
    This function retrieves a cookie immediately instead of relying on PlayerCookieResponseEvent. It waits for a response from the player for up to 5 seconds. It returns an error if:

    • The key is empty.
    • The player is using a version lower than 1.20.5.
    • An error occurs while sending the packet.
    • The response takes longer than 5 seconds.

I added a new event called PlayerCookieResponseEvent, which listens for cookie response packets. This event contains the player, key, and payload. If the requested cookie is not found, an empty payload is returned.

You can test the features with this wonderful mod that I found called: CookieJar by Tis_awesomeness.

@robinbraemer

Copy link
Copy Markdown
Member

Thank you! I'll try to merge when I find the time

Comment thread pkg/edition/java/proxy/player.go Outdated
Comment thread pkg/edition/java/proxy/player.go Outdated
Comment thread pkg/edition/java/proxy/player.go Outdated
Comment thread pkg/edition/java/proxy/player.go Outdated
Comment thread pkg/edition/java/proxy/player.go Outdated
Comment thread pkg/edition/java/proxy/session_client_auth.go Outdated
Comment thread pkg/edition/java/proxy/session_client_auth.go Outdated
Comment thread pkg/edition/java/proxy/session_client_auth.go Outdated
Comment thread pkg/edition/java/proxy/session_client_config.go
Comment thread pkg/edition/java/proxy/session_client_play.go Outdated
@robinbraemer

Copy link
Copy Markdown
Member

As an afterthought, can we move all public cookie logic into a standalone package? I'm trying to avoid adding more functions to the Player interface. I'm thinking a cookie package similar to https://github.com/minekube/gate/tree/master/pkg/edition/java/bossbar.

@BorisP1234

Copy link
Copy Markdown
Contributor Author

Okay I'll take a look at it 👍

@BorisP1234 BorisP1234 reopened this May 1, 2025
@BorisP1234 BorisP1234 requested a review from robinbraemer May 1, 2025 22:40
@BorisP1234

Copy link
Copy Markdown
Contributor Author

Okay I think i managed to resolve every requested change. @robinbraemer Is this how you visioned the cookie package?

The only thing I don't really like is how the CookieResponseEvent doesn't return the Cookie structure, since its not possible due to import cycle. This also made it so I needed to put the map in the proxy package instead of the cookie package but I don't think that is a problem.

@SinonCute

Copy link
Copy Markdown

When this can be merged, I’m waiting for it

@robinbraemer

robinbraemer commented May 15, 2025

Copy link
Copy Markdown
Member

There are a few things I have to iterate on.

  • thinking ideal interface
  • avoid full proxy.Player
  • channel handling
  • thinking sync Pool is the right choice (there is a potential leak when it is created when doesn’t exist yet)
  • testing with varint + writebytes or writebytesraw should be used

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants