-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathprudp_packet_v0.go
388 lines (295 loc) · 10.1 KB
/
prudp_packet_v0.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
package nex
import (
"crypto/hmac"
"crypto/md5"
"encoding/binary"
"errors"
"fmt"
"net"
"slices"
"github.com/PretendoNetwork/nex-go/v2/constants"
)
// PRUDPPacketV0 represents a PRUDPv0 packet
type PRUDPPacketV0 struct {
PRUDPPacket
}
// Copy copies the packet into a new PRUDPPacketV0
//
// Retains the same PRUDPConnection pointer
func (p *PRUDPPacketV0) Copy() PRUDPPacketInterface {
copied, _ := NewPRUDPPacketV0(p.server, p.sender, nil)
copied.server = p.server
copied.sourceVirtualPort = p.sourceVirtualPort
copied.destinationVirtualPort = p.destinationVirtualPort
copied.packetType = p.packetType
copied.flags = p.flags
copied.sessionID = p.sessionID
copied.substreamID = p.substreamID
if p.signature != nil {
copied.signature = append([]byte(nil), p.signature...)
}
copied.sequenceID = p.sequenceID
if p.connectionSignature != nil {
copied.connectionSignature = append([]byte(nil), p.connectionSignature...)
}
copied.fragmentID = p.fragmentID
if p.payload != nil {
copied.payload = append([]byte(nil), p.payload...)
}
if p.message != nil {
copied.message = p.message.Copy()
}
return copied
}
// Version returns the packets PRUDP version
func (p *PRUDPPacketV0) Version() int {
return int(p.version)
}
func (p *PRUDPPacketV0) decode() error {
// * Header is technically 11 bytes but checking for 12 includes the checksum
if p.readStream.Remaining() < 12 {
return errors.New("Failed to read PRUDPv0 header. Not have enough data")
}
server := p.server
start := p.readStream.ByteOffset()
source, err := p.readStream.ReadUInt8()
if err != nil {
return fmt.Errorf("Failed to read PRUDPv0 source. %s", err.Error())
}
p.sourceVirtualPort = VirtualPort(source)
destination, err := p.readStream.ReadUInt8()
if err != nil {
return fmt.Errorf("Failed to read PRUDPv0 destination. %s", err.Error())
}
p.destinationVirtualPort = VirtualPort(destination)
if server.PRUDPV0Settings.IsQuazalMode {
typeAndFlags, err := p.readStream.ReadUInt8()
if err != nil {
return fmt.Errorf("Failed to read PRUDPv0 type and flags. %s", err.Error())
}
p.flags = uint16(typeAndFlags >> 3)
p.packetType = uint16(typeAndFlags & 7)
} else {
typeAndFlags, err := p.readStream.ReadUInt16LE()
if err != nil {
return fmt.Errorf("Failed to read PRUDPv0 type and flags. %s", err.Error())
}
p.flags = typeAndFlags >> 4
p.packetType = typeAndFlags & 0xF
}
p.sessionID, err = p.readStream.ReadUInt8()
if err != nil {
return fmt.Errorf("Failed to read PRUDPv0 session ID. %s", err.Error())
}
p.signature = p.readStream.ReadBytesNext(4)
p.sequenceID, err = p.readStream.ReadUInt16LE()
if err != nil {
return fmt.Errorf("Failed to read PRUDPv0 sequence ID. %s", err.Error())
}
if p.packetType == constants.SynPacket || p.packetType == constants.ConnectPacket {
if p.readStream.Remaining() < 4 {
return errors.New("Failed to read PRUDPv0 connection signature. Not have enough data")
}
p.connectionSignature = p.readStream.ReadBytesNext(4)
}
if p.packetType == constants.DataPacket {
if p.readStream.Remaining() < 1 {
return errors.New("Failed to read PRUDPv0 fragment ID. Not have enough data")
}
p.fragmentID, err = p.readStream.ReadUInt8()
if err != nil {
return fmt.Errorf("Failed to read PRUDPv0 fragment ID. %s", err.Error())
}
}
var payloadSize uint16
if p.HasFlag(constants.PacketFlagHasSize) {
if p.readStream.Remaining() < 2 {
return errors.New("Failed to read PRUDPv0 payload size. Not have enough data")
}
payloadSize, err = p.readStream.ReadUInt16LE()
if err != nil {
return fmt.Errorf("Failed to read PRUDPv0 payload size. %s", err.Error())
}
} else {
// * Some Quazal games use a 4 byte checksum. NEX uses 1 byte
if server.PRUDPV0Settings.UseEnhancedChecksum {
payloadSize = uint16(p.readStream.Remaining() - 4)
} else {
payloadSize = uint16(p.readStream.Remaining() - 1)
}
}
if p.readStream.Remaining() < uint64(payloadSize) {
return errors.New("Failed to read PRUDPv0 payload. Not have enough data")
}
p.payload = p.readStream.ReadBytesNext(int64(payloadSize))
if server.PRUDPV0Settings.UseEnhancedChecksum && p.readStream.Remaining() < 4 {
return errors.New("Failed to read PRUDPv0 checksum. Not have enough data")
} else if p.readStream.Remaining() < 1 {
return errors.New("Failed to read PRUDPv0 checksum. Not have enough data")
}
checksumData := p.readStream.Bytes()[start:p.readStream.ByteOffset()]
var checksum uint32
var checksumU8 uint8
if server.PRUDPV0Settings.UseEnhancedChecksum {
checksum, err = p.readStream.ReadUInt32LE()
} else {
checksumU8, err = p.readStream.ReadUInt8()
checksum = uint32(checksumU8)
}
if err != nil {
return fmt.Errorf("Failed to read PRUDPv0 checksum. %s", err.Error())
}
calculatedChecksum := p.server.PRUDPV0Settings.ChecksumCalculator(p, checksumData)
if checksum != calculatedChecksum {
return errors.New("Invalid PRUDPv0 checksum")
}
return nil
}
// Bytes encodes a PRUDPv0 packet into a byte slice
func (p *PRUDPPacketV0) Bytes() []byte {
server := p.server
stream := NewByteStreamOut(server.LibraryVersions, server.ByteStreamSettings)
stream.WriteUInt8(uint8(p.sourceVirtualPort))
stream.WriteUInt8(uint8(p.destinationVirtualPort))
if server.PRUDPV0Settings.IsQuazalMode {
stream.WriteUInt8(uint8(p.packetType | (p.flags << 3)))
} else {
stream.WriteUInt16LE(p.packetType | (p.flags << 4))
}
stream.WriteUInt8(p.sessionID)
stream.Grow(int64(len(p.signature)))
stream.WriteBytesNext(p.signature)
stream.WriteUInt16LE(p.sequenceID)
if p.packetType == constants.SynPacket || p.packetType == constants.ConnectPacket {
stream.Grow(int64(len(p.connectionSignature)))
stream.WriteBytesNext(p.connectionSignature)
}
if p.packetType == constants.DataPacket {
stream.WriteUInt8(p.fragmentID)
}
if p.HasFlag(constants.PacketFlagHasSize) {
stream.WriteUInt16LE(uint16(len(p.payload)))
}
if len(p.payload) > 0 {
stream.Grow(int64(len(p.payload)))
stream.WriteBytesNext(p.payload)
}
checksum := p.server.PRUDPV0Settings.ChecksumCalculator(p, stream.Bytes())
if server.PRUDPV0Settings.UseEnhancedChecksum {
stream.WriteUInt32LE(checksum)
} else {
stream.WriteUInt8(uint8(checksum))
}
return stream.Bytes()
}
func (p *PRUDPPacketV0) calculateConnectionSignature(addr net.Addr) ([]byte, error) {
return p.server.PRUDPV0Settings.ConnectionSignatureCalculator(p, addr)
}
func (p *PRUDPPacketV0) calculateSignature(sessionKey, connectionSignature []byte) []byte {
return p.server.PRUDPV0Settings.SignatureCalculator(p, sessionKey, connectionSignature)
}
// NewPRUDPPacketV0 creates and returns a new PacketV0 using the provided Client and stream
func NewPRUDPPacketV0(server *PRUDPServer, connection *PRUDPConnection, readStream *ByteStreamIn) (*PRUDPPacketV0, error) {
packet := &PRUDPPacketV0{
PRUDPPacket: PRUDPPacket{
sender: connection,
readStream: readStream,
version: 0,
},
}
packet.server = server
if readStream != nil {
err := packet.decode()
if err != nil {
return nil, fmt.Errorf("Failed to decode PRUDPv0 packet. %s", err.Error())
}
}
return packet, nil
}
// NewPRUDPPacketsV0 reads all possible PRUDPv0 packets from the stream
func NewPRUDPPacketsV0(server *PRUDPServer, connection *PRUDPConnection, readStream *ByteStreamIn) ([]PRUDPPacketInterface, error) {
packets := make([]PRUDPPacketInterface, 0)
for readStream.Remaining() > 0 {
packet, err := NewPRUDPPacketV0(server, connection, readStream)
if err != nil {
return packets, err
}
packets = append(packets, packet)
}
return packets, nil
}
func defaultPRUDPv0ConnectionSignature(packet *PRUDPPacketV0, addr net.Addr) ([]byte, error) {
var ip net.IP
var port int
switch v := addr.(type) {
case *net.UDPAddr:
ip = v.IP.To4()
port = v.Port
default:
return nil, fmt.Errorf("Unsupported network type: %T", addr)
}
portBytes := make([]byte, 2)
binary.BigEndian.PutUint16(portBytes, uint16(port))
data := append(ip, portBytes...)
hash := md5.Sum(data)
signatureBytes := hash[:4]
slices.Reverse(signatureBytes)
return signatureBytes, nil
}
func defaultPRUDPv0CalculateSignature(packet *PRUDPPacketV0, sessionKey, connectionSignature []byte) []byte {
if !packet.server.PRUDPV0Settings.LegacyConnectionSignature {
if packet.packetType == constants.DataPacket {
return packet.server.PRUDPV0Settings.DataSignatureCalculator(packet, sessionKey)
}
if packet.packetType == constants.DisconnectPacket && packet.server.AccessKey != "ridfebb9" {
return packet.server.PRUDPV0Settings.DataSignatureCalculator(packet, sessionKey)
}
}
if len(connectionSignature) != 0 {
return connectionSignature
}
return make([]byte, 4)
}
func defaultPRUDPv0CalculateDataSignature(packet *PRUDPPacketV0, sessionKey []byte) []byte {
server := packet.server
data := packet.payload
if server.AccessKey != "ridfebb9" {
header := []byte{0, 0, packet.fragmentID}
binary.LittleEndian.PutUint16(header[:2], packet.sequenceID)
data = append(sessionKey, header...)
data = append(data, packet.payload...)
}
if len(data) > 0 {
key := md5.Sum([]byte(server.AccessKey))
mac := hmac.New(md5.New, key[:])
mac.Write(data)
digest := mac.Sum(nil)
return digest[:4]
}
return []byte{0x78, 0x56, 0x34, 0x12}
}
func defaultPRUDPv0CalculateChecksum(packet *PRUDPPacketV0, data []byte) uint32 {
server := packet.server
checksum := sum[byte, uint32]([]byte(server.AccessKey))
if server.PRUDPV0Settings.UseEnhancedChecksum {
padSize := (len(data) + 3) &^ 3
data = append(data, make([]byte, padSize-len(data))...)
words := make([]uint32, len(data)/4)
for i := 0; i < len(data)/4; i++ {
words[i] = binary.LittleEndian.Uint32(data[i*4 : (i+1)*4])
}
result := (checksum & 0xFF) + sum[uint32, uint32](words)
return result & 0xFFFFFFFF
} else {
words := make([]uint32, len(data)/4)
for i := 0; i < len(data)/4; i++ {
words[i] = binary.LittleEndian.Uint32(data[i*4 : (i+1)*4])
}
temp := sum[uint32, uint32](words) & 0xFFFFFFFF
checksum += sum[byte, uint32](data[len(data)&^3:])
tempBytes := make([]byte, 4)
binary.LittleEndian.PutUint32(tempBytes, temp)
checksum += sum[byte, uint32](tempBytes)
return checksum & 0xFF
}
}