Go 语言请求DNS解析结果

package ksyunwarning

import (
    "context"
    "fmt"
    "net"
    "time"
)

// LookupDomainNameIp 使用net包做DNS解析请求
func LookupDomainNameIp(domainString, nameServer string) (dst []string) {
    r := &net.Resolver{
        PreferGo: true,
        Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
            d := net.Dialer{
                Timeout: 3 * time.Second,
            }
            return d.DialContext(ctx, "udp", fmt.Sprintf("%s:53", nameServer))
        },
    }
    var err error
    dst, err = r.LookupHost(context.Background(), domainString)
    if err != nil {
        s.fileLogger.Error("LookupDomainNameIp LookupHost Err : %v", err)
    }
    return
}

 参考:http://www.linvon.cn/posts/golang-%E6%8C%87%E5%AE%9A-dns-%E6%9C%8D%E5%8A%A1%E5%99%A8%E5%81%9A%E5%9F%9F%E5%90%8D%E8%A7%A3%E6%9E%90/

posted on 2023-09-05 20:50  zhangmingda  阅读(81)  评论(0编辑  收藏  举报

导航