mirror of
https://github.com/YspCoder/clawgo.git
synced 2026-05-03 03:37:30 +08:00
refine whatsapp bridge defaults and webui polish
This commit is contained in:
@@ -1218,12 +1218,12 @@ func (s *Server) handleWebUIWhatsAppLogout(w http.ResponseWriter, r *http.Reques
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
waCfg, err := s.loadWhatsAppConfig()
|
||||
cfg, err := s.loadConfig()
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
logoutURL, err := channels.BridgeLogoutURL(strings.TrimSpace(waCfg.BridgeURL))
|
||||
logoutURL, err := channels.BridgeLogoutURL(s.resolveWhatsAppBridgeURL(cfg))
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
@@ -1272,14 +1272,15 @@ func (s *Server) handleWebUIWhatsAppQR(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (s *Server) webUIWhatsAppStatusPayload(ctx context.Context) (map[string]interface{}, int) {
|
||||
waCfg, err := s.loadWhatsAppConfig()
|
||||
cfg, err := s.loadConfig()
|
||||
if err != nil {
|
||||
return map[string]interface{}{
|
||||
"ok": false,
|
||||
"error": err.Error(),
|
||||
}, http.StatusInternalServerError
|
||||
}
|
||||
bridgeURL := strings.TrimSpace(waCfg.BridgeURL)
|
||||
waCfg := cfg.Channels.WhatsApp
|
||||
bridgeURL := s.resolveWhatsAppBridgeURL(cfg)
|
||||
statusURL, err := channels.BridgeStatusURL(bridgeURL)
|
||||
if err != nil {
|
||||
return map[string]interface{}{
|
||||
@@ -1344,15 +1345,77 @@ func (s *Server) webUIWhatsAppStatusPayload(ctx context.Context) (map[string]int
|
||||
}
|
||||
|
||||
func (s *Server) loadWhatsAppConfig() (cfgpkg.WhatsAppConfig, error) {
|
||||
cfg, err := s.loadConfig()
|
||||
if err != nil {
|
||||
return cfgpkg.WhatsAppConfig{}, err
|
||||
}
|
||||
return cfg.Channels.WhatsApp, nil
|
||||
}
|
||||
|
||||
func (s *Server) loadConfig() (*cfgpkg.Config, error) {
|
||||
configPath := strings.TrimSpace(s.configPath)
|
||||
if configPath == "" {
|
||||
configPath = filepath.Join(cfgpkg.GetConfigDir(), "config.json")
|
||||
}
|
||||
cfg, err := cfgpkg.LoadConfig(configPath)
|
||||
if err != nil {
|
||||
return cfgpkg.WhatsAppConfig{}, err
|
||||
return nil, err
|
||||
}
|
||||
return cfg.Channels.WhatsApp, nil
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
func (s *Server) resolveWhatsAppBridgeURL(cfg *cfgpkg.Config) string {
|
||||
if cfg == nil {
|
||||
return ""
|
||||
}
|
||||
raw := strings.TrimSpace(cfg.Channels.WhatsApp.BridgeURL)
|
||||
if raw == "" {
|
||||
return embeddedWhatsAppBridgeURL(cfg.Gateway.Host, cfg.Gateway.Port)
|
||||
}
|
||||
hostPort := comparableBridgeHostPort(raw)
|
||||
if hostPort == "" {
|
||||
return raw
|
||||
}
|
||||
if hostPort == "127.0.0.1:3001" || hostPort == "localhost:3001" {
|
||||
return embeddedWhatsAppBridgeURL(cfg.Gateway.Host, cfg.Gateway.Port)
|
||||
}
|
||||
if hostPort == comparableGatewayHostPort(cfg.Gateway.Host, cfg.Gateway.Port) {
|
||||
return embeddedWhatsAppBridgeURL(cfg.Gateway.Host, cfg.Gateway.Port)
|
||||
}
|
||||
return raw
|
||||
}
|
||||
|
||||
func embeddedWhatsAppBridgeURL(host string, port int) string {
|
||||
host = strings.TrimSpace(host)
|
||||
switch host {
|
||||
case "", "0.0.0.0", "::", "[::]":
|
||||
host = "127.0.0.1"
|
||||
}
|
||||
return fmt.Sprintf("ws://%s:%d/whatsapp/ws", host, port)
|
||||
}
|
||||
|
||||
func comparableBridgeHostPort(raw string) string {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" {
|
||||
return ""
|
||||
}
|
||||
if !strings.Contains(raw, "://") {
|
||||
return strings.ToLower(raw)
|
||||
}
|
||||
u, err := url.Parse(raw)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return strings.ToLower(strings.TrimSpace(u.Host))
|
||||
}
|
||||
|
||||
func comparableGatewayHostPort(host string, port int) string {
|
||||
host = strings.TrimSpace(strings.ToLower(host))
|
||||
switch host {
|
||||
case "", "0.0.0.0", "::", "[::]":
|
||||
host = "127.0.0.1"
|
||||
}
|
||||
return fmt.Sprintf("%s:%d", host, port)
|
||||
}
|
||||
|
||||
func renderQRCodeSVG(code *qr.Code, scale, quietZone int) string {
|
||||
|
||||
@@ -7,10 +7,13 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -167,6 +170,76 @@ func TestHandleWebUIWhatsAppStatusWithNestedBridgePath(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleWebUIWhatsAppStatusMapsLegacyBridgeURLToEmbeddedPath(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
bridge := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.URL.Path {
|
||||
case "/whatsapp/status":
|
||||
_ = json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
"state": "connected",
|
||||
"connected": true,
|
||||
"logged_in": true,
|
||||
"bridge_addr": "127.0.0.1:7788",
|
||||
"user_jid": "8613012345678@s.whatsapp.net",
|
||||
"qr_available": false,
|
||||
"last_event": "connected",
|
||||
"updated_at": "2026-03-09T12:00:00+08:00",
|
||||
})
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}))
|
||||
defer bridge.Close()
|
||||
|
||||
u, err := url.Parse(bridge.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("parse bridge url: %v", err)
|
||||
}
|
||||
host, portRaw, err := net.SplitHostPort(u.Host)
|
||||
if err != nil {
|
||||
t.Fatalf("split host port: %v", err)
|
||||
}
|
||||
port, err := strconv.Atoi(portRaw)
|
||||
if err != nil {
|
||||
t.Fatalf("atoi port: %v", err)
|
||||
}
|
||||
|
||||
tmp := t.TempDir()
|
||||
cfgPath := filepath.Join(tmp, "config.json")
|
||||
cfg := cfgpkg.DefaultConfig()
|
||||
cfg.Logging.Enabled = false
|
||||
cfg.Gateway.Host = host
|
||||
cfg.Gateway.Port = port
|
||||
cfg.Channels.WhatsApp.Enabled = true
|
||||
cfg.Channels.WhatsApp.BridgeURL = "ws://localhost:3001"
|
||||
if err := cfgpkg.SaveConfig(cfgPath, cfg); err != nil {
|
||||
t.Fatalf("save config: %v", err)
|
||||
}
|
||||
|
||||
srv := NewServer("127.0.0.1", 0, "", nil)
|
||||
srv.SetConfigPath(cfgPath)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/webui/api/whatsapp/status", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
srv.handleWebUIWhatsAppStatus(rec, req)
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("expected 200, got %d: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
if !strings.Contains(rec.Body.String(), `"bridge_running":true`) {
|
||||
t.Fatalf("expected bridge_running=true, got: %s", rec.Body.String())
|
||||
}
|
||||
var payload map[string]any
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &payload); err != nil {
|
||||
t.Fatalf("unmarshal payload: %v", err)
|
||||
}
|
||||
bridgeURL, _ := payload["bridge_url"].(string)
|
||||
if !strings.HasSuffix(bridgeURL, "/whatsapp/ws") {
|
||||
t.Fatalf("expected embedded whatsapp bridge url, got: %s", rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleWebUIConfigRequiresConfirmForProviderAPIBaseChange(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user