From 5612403d483d0dc86c338595531ad97c1832ae4c Mon Sep 17 00:00:00 2001 From: ryan Date: Sun, 31 May 2026 20:02:01 +0800 Subject: [PATCH] =?UTF-8?q?[=E4=BC=98=E5=8C=96]=20=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- openflare_agent/internal/config/config.go | 26 ++-------------------- openflare_agent/internal/nginx/manager.go | 26 ++-------------------- openflare_server/utils/slice.go | 27 +++++++++++++++++++++++ 3 files changed, 31 insertions(+), 48 deletions(-) diff --git a/openflare_agent/internal/config/config.go b/openflare_agent/internal/config/config.go index de331544..b1e9d6bb 100644 --- a/openflare_agent/internal/config/config.go +++ b/openflare_agent/internal/config/config.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "net" + "openflare/utils" "openflare/utils/geoip" "openflare/utils/geoip/iputil" "os" @@ -149,7 +150,7 @@ func Load(path string) (*Config, error) { func applyDefaults(cfg *Config, baseDir string) { baseDir = filepath.Clean(baseDir) cfg.AgentVersion = AgentVersion - cfg.OpenrestyResolvers = normalizeResolverList(cfg.OpenrestyResolvers) + cfg.OpenrestyResolvers = utils.UniqueAndCleanStringSlice(cfg.OpenrestyResolvers) if cfg.OpenrestyPath == "" { cfg.OpenrestyPath = "openresty" } @@ -405,29 +406,6 @@ func detectHostname() string { return strings.TrimSpace(host) } -func normalizeResolverList(values []string) []string { - if len(values) == 0 { - return nil - } - result := make([]string, 0, len(values)) - seen := make(map[string]struct{}, len(values)) - for _, value := range values { - trimmed := strings.TrimSpace(value) - if trimmed == "" { - continue - } - if _, ok := seen[trimmed]; ok { - continue - } - seen[trimmed] = struct{}{} - result = append(result, trimmed) - } - if len(result) == 0 { - return nil - } - return result -} - func firstNonEmpty(values ...string) string { for _, value := range values { if strings.TrimSpace(value) != "" { diff --git a/openflare_agent/internal/nginx/manager.go b/openflare_agent/internal/nginx/manager.go index e8844121..9acb95be 100644 --- a/openflare_agent/internal/nginx/manager.go +++ b/openflare_agent/internal/nginx/manager.go @@ -19,6 +19,7 @@ import ( "strings" "time" + "openflare/utils" openrestyrender "openflare/utils/render/openresty" "openflare-agent/internal/protocol" @@ -1110,7 +1111,7 @@ func ResolverDirective(openrestyPath string, explicitResolvers []string) string } func resolverAddresses(openrestyPath string, explicitResolvers []string) []string { - if resolvers := normalizeResolverAddresses(explicitResolvers); len(resolvers) > 0 { + if resolvers := utils.UniqueAndCleanStringSlice(explicitResolvers); len(resolvers) > 0 { return resolvers } data, err := os.ReadFile("/etc/resolv.conf") @@ -1153,29 +1154,6 @@ func isUsableDockerResolver(addr string) bool { return !ip.IsLoopback() && !ip.IsUnspecified() } -func normalizeResolverAddresses(values []string) []string { - if len(values) == 0 { - return nil - } - resolvers := make([]string, 0, len(values)) - seen := make(map[string]struct{}, len(values)) - for _, value := range values { - addr := strings.TrimSpace(value) - if addr == "" { - continue - } - if _, ok := seen[addr]; ok { - continue - } - seen[addr] = struct{}{} - resolvers = append(resolvers, addr) - } - if len(resolvers) == 0 { - return nil - } - return resolvers -} - func RequiresRuntimeResolver(originURL string) bool { parsed, err := url.Parse(strings.TrimSpace(originURL)) if err != nil || parsed.Hostname() == "" { diff --git a/openflare_server/utils/slice.go b/openflare_server/utils/slice.go index 5d2433ed..00128f75 100644 --- a/openflare_server/utils/slice.go +++ b/openflare_server/utils/slice.go @@ -1,5 +1,7 @@ package utils +import "strings" + // Unique returns a new slice containing only the unique elements of the input slice, // preserving their original order. func Unique[T comparable](slice []T) []T { @@ -17,3 +19,28 @@ func Unique[T comparable](slice []T) []T { } return result } + +// UniqueAndCleanStringSlice trims spaces, removes empty elements, and returns only the unique elements +// of the input string slice. It preserves order and returns nil if the resulting slice is empty. +func UniqueAndCleanStringSlice(slice []string) []string { + if slice == nil { + return nil + } + seen := make(map[string]struct{}) + result := make([]string, 0) + for _, item := range slice { + trimmed := strings.TrimSpace(item) + if trimmed == "" { + continue + } + if _, ok := seen[trimmed]; ok { + continue + } + seen[trimmed] = struct{}{} + result = append(result, trimmed) + } + if len(result) == 0 { + return nil + } + return result +}