-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCOutPacket.go
More file actions
229 lines (207 loc) · 5.61 KB
/
COutPacket.go
File metadata and controls
229 lines (207 loc) · 5.61 KB
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
package msnet
import (
"encoding/binary"
"fmt"
"strings"
"time"
"github.com/zhyonc/msnet/enum"
"github.com/zhyonc/msnet/internal/crypt"
)
type oPacket struct {
SendBuff []byte
Offset int
IsEncryptedByShanda bool
}
func NewCOutPacket(nType uint16) COutPacket {
p := &oPacket{
SendBuff: make([]byte, 0),
IsEncryptedByShanda: false,
}
p.Encode2(int16(nType))
return p
}
func NewCOutPacketByte(nType uint8) COutPacket {
p := &oPacket{
SendBuff: make([]byte, 0),
IsEncryptedByShanda: false,
}
p.Encode1(int8(nType))
return p
}
// GetType implements COutPacket
func (p *oPacket) GetType() uint16 {
if len(p.SendBuff) >= 2 {
return uint16(p.SendBuff[0]) | uint16(p.SendBuff[1])<<8
}
return 0
}
// GetTypeByte implements COutPacket.
func (p *oPacket) GetTypeByte() uint8 {
if len(p.SendBuff) >= 1 {
return uint8(p.SendBuff[0])
}
return 0
}
// GetOffset implements COutPacket.
func (p *oPacket) GetOffset() int {
return p.Offset
}
// GetLength implements COutPacket.
func (p *oPacket) GetLength() int {
return len(p.SendBuff)
}
// GetSendBuffer implements COutPacket
func (p *oPacket) GetSendBuffer() []byte {
return p.SendBuff
}
// EncodeBool implements COutPacket
func (p *oPacket) EncodeBool(b bool) {
var n byte
if b {
n = 1
}
p.SendBuff = append(p.SendBuff, n)
p.Offset++
}
// Encode1 implements COutPacket
func (p *oPacket) Encode1(n int8) {
p.SendBuff = append(p.SendBuff, byte(n))
p.Offset++
}
// Encode2 implements COutPacket
func (p *oPacket) Encode2(n int16) {
p.SendBuff = append(p.SendBuff, byte(n), byte(n>>8))
p.Offset += 2
}
// Encode4 implements COutPacket
func (p *oPacket) Encode4(n int32) {
buf := make([]byte, 4)
for i := range 4 {
buf[i] = byte(n >> (i * 8))
}
p.SendBuff = append(p.SendBuff, buf...)
p.Offset += 4
}
// Encode8 implements COutPacket
func (p *oPacket) Encode8(n int64) {
buf := make([]byte, 8)
for i := range 8 {
buf[i] = byte(n >> (i * 8))
}
p.SendBuff = append(p.SendBuff, buf...)
p.Offset += 8
}
// EncodeFT implements COutPacket
func (p *oPacket) EncodeFT(t time.Time) {
// Convert the time.Time value to nanoseconds since the Unix epoch
nano := t.UnixNano() // nano=currentTime-8hours
// Add the local time zone offset
_, offset := t.Zone()
offsetNano := int64(offset) * int64(time.Second)
nano += offsetNano
// Convert from nanoseconds to 100-nanosecond intervals (the unit used by FileTime)
ft := nano / 100
// Add the difference between the Unix and FileTime epochs
ft += fileTimeEpochDiff
p.Encode8(ft)
}
// EncodeStr implements COutPacket
func (p *oPacket) EncodeStr(s string) {
buf := []byte(s) // ASCII Code
bufLen := len(buf)
p.Encode2(int16(bufLen))
p.SendBuff = append(p.SendBuff, buf...)
p.Offset += bufLen
}
// EncodeLocalStr implements COutPacket
func (p *oPacket) EncodeLocalStr(s string) {
buf := GetLangBuf(s)
p.Encode2(int16(len(buf)))
p.EncodeBuffer(buf)
}
// EncodeLocalName implements COutPacket
func (p *oPacket) EncodeLocalName(s string) {
localeBuf := make([]byte, 13)
buf := GetLangBuf(s)
bufLen := len(buf)
if bufLen > 0 {
copy(localeBuf, buf)
}
p.EncodeBuffer(localeBuf)
p.Offset += bufLen
}
// EncodeBuffer implements COutPacket
func (p *oPacket) EncodeBuffer(buf []byte) {
p.SendBuff = append(p.SendBuff, buf...)
p.Offset += len(buf)
}
// MakeBufferList implements COutPacket
func (p *oPacket) MakeBufferList(bEnc bool, dwKey []byte) []byte {
headerLen := uint16(headerLength)
dataLen := uint16(len(p.SendBuff))
var bufferList []byte
if bEnc {
bufferList = make([]byte, headerLen+dataLen)
copy(bufferList[headerLen:], p.SendBuff)
if gSetting.IsXORCipher {
// Encrypt packet header
uSeqBaseN := ^gSetting.MSVersion
HIWORD := binary.LittleEndian.Uint16(dwKey[2:4])
uRawSeq := HIWORD ^ uSeqBaseN
// Put encrypted header into buffer list
binary.LittleEndian.PutUint16(bufferList, uRawSeq)
binary.LittleEndian.PutUint16(bufferList[2:4], dataLen)
// Encrypt packet data
(*crypt.XORCipher).Encrypt(nil, bufferList[headerLength:], dwKey)
} else {
// Encrypt packet header
uSeqBaseN := ^gSetting.MSVersion
HIWORD := binary.LittleEndian.Uint16(dwKey[2:4])
uRawSeq := HIWORD ^ uSeqBaseN
dataLen ^= uRawSeq
// Put encrypted header into buffer list
binary.LittleEndian.PutUint16(bufferList, uRawSeq)
binary.LittleEndian.PutUint16(bufferList[2:4], dataLen)
// IsEncryptedByShanda
if gSetting.MSRegion > enum.TMS || (gSetting.MSRegion == enum.CMS && gSetting.MSVersion < 86) {
(*crypt.CIOBufferManipulator).En(nil, bufferList[headerLen:])
p.IsEncryptedByShanda = true
}
// Switch AESKey
var aesKey [32]byte
if gSetting.IsCycleAESKey {
aesKey = crypt.GetCycleAESKey(gSetting.MSRegion, gSetting.MSVersion)
} else {
aesKey = gSetting.AESKeyEncrypt
}
// Encrypt packet data
for i := 4; i < len(bufferList); i += maxDataLength {
end := min(i+maxDataLength, len(bufferList))
(*crypt.CAESCipher).Encrypt(nil, aesKey, bufferList[i:end], dwKey)
}
}
} else {
// Encode packet header for CClientSocket::OnConnect
bufferList = make([]byte, headerLen+dataLen-2)
binary.LittleEndian.PutUint16(bufferList, dataLen)
binary.LittleEndian.PutUint16(bufferList[2:4], gSetting.MSVersion)
copy(bufferList[headerLen:], p.SendBuff[2:])
}
return bufferList
}
// DumpString implements COutPacket
func (p *oPacket) DumpString(nSize int) string {
length := len(p.SendBuff)
if nSize <= 0 || nSize > length {
nSize = length
}
var builder strings.Builder
for i := range nSize {
v := p.SendBuff[i]
builder.WriteString(fmt.Sprintf("%02X", v))
if i < nSize-1 {
builder.WriteString(" ")
}
}
return builder.String()
}