From 3a2c865ff6acf9a71cfd1ec127b6cbdf225498fb Mon Sep 17 00:00:00 2001 From: Wim Date: Tue, 18 Feb 2020 17:09:11 +0100 Subject: [PATCH] Fix high cpu usage with IPv6 recursor address. Closes #6120 (#6128) --- agent/dns.go | 13 ++++++++++--- agent/dns_test.go | 15 +++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/agent/dns.go b/agent/dns.go index 73ae8e7c5d..e44a6fda96 100644 --- a/agent/dns.go +++ b/agent/dns.go @@ -274,9 +274,16 @@ func recursorAddr(recursor string) (string, error) { // Add the port if none START: _, _, err := net.SplitHostPort(recursor) - if ae, ok := err.(*net.AddrError); ok && ae.Err == "missing port in address" { - recursor = ipaddr.FormatAddressPort(recursor, 53) - goto START + if ae, ok := err.(*net.AddrError); ok { + if ae.Err == "missing port in address" { + recursor = ipaddr.FormatAddressPort(recursor, 53) + goto START + } else if ae.Err == "too many colons in address" { + if ip := net.ParseIP(recursor); ip != nil && ip.To4() == nil { + recursor = ipaddr.FormatAddressPort(recursor, 53) + goto START + } + } } if err != nil { return "", err diff --git a/agent/dns_test.go b/agent/dns_test.go index c04e53fa23..57d420af2d 100644 --- a/agent/dns_test.go +++ b/agent/dns_test.go @@ -109,6 +109,21 @@ func TestRecursorAddr(t *testing.T) { if addr != "8.8.8.8:53" { t.Fatalf("bad: %v", addr) } + addr, err = recursorAddr("2001:4860:4860::8888") + if err != nil { + t.Fatalf("err: %v", err) + } + if addr != "[2001:4860:4860::8888]:53" { + t.Fatalf("bad: %v", addr) + } + addr, err = recursorAddr("1.2.3.4::53") + if err == nil || !strings.Contains(err.Error(), "too many colons in address") { + t.Fatalf("err: %v", err) + } + addr, err = recursorAddr("2001:4860:4860::8888:::53") + if err == nil || !strings.Contains(err.Error(), "too many colons in address") { + t.Fatalf("err: %v", err) + } } func TestEncodeKVasRFC1464(t *testing.T) {