/
protocol.go
79 lines (59 loc) · 2.38 KB
/
protocol.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
package battleye
// Protocol offers an interface representation for BattlEye communications
//go:generate counterfeiter -o ../mocks/protocol.go --fake-name Protocol . Protocol
type Protocol interface {
//BuildPacket creates a new packet with data and type
BuildPacket([]byte, Type) Packet
//BuildLoginPacket creates a login packet with password
BuildLoginPacket(string) Packet
//BuildCmdPacket creates a packet with cmd and seq
BuildCmdPacket([]byte, Sequence) Packet
//BuildKeepAlivePacket creates a keepAlivePacket with seq
BuildKeepAlivePacket(Sequence) Packet
//BuildMsgAckPacket creates a server message packet with seq
BuildMsgAckPacket(Sequence) Packet
// Verify if the packet is valid
Verify(Packet) error
// Sequence extracts the seq number from a packet
Sequence(Packet) (Sequence, error)
// Type determines the kind of response from a packet
Type(Packet) (Type, error)
// Data returns the actual data inside the packet
Data(Packet) ([]byte, error)
// VerifyLogin returns nil on successful login
// and a respective error on failed login
VerifyLogin(Packet) error
// Multi checks whether a packet is part of a multiPacketResponse
// Returns: packetCount, currentPacket and isSingle
Multi(Packet) (byte, byte, bool)
}
type protocol struct{}
// New provides a real and tested implementation of Protocol
func New() Protocol {
return &protocol{}
}
// Sequence is used by BattlEye to keep track of transactions
type Sequence uint32
//BuildPacket creates a new packet with data and type
func (p *protocol) BuildPacket(data []byte, t Type) Packet {
data = append([]byte{0xFF, byte(t)}, data...)
checksum := makeChecksum(data)
header := buildHeader(checksum)
return append(header, data...)
}
//BuildLoginPacket creates a login packet with password
func (p *protocol) BuildLoginPacket(pw string) Packet {
return p.BuildPacket([]byte(pw), Login)
}
//BuildCmdPacket creates a packet with cmd and seq
func (p *protocol) BuildCmdPacket(cmd []byte, seq Sequence) Packet {
return p.BuildPacket(append([]byte{byte(seq)}, cmd...), Command)
}
//BuildKeepAlivePacket creates a keepAlivePacket with seq
func (p *protocol) BuildKeepAlivePacket(seq Sequence) Packet {
return p.BuildPacket([]byte{byte(seq)}, Command)
}
//BuildMsgAckPacket creates a server message packet with seq
func (p *protocol) BuildMsgAckPacket(seq Sequence) Packet {
return p.BuildPacket([]byte{byte(seq)}, ServerMessage)
}