From e0e35ccb15a598e9a136cc179a5485e4237d2653 Mon Sep 17 00:00:00 2001 From: ForestL <45709305+ForestL18@users.noreply.github.com> Date: Sun, 18 Aug 2024 12:19:11 +0800 Subject: [PATCH 1/2] Update system_windows.go --- dns/system_windows.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/dns/system_windows.go b/dns/system_windows.go index 47c1ebaa4e..c87a37b19d 100644 --- a/dns/system_windows.go +++ b/dns/system_windows.go @@ -16,6 +16,9 @@ func dnsReadConfig() (servers []string, err error) { if err != nil { return } + + seenIPs := make(map[string]bool) + for _, aa := range aas { for dns := aa.FirstDnsServerAddress; dns != nil; dns = dns.Next { sa, err := dns.Address.Sockaddr.Sockaddr() @@ -40,7 +43,12 @@ func dnsReadConfig() (servers []string, err error) { // Unexpected type. continue } - servers = append(servers, ip.String()) + + ipStr := ip.String() + if !seenIPs[ipStr] { + seenIPs[ipStr] = true + servers = append(servers, ipStr) + } } } return From 95725c121e23fa76c83d3bd93c0c858137c2be7c Mon Sep 17 00:00:00 2001 From: ForestL <45709305+ForestL18@users.noreply.github.com> Date: Sun, 18 Aug 2024 12:55:00 +0800 Subject: [PATCH 2/2] Using map[string]struct{} as a set to store unique values --- dns/system_windows.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/dns/system_windows.go b/dns/system_windows.go index c87a37b19d..28be405d1a 100644 --- a/dns/system_windows.go +++ b/dns/system_windows.go @@ -17,7 +17,7 @@ func dnsReadConfig() (servers []string, err error) { return } - seenIPs := make(map[string]bool) + uniqueIPs := make(map[string]struct{}) for _, aa := range aas { for dns := aa.FirstDnsServerAddress; dns != nil; dns = dns.Next { @@ -44,13 +44,15 @@ func dnsReadConfig() (servers []string, err error) { continue } - ipStr := ip.String() - if !seenIPs[ipStr] { - seenIPs[ipStr] = true - servers = append(servers, ipStr) - } + uniqueIPs[ip.String()] = struct{}{} } } + + servers = make([]string, 0, len(uniqueIPs)) + for ip := range uniqueIPs { + servers = append(servers, ip) + } + return }