github.com/TeaOSLab/EdgeNode@v1.3.8/internal/utils/lookup.go (about) 1 package utils 2 3 import ( 4 "github.com/TeaOSLab/EdgeCommon/pkg/configutils" 5 "github.com/miekg/dns" 6 ) 7 8 // LookupCNAME 获取CNAME 9 func LookupCNAME(host string) (string, error) { 10 config, err := dns.ClientConfigFromFile("/etc/resolv.conf") 11 if err != nil { 12 return "", err 13 } 14 15 c := new(dns.Client) 16 m := new(dns.Msg) 17 18 m.SetQuestion(host+".", dns.TypeCNAME) 19 m.RecursionDesired = true 20 21 var lastErr error 22 for _, serverAddr := range config.Servers { 23 r, _, err := c.Exchange(m, configutils.QuoteIP(serverAddr)+":"+config.Port) 24 if err != nil { 25 lastErr = err 26 continue 27 } 28 if len(r.Answer) == 0 { 29 continue 30 } 31 32 return r.Answer[0].(*dns.CNAME).Target, nil 33 } 34 return "", lastErr 35 }