[优化] 增加对节点 IP 的自动探测,优先通过第三方 API 获取公网 IP

This commit is contained in:
ryan
2026-05-30 10:18:05 +08:00
parent 2a00d21987
commit 4e339caa9a
13 changed files with 370 additions and 9 deletions
+24
View File
@@ -1,10 +1,12 @@
package config
import (
"context"
"encoding/json"
"errors"
"fmt"
"net"
"openflare/utils/geoip"
"openflare/utils/geoip/iputil"
"os"
pathpkg "path"
@@ -27,6 +29,11 @@ const (
defaultObservabilityReplayMinutes = 15
)
var (
lookupOutboundIP = geoip.GetOutboundIP
lookupLocalIP = detectLocalNodeIP
)
type Config struct {
ServerURL string `json:"server_url"`
AgentToken string `json:"agent_token"`
@@ -403,6 +410,23 @@ func firstNonEmpty(values ...string) string {
}
func detectNodeIP() string {
if ip := detectOutboundNodeIP(); ip != "" {
return ip
}
return lookupLocalIP()
}
func detectOutboundNodeIP() string {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
ip, err := lookupOutboundIP(ctx)
if err != nil || ip == nil {
return ""
}
return ip.String()
}
func detectLocalNodeIP() string {
interfaces, err := net.Interfaces()
if err != nil {
return ""