-
Notifications
You must be signed in to change notification settings - Fork 0
/
packet.go
177 lines (166 loc) · 3.84 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package gorcon
import (
"io"
"golang.org/x/exp/slices"
)
type PacketType int32
const (
PacketTypeCommand PacketType = 2
PacketTypeLogin PacketType = 3
PacketTypeResponse PacketType = 0
PayloadChunkLength int = 4096
)
type BinaryPacket struct {
RequestID int32
Type PacketType
Body []byte
}
type Packet struct {
RequestID int32
Type PacketType
Body string
}
func (bp *BinaryPacket) Text() (*Packet, error) {
zero := slices.Index(bp.Body, 0)
if zero == -1 {
return nil, &RCONErrorNotZeroTerminatedPayload{
RequestID: bp.RequestID,
Type: bp.Type,
Payload: bp.Body,
}
}
return &Packet{
RequestID: bp.RequestID,
Type: bp.Type,
Body: string(bp.Body[:zero]),
}, nil
}
func ReadBinaryPacket(r io.Reader) (*BinaryPacket, error) {
rawLength := make([]byte, 4)
n, err := r.Read(rawLength)
if err != nil {
return nil, err
}
if n != 4 {
return nil, &RCONErrorInvalidLength{
Expected: 4,
Got: n,
Condition: Exactly,
}
}
length := MCEndian.Uint32(rawLength)
if length < 10 || length > 1456 {
return nil, &RCONErrorInvalidLength{
Expected: 10,
Expected2: 1456,
Got: n,
Condition: Range,
}
}
data := make([]byte, length)
n, err = r.Read(data)
if err != nil {
return nil, err
}
if n != int(length) {
return nil, &RCONErrorInvalidLength{
Expected: int(length),
Got: n,
Condition: Exactly,
}
}
return &BinaryPacket{
RequestID: int32(MCEndian.Uint32(data)),
Type: PacketType(MCEndian.Uint32(data[4:])),
Body: data[8 : len(data)-1],
}, nil
}
func ReadPacket(r io.Reader) (*Packet, error) {
bp, err := ReadBinaryPacket(r)
if err != nil {
return nil, err
}
zero := slices.Index(bp.Body, 0)
if zero == -1 {
return nil, &RCONErrorNotZeroTerminatedPayload{
RequestID: bp.RequestID,
Type: bp.Type,
Payload: bp.Body,
}
}
return &Packet{
RequestID: bp.RequestID,
Type: bp.Type,
Body: string(bp.Body[:zero]),
}, nil
/*rawLength := make([]byte, 4)
n, err := r.Read(rawLength)
if err != nil {
return nil, err
}
if n != 4 {
return nil, &RCONErrorInvalidLength{
Expected: 4,
Got: n,
Condition: Exactly,
}
}
length := MCEndian.Uint32(rawLength)
if length < 10 || length > 1456 {
return nil, &RCONErrorInvalidLength{
Expected: 10,
Expected2: 1456,
Got: n,
Condition: Range,
}
}
payload := make([]byte, length)
n, err = r.Read(payload)
if err != nil {
return nil, err
}
if n != int(length) {
return nil, &RCONErrorInvalidLength{
Expected: int(length),
Got: n,
Condition: Exactly,
}
}
data := payload[8 : length-2]
zero := slices.Index(data, 0)
if zero == -1 {
return nil, &RCONErrorNotZeroTerminatedPayload{
RequestID: int32(MCEndian.Uint32(payload)),
Type: PacketType(MCEndian.Uint32(payload[4:])),
Payload: data,
}
}
return &Packet{
RequestID: int32(MCEndian.Uint32(payload)),
Type: PacketType(MCEndian.Uint32(payload[4:])),
Body: string(data[:zero]),
}, nil*/
}
func WriteBinaryPacket(w io.Writer, packet BinaryPacket) (int, error) {
payload := []byte(packet.Body)
payloadLength := len(payload)
data := make([]byte, 15+payloadLength)
MCEndian.PutUint32(data, uint32(11+payloadLength))
MCEndian.PutUint32(data[4:], uint32(packet.RequestID))
MCEndian.PutUint32(data[8:], uint32(packet.Type))
copy(data[12:], payload)
data[13+payloadLength] = 0
data[14+payloadLength] = 0
return w.Write(data)
}
func WritePacket(w io.Writer, packet Packet) (int, error) {
payload := []byte(packet.Body)
payloadLength := len(payload)
data := make([]byte, 14+payloadLength)
MCEndian.PutUint32(data, uint32(10+payloadLength))
MCEndian.PutUint32(data[4:], uint32(packet.RequestID))
MCEndian.PutUint32(data[8:], uint32(packet.Type))
copy(data[12:], payload)
data[13+payloadLength] = 0
return w.Write(data)
}