Skip to content

Commit

Permalink
feat: add cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
BorisP1234 committed Feb 20, 2025
1 parent c5f087e commit 43b5a94
Show file tree
Hide file tree
Showing 11 changed files with 216 additions and 4 deletions.
22 changes: 22 additions & 0 deletions pkg/edition/java/proto/packet/cookie/cookie_request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package cookie

import (
"io"

"go.minekube.com/common/minecraft/key"
"go.minekube.com/gate/pkg/edition/java/proto/util"
"go.minekube.com/gate/pkg/gate/proto"
)

type CookieRequest struct {
Key key.Key
}

func (c *CookieRequest) Encode(ctx *proto.PacketContext, wr io.Writer) error {
return util.WriteKey(wr, c.Key)
}

func (c *CookieRequest) Decode(ctx *proto.PacketContext, rd io.Reader) (err error) {
c.Key, err = util.ReadKey(rd)
return err
}
32 changes: 32 additions & 0 deletions pkg/edition/java/proto/packet/cookie/cookie_response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package cookie

import (
"io"

"go.minekube.com/common/minecraft/key"
"go.minekube.com/gate/pkg/edition/java/proto/util"
"go.minekube.com/gate/pkg/gate/proto"
)

type CookieResponse struct {
Key key.Key
Payload []byte
}

func (c *CookieResponse) Encode(ctx *proto.PacketContext, wr io.Writer) error {
if err := util.WriteKey(wr, c.Key); err != nil {
return err
}

return util.WriteBytes(wr, c.Payload)
}

func (c *CookieResponse) Decode(ctx *proto.PacketContext, rd io.Reader) (err error) {
c.Key, err = util.ReadKey(rd)
if err != nil {
return err
}

c.Payload, err = util.ReadRawBytes(rd)
return err
}
32 changes: 32 additions & 0 deletions pkg/edition/java/proto/packet/cookie/cookie_store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package cookie

import (
"io"

"go.minekube.com/common/minecraft/key"
"go.minekube.com/gate/pkg/edition/java/proto/util"
"go.minekube.com/gate/pkg/gate/proto"
)

type CookieStore struct {
Key key.Key
Payload []byte
}

func (c *CookieStore) Encode(ctx *proto.PacketContext, wr io.Writer) error {
if err := util.WriteKey(wr, c.Key); err != nil {
return err
}

return util.WriteBytes(wr, c.Payload)
}

