-
-
Notifications
You must be signed in to change notification settings - Fork 94
/
packet.go
77 lines (69 loc) · 2.24 KB
/
packet.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package minecraft
import (
"bytes"
"errors"
"fmt"
"github.com/sandertv/gophertunnel/minecraft/protocol/packet"
)
// packetData holds the data of a Minecraft packet.
type packetData struct {
h *packet.Header
full []byte
payload *bytes.Buffer
}
// parseData parses the packet data slice passed into a packetData struct.
func parseData(data []byte, conn *Conn) (*packetData, error) {
buf := bytes.NewBuffer(data)
header := &packet.Header{}
if err := header.Read(buf); err != nil {
// We don't return this as an error as it's not in the hand of the user to control this. Instead,
// we return to reading a new packet.
return nil, fmt.Errorf("read packet header: %w", err)
}
if conn.packetFunc != nil {
// The packet func was set, so we call it.
conn.packetFunc(*header, buf.Bytes(), conn.RemoteAddr(), conn.LocalAddr())
}
return &packetData{h: header, full: data, payload: buf}, nil
}
type unknownPacketError struct {
id uint32
}
func (err unknownPacketError) Error() string {
return fmt.Sprintf("unexpected packet (ID=%v)", err.id)
}
// decode decodes the packet payload held in the packetData and returns the packet.Packet decoded.
func (p *packetData) decode(conn *Conn) (pks []packet.Packet, err error) {
defer func() {
if recoveredErr := recover(); recoveredErr != nil {
err = fmt.Errorf("decode packet %v: %w", p.h.PacketID, recoveredErr.(error))
}
if err == nil {
return
}
if ok := errors.As(err, &unknownPacketError{}); ok || conn.disconnectOnInvalidPacket {
_ = conn.Close()
}
}()
// Attempt to fetch the packet with the right packet ID from the pool.
pkFunc, ok := conn.pool[p.h.PacketID]
var pk packet.Packet
if !ok {
// No packet with the ID. This may be a custom packet of some sorts.
pk = &packet.Unknown{PacketID: p.h.PacketID}
if conn.disconnectOnUnknownPacket {
return nil, unknownPacketError{id: p.h.PacketID}
}
} else {
pk = pkFunc()
}
r := conn.proto.NewReader(p.payload, conn.shieldID.Load(), conn.readerLimits)
pk.Marshal(r)
if p.payload.Len() != 0 {
err = fmt.Errorf("decode packet %T: %v unread bytes left: 0x%x", pk, p.payload.Len(), p.payload.Bytes())
}
if conn.disconnectOnInvalidPacket && err != nil {
return nil, err
}
return conn.proto.ConvertToLatest(pk, conn), err
}