forked from LagrangeDev/LagrangeGo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
richmedia.go
151 lines (146 loc) · 4.13 KB
/
richmedia.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
package client
import (
"bytes"
"encoding/hex"
"errors"
highway2 "github.com/2mf8/LagrangeGo/client/internal/highway"
"github.com/2mf8/LagrangeGo/client/packets/oidb"
message2 "github.com/2mf8/LagrangeGo/client/packets/pb/message"
"github.com/2mf8/LagrangeGo/client/packets/pb/service/highway"
oidb2 "github.com/2mf8/LagrangeGo/client/packets/pb/service/oidb"
"github.com/2mf8/LagrangeGo/internal/proto"
"github.com/2mf8/LagrangeGo/message"
"github.com/2mf8/LagrangeGo/utils/binary"
)
func oidbIPv4ToNTHighwayIPv4(ipv4s []*oidb2.IPv4) []*highway.NTHighwayIPv4 {
hwipv4s := make([]*highway.NTHighwayIPv4, len(ipv4s))
for i, ip := range ipv4s {
hwipv4s[i] = &highway.NTHighwayIPv4{
Domain: &highway.NTHighwayDomain{
IsEnable: true,
IP: binary.UInt32ToIPV4Address(ip.OutIP),
},
Port: ip.OutPort,
}
}
return hwipv4s
}
func (c *QQClient) ImageUploadPrivate(targetUid string, element message.IMessageElement) (*message.FriendImageElement, error) {
image, ok := element.(*message.FriendImageElement)
if !ok {
return nil, errors.New("element type is not friend image")
}
req, err := oidb.BuildImageUploadReq(targetUid, image.Stream)
if err != nil {
return nil, err
}
resp, err := c.sendOidbPacketAndWait(req)
if err != nil {
return nil, err
}
uploadResp, err := oidb.ParseImageUploadResp(resp)
if err != nil {
return nil, err
}
ukey := uploadResp.Upload.UKey.Unwrap()
c.debugln("private image upload ukey:", ukey)
if ukey != "" {
index := uploadResp.Upload.MsgInfo.MsgInfoBody[0].Index
sha1hash, err := hex.DecodeString(index.Info.FileSha1)
if err != nil {
return nil, err
}
extend := &highway.NTV2RichMediaHighwayExt{
FileUuid: index.FileUuid,
UKey: ukey,
Network: &highway.NTHighwayNetwork{
IPv4S: oidbIPv4ToNTHighwayIPv4(uploadResp.Upload.IPv4S),
},
MsgInfoBody: uploadResp.Upload.MsgInfo.MsgInfoBody,
BlockSize: uint32(highway2.BlockSize),
Hash: &highway.NTHighwayHash{
FileSha1: [][]byte{sha1hash},
},
}
extStream, err := proto.Marshal(extend)
if err != nil {
return nil, err
}
md5hash, err := hex.DecodeString(index.Info.FileHash)
if err != nil {
return nil, err
}
err = c.highwayUpload(1003,
bytes.NewReader(image.Stream), uint64(len(image.Stream)),
md5hash, extStream,
)
if err != nil {
return nil, err
}
}
image.MsgInfo = uploadResp.Upload.MsgInfo
compatImage := &message2.NotOnlineImage{}
err = proto.Unmarshal(uploadResp.Upload.CompatQMsg, compatImage)
if err != nil {
return nil, err
}
image.CompatImage = compatImage
return image, nil
}
func (c *QQClient) ImageUploadGroup(groupUin uint32, element message.IMessageElement) (*message.GroupImageElement, error) {
image, ok := element.(*message.GroupImageElement)
if !ok {
return nil, errors.New("element type is not group image")
}
req, err := oidb.BuildGroupImageUploadReq(groupUin, image.Stream)
if err != nil {
return nil, err
}
resp, err := c.sendOidbPacketAndWait(req)
if err != nil {
return nil, err
}
uploadResp, err := oidb.ParseGroupImageUploadResp(resp)
if err != nil {
return nil, err
}
ukey := uploadResp.Upload.UKey.Unwrap()
c.debugln("private image upload ukey:", ukey)
if ukey != "" {
index := uploadResp.Upload.MsgInfo.MsgInfoBody[0].Index
sha1hash, err := hex.DecodeString(index.Info.FileSha1)
if err != nil {
return nil, err
}
extend := &highway.NTV2RichMediaHighwayExt{
FileUuid: index.FileUuid,
UKey: ukey,
Network: &highway.NTHighwayNetwork{
IPv4S: oidbIPv4ToNTHighwayIPv4(uploadResp.Upload.IPv4S),
},
MsgInfoBody: uploadResp.Upload.MsgInfo.MsgInfoBody,
BlockSize: uint32(highway2.BlockSize),
Hash: &highway.NTHighwayHash{
FileSha1: [][]byte{sha1hash},
},
}
extStream, err := proto.Marshal(extend)
if err != nil {
return nil, err
}
md5hash, err := hex.DecodeString(index.Info.FileHash)
if err != nil {
return nil, err
}
err = c.highwayUpload(1004,
bytes.NewReader(image.Stream), uint64(len(image.Stream)),
md5hash, extStream,
)
if err != nil {
return nil, err
}
}
image.MsgInfo = uploadResp.Upload.MsgInfo
image.CompatFace = uploadResp.Upload.CompatQMsg
return image, nil
}