func (c *CookieStore) Decode(ctx *proto.PacketContext, rd io.Reader) (err error) {
c.Key, err = util.ReadKey(rd)
if err != nil {
return err
}

c.Payload, err = util.ReadBytes(rd)
return err
}
24 changes: 24 additions & 0 deletions pkg/edition/java/proto/state/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"go.minekube.com/gate/pkg/edition/java/proto/packet/bossbar"
"go.minekube.com/gate/pkg/edition/java/proto/packet/chat"
"go.minekube.com/gate/pkg/edition/java/proto/packet/config"
"go.minekube.com/gate/pkg/edition/java/proto/packet/cookie"
"go.minekube.com/gate/pkg/edition/java/proto/packet/plugin"
"go.minekube.com/gate/pkg/edition/java/proto/packet/tablist/legacytablist"
"go.minekube.com/gate/pkg/edition/java/proto/packet/tablist/playerinfo"
Expand Down Expand Up @@ -61,6 +62,9 @@ func init() {
Config.ServerBound.Register(&config.KnownPacks{},
m(0x07, version.Minecraft_1_20_5),
)
Config.ServerBound.Register(&cookie.CookieResponse{},
m(0x01, version.Minecraft_1_20_5),
)

Config.ClientBound.Register(&plugin.Message{},
m(0x00, version.Minecraft_1_20_2),
Expand Down Expand Up @@ -117,6 +121,12 @@ func init() {
Config.ClientBound.Register(&p.ServerLinks{},
m(0x10, version.Minecraft_1_21),
)
Config.ClientBound.Register(&cookie.CookieRequest{},
m(0x00, version.Minecraft_1_20_5),
)
Config.ClientBound.Register(&cookie.CookieStore{},
m(0x0A, version.Minecraft_1_20_5),
)

Login.ServerBound.Register(&p.ServerLogin{},
m(0x00, version.Minecraft_1_7_2))
Expand All @@ -126,6 +136,9 @@ func init() {
m(0x02, version.Minecraft_1_13))
Login.ServerBound.Register(&p.LoginAcknowledged{},
m(0x03, version.Minecraft_1_20_2))
Login.ServerBound.Register(&cookie.CookieResponse{},
m(0x04, version.Minecraft_1_20_5),
)

Login.ClientBound.Register(&p.Disconnect{},
m(0x00, version.Minecraft_1_7_2))
Expand All @@ -137,6 +150,8 @@ func init() {
m(0x03, version.Minecraft_1_8))
Login.ClientBound.Register(&p.LoginPluginMessage{},
m(0x04, version.Minecraft_1_13))
Login.ClientBound.Register(&cookie.CookieRequest{},
m(0x05, version.Minecraft_1_20_5))

Play.ServerBound.Fallback = false
Play.ClientBound.Fallback = false
Expand Down Expand Up @@ -259,6 +274,9 @@ func init() {
m(0x0C, version.Minecraft_1_20_5),
m(0x0E, version.Minecraft_1_21_2),
)
Play.ServerBound.Register(&cookie.CookieResponse{},
m(0x13, version.Minecraft_1_20_5),
)

Play.ClientBound.Register(&p.KeepAlive{},
m(0x00, version.Minecraft_1_7_2),
Expand Down Expand Up @@ -570,4 +588,10 @@ func init() {
Play.ClientBound.Register(&p.ServerLinks{},
m(0x7B, version.Minecraft_1_21),
)
Play.ClientBound.Register(&cookie.CookieRequest{},
m(0x16, version.Minecraft_1_20_5),
)
Play.ClientBound.Register(&cookie.CookieStore{},
m(0x72, version.Minecraft_1_20_5),
)
}
2 changes: 1 addition & 1 deletion pkg/edition/java/proto/util/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (
"encoding/binary"
"errors"
"fmt"
"go.minekube.com/common/minecraft/key"
"io"
"math"
"strings"
"time"

"go.minekube.com/common/minecraft/key"
"go.minekube.com/gate/pkg/edition/java/profile"
"go.minekube.com/gate/pkg/util/uuid"
)
Expand Down
34 changes: 34 additions & 0 deletions pkg/edition/java/proxy/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"go.minekube.com/brigodier"
"go.minekube.com/common/minecraft/component"
"go.minekube.com/common/minecraft/key"

"go.minekube.com/gate/pkg/command"
"go.minekube.com/gate/pkg/edition/java/forge/modinfo"
Expand Down Expand Up @@ -1163,3 +1164,36 @@ func (r *ReadyEvent) Addr() string { return r.addr }
// Subscribe to this event to gracefully stop any subtasks,
// such as plugin dependencies.
type ShutdownEvent struct{}

// PlayerCookieResponseEvent is fired when a player sends the cookie requested from the server.
type PlayerCookieResponseEvent struct {
player Player
key key.Key
payload []byte
}

func newPlayerCookieResponseEvent(player Player, key key.Key, payload []byte) *PlayerCookieResponseEvent {
return &PlayerCookieResponseEvent{
player: player,
key: key,
payload: payload,
}
}

// Player returns the player from whom the cookie has been received.
func (c *PlayerCookieResponseEvent) Player() Player { return c.player }

// Key returns the provider of the responded cookie.
// For example: minecraft:cookie
func (c *PlayerCookieResponseEvent) Key() key.Key { return c.key }

// Payload returns the payload of the responded cookie.
func (c *PlayerCookieResponseEvent) Payload() []byte {
if len(c.payload) <= 2 {
return []byte{}
}
// The payload in the cookie response packet has two bytes before the actual payload, which show the length.
// So the packet should use the util.ReadBytesLen(rd, 5120) but unfortunately i couldn't get that to work, as it makes the payload empty.
// TODO: fix this instead of this shortcut
return c.payload[2:]
}
44 changes: 44 additions & 0 deletions pkg/edition/java/proxy/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/robinbraemer/event"

cfgpacket "go.minekube.com/gate/pkg/edition/java/proto/packet/config"
"go.minekube.com/gate/pkg/edition/java/proto/packet/cookie"
"go.minekube.com/gate/pkg/edition/java/proto/state"
"go.minekube.com/gate/pkg/edition/java/proto/state/states"
"go.minekube.com/gate/pkg/edition/java/proxy/internal/resourcepack"
Expand All @@ -23,6 +24,7 @@ import (
"github.com/go-logr/logr"
"go.minekube.com/common/minecraft/component"
"go.minekube.com/common/minecraft/component/codec/legacy"
"go.minekube.com/common/minecraft/key"
"go.uber.org/atomic"

"go.minekube.com/gate/pkg/edition/java/config"
Expand Down Expand Up @@ -108,6 +110,11 @@ type Player interface { // TODO convert to struct(?) bc this is a lot of methods
//
// Deprecated: Use PendingResourcePacks instead.
PendingResourcePack() *ResourcePackInfo

StoreCookie(key key.Key, payload []byte) error

// Sends request for a cookie which the player will respond to in the PlayerCookieResponseEvent if he has the cookie
RequestCookie(key key.Key) error
}

type connectedPlayer struct {
Expand Down Expand Up @@ -735,3 +742,40 @@ func (p *connectedPlayer) BackendInFlight() proto.PacketWriter {
}
return nil
}

func (p *connectedPlayer) StoreCookie(key key.Key, payload []byte) error {
if strings.TrimSpace(key.String()) == "" {
return errors.New("empty key")
}

if len(payload) > 5*1024 {
return errors.New("payload size exceeds 5 kiB")
}

if p.Protocol().Lower(version.Minecraft_1_20_5) {
return fmt.Errorf("%w: but player is on %s", ErrTransferUnsupportedClientProtocol, p.Protocol())
}

if p.State() != state.Play && p.State() != state.Config {
return errors.New("CookieStore packet can only be send in the Play and Configuration State")
}

return p.WritePacket(&cookie.CookieStore{
Key: key,
Payload: payload,
})
}

func (p *connectedPlayer) RequestCookie(key key.Key) error {
if strings.TrimSpace(key.String()) == "" {
return errors.New("empty key")
}

if p.Protocol().Lower(version.Minecraft_1_20_5) {
return fmt.Errorf("%w: but player is on %s", ErrTransferUnsupportedClientProtocol, p.Protocol())
}

return p.WritePacket(&cookie.CookieRequest{
Key: key,
})
}
12 changes: 10 additions & 2 deletions pkg/edition/java/proxy/session_client_auth.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package proxy

import (
"sync/atomic"

"github.com/go-logr/logr"
"github.com/robinbraemer/event"
"go.minekube.com/common/minecraft/component"
"go.minekube.com/gate/pkg/edition/java/config"
"go.minekube.com/gate/pkg/edition/java/netmc"
"go.minekube.com/gate/pkg/edition/java/profile"
"go.minekube.com/gate/pkg/edition/java/proto/packet"
"go.minekube.com/gate/pkg/edition/java/proto/packet/cookie"
"go.minekube.com/gate/pkg/edition/java/proto/state"
"go.minekube.com/gate/pkg/edition/java/proto/version"
"go.minekube.com/gate/pkg/edition/java/proxy/crypto"
"go.minekube.com/gate/pkg/gate/proto"
"go.minekube.com/gate/pkg/util/uuid"
"sync/atomic"
)

type authSessionHandler struct {
Expand Down Expand Up @@ -228,9 +230,11 @@ func (a *authSessionHandler) connectToInitialServer(player *connectedPlayer) {
func (a *authSessionHandler) Deactivated() {}

func (a *authSessionHandler) HandlePacket(pc *proto.PacketContext) {
switch pc.Packet.(type) {
switch t := pc.Packet.(type) {
case *packet.LoginAcknowledged:
a.handleLoginAcknowledged()
case *cookie.CookieResponse:
a.handleCookieResponse(t)
default:
a.log.Info("unexpected packet during auth session",
"packet", pc.Packet,
Expand Down Expand Up @@ -265,3 +269,7 @@ func (a *authSessionHandler) handleLoginAcknowledged() bool {
}
return true
}

func (a *authSessionHandler) handleCookieResponse(p *cookie.CookieResponse) {
event.FireParallel(a.eventMgr, newPlayerCookieResponseEvent(a.connectedPlayer, p.Key, p.Payload))
}
8 changes: 8 additions & 0 deletions pkg/edition/java/proxy/session_client_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package proxy

import (
"bytes"

"github.com/go-logr/logr"
"github.com/robinbraemer/event"
"go.minekube.com/gate/pkg/edition/java/proto/packet"
"go.minekube.com/gate/pkg/edition/java/proto/packet/config"
"go.minekube.com/gate/pkg/edition/java/proto/packet/cookie"
"go.minekube.com/gate/pkg/edition/java/proto/packet/plugin"
"go.minekube.com/gate/pkg/edition/java/proto/state"
"go.minekube.com/gate/pkg/edition/java/proto/util"
Expand Down Expand Up @@ -67,6 +69,8 @@ func (h *clientConfigSessionHandler) HandlePacket(pc *proto.PacketContext) {
}
case *config.KnownPacks:
h.handleKnownPacks(p, pc)
case *cookie.CookieResponse:
h.handleCookieResponse(p)
default:
forwardToServer(pc, h.player)
}
Expand Down Expand Up @@ -141,3 +145,7 @@ func (h *clientConfigSessionHandler) handleKnownPacks(p *config.KnownPacks, pc *
func (h *clientConfigSessionHandler) event() event.Manager {
return h.player.proxy.Event()
}

func (h *clientConfigSessionHandler) handleCookieResponse(p *cookie.CookieResponse) {
event.FireParallel(h.event(), newPlayerCookieResponseEvent(h.player, p.Key, p.Payload))
}
3 changes: 2 additions & 1 deletion pkg/edition/java/proxy/session_client_initial_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import (
"context"
"crypto/rand"
"errors"
"go.minekube.com/gate/pkg/edition/java/proto/state"
"regexp"
"time"

"go.minekube.com/gate/pkg/edition/java/proto/state"

"github.com/go-logr/logr"
"go.minekube.com/common/minecraft/color"
"go.minekube.com/common/minecraft/component"
Expand Down
Loading

0 comments on commit 43b5a94

Please sign in to comment.