Skip to content

Commit

Permalink
full cookie support
Browse files Browse the repository at this point in the history
  • Loading branch information
Tnze committed Jun 16, 2024
1 parent 5f5db75 commit 7d54712
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 17 deletions.
2 changes: 2 additions & 0 deletions bot/basic/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ func NewPlayer(c *bot.Client, settings Settings, events EventsListener) *Player
bot.PacketHandler{Priority: 0, ID: packetid.ClientboundKeepAlive, F: p.handleKeepAlivePacket},
bot.PacketHandler{Priority: 0, ID: packetid.ClientboundRespawn, F: p.handleRespawnPacket},
bot.PacketHandler{Priority: 0, ID: packetid.ClientboundPing, F: p.handlePingPacket},
bot.PacketHandler{Priority: 0, ID: packetid.ClientboundCookieRequest, F: p.handleCookieRequestPacket},
bot.PacketHandler{Priority: 0, ID: packetid.ClientboundStoreCookie, F: p.handleStoreCookiePacket},
)
events.attach(p)
return p
Expand Down
37 changes: 37 additions & 0 deletions bot/basic/cookie.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package basic

import (
"github.com/Tnze/go-mc/data/packetid"
pk "github.com/Tnze/go-mc/net/packet"
)

func (p *Player) handleCookieRequestPacket(packet pk.Packet) error {
var key pk.Identifier
err := packet.Scan(&key)
if err != nil {
return Error{err}
}
cookieContent := p.c.Cookies[string(key)]
err = p.c.Conn.WritePacket(pk.Marshal(
packetid.ServerboundCookieResponse,
key, pk.OptionEncoder[pk.ByteArray]{
Has: cookieContent != nil,
Val: pk.ByteArray(cookieContent),
},
))
if err != nil {
return Error{err}
}
return nil
}

func (p *Player) handleStoreCookiePacket(packet pk.Packet) error {
var key pk.Identifier
var payload pk.ByteArray
err := packet.Scan(&key, &payload)
if err != nil {
return Error{err}
}
p.c.Cookies[string(key)] = []byte(payload)
return nil
}
1 change: 1 addition & 0 deletions bot/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type Client struct {
Name string
UUID uuid.UUID
Registries registry.NetworkCodec
Cookies map[string][]byte

// Ingame packet handlers
Events Events
Expand Down
22 changes: 6 additions & 16 deletions bot/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ import (
)

type ConfigHandler interface {
GetCookie(key pk.Identifier) []byte
SetCookie(key pk.Identifier, payload []byte)

EnableFeature(features []pk.Identifier)

PushResourcePack(res ResourcePack)
Expand Down Expand Up @@ -60,10 +57,13 @@ func (c *Client) joinConfiguration(conn *net.Conn) error {
if err != nil {
return ConfigErr{"cookie request", err}
}
cookieContent := c.ConfigHandler.GetCookie(key)
cookieContent := c.Cookies[string(key)]
err = conn.WritePacket(pk.Marshal(
packetid.ServerboundConfigCookieResponse,
pk.ByteArray(cookieContent),
key, pk.OptionEncoder[pk.ByteArray]{
Has: cookieContent != nil,
Val: pk.ByteArray(cookieContent),
},
))
if err != nil {
return ConfigErr{"cookie response", err}
Expand Down Expand Up @@ -230,7 +230,7 @@ func (c *Client) joinConfiguration(conn *net.Conn) error {
if err != nil {
return ConfigErr{"store cookie", err}
}
c.ConfigHandler.SetCookie(key, []byte(payload))
c.Cookies[string(key)] = []byte(payload)

case packetid.ClientboundConfigTransfer:
var host pk.String
Expand Down Expand Up @@ -328,25 +328,15 @@ func (d *DataPack) ReadFrom(r io.Reader) (n int64, err error) {
}

type DefaultConfigHandler struct {
cookies map[pk.Identifier][]byte
resourcesPack []ResourcePack
}

func NewDefaultConfigHandler() *DefaultConfigHandler {
return &DefaultConfigHandler{
cookies: make(map[pk.String][]byte),
resourcesPack: make([]ResourcePack, 0),
}
}

func (d *DefaultConfigHandler) GetCookie(key pk.Identifier) []byte {
return d.cookies[key]
}

func (d *DefaultConfigHandler) SetCookie(key pk.Identifier, payload []byte) {
d.cookies[key] = payload
}

func (d *DefaultConfigHandler) EnableFeature(features []pk.Identifier) {}

func (d *DefaultConfigHandler) PushResourcePack(res ResourcePack) {
Expand Down
20 changes: 19 additions & 1 deletion bot/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,24 @@ func (c *Client) joinLogin(conn *net.Conn) error {
)); err != nil {
return LoginErr{"login Plugin", err}
}

case packetid.ClientboundLoginCookieRequest:
var key pk.Identifier
err := p.Scan(&key)
if err != nil {
return LoginErr{"cookie request", err}
}
cookieContent := c.Cookies[string(key)]
err = conn.WritePacket(pk.Marshal(
packetid.ServerboundLoginCookieResponse,
key, pk.OptionEncoder[pk.ByteArray]{
Has: cookieContent != nil,
Val: pk.ByteArray(cookieContent),
},
))
if err != nil {
return LoginErr{"cookie response", err}
}
}
}
}
Expand Down Expand Up @@ -303,7 +321,7 @@ func genEncryptionKeyResponse(shareSecret, publicKey, verifyToken []byte) (erp p
return erp, err
}
return pk.Marshal(
packetid.ServerboundLoginEncryptionResponse,
packetid.ServerboundLoginKey,
pk.ByteArray(cryptPK),
pk.ByteArray(verifyT),
), nil
Expand Down

0 comments on commit 7d54712

Please sign in to comment.