forked from tulir/whatsmeow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload.go
More file actions
304 lines (277 loc) · 10.5 KB
/
upload.go
File metadata and controls
304 lines (277 loc) · 10.5 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// Copyright (c) 2024 Tulir Asokan
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package whatsmeow
import (
"bytes"
"context"
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"strings"
"go.mau.fi/util/random"
"go.mau.fi/whatsmeow/socket"
"go.mau.fi/whatsmeow/util/cbcutil"
)
// UploadResponse contains the data from the attachment upload, which can be put into a message to send the attachment.
type UploadResponse struct {
URL string `json:"url"`
DirectPath string `json:"direct_path"`
Handle string `json:"handle"`
ObjectID string `json:"object_id"`
MediaKey []byte `json:"-"`
FileEncSHA256 []byte `json:"-"`
FileSHA256 []byte `json:"-"`
FileLength uint64 `json:"-"`
}
// Upload uploads the given attachment to WhatsApp servers.
//
// You should copy the fields in the response to the corresponding fields in a protobuf message.
//
// For example, to send an image:
//
// resp, err := cli.Upload(context.Background(), yourImageBytes, whatsmeow.MediaImage)
// // handle error
//
// imageMsg := &waE2E.ImageMessage{
// Caption: proto.String("Hello, world!"),
// Mimetype: proto.String("image/png"), // replace this with the actual mime type
// // you can also optionally add other fields like ContextInfo and JpegThumbnail here
//
// URL: &resp.URL,
// DirectPath: &resp.DirectPath,
// MediaKey: resp.MediaKey,
// FileEncSHA256: resp.FileEncSHA256,
// FileSHA256: resp.FileSHA256,
// FileLength: &resp.FileLength,
// }
// _, err = cli.SendMessage(context.Background(), targetJID, &waE2E.Message{
// ImageMessage: imageMsg,
// })
// // handle error again
//
// The same applies to the other message types like DocumentMessage, just replace the struct type and Message field name.
func (cli *Client) Upload(ctx context.Context, plaintext []byte, appInfo MediaType) (resp UploadResponse, err error) {
resp.FileLength = uint64(len(plaintext))
resp.MediaKey = random.Bytes(32)
plaintextSHA256 := sha256.Sum256(plaintext)
resp.FileSHA256 = plaintextSHA256[:]
iv, cipherKey, macKey, _ := getMediaKeys(resp.MediaKey, appInfo)
var ciphertext []byte
ciphertext, err = cbcutil.Encrypt(cipherKey, iv, plaintext)
if err != nil {
err = fmt.Errorf("failed to encrypt file: %w", err)
return
}
h := hmac.New(sha256.New, macKey)
h.Write(iv)
h.Write(ciphertext)
dataToUpload := append(ciphertext, h.Sum(nil)[:10]...)
dataHash := sha256.Sum256(dataToUpload)
resp.FileEncSHA256 = dataHash[:]
err = cli.rawUpload(ctx, bytes.NewReader(dataToUpload), uint64(len(dataToUpload)), resp.FileEncSHA256, appInfo, false, &resp)
return
}
// UploadReader uploads the given attachment to WhatsApp servers.
//
// This is otherwise identical to [Upload], but it reads the plaintext from an [io.Reader] instead of a byte slice.
// A temporary file is required for the encryption process. If tempFile is nil, a temporary file will be created
// and deleted after the upload.
//
// To use only one file, pass the same file as both plaintext and tempFile. This will cause the file to be overwritten with encrypted data.
func (cli *Client) UploadReader(ctx context.Context, plaintext io.Reader, tempFile io.ReadWriteSeeker, appInfo MediaType) (resp UploadResponse, err error) {
resp.MediaKey = random.Bytes(32)
iv, cipherKey, macKey, _ := getMediaKeys(resp.MediaKey, appInfo)
if tempFile == nil {
tempFile, err = os.CreateTemp("", "whatsmeow-upload-*")
if err != nil {
err = fmt.Errorf("failed to create temporary file: %w", err)
return
}
defer func() {
tempFileFile := tempFile.(*os.File)
_ = tempFileFile.Close()
_ = os.Remove(tempFileFile.Name())
}()
}
var uploadSize uint64
resp.FileSHA256, resp.FileEncSHA256, resp.FileLength, uploadSize, err = cbcutil.EncryptStream(cipherKey, iv, macKey, plaintext, tempFile)
if err != nil {
err = fmt.Errorf("failed to encrypt file: %w", err)
return
}
_, err = tempFile.Seek(0, io.SeekStart)
if err != nil {
err = fmt.Errorf("failed to seek to start of temporary file: %w", err)
return
}
err = cli.rawUpload(ctx, tempFile, uploadSize, resp.FileEncSHA256, appInfo, false, &resp)
return
}
// UploadNewsletter uploads the given attachment to WhatsApp servers without encrypting it first.
//
// Newsletter media works mostly the same way as normal media, with a few differences:
// * Since it's unencrypted, there's no MediaKey or FileEncSHA256 fields.
// * There's a "media handle" that needs to be passed in SendRequestExtra.
//
// Example:
//
// resp, err := cli.UploadNewsletter(context.Background(), yourImageBytes, whatsmeow.MediaImage)
// // handle error
//
// imageMsg := &waE2E.ImageMessage{
// // Caption, mime type and other such fields work like normal
// Caption: proto.String("Hello, world!"),
// Mimetype: proto.String("image/png"),
//
// // URL and direct path are also there like normal media
// URL: &resp.URL,
// DirectPath: &resp.DirectPath,
// FileSHA256: resp.FileSHA256,
// FileLength: &resp.FileLength,
// // Newsletter media isn't encrypted, so the media key and file enc sha fields are not applicable
// }
// _, err = cli.SendMessage(context.Background(), newsletterJID, &waE2E.Message{
// ImageMessage: imageMsg,
// }, whatsmeow.SendRequestExtra{
// // Unlike normal media, newsletters also include a "media handle" in the send request.
// MediaHandle: resp.Handle,
// })
// // handle error again
func (cli *Client) UploadNewsletter(ctx context.Context, data []byte, appInfo MediaType) (resp UploadResponse, err error) {
resp.FileLength = uint64(len(data))
hash := sha256.Sum256(data)
resp.FileSHA256 = hash[:]
err = cli.rawUpload(ctx, bytes.NewReader(data), resp.FileLength, resp.FileSHA256, appInfo, true, &resp)
return
}
// UploadNewsletterReader uploads the given attachment to WhatsApp servers without encrypting it first.
//
// This is otherwise identical to [UploadNewsletter], but it reads the plaintext from an [io.Reader] instead of a byte slice.
// Unlike [UploadReader], this does not require a temporary file. However, the data needs to be hashed first,
// so an [io.ReadSeeker] is required to be able to read the data twice.
func (cli *Client) UploadNewsletterReader(ctx context.Context, data io.ReadSeeker, appInfo MediaType) (resp UploadResponse, err error) {
hasher := sha256.New()
var fileLength int64
fileLength, err = io.Copy(hasher, data)
resp.FileLength = uint64(fileLength)
resp.FileSHA256 = hasher.Sum(nil)
_, err = data.Seek(0, io.SeekStart)
if err != nil {
err = fmt.Errorf("failed to seek to start of data: %w", err)
return
}
err = cli.rawUpload(ctx, data, resp.FileLength, resp.FileSHA256, appInfo, true, &resp)
return
}
func (cli *Client) rawUpload(ctx context.Context, dataToUpload io.Reader, uploadSize uint64, fileHash []byte, appInfo MediaType, newsletter bool, resp *UploadResponse) error {
mediaConn, err := cli.refreshMediaConn(ctx, false)
if err != nil {
return fmt.Errorf("failed to refresh media connections: %w", err)
}
token := base64.URLEncoding.EncodeToString(fileHash)
q := url.Values{
"auth": []string{mediaConn.Auth},
"token": []string{token},
}
mmsType := mediaTypeToMMSType[appInfo]
uploadPrefix := "mms"
if cli.MessengerConfig != nil {
uploadPrefix = "wa-msgr/mms"
// Messenger upload only allows voice messages, not audio files
if mmsType == "audio" {
mmsType = "ptt"
}
}
if newsletter {
mmsType = fmt.Sprintf("newsletter-%s", mmsType)
uploadPrefix = "newsletter"
}
var host string
// Hacky hack to prefer last option (rupload.facebook.com) for messenger uploads.
// For some reason, the primary host doesn't work, even though it has the <upload/> tag.
if cli.MessengerConfig != nil {
host = mediaConn.Hosts[len(mediaConn.Hosts)-1].Hostname
} else {
host = mediaConn.Hosts[0].Hostname
}
uploadURL := url.URL{
Scheme: "https",
Host: host,
Path: fmt.Sprintf("/%s/%s/%s", uploadPrefix, mmsType, token),
RawQuery: q.Encode(),
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, uploadURL.String(), dataToUpload)
if err != nil {
return fmt.Errorf("failed to prepare request: %w", err)
}
req.ContentLength = int64(uploadSize)
req.Header.Set("Origin", socket.Origin)
req.Header.Set("Referer", socket.Origin+"/")
httpResp, err := cli.mediaHTTP.Do(req)
if err != nil {
err = fmt.Errorf("failed to execute request: %w", err)
} else if httpResp.StatusCode != http.StatusOK {
err = fmt.Errorf("upload failed with status code %d", httpResp.StatusCode)
} else if err = json.NewDecoder(httpResp.Body).Decode(&resp); err != nil {
err = fmt.Errorf("failed to parse upload response: %w", err)
}
if httpResp != nil {
_ = httpResp.Body.Close()
}
return err
}
// DeleteMedia deletes the media at the given direct path from WhatsApp servers.
//
// This is only used for things like history syncs, which should be deleted after processing.
func (cli *Client) DeleteMedia(ctx context.Context, appInfo MediaType, directPath string, encFileHash []byte, encHandle string) error {
mediaConn, err := cli.refreshMediaConn(ctx, false)
if err != nil {
return fmt.Errorf("failed to refresh media connections: %w", err)
}
queryStart := strings.IndexByte(directPath, '?')
if queryStart > 0 {
directPath = directPath[:queryStart]
}
token := base64.URLEncoding.EncodeToString(encFileHash)
query := url.Values{
"token": []string{token},
"d_md": []string{base64.RawURLEncoding.EncodeToString([]byte(directPath))},
"auth": []string{mediaConn.Auth},
}
if encHandle != "" {
query.Set("e_handle", encHandle)
}
deleteURL := url.URL{
Scheme: "https",
Host: mediaConn.Hosts[0].Hostname,
Path: fmt.Sprintf("/mms/%s/%s", mediaTypeToMMSType[appInfo], token),
RawQuery: query.Encode(),
}
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, deleteURL.String(), nil)
if err != nil {
return fmt.Errorf("failed to prepare request: %w", err)
}
req.Header.Set("Origin", socket.Origin)
req.Header.Set("Referer", socket.Origin+"/")
// TODO non-on-demand backfills may require this? it's in the initial bootstrap payload and may need to be persisted
//req.Header.Set("Companion_User_Secret", companionMetaNonce)
httpResp, err := cli.mediaHTTP.Do(req)
if err != nil {
err = fmt.Errorf("failed to execute request: %w", err)
} else if httpResp.StatusCode < 200 || httpResp.StatusCode >= 300 {
err = fmt.Errorf("media delete failed with status code %d", httpResp.StatusCode)
}
if httpResp != nil {
_ = httpResp.Body.Close()
}
return err
}