-
Notifications
You must be signed in to change notification settings - Fork 0
/
mc_packet.go
282 lines (230 loc) · 6.6 KB
/
mc_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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// Based on https://github.com/realDragonium/Ultraviolet/blob/main/mc/packet.go
package main
import (
"bytes"
"errors"
"fmt"
"io"
"strings"
)
const (
MaxPacketSize = 2097151
ServerBoundHandshakePacketID byte = 0x00
ServerBoundLoginStartPacketID byte = 0x00
ForgeSeparator = "\x00"
RealIPSeparator = "///"
)
var (
ErrInvalidPacketID = errors.New("invalid packet id")
ErrPacketTooBig = errors.New("packet contains too much data")
ErrExtraData = errors.New("packet contains extra unread data")
ErrUnsupported = errors.New("packet contains unsupported data")
)
// Packet is the raw representation of message that is send between the client and the server
type Packet struct {
ID byte
Data []byte
}
// Scan decodes and copies the Packet data into the fields
func (pk Packet) Scan(fields ...FieldDecoder) (*bytes.Reader, error) {
r := bytes.NewReader(pk.Data)
for _, field := range fields {
if err := field.Decode(r); err != nil {
return r, err
}
}
// if r.Len() > 0 {
// return ErrExtraData
// }
return r, nil
}
// Marshal encodes the packet and all it's fields
func (pk *Packet) Encode() []byte {
var packedData []byte
data := []byte{pk.ID}
data = append(data, pk.Data...)
packetLength := McVarInt(int32(len(data))).Encode()
packedData = append(packedData, packetLength...)
return append(packedData, data...)
}
func ReadPacket(r DecodeReader) (Packet, error) {
var packetLength McVarInt
err := packetLength.Decode(r)
if err != nil {
return Packet{}, err
}
if packetLength < 1 {
return Packet{}, fmt.Errorf("packet length too short")
}
data := make([]byte, packetLength)
if _, err := io.ReadFull(r, data); err != nil {
return Packet{}, fmt.Errorf("reading the content of the packet failed: %v", err)
}
return Packet{
ID: data[0],
Data: data[1:],
}, nil
}
///////////////////////////////////////////////////////////////////////////////
type ServerBoundHandshake struct {
ProtocolVersion McVarInt
ServerRawAddress McString
Address string
ServerPort McUnsignedShort
NextState McVarInt
}
const (
HandshakeStatus = 1
HandshakeLogin = 2
)
func (pk ServerBoundHandshake) ToPacket() *Packet {
var packet = &Packet{}
packet.ID = ServerBoundHandshakePacketID
packet.Data = pk.ProtocolVersion.Encode()
packet.Data = append(packet.Data, pk.ServerRawAddress.Encode()...)
packet.Data = append(packet.Data, pk.ServerPort.Encode()...)
packet.Data = append(packet.Data, pk.NextState.Encode()...)
return packet
}
func DecodeServerBoundHandshake(packet Packet) (ServerBoundHandshake, error) {
var pk ServerBoundHandshake
if packet.ID != ServerBoundHandshakePacketID {
return pk, ErrInvalidPacketID
}
_, err := packet.Scan(&pk.ProtocolVersion, &pk.ServerRawAddress, &pk.ServerPort, &pk.NextState)
if err != nil {
return pk, err
}
// Parse ServerAddress
addr := string(pk.ServerRawAddress)
addr = strings.Split(addr, ForgeSeparator)[0]
addr = strings.Split(addr, RealIPSeparator)[0]
pk.Address = addr
return pk, nil
}
///////////////////////////////////////////////////////////////////////////////
type ServerLoginStartOLD struct { // 1.18.2 and older
Nickname McString
}
func (pk ServerLoginStartOLD) ToPacket() *Packet {
var packet = &Packet{}
packet.ID = ServerBoundLoginStartPacketID
packet.Data = pk.Nickname.Encode()
return packet
}
func DecodeServerBoundLoginStartOLD(packet Packet) (ServerLoginStartOLD, error) {
var pk ServerLoginStartOLD
if packet.ID != ServerBoundLoginStartPacketID {
return pk, ErrInvalidPacketID
}
_, err := packet.Scan(&pk.Nickname)
if err != nil {
return pk, err
}
return pk, nil
}
///////////////////////////////////////////////////////////////////////////////
type ServerLoginStart759 struct { // 1.19 - 1.19.2
Nickname McString
HasSigData McByte
HasUUID McByte
UUID McUUID
}
func (pk ServerLoginStart759) ToPacket() *Packet {
var packet = &Packet{}
packet.ID = ServerBoundLoginStartPacketID
packet.Data = pk.Nickname.Encode()
packet.Data = append(packet.Data, pk.HasSigData.Encode()...)
packet.Data = append(packet.Data, pk.HasUUID.Encode()...)
if pk.HasUUID != 0 {
packet.Data = append(packet.Data, pk.UUID.Encode()...)
}
return packet
}
func DecodeServerBoundLoginStart759(packet Packet) (ServerLoginStart759, error) {
var pk ServerLoginStart759
if packet.ID != ServerBoundLoginStartPacketID {
return pk, ErrInvalidPacketID
}
reader, err := packet.Scan(&pk.Nickname)
if err != nil {
return pk, err
}
pk.HasSigData.Decode(reader)
if pk.HasSigData != 0 {
return pk, ErrUnsupported
}
pk.HasUUID.Decode(reader)
if pk.HasUUID != 0 {
pk.UUID.Decode(reader)
}
return pk, nil
}
///////////////////////////////////////////////////////////////////////////////
type ServerLoginStart761 struct { // 1.19.3 - 1.20.1
Nickname McString
HasUUID McByte
UUID McUUID
}
func (pk ServerLoginStart761) ToPacket() *Packet {
var packet = &Packet{}
packet.ID = ServerBoundLoginStartPacketID
packet.Data = pk.Nickname.Encode()
packet.Data = append(packet.Data, pk.HasUUID.Encode()...)
if pk.HasUUID != 0 {
packet.Data = append(packet.Data, pk.UUID.Encode()...)
}
return packet
}
func DecodeServerBoundLoginStart761(packet Packet) (ServerLoginStart761, error) {
var pk ServerLoginStart761
if packet.ID != ServerBoundLoginStartPacketID {
return pk, ErrInvalidPacketID
}
reader, err := packet.Scan(&pk.Nickname)
if err != nil {
return pk, err
}
pk.HasUUID.Decode(reader)
if pk.HasUUID != 0 {
pk.UUID.Decode(reader)
}
return pk, nil
}
///////////////////////////////////////////////////////////////////////////////
type ServerLoginStart764 struct { // 1.20.2 - last
Nickname McString
UUID McUUID
}
func (pk ServerLoginStart764) ToPacket() *Packet {
var packet = &Packet{}
packet.ID = ServerBoundLoginStartPacketID
packet.Data = pk.Nickname.Encode()
packet.Data = append(packet.Data, pk.UUID.Encode()...)
return packet
}
func DecodeServerBoundLoginStart764(packet Packet) (ServerLoginStart764, error) {
var pk ServerLoginStart764
if packet.ID != ServerBoundLoginStartPacketID {
return pk, ErrInvalidPacketID
}
_, err := packet.Scan(&pk.Nickname, &pk.UUID)
if err != nil {
return pk, err
}
return pk, nil
}
///////////////////////////////////////////////////////////////////////////////
type StatusJSON struct {
Version StatusVersionJSON `json:"version"`
// Players PlaydersJSON `json:"players"`
Description StatusDescriptionJSON `json:"description"`
Favicon string `json:"favicon"`
}
type StatusVersionJSON struct {
Name string `json:"name"`
Protocol int `json:"protocol"`
}
type StatusDescriptionJSON struct {
Text string `json:"text"`
}