(queryType, domain string, dnsServerList []string)
| 44 | } |
| 45 | |
| 46 | func ResolveDomainAtServerList(queryType, domain string, dnsServerList []string) (string, error) { |
| 47 | |
| 48 | if len(dnsServerList) == 0 { |
| 49 | if queryType == "AAAA" { |
| 50 | dnsServerList = DefaultIPv6DNSServerList |
| 51 | } else { |
| 52 | dnsServerList = DefaultIPv4DNSServerList |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | //some name that ought to exist, does not exist (NXDOMAIN) |
| 57 | |
| 58 | querytype, querytypeOk := dns.StringToType[strings.ToUpper(queryType)] |
| 59 | if !querytypeOk { |
| 60 | return "", fmt.Errorf("queryType error:%s", queryType) |
| 61 | } |
| 62 | |
| 63 | if strings.HasPrefix(domain, "*.") { |
| 64 | randomStr := stringsp.GetRandomString(8) |
| 65 | domain = strings.Replace(domain, "*", randomStr, 1) |
| 66 | } |
| 67 | |
| 68 | domain = dns.Fqdn(domain) |
| 69 | domain, err := idna.ToASCII(domain) |
| 70 | if err != nil { |
| 71 | return "", fmt.Errorf(` idna.ToASCII(domain) error:%s`, err.Error()) |
| 72 | } |
| 73 | |
| 74 | m := new(dns.Msg) |
| 75 | m.SetQuestion(domain, querytype) |
| 76 | m.MsgHdr.RecursionDesired = true |
| 77 | |
| 78 | c := new(dns.Client) |
| 79 | noExistTimes := 0 |
| 80 | for _, dnsServer := range dnsServerList { |
| 81 | c.Net = "" |
| 82 | ipaddr, err := resolveDomain(m, c, dnsServer) |
| 83 | if err != nil { |
| 84 | //log.Printf("[%s]===>[%s][%s] ResolveDomain error:%s", dnsServer, queryType, domain, err.Error()) |
| 85 | if strings.Contains(err.Error(), "some name that ought to exist, does not exist (NXDOMAIN)") { |
| 86 | noExistTimes++ |
| 87 | if noExistTimes >= 4 { |
| 88 | return "", fmt.Errorf("解析域名[%s][%s]IP失败:noExistTimes", queryType, domain) |
| 89 | } |
| 90 | } |
| 91 | continue |
| 92 | } |
| 93 | return ipaddr, nil |
| 94 | } |
| 95 | |
| 96 | return "", fmt.Errorf("解析域名[%s][%s]IP失败", queryType, domain) |
| 97 | } |
| 98 | |
| 99 | func resolveDomain(msg *dns.Msg, client *dns.Client, dnsServer string) (string, error) { |
| 100 |
no test coverage detected