refine whatsapp bridge defaults and webui polish

This commit is contained in:
lpf
2026-03-10 12:07:56 +08:00
parent 8a6a2755de
commit c7b159d2ed
29 changed files with 1389 additions and 432 deletions

View File

@@ -450,7 +450,7 @@ func (s *WhatsAppBridgeService) ServeWS(w http.ResponseWriter, r *http.Request)
func (s *WhatsAppBridgeService) wrapHandler(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if s.localOnly && !isLoopbackRequest(r) {
if s.localOnly && !isLocalRequest(r) {
http.Error(w, "forbidden", http.StatusForbidden)
return
}
@@ -954,14 +954,43 @@ func joinBridgeRoute(basePath, endpoint string) string {
return basePath + "/" + strings.TrimPrefix(endpoint, "/")
}
func isLoopbackRequest(r *http.Request) bool {
func isLocalRequest(r *http.Request) bool {
if r == nil {
return false
}
host, _, err := net.SplitHostPort(strings.TrimSpace(r.RemoteAddr))
addrs, err := net.InterfaceAddrs()
if err != nil {
host = strings.TrimSpace(r.RemoteAddr)
return false
}
return isLocalRemoteAddr(strings.TrimSpace(r.RemoteAddr), addrs)
}
func isLocalRemoteAddr(remoteAddr string, localAddrs []net.Addr) bool {
host, _, err := net.SplitHostPort(strings.TrimSpace(remoteAddr))
if err != nil {
host = strings.TrimSpace(remoteAddr)
}
ip := net.ParseIP(host)
return ip != nil && ip.IsLoopback()
if ip == nil {
return false
}
if ip.IsLoopback() {
return true
}
for _, addr := range localAddrs {
if addr == nil {
continue
}
switch v := addr.(type) {
case *net.IPNet:
if v.IP != nil && v.IP.Equal(ip) {
return true
}
case *net.IPAddr:
if v.IP != nil && v.IP.Equal(ip) {
return true
}
}
}
return false
}

View File

@@ -3,6 +3,7 @@ package channels
import (
"context"
"encoding/json"
"net"
"testing"
"time"
@@ -63,6 +64,32 @@ func TestBridgeStatusURLWithNestedPath(t *testing.T) {
}
}
func TestIsLocalRemoteAddr(t *testing.T) {
ipv4Net := &net.IPNet{IP: net.ParseIP("192.168.1.10"), Mask: net.CIDRMask(24, 32)}
ipv6Net := &net.IPNet{IP: net.ParseIP("fe80::1"), Mask: net.CIDRMask(64, 128)}
tests := []struct {
name string
remoteAddr string
want bool
}{
{name: "loopback", remoteAddr: "127.0.0.1:4321", want: true},
{name: "local interface ipv4", remoteAddr: "192.168.1.10:4321", want: true},
{name: "local interface ipv6", remoteAddr: "[fe80::1]:4321", want: true},
{name: "non local ip", remoteAddr: "192.168.1.11:4321", want: false},
{name: "invalid host", remoteAddr: "not-an-ip", want: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := isLocalRemoteAddr(tt.remoteAddr, []net.Addr{ipv4Net, ipv6Net})
if got != tt.want {
t.Fatalf("got %v want %v", got, tt.want)
}
})
}
}
func TestNormalizeWhatsAppRecipientJID(t *testing.T) {
tests := []struct {
input string