-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathprudp_packet_lite.go
359 lines (285 loc) · 10.5 KB
/
prudp_packet_lite.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
package nex
import (
"crypto/hmac"
"crypto/md5"
"encoding/binary"
"fmt"
"net"
"github.com/PretendoNetwork/nex-go/v2/constants"
)
// PRUDPPacketLite represents a PRUDPLite packet
type PRUDPPacketLite struct {
PRUDPPacket
sourceVirtualPortStreamType constants.StreamType
sourceVirtualPortStreamID uint8
destinationVirtualPortStreamType constants.StreamType
destinationVirtualPortStreamID uint8
optionsLength uint8
minorVersion uint32
supportedFunctions uint32
maximumSubstreamID uint8
initialUnreliableSequenceID uint16
liteSignature []byte
}
// SetSourceVirtualPortStreamType sets the packets source VirtualPort StreamType
func (p *PRUDPPacketLite) SetSourceVirtualPortStreamType(streamType constants.StreamType) {
p.sourceVirtualPortStreamType = streamType
}
// SourceVirtualPortStreamType returns the packets source VirtualPort StreamType
func (p *PRUDPPacketLite) SourceVirtualPortStreamType() constants.StreamType {
return p.sourceVirtualPortStreamType
}
// SetSourceVirtualPortStreamID sets the packets source VirtualPort port number
func (p *PRUDPPacketLite) SetSourceVirtualPortStreamID(port uint8) {
p.sourceVirtualPortStreamID = port
}
// SourceVirtualPortStreamID returns the packets source VirtualPort port number
func (p *PRUDPPacketLite) SourceVirtualPortStreamID() uint8 {
return p.sourceVirtualPort.StreamID()
}
// SetDestinationVirtualPortStreamType sets the packets destination VirtualPort constants.StreamType
func (p *PRUDPPacketLite) SetDestinationVirtualPortStreamType(streamType constants.StreamType) {
p.destinationVirtualPortStreamType = streamType
}
// DestinationVirtualPortStreamType returns the packets destination VirtualPort constants.StreamType
func (p *PRUDPPacketLite) DestinationVirtualPortStreamType() constants.StreamType {
return p.destinationVirtualPortStreamType
}
// SetDestinationVirtualPortStreamID sets the packets destination VirtualPort port number
func (p *PRUDPPacketLite) SetDestinationVirtualPortStreamID(port uint8) {
p.destinationVirtualPortStreamID = port
}
// DestinationVirtualPortStreamID returns the packets destination VirtualPort port number
func (p *PRUDPPacketLite) DestinationVirtualPortStreamID() uint8 {
return p.destinationVirtualPortStreamID
}
// Copy copies the packet into a new PRUDPPacketLite
//
// Retains the same PRUDPConnection pointer
func (p *PRUDPPacketLite) Copy() PRUDPPacketInterface {
copied, _ := NewPRUDPPacketLite(p.server, p.sender, nil)
copied.server = p.server
copied.sourceVirtualPortStreamType = p.sourceVirtualPortStreamType
copied.sourceVirtualPortStreamID = p.sourceVirtualPortStreamID
copied.destinationVirtualPortStreamType = p.destinationVirtualPortStreamType
copied.destinationVirtualPortStreamID = p.destinationVirtualPortStreamID
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()
}
copied.optionsLength = p.optionsLength
copied.minorVersion = p.minorVersion
copied.supportedFunctions = p.supportedFunctions
copied.maximumSubstreamID = p.maximumSubstreamID
copied.initialUnreliableSequenceID = p.initialUnreliableSequenceID
return copied
}
// Version returns the packets PRUDP version
func (p *PRUDPPacketLite) Version() int {
return 2
}
// decode parses the packets data
func (p *PRUDPPacketLite) decode() error {
magic, err := p.readStream.ReadUInt8()
if err != nil {
return fmt.Errorf("Failed to read PRUDPLite magic. %s", err.Error())
}
if magic != 0x80 {
return fmt.Errorf("Invalid PRUDPLite magic. Expected 0x80, got 0x%x", magic)
}
p.optionsLength, err = p.readStream.ReadUInt8()
if err != nil {
return fmt.Errorf("Failed to decode PRUDPLite options length. %s", err.Error())
}
payloadLength, err := p.readStream.ReadUInt16LE()
if err != nil {
return fmt.Errorf("Failed to decode PRUDPLite payload length. %s", err.Error())
}
streamTypes, err := p.readStream.ReadUInt8()
if err != nil {
return fmt.Errorf("Failed to decode PRUDPLite virtual ports stream types. %s", err.Error())
}
p.sourceVirtualPortStreamType = constants.StreamType(streamTypes >> 4)
p.destinationVirtualPortStreamType = constants.StreamType(streamTypes & 0xF)
p.sourceVirtualPortStreamID, err = p.readStream.ReadUInt8()
if err != nil {
return fmt.Errorf("Failed to decode PRUDPLite virtual source port. %s", err.Error())
}
p.destinationVirtualPortStreamID, err = p.readStream.ReadUInt8()
if err != nil {
return fmt.Errorf("Failed to decode PRUDPLite virtual destination port. %s", err.Error())
}
p.fragmentID, err = p.readStream.ReadUInt8()
if err != nil {
return fmt.Errorf("Failed to decode PRUDPLite fragment ID. %s", err.Error())
}
typeAndFlags, err := p.readStream.ReadUInt16LE()
if err != nil {
return fmt.Errorf("Failed to read PRUDPLite type and flags. %s", err.Error())
}
p.flags = typeAndFlags >> 4
p.packetType = typeAndFlags & 0xF
p.sequenceID, err = p.readStream.ReadUInt16LE()
if err != nil {
return fmt.Errorf("Failed to decode PRUDPLite sequence ID. %s", err.Error())
}
err = p.decodeOptions()
if err != nil {
return fmt.Errorf("Failed to decode PRUDPLite options. %s", err.Error())
}
p.payload = p.readStream.ReadBytesNext(int64(payloadLength))
return nil
}
// Bytes encodes a PRUDPLite packet into a byte slice
func (p *PRUDPPacketLite) Bytes() []byte {
options := p.encodeOptions()
stream := NewByteStreamOut(p.server.LibraryVersions, p.server.ByteStreamSettings)
stream.WriteUInt8(0x80)
stream.WriteUInt8(uint8(len(options)))
stream.WriteUInt16LE(uint16(len(p.payload)))
stream.WriteUInt8(uint8((p.sourceVirtualPortStreamType << 4) | p.destinationVirtualPortStreamType))
stream.WriteUInt8(p.sourceVirtualPortStreamID)
stream.WriteUInt8(p.destinationVirtualPortStreamID)
stream.WriteUInt8(p.fragmentID)
stream.WriteUInt16LE(p.packetType | (p.flags << 4))
stream.WriteUInt16LE(p.sequenceID)
stream.Grow(int64(len(options)))
stream.WriteBytesNext(options)
stream.Grow(int64(len(p.payload)))
stream.WriteBytesNext(p.payload)
return stream.Bytes()
}
func (p *PRUDPPacketLite) decodeOptions() error {
data := p.readStream.ReadBytesNext(int64(p.optionsLength))
optionsStream := NewByteStreamIn(data, p.server.LibraryVersions, p.server.ByteStreamSettings)
for optionsStream.Remaining() > 0 {
optionID, err := optionsStream.ReadUInt8()
if err != nil {
return err
}
optionSize, err := optionsStream.ReadUInt8() // * Options size. We already know the size based on the ID, though
if err != nil {
return err
}
if p.packetType == constants.SynPacket || p.packetType == constants.ConnectPacket {
if optionID == 0 {
p.supportedFunctions, err = optionsStream.ReadUInt32LE()
p.minorVersion = p.supportedFunctions & 0xFF
p.supportedFunctions = p.supportedFunctions >> 8
}
if optionID == 1 {
p.connectionSignature = optionsStream.ReadBytesNext(int64(optionSize))
}
if optionID == 4 {
p.maximumSubstreamID, err = optionsStream.ReadUInt8()
}
}
if p.packetType == constants.ConnectPacket {
if optionID == 3 {
p.initialUnreliableSequenceID, err = optionsStream.ReadUInt16LE()
}
}
if p.packetType == constants.DataPacket {
if optionID == 2 {
p.fragmentID, err = optionsStream.ReadUInt8()
}
}
if p.packetType == constants.ConnectPacket && !p.HasFlag(constants.PacketFlagAck) {
if optionID == 0x80 {
p.liteSignature = optionsStream.ReadBytesNext(int64(optionSize))
}
}
// * Only one option is processed at a time, so we can
// * just check for errors here rather than after EVERY
// * read
if err != nil {
return err
}
}
return nil
}
func (p *PRUDPPacketLite) encodeOptions() []byte {
optionsStream := NewByteStreamOut(p.server.LibraryVersions, p.server.ByteStreamSettings)
if p.packetType == constants.SynPacket || p.packetType == constants.ConnectPacket {
optionsStream.WriteUInt8(0)
optionsStream.WriteUInt8(4)
optionsStream.WriteUInt32LE(p.minorVersion | (p.supportedFunctions << 8))
if p.packetType == constants.SynPacket && p.HasFlag(constants.PacketFlagAck) {
optionsStream.WriteUInt8(1)
optionsStream.WriteUInt8(16)
optionsStream.Grow(16)
optionsStream.WriteBytesNext(p.connectionSignature)
}
if p.packetType == constants.ConnectPacket && !p.HasFlag(constants.PacketFlagAck) {
optionsStream.WriteUInt8(1)
optionsStream.WriteUInt8(16)
optionsStream.Grow(16)
optionsStream.WriteBytesNext(p.liteSignature)
}
}
return optionsStream.Bytes()
}
func (p *PRUDPPacketLite) calculateConnectionSignature(addr net.Addr) ([]byte, error) {
var ip net.IP
var port int
switch v := addr.(type) {
case *net.TCPAddr:
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 := hmac.New(md5.New, p.server.PRUDPv1ConnectionSignatureKey)
hash.Write(data)
return hash.Sum(nil), nil
}
func (p *PRUDPPacketLite) calculateSignature(sessionKey, connectionSignature []byte) []byte {
// * PRUDPLite has no signature
return make([]byte, 0)
}
// NewPRUDPPacketLite creates and returns a new PacketLite using the provided Client and stream
func NewPRUDPPacketLite(server *PRUDPServer, connection *PRUDPConnection, readStream *ByteStreamIn) (*PRUDPPacketLite, error) {
packet := &PRUDPPacketLite{
PRUDPPacket: PRUDPPacket{
sender: connection,
readStream: readStream,
},
}
packet.server = server
if readStream != nil {
err := packet.decode()
if err != nil {
return nil, fmt.Errorf("Failed to decode PRUDPLite packet. %s", err.Error())
}
}
return packet, nil
}
// NewPRUDPPacketsLite reads all possible PRUDPLite packets from the stream
func NewPRUDPPacketsLite(server *PRUDPServer, connection *PRUDPConnection, readStream *ByteStreamIn) ([]PRUDPPacketInterface, error) {
packets := make([]PRUDPPacketInterface, 0)
for readStream.Remaining() > 0 {
packet, err := NewPRUDPPacketLite(server, connection, readStream)
if err != nil {
return packets, err
}
packets = append(packets, packet)
}
return packets, nil
}