Updated qr code events and tls server
Some checks failed
CI / Test (1.22) (push) Failing after -25m23s
CI / Test (1.23) (push) Failing after -25m25s
CI / Build (push) Failing after -25m51s
CI / Lint (push) Failing after -25m40s

This commit is contained in:
2025-12-29 17:22:06 +02:00
parent bb9aa01519
commit 94fc899bab
17 changed files with 929 additions and 26 deletions

View File

@@ -1,13 +1,17 @@
package whatsmeow
import (
"bytes"
"context"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"fmt"
"image"
"image/png"
"os"
"path/filepath"
"sync"
"time"
"git.warky.dev/wdevs/whatshooked/pkg/config"
@@ -22,6 +26,7 @@ import (
waEvents "go.mau.fi/whatsmeow/types/events"
waLog "go.mau.fi/whatsmeow/util/log"
"google.golang.org/protobuf/proto"
"rsc.io/qr"
_ "github.com/mattn/go-sqlite3"
)
@@ -37,6 +42,8 @@ type Client struct {
mediaConfig config.MediaConfig
showQR bool
keepAliveCancel context.CancelFunc
qrCode string
qrCodeMutex sync.RWMutex
}
// NewClient creates a new whatsmeow client
@@ -111,17 +118,28 @@ func (c *Client) Connect(ctx context.Context) error {
case "code":
logging.Info("QR code received for pairing", "account_id", c.id)
// Store QR code
c.qrCodeMutex.Lock()
c.qrCode = evt.Code
c.qrCodeMutex.Unlock()
// Generate QR code URL
qrURL := c.generateQRCodeURL()
// Display QR code in terminal
fmt.Println("\n========================================")
fmt.Printf("WhatsApp QR Code for account: %s\n", c.id)
fmt.Printf("Phone: %s\n", c.phoneNumber)
fmt.Println("========================================")
fmt.Println("Scan this QR code with WhatsApp on your phone:")
fmt.Println()
qrterminal.GenerateHalfBlock(evt.Code, qrterminal.L, os.Stdout)
fmt.Println()
fmt.Printf("Or open in browser: %s\n", qrURL)
fmt.Println("========================================")
// Publish QR code event
c.eventBus.Publish(events.WhatsAppQRCodeEvent(ctx, c.id, evt.Code))
// Publish QR code event with URL
c.eventBus.Publish(events.WhatsAppQRCodeEvent(ctx, c.id, evt.Code, qrURL))
case "success":
logging.Info("Pairing successful", "account_id", c.id, "phone", c.phoneNumber)
@@ -675,3 +693,59 @@ func getExtensionFromMimeType(mimeType string) string {
}
return ""
}
// generateQRCodeURL generates a URL for accessing the QR code
func (c *Client) generateQRCodeURL() string {
baseURL := c.mediaConfig.BaseURL
if baseURL == "" {
baseURL = "http://localhost:8080"
}
return fmt.Sprintf("%s/api/qr/%s", baseURL, c.id)
}
// GetQRCodePNG generates a PNG image of the current QR code
func (c *Client) GetQRCodePNG() ([]byte, error) {
c.qrCodeMutex.RLock()
qrCodeData := c.qrCode
c.qrCodeMutex.RUnlock()
if qrCodeData == "" {
return nil, fmt.Errorf("no QR code available")
}
// Generate QR code using rsc.io/qr
code, err := qr.Encode(qrCodeData, qr.L)
if err != nil {
return nil, fmt.Errorf("failed to encode QR code: %w", err)
}
// Scale the QR code for better visibility (8x scale)
img := code.Image()
scale := 8
bounds := img.Bounds()
scaledWidth := bounds.Dx() * scale
scaledHeight := bounds.Dy() * scale
// Create a new image with scaled dimensions
scaledImg := image.NewRGBA(image.Rect(0, 0, scaledWidth, scaledHeight))
// Scale the image
for y := 0; y < bounds.Dy(); y++ {
for x := 0; x < bounds.Dx(); x++ {
pixel := img.At(x, y)
for sy := 0; sy < scale; sy++ {
for sx := 0; sx < scale; sx++ {
scaledImg.Set(x*scale+sx, y*scale+sy, pixel)
}
}
}
}
// Encode to PNG
var buf bytes.Buffer
if err := png.Encode(&buf, scaledImg); err != nil {
return nil, fmt.Errorf("failed to encode PNG: %w", err)
}
return buf.Bytes(), nil
}