forked from Mrs4s/MiraiGo
/
ptt.go
206 lines (198 loc) · 5.79 KB
/
ptt.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
package client
import (
"crypto/md5"
"encoding/hex"
"errors"
"fmt"
"github.com/myxtype/MiraiGo/binary"
"github.com/myxtype/MiraiGo/client/pb"
"github.com/myxtype/MiraiGo/client/pb/cmd0x346"
"github.com/myxtype/MiraiGo/client/pb/msg"
"github.com/myxtype/MiraiGo/message"
"github.com/myxtype/MiraiGo/protocol/packets"
"google.golang.org/protobuf/proto"
)
// 语音相关处理逻辑
// UploadGroupPtt 将语音数据使用群语音通道上传到服务器, 返回 message.GroupVoiceElement 可直接发送
func (c *QQClient) UploadGroupPtt(groupCode int64, voice []byte) (*message.GroupVoiceElement, error) {
h := md5.Sum(voice)
seq, pkt := c.buildGroupPttStorePacket(groupCode, h[:], int32(len(voice)), 0, int32(len(voice)))
r, err := c.sendAndWait(seq, pkt)
if err != nil {
return nil, err
}
rsp := r.(pttUploadResponse)
if rsp.ResultCode != 0 {
return nil, errors.New(rsp.Message)
}
if rsp.IsExists {
goto ok
}
for i, ip := range rsp.UploadIp {
err := c.uploadPtt(ip, rsp.UploadPort[i], rsp.UploadKey, rsp.FileKey, voice, h[:])
if err != nil {
continue
}
goto ok
}
return nil, errors.New("upload failed")
ok:
return &message.GroupVoiceElement{
Ptt: &msg.Ptt{
FileType: 4,
SrcUin: c.Uin,
FileMd5: h[:],
FileName: hex.EncodeToString(h[:]) + ".amr",
FileSize: int32(len(voice)),
GroupFileKey: rsp.FileKey,
BoolValid: true,
PbReserve: []byte{8, 0, 40, 0, 56, 0},
}}, nil
}
// UploadPrivatePtt 将语音数据使用好友语音通道上传到服务器, 返回 message.PrivateVoiceElement 可直接发送
func (c *QQClient) UploadPrivatePtt(target int64, voice []byte) (*message.PrivateVoiceElement, error) {
h := md5.Sum(voice)
i, err := c.sendAndWait(c.buildPrivatePttStorePacket(target, h[:], int32(len(voice)), int32(len(voice))))
if err != nil {
return nil, err
}
rsp := i.(pttUploadResponse)
if rsp.IsExists {
goto ok
}
for i, ip := range rsp.UploadIp {
err := c.uploadPtt(ip, rsp.UploadPort[i], rsp.UploadKey, rsp.FileKey, voice, h[:])
if err != nil {
continue
}
goto ok
}
return nil, errors.New("upload failed")
ok:
return &message.PrivateVoiceElement{
Ptt: &msg.Ptt{
FileType: 4,
SrcUin: c.Uin,
FileMd5: h[:],
FileName: hex.EncodeToString(h[:]) + ".amr",
FileSize: int32(len(voice)),
FileKey: rsp.FileKey,
BoolValid: true,
PbReserve: []byte{8, 0, 40, 0, 56, 0},
}}, nil
}
// PttStore.GroupPttUp
func (c *QQClient) buildGroupPttStorePacket(groupCode int64, md5 []byte, size, codec, voiceLength int32) (uint16, []byte) {
seq := c.nextSeq()
req := &pb.D388ReqBody{
NetType: 3,
Subcmd: 3,
MsgTryUpPttReq: []*pb.TryUpPttReq{
{
GroupCode: groupCode,
SrcUin: c.Uin,
FileMd5: md5,
FileSize: int64(size),
FileName: md5,
SrcTerm: 5,
PlatformType: 9,
BuType: 4,
InnerIp: 0,
BuildVer: "6.5.5.663",
VoiceLength: voiceLength,
Codec: codec,
VoiceType: 1,
BoolNewUpChan: true,
},
},
Extension: EmptyBytes,
}
payload, _ := proto.Marshal(req)
packet := packets.BuildUniPacket(c.Uin, seq, "PttStore.GroupPttUp", 1, c.OutGoingPacketSessionId, EmptyBytes, c.sigInfo.d2Key, payload)
return seq, packet
}
// PttStore.GroupPttUp
func decodeGroupPttStoreResponse(_ *QQClient, _ uint16, payload []byte) (interface{}, error) {
pkt := pb.D388RespBody{}
err := proto.Unmarshal(payload, &pkt)
if err != nil {
return nil, err
}
rsp := pkt.MsgTryUpPttRsp[0]
if rsp.Result != 0 {
return pttUploadResponse{
ResultCode: rsp.Result,
Message: rsp.FailMsg,
}, nil
}
if rsp.BoolFileExit {
return pttUploadResponse{IsExists: true}, nil
}
var ip []string
for _, i := range rsp.Uint32UpIp {
ip = append(ip, binary.UInt32ToIPV4Address(uint32(i)))
}
return pttUploadResponse{
UploadKey: rsp.UpUkey,
UploadIp: ip,
UploadPort: rsp.Uint32UpPort,
FileKey: rsp.FileKey,
}, nil
}
// PttCenterSvr.pb_pttCenter_CMD_REQ_APPLY_UPLOAD-500
func (c *QQClient) buildPrivatePttStorePacket(target int64, md5 []byte, size, voiceLength int32) (uint16, []byte) {
seq := c.nextSeq()
req := &cmd0x346.C346ReqBody{
Cmd: 500,
Seq: int32(seq),
ApplyUploadReq: &cmd0x346.ApplyUploadReq{
SenderUin: c.Uin,
RecverUin: target,
FileType: 2,
FileSize: int64(size),
FileName: hex.EncodeToString(md5),
Bytes_10MMd5: md5, // 超过10M可能会炸
},
BusinessId: 17,
ClientType: 104,
ExtensionReq: &cmd0x346.ExtensionReq{
Id: 3,
PttFormat: 1,
NetType: 3,
VoiceType: 2,
PttTime: voiceLength,
},
}
payload, _ := proto.Marshal(req)
packet := packets.BuildUniPacket(c.Uin, seq, "PttCenterSvr.pb_pttCenter_CMD_REQ_APPLY_UPLOAD-500", 1, c.OutGoingPacketSessionId, EmptyBytes, c.sigInfo.d2Key, payload)
return seq, packet
}
// PttCenterSvr.pb_pttCenter_CMD_REQ_APPLY_UPLOAD-500
func decodePrivatePttStoreResponse(c *QQClient, _ uint16, payload []byte) (interface{}, error) {
rsp := cmd0x346.C346RspBody{}
if err := proto.Unmarshal(payload, &rsp); err != nil {
c.Error("unmarshal cmd0x346 rsp body error: %v", err)
return nil, err
}
if rsp.ApplyUploadRsp == nil {
c.Error("decode apply upload 500 error: apply rsp is nil.")
return nil, errors.New("apply rsp is nil")
}
if rsp.ApplyUploadRsp.RetCode != 0 {
c.Error("decode apply upload 500 error: %v", rsp.ApplyUploadRsp.RetCode)
return nil, errors.New(fmt.Sprint(rsp.ApplyUploadRsp.RetCode))
}
if rsp.ApplyUploadRsp.BoolFileExist {
return pttUploadResponse{IsExists: true}, nil
}
var port []int32
for range rsp.ApplyUploadRsp.UploadipList {
port = append(port, rsp.ApplyUploadRsp.UploadPort)
}
return pttUploadResponse{
UploadKey: rsp.ApplyUploadRsp.UploadKey,
UploadIp: rsp.ApplyUploadRsp.UploadipList,
UploadPort: port,
FileKey: rsp.ApplyUploadRsp.Uuid,
}, nil
}