Spaces:
Running
Running
Commit ·
abe7655
1
Parent(s): ab3d262
okay
Browse files
whatsapp-service/cmd/agentdeck-whatsapp-service/main.go
CHANGED
|
@@ -6,7 +6,9 @@ import (
|
|
| 6 |
"flag"
|
| 7 |
"fmt"
|
| 8 |
"log"
|
|
|
|
| 9 |
"net/http"
|
|
|
|
| 10 |
"os"
|
| 11 |
"os/signal"
|
| 12 |
"strings"
|
|
@@ -263,7 +265,9 @@ func initRedis(redisURL string) (*redis.Client, error) {
|
|
| 263 |
if redisURL == "" {
|
| 264 |
return nil, fmt.Errorf("REDIS_URL is required for caching")
|
| 265 |
}
|
| 266 |
-
|
|
|
|
|
|
|
| 267 |
if err != nil {
|
| 268 |
return nil, fmt.Errorf("invalid REDIS_URL: %v", err)
|
| 269 |
}
|
|
@@ -277,6 +281,39 @@ func initRedis(redisURL string) (*redis.Client, error) {
|
|
| 277 |
return client, nil
|
| 278 |
}
|
| 279 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 280 |
// @title AgentDeck Whatsapp Service
|
| 281 |
// @version 1.0
|
| 282 |
// @description AgentDeck Whatsapp Service - whatsmeow
|
|
|
|
| 6 |
"flag"
|
| 7 |
"fmt"
|
| 8 |
"log"
|
| 9 |
+
"net"
|
| 10 |
"net/http"
|
| 11 |
+
"net/url"
|
| 12 |
"os"
|
| 13 |
"os/signal"
|
| 14 |
"strings"
|
|
|
|
| 265 |
if redisURL == "" {
|
| 266 |
return nil, fmt.Errorf("REDIS_URL is required for caching")
|
| 267 |
}
|
| 268 |
+
// Container runtimes often lack IPv6 routes; pin hostnames to IPv4 so the
|
| 269 |
+
// dial cannot fail with "network is unreachable" on an AAAA-only result.
|
| 270 |
+
opts, err := redis.ParseURL(forceIPv4RedisURL(redisURL))
|
| 271 |
if err != nil {
|
| 272 |
return nil, fmt.Errorf("invalid REDIS_URL: %v", err)
|
| 273 |
}
|
|
|
|
| 281 |
return client, nil
|
| 282 |
}
|
| 283 |
|
| 284 |
+
// forceIPv4RedisURL resolves the host of a redis:// URL to its IPv4 address.
|
| 285 |
+
// Falls back to the original URL unchanged on any resolution/parse error.
|
| 286 |
+
func forceIPv4RedisURL(redisURL string) string {
|
| 287 |
+
u, err := url.Parse(redisURL)
|
| 288 |
+
if err != nil {
|
| 289 |
+
return redisURL
|
| 290 |
+
}
|
| 291 |
+
host := u.Hostname()
|
| 292 |
+
if host == "" || net.ParseIP(host) != nil {
|
| 293 |
+
return redisURL
|
| 294 |
+
}
|
| 295 |
+
ips, err := net.LookupIP(host)
|
| 296 |
+
if err != nil {
|
| 297 |
+
return redisURL
|
| 298 |
+
}
|
| 299 |
+
var v4 string
|
| 300 |
+
for _, ip := range ips {
|
| 301 |
+
if ip4 := ip.To4(); ip4 != nil {
|
| 302 |
+
v4 = ip4.String()
|
| 303 |
+
break
|
| 304 |
+
}
|
| 305 |
+
}
|
| 306 |
+
if v4 == "" {
|
| 307 |
+
return redisURL
|
| 308 |
+
}
|
| 309 |
+
if port := u.Port(); port != "" {
|
| 310 |
+
u.Host = net.JoinHostPort(v4, port)
|
| 311 |
+
} else {
|
| 312 |
+
u.Host = v4
|
| 313 |
+
}
|
| 314 |
+
return u.String()
|
| 315 |
+
}
|
| 316 |
+
|
| 317 |
// @title AgentDeck Whatsapp Service
|
| 318 |
// @version 1.0
|
| 319 |
// @description AgentDeck Whatsapp Service - whatsmeow
|