forked from asternic/wuzapi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.go
More file actions
598 lines (497 loc) · 16.5 KB
/
Copy pathhelpers.go
File metadata and controls
598 lines (497 loc) · 16.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
package main
import (
"bytes"
"context"
"crypto/aes"
"crypto/cipher"
"crypto/hmac"
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"image"
_ "image/gif"
"image/jpeg"
_ "image/png"
"io"
"net/http"
"net/url"
"os"
"regexp"
"runtime/debug"
"strings"
"sync"
"time"
"golang.org/x/sync/singleflight"
"github.com/patrickmn/go-cache"
_ "golang.org/x/image/webp"
"github.com/PuerkitoBio/goquery"
"github.com/jmoiron/sqlx"
"github.com/nfnt/resize"
"github.com/rs/zerolog/log"
)
const (
openGraphFetchTimeout = 5 * time.Second
openGraphPageMaxBytes = 2 * 1024 * 1024 // 2MB
openGraphImageMaxBytes = 10 * 1024 * 1024 // 10MB
openGraphThumbnailWidth = 100
openGraphThumbnailHeight = 100
openGraphJpegQuality = 80
openGraphMaxImageDim = 4000 // Max width or height for Open Graph images
openGraphUserFetchLimit = 20 // Limit concurrent Open Graph fetches per user
)
type openGraphResult struct {
Title string
Description string
ImageData []byte
}
type UserSemaphoreManager struct {
pools sync.Map
}
func NewUserSemaphoreManager() *UserSemaphoreManager {
return &UserSemaphoreManager{}
}
func (usm *UserSemaphoreManager) ForUser(userID string) chan struct{} {
// LoadOrStore provides an atomic way to get or create a semaphore.
pool, _ := usm.pools.LoadOrStore(userID, make(chan struct{}, openGraphUserFetchLimit))
return pool.(chan struct{})
}
var (
urlRegex = regexp.MustCompile(`https?://[^\s"']*[^\"'\s\.,!?()[\]{}]`)
userSemaphoreManager = NewUserSemaphoreManager()
openGraphGroup singleflight.Group
openGraphCache = cache.New(5*time.Minute, 10*time.Minute) // Cache Open Graph data for 5 minutes, cleanup every 10 minutes
)
func Find(slice []string, val string) bool {
for _, item := range slice {
if item == val {
return true
}
}
return false
}
func isHTTPURL(input string) bool {
parsed, err := url.ParseRequestURI(input)
if err != nil {
return false
}
if parsed.Scheme != "http" && parsed.Scheme != "https" {
return false
}
return parsed.Host != ""
}
func fetchURLBytes(ctx context.Context, resourceURL string, limit int64) ([]byte, string, error) {
req, err := http.NewRequestWithContext(ctx, "GET", resourceURL, nil)
if err != nil {
return nil, "", err
}
resp, err := globalHTTPClient.Do(req)
if err != nil {
return nil, "", err
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return nil, "", fmt.Errorf("unexpected status code %d", resp.StatusCode)
}
lr := io.LimitReader(resp.Body, limit+1)
data, err := io.ReadAll(lr)
if err != nil {
return nil, "", err
}
if int64(len(data)) > limit {
return nil, "", fmt.Errorf("response exceeds allowed size (%d bytes)", limit)
}
contentType := resp.Header.Get("Content-Type")
if contentType == "" {
contentType = http.DetectContentType(data)
}
return data, contentType, nil
}
func getOpenGraphData(ctx context.Context, urlStr string, userID string) (title, description string, imageData []byte) {
// Check cache first
if cachedData, found := openGraphCache.Get(urlStr); found {
if data, ok := cachedData.(openGraphResult); ok {
log.Debug().Str("url", urlStr).Msg("Open Graph data fetched from cache")
return data.Title, data.Description, data.ImageData
}
}
v, err, _ := openGraphGroup.Do(urlStr, func() (res any, err error) {
ctx, cancel := context.WithTimeout(ctx, openGraphFetchTimeout)
defer cancel()
// Acquire a token from the semaphore pool
userPool := userSemaphoreManager.ForUser(userID)
select {
case userPool <- struct{}{}:
defer func() { <-userPool }()
case <-ctx.Done():
log.Warn().Str("url", urlStr).Msg("Open Graph data fetch timed out while waiting for a worker")
return nil, ctx.Err()
}
// Recover from panics and convert to error
defer func() {
if r := recover(); r != nil {
stack := debug.Stack()
log.Error().
Interface("panic_info", r).
Str("url", urlStr).
Bytes("stack", stack).
Msg("Panic recovered while fetching Open Graph data")
err = fmt.Errorf("panic: %v", r)
}
}()
// Fetch Open Graph data
title, description, imageData := fetchOpenGraphData(ctx, urlStr)
// Store in cache
openGraphCache.Set(urlStr, openGraphResult{title, description, imageData}, cache.DefaultExpiration)
return openGraphResult{title, description, imageData}, nil
})
if err != nil {
log.Error().Err(err).Str("url", urlStr).Msg("Error fetching Open Graph data via singleflight")
return "", "", nil
}
if v == nil {
return "", "", nil
}
data := v.(openGraphResult)
return data.Title, data.Description, data.ImageData
}
// Update entry in User map
func updateUserInfo(values interface{}, field string, value string) interface{} {
log.Debug().Str("field", field).Str("value", value).Msg("User info updated")
values.(Values).m[field] = value
return values
}
// webhook for regular messages
func callHook(myurl string, payload map[string]string, userID string) {
callHookWithHmac(myurl, payload, userID, nil)
}
// webhook for regular messages with HMAC
func callHookWithHmac(myurl string, payload map[string]string, userID string, encryptedHmacKey []byte) {
log.Info().Str("url", myurl).Str("userID", userID).Msg("Sending POST to client")
// Log the payload map
log.Debug().Msg("Payload:")
for key, value := range payload {
log.Debug().Str(key, value).Msg("")
}
client := clientManager.GetHTTPClient(userID)
format := os.Getenv("WEBHOOK_FORMAT")
if format == "json" {
// Send as pure JSON
// The original payload is a map[string]string, but we want to send the postmap (map[string]interface{})
// So we try to decode the jsonData field if it exists, otherwise we send the original payload
var body interface{} = payload
var jsonBody []byte
if jsonStr, ok := payload["jsonData"]; ok {
var postmap map[string]interface{}
err := json.Unmarshal([]byte(jsonStr), &postmap)
if err == nil {
if instanceName, ok := payload["instanceName"]; ok {
postmap["instanceName"] = instanceName
}
postmap["userID"] = userID
body = postmap
}
}
// Marshal body to JSON for HMAC signature
jsonBody, marshalErr := json.Marshal(body)
if marshalErr != nil {
log.Error().Err(marshalErr).Msg("Failed to marshal body for HMAC")
}
// Generate HMAC signature if key exists
var hmacSignature string
var err error
if len(encryptedHmacKey) > 0 && len(jsonBody) > 0 {
hmacSignature, err = generateHmacSignature(jsonBody, encryptedHmacKey)
if err != nil {
log.Error().Err(err).Msg("Failed to generate HMAC signature")
} else {
log.Debug().Str("hmacSignature", hmacSignature).Msg("Generated HMAC signature")
}
}
req := client.R().
SetHeader("Content-Type", "application/json").
SetBody(body)
// Add HMAC signature header if available
if hmacSignature != "" {
req.SetHeader("x-hmac-signature", hmacSignature)
}
_, postErr := req.Post(myurl)
if postErr != nil {
log.Debug().Str("error", postErr.Error())
}
} else {
/// Default: send as form-urlencoded
// Generate HMAC signature if encrypted key exists
var hmacSignature string
var err error
if len(encryptedHmacKey) > 0 {
formData := url.Values{}
for k, v := range payload {
formData.Add(k, v)
}
formString := formData.Encode() // "token=abc&message=hello"
hmacSignature, err = generateHmacSignature([]byte(formString), encryptedHmacKey)
if err != nil {
log.Error().Err(err).Msg("Failed to generate HMAC signature")
} else {
log.Debug().Str("hmacSignature", hmacSignature).Msg("Generated HMAC signature for form-data")
}
}
req := client.R().SetFormData(payload)
// Add HMAC signature header if available
if hmacSignature != "" {
req.SetHeader("x-hmac-signature", hmacSignature)
}
_, postErr := req.Post(myurl)
if postErr != nil {
log.Debug().Str("error", postErr.Error())
}
}
}
// webhook for messages with file attachments
func callHookFile(myurl string, payload map[string]string, userID string, file string) error {
return callHookFileWithHmac(myurl, payload, userID, file, nil)
}
// webhook for messages with file attachments and HMAC
func callHookFileWithHmac(myurl string, payload map[string]string, userID string, file string, encryptedHmacKey []byte) error {
log.Info().Str("file", file).Str("url", myurl).Msg("Sending POST")
client := clientManager.GetHTTPClient(userID)
// Create final payload map
finalPayload := make(map[string]string)
for k, v := range payload {
finalPayload[k] = v
}
finalPayload["file"] = file
log.Debug().Interface("finalPayload", finalPayload).Msg("Final payload to be sent")
// Generate HMAC signature if key exists
var hmacSignature string
var jsonPayload []byte
var err error
if len(encryptedHmacKey) > 0 {
// Para multipart/form-data, assinar a representação JSON do payload final
jsonPayload, err = json.Marshal(finalPayload)
if err != nil {
log.Error().Err(err).Msg("Failed to marshal payload for HMAC")
} else {
hmacSignature, err = generateHmacSignature(jsonPayload, encryptedHmacKey)
if err != nil {
log.Error().Err(err).Msg("Failed to generate HMAC signature")
} else {
log.Debug().Str("hmacSignature", hmacSignature).Msg("Generated HMAC signature for file webhook")
}
}
}
req := client.R().
SetFiles(map[string]string{
"file": file,
}).
SetFormData(finalPayload)
// Add HMAC signature header if available
if hmacSignature != "" {
req.SetHeader("x-hmac-signature", hmacSignature)
}
resp, err := req.Post(myurl)
if err != nil {
log.Error().Err(err).Str("url", myurl).Msg("Failed to send POST request")
return fmt.Errorf("failed to send POST request: %w", err)
}
log.Debug().Interface("payload", finalPayload).Msg("Payload sent to webhook")
log.Info().Int("status", resp.StatusCode()).Str("body", string(resp.Body())).Msg("POST request completed")
return nil
}
func (s *server) respondWithJSON(w http.ResponseWriter, statusCode int, payload interface{}) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
if err := enc.Encode(payload); err != nil {
log.Error().Err(err).Msg("Failed to encode JSON response")
http.Error(w, "internal server error", http.StatusInternalServerError)
return
}
w.WriteHeader(statusCode)
if _, err := w.Write(buf.Bytes()); err != nil {
log.Error().Err(err).Msg("Failed to write response body")
}
}
// ProcessOutgoingMedia handles media processing for outgoing messages with S3 support
func ProcessOutgoingMedia(userID string, contactJID string, messageID string, data []byte, mimeType string, fileName string, db *sqlx.DB) (map[string]interface{}, error) {
// Check if S3 is enabled for this user
var s3Config struct {
Enabled bool `db:"s3_enabled"`
MediaDelivery string `db:"media_delivery"`
}
err := db.Get(&s3Config, "SELECT s3_enabled, media_delivery FROM users WHERE id = $1", userID)
if err != nil {
log.Error().Err(err).Msg("Failed to get S3 config")
s3Config.Enabled = false
s3Config.MediaDelivery = "base64"
}
// Process S3 upload if enabled
if s3Config.Enabled && (s3Config.MediaDelivery == "s3" || s3Config.MediaDelivery == "both") {
// Process S3 upload (outgoing messages are always in outbox)
s3Data, err := GetS3Manager().ProcessMediaForS3(
context.Background(),
userID,
contactJID,
messageID,
data,
mimeType,
fileName,
false, // isIncoming = false for sent messages
)
if err != nil {
log.Error().Err(err).Msg("Failed to upload media to S3")
// Continue even if S3 upload fails
} else {
return s3Data, nil
}
}
return nil, nil
}
// generateHmacSignature generates HMAC-SHA256 signature for webhook payload
func generateHmacSignature(payload []byte, encryptedHmacKey []byte) (string, error) {
if len(encryptedHmacKey) == 0 {
return "", nil
}
// Decrypt HMAC key
hmacKey, err := decryptHMACKey(encryptedHmacKey)
if err != nil {
return "", fmt.Errorf("failed to decrypt HMAC key: %w", err)
}
// Generate HMAC
h := hmac.New(sha256.New, []byte(hmacKey))
h.Write(payload)
return hex.EncodeToString(h.Sum(nil)), nil
}
func encryptHMACKey(plainText string) ([]byte, error) {
if *globalEncryptionKey == "" {
return nil, fmt.Errorf("encryption key not configured")
}
block, err := aes.NewCipher([]byte(*globalEncryptionKey))
if err != nil {
return nil, fmt.Errorf("failed to create cipher: %w", err)
}
gcm, err := cipher.NewGCM(block)
if err != nil {
return nil, fmt.Errorf("failed to create GCM: %w", err)
}
nonce := make([]byte, gcm.NonceSize())
if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
return nil, fmt.Errorf("failed to generate nonce: %w", err)
}
ciphertext := gcm.Seal(nonce, nonce, []byte(plainText), nil)
return ciphertext, nil
}
// decryptHMACKey decrypts HMAC key using AES-GCM
func decryptHMACKey(encryptedData []byte) (string, error) {
if *globalEncryptionKey == "" {
return "", fmt.Errorf("encryption key not configured")
}
block, err := aes.NewCipher([]byte(*globalEncryptionKey))
if err != nil {
return "", fmt.Errorf("failed to create cipher: %w", err)
}
gcm, err := cipher.NewGCM(block)
if err != nil {
return "", fmt.Errorf("failed to create GCM: %w", err)
}
nonceSize := gcm.NonceSize()
if len(encryptedData) < nonceSize {
return "", fmt.Errorf("ciphertext too short")
}
nonce, ciphertext := encryptedData[:nonceSize], encryptedData[nonceSize:]
plaintext, err := gcm.Open(nil, nonce, ciphertext, nil)
if err != nil {
return "", fmt.Errorf("failed to decrypt: %w", err)
}
return string(plaintext), nil
}
func extractFirstURL(text string) string {
match := urlRegex.FindString(text)
if match == "" {
return ""
}
return match
}
func fetchOpenGraphData(ctx context.Context, urlStr string) (string, string, []byte) {
pageData, _, err := fetchURLBytes(ctx, urlStr, openGraphPageMaxBytes)
if err != nil {
log.Warn().Err(err).Str("url", urlStr).Msg("Failed to fetch URL for Open Graph data")
return "", "", nil
}
doc, err := goquery.NewDocumentFromReader(bytes.NewReader(pageData))
if err != nil {
log.Warn().Err(err).Str("url", urlStr).Msg("Failed to parse HTML for Open Graph data")
return "", "", nil
}
title := doc.Find(`meta[property="og:title"]`).AttrOr("content", "")
if title == "" {
title = strings.TrimSpace(doc.Find("title").Text())
}
description := doc.Find(`meta[property="og:description"]`).AttrOr("content", "")
if description == "" {
description = doc.Find(`meta[name="description"]`).AttrOr("content", "")
}
var imageURLStr string
selectors := []struct {
selector string
attr string
}{
{`meta[property="og:image"]`, "content"},
{`meta[property="twitter:image"]`, "content"},
{`link[rel="apple-touch-icon"]`, "href"},
{`link[rel="icon"]`, "href"},
}
for _, s := range selectors {
imageURLStr, _ = doc.Find(s.selector).Attr(s.attr)
if imageURLStr != "" {
break
}
}
pageURL, err := url.Parse(urlStr)
if err != nil {
log.Warn().Err(err).Str("url", urlStr).Msg("Failed to parse page URL for resolving image URL")
return title, description, nil
}
imageData := fetchOpenGraphImage(ctx, pageURL, imageURLStr)
return title, description, imageData
}
func fetchOpenGraphImage(ctx context.Context, pageURL *url.URL, imageURLStr string) []byte {
imageURL, err := url.Parse(imageURLStr)
if err != nil {
log.Warn().Err(err).Str("imageURL", imageURLStr).Msg("Failed to parse Open Graph image URL")
return nil
}
resolvedImageURL := pageURL.ResolveReference(imageURL).String()
imgBytes, _, err := fetchURLBytes(ctx, resolvedImageURL, openGraphImageMaxBytes)
if err != nil {
log.Warn().Err(err).Str("imageURL", resolvedImageURL).Msg("Failed to fetch Open Graph image")
return nil
}
imgConfig, _, err := image.DecodeConfig(bytes.NewReader(imgBytes))
if err != nil {
log.Warn().Err(err).Str("imageURL", resolvedImageURL).Msg("Failed to decode Open Graph image config")
return nil
}
if imgConfig.Width > openGraphMaxImageDim || imgConfig.Height > openGraphMaxImageDim {
log.Warn().
Int("width", imgConfig.Width).
Int("height", imgConfig.Height).
Str("imageURL", resolvedImageURL).
Msg("Open Graph image dimensions too large")
return nil
}
img, _, err := image.Decode(bytes.NewReader(imgBytes))
if err != nil {
log.Warn().Err(err).Str("imageURL", resolvedImageURL).Msg("Failed to decode Open Graph image")
return nil
}
thumbnail := resize.Thumbnail(openGraphThumbnailWidth, openGraphThumbnailHeight, img, resize.Lanczos3)
var buf bytes.Buffer
if err := jpeg.Encode(&buf, thumbnail, &jpeg.Options{Quality: openGraphJpegQuality}); err != nil {
log.Warn().Err(err).Msg("Failed to encode thumbnail to JPEG")
return nil
}
return buf.Bytes()
}