Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: panic occurred when receive voice record in group message #349

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions client/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,16 +195,16 @@ func (c *QQClient) parsePrivateMessage(msg *msg.Message) *message.PrivateMessage
Sender: sender,
Self: c.Uin,
Elements: func() []message.IMessageElement {
if msg.Body.RichText.Ptt != nil {
return []message.IMessageElement{
&message.VoiceElement{
Name: msg.Body.RichText.Ptt.FileName.Unwrap(),
Md5: msg.Body.RichText.Ptt.FileMd5,
Size: msg.Body.RichText.Ptt.FileSize.Unwrap(),
Url: string(msg.Body.RichText.Ptt.DownPara),
},
}
}
// if msg.Body.RichText.Ptt != nil {
// return []message.IMessageElement{
// &message.VoiceElement{
// Name: msg.Body.RichText.Ptt.FileName.Unwrap(),
// Md5: msg.Body.RichText.Ptt.FileMd5,
// Size: msg.Body.RichText.Ptt.FileSize.Unwrap(),
// Url: string(msg.Body.RichText.Ptt.DownPara),
// },
// }
// }
return message.ParseMessageElems(msg.Body.RichText.Elems)
}(),
}
Expand Down
75 changes: 49 additions & 26 deletions client/pb/msg/msg.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions client/pb/msg/msg.proto
Original file line number Diff line number Diff line change
Expand Up @@ -883,15 +883,16 @@ message PbMultiMediaElement {
message Meta {
message Data {
optional int32 FileLen = 1;
optional bytes PicMd5 = 2;
optional bytes FileMd5 = 2;
optional string FileName = 4;
}
optional Data data = 1;
optional string FilePath = 2;
}
optional Meta meta = 1;

message Data {
optional string ImgURL = 2;
optional string ImgURL = 1;
optional string Domain = 3;
}
optional Data data = 2;
Expand Down
94 changes: 94 additions & 0 deletions client/pb/richmedia/ntv2.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

78 changes: 78 additions & 0 deletions client/pb/richmedia/ntv2.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
syntax = "proto3";

option go_package = "github.com/Mrs4s/MiraiGo/client/pb/richmedia";

message NTV2RichMediaReq {
MultiMediaReqHead ReqHead = 1;
DownloadReq Download = 3;
}

message MultiMediaReqHead {
CommonHead Common = 1;
SceneInfo Scene = 2;
ClientMeta Client = 3;
}

message CommonHead {
uint32 RequestId = 1;
uint32 Command = 2;
}

message SceneInfo {
uint32 RequestType = 101;
uint32 BusinessType = 102;
uint32 SceneType = 200;
optional C2CUserInfo C2C = 201;
optional NTGroupInfo Group = 202;
}

message ClientMeta {
uint32 AgentType = 1;
}

message C2CUserInfo {
uint32 AccountType = 1;
string TargetUid = 2;
}

message NTGroupInfo {
uint32 GroupUin = 1;
}

message DownloadReq {
IndexNode Node = 1;
}

message IndexNode {
FileInfo Info = 1;
string FileUuid = 2;
uint32 StoreId = 3;
}

message FileInfo {
FileType Type = 5;
uint32 Time = 8;
}

message FileType {
uint32 Type = 1;
uint32 VoiceFormat = 4;
}

message NTV2RichMediaRsp {
MediaResp MediaResp = 4;
}

message MediaResp {
DownloadResp DownloadResp = 3;
}

message DownloadResp {
string Rkey = 1;
DownloadInfo Info = 3;
}

message DownloadInfo {
string Domain = 1;
string UrlPath = 2;
}
74 changes: 74 additions & 0 deletions client/richmedia.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package client

import (
"fmt"

"github.com/Mrs4s/MiraiGo/client/pb/richmedia"
"github.com/Mrs4s/MiraiGo/internal/proto"
)

// OidbSvcTrpcTcp.0x126d_200
func (c *QQClient) buildRecordDownloadReqPacket(Uid string, FileId string, groupUin int64, isGroup bool) (uint16, []byte) {
scene := &richmedia.SceneInfo{
RequestType: 2,
BusinessType: 3,
SceneType: 1,
C2C: &richmedia.C2CUserInfo{
AccountType: 2,
TargetUid: Uid,
},
}
if isGroup {
scene.RequestType = 1
scene.SceneType = 2
scene.Group = &richmedia.NTGroupInfo{
GroupUin: uint32(groupUin),
}
}
body := &richmedia.NTV2RichMediaReq{
ReqHead: &richmedia.MultiMediaReqHead{
Common: &richmedia.CommonHead{
RequestId: 3,
Command: 200,
},
Scene: scene,
Client: &richmedia.ClientMeta{
AgentType: 2,
},
},
Download: &richmedia.DownloadReq{
Node: &richmedia.IndexNode{
Info: &richmedia.FileInfo{
Type: &richmedia.FileType{
Type: 3,
VoiceFormat: 1,
},
Time: 1,
},
FileUuid: FileId,
StoreId: 1,
},
},
}
b, err := proto.Marshal(body)
if err != nil {
fmt.Println(err)
}
payload := c.packOIDBPackage(4717, 200, b)
return c.uniPacket("OidbSvcTrpcTcp.0x126d_200", payload)
}

func (c *QQClient) ParseRecordDownloadRspPacket(body []byte) string {
rp := &richmedia.NTV2RichMediaRsp{}
if err := proto.Unmarshal(body, rp); err != nil && rp.MediaResp.DownloadResp.Info != nil {
c.error("parse RecordDownloadRspPacket error: %v", err)
return ""
}
return fmt.Sprintf("https://%s%s%s", rp.MediaResp.DownloadResp.Info.Domain, rp.MediaResp.DownloadResp.Info.UrlPath, rp.MediaResp.DownloadResp.Rkey)
}

// GetRecordDownloadUrl 获取语音文件下载地址
func (c *QQClient) GetRecordDownloadUrl(selfUid string, FileId string, groupUin int64, isGroup bool) string {
body, _ := c.sendAndWaitDynamic(c.buildRecordDownloadReqPacket(selfUid, FileId, groupUin, isGroup))
return c.ParseRecordDownloadRspPacket(body)
}
10 changes: 6 additions & 4 deletions message/elements.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ type TextElement struct {
}

type VoiceElement struct {
Name string
Md5 []byte
Size int32
Url string
Name string
Md5 []byte
Size int32
Url string
FileId string
IsGroup bool

// --- sending ---
Data []byte
Expand Down
Loading