diff --git a/bot/basic/basic.go b/bot/basic/basic.go index 2054c235..2e97ee05 100644 --- a/bot/basic/basic.go +++ b/bot/basic/basic.go @@ -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 diff --git a/bot/basic/cookie.go b/bot/basic/cookie.go new file mode 100644 index 00000000..41ba02ba --- /dev/null +++ b/bot/basic/cookie.go @@ -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 +} diff --git a/bot/client.go b/bot/client.go index 8a7ac98c..9b0c3cce 100644 --- a/bot/client.go +++ b/bot/client.go @@ -22,6 +22,7 @@ type Client struct { Name string UUID uuid.UUID Registries registry.NetworkCodec + Cookies map[string][]byte // Ingame packet handlers Events Events diff --git a/bot/configuration.go b/bot/configuration.go index 10dac57e..6102709e 100644 --- a/bot/configuration.go +++ b/bot/configuration.go @@ -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) @@ -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} @@ -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 @@ -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) { diff --git a/bot/login.go b/bot/login.go index 62c58b2c..4c647a86 100644 --- a/bot/login.go +++ b/bot/login.go @@ -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} + } } } } @@ -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