forked from PretendoNetwork/nex-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
137 lines (112 loc) · 3.88 KB
/
client.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
package nex
import (
"crypto/rc4"
"net"
)
// Client represents a connected or non-connected PRUDP client
type Client struct {
address *net.UDPAddr
server *Server
cipher *rc4.Cipher
decipher *rc4.Cipher
signatureKey []byte
signatureBase int
secureKey []byte
serverConnectionSignature []byte
clientConnectionSignature []byte
sessionID int
sessionKey []byte
sequenceIDIn *Counter
sequenceIDOut *Counter
}
// Reset resets the Client to default values
func (client *Client) Reset() {
client.sequenceIDIn = NewCounter(0)
client.sequenceIDOut = NewCounter(0)
client.UpdateAccessKey(client.Server().AccessKey())
client.UpdateRC4Key([]byte("CD&ML"))
if client.Server().PrudpVersion() == 0 {
client.SetServerConnectionSignature(make([]byte, 4))
client.SetClientConnectionSignature(make([]byte, 4))
} else {
client.SetServerConnectionSignature([]byte{})
client.SetClientConnectionSignature([]byte{})
}
}
// Address returns the clients UDP address
func (client *Client) Address() *net.UDPAddr {
return client.address
}
// Server returns the server the client is currently connected to
func (client *Client) Server() *Server {
return client.server
}
// UpdateRC4Key sets the client RC4 stream key
func (client *Client) UpdateRC4Key(RC4Key []byte) {
cipher, _ := rc4.NewCipher(RC4Key)
client.cipher = cipher
decipher, _ := rc4.NewCipher(RC4Key)
client.decipher = decipher
}
// Cipher returns the RC4 cipher stream for out-bound packets
func (client *Client) Cipher() *rc4.Cipher {
return client.cipher
}
// Decipher returns the RC4 cipher stream for in-bound packets
func (client *Client) Decipher() *rc4.Cipher {
return client.decipher
}
// UpdateAccessKey sets the client signature base and signature key
func (client *Client) UpdateAccessKey(accessKey string) {
client.signatureBase = sum([]byte(accessKey))
client.signatureKey = MD5Hash([]byte(accessKey))
}
// SignatureBase returns the v0 checksum signature base
func (client *Client) SignatureBase() int {
return client.signatureBase
}
// SignatureKey returns signature key
func (client *Client) SignatureKey() []byte {
return client.signatureKey
}
// SetServerConnectionSignature sets the clients server-side connection signature
func (client *Client) SetServerConnectionSignature(serverConnectionSignature []byte) {
client.serverConnectionSignature = serverConnectionSignature
}
// ServerConnectionSignature returns the clients server-side connection signature
func (client *Client) ServerConnectionSignature() []byte {
return client.serverConnectionSignature
}
// SetClientConnectionSignature sets the clients client-side connection signature
func (client *Client) SetClientConnectionSignature(clientConnectionSignature []byte) {
client.clientConnectionSignature = clientConnectionSignature
}
// ClientConnectionSignature returns the clients client-side connection signature
func (client *Client) ClientConnectionSignature() []byte {
return client.clientConnectionSignature
}
// SequenceIDCounterOut returns the clients packet SequenceID counter for out-going packets
func (client *Client) SequenceIDCounterOut() *Counter {
return client.sequenceIDOut
}
// SequenceIDCounterIn returns the clients packet SequenceID counter for incoming packets
func (client *Client) SequenceIDCounterIn() *Counter {
return client.sequenceIDIn
}
// SetSessionKey sets the clients session key
func (client *Client) SetSessionKey(sessionKey []byte) {
client.sessionKey = sessionKey
}
// SessionKey returns the clients session key
func (client *Client) SessionKey() []byte {
return client.sessionKey
}
// NewClient returns a new PRUDP client
func NewClient(address *net.UDPAddr, server *Server) *Client {
client := &Client{
address: address,
server: server,
}
client.Reset()
return client
}