有没有办法指定用于名称查找的 DNS 服务器?
看着 https://golang.org/pkg/net/#LookupHost似乎它只会使用本地解析器
LookupHost looks up the given host using the local resolver. It returns a slice
of that host's addresses.
也早在那个链接上
It can use a pure Go resolver that sends DNS requests directly to
the servers listed in /etc/resolv.conf,
如何像使用 dig 那样对任意服务器进行查找?
dig @8.8.8.8 google.com
最佳答案
来自 /u/g-a-c 的回答|在 reddit
如果我正确地阅读了该文档(也许不是)...
使用您要使用的 DNS 地址( https://golang.org/pkg/net/#Resolver )创建自己的本地解析器和自定义拨号器,然后使用该解析器的 LookupAddr 函数?
编辑:
package main
import (
"context"
"net"
"time"
)
func main() {
r := &net.Resolver{
PreferGo: true,
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
d := net.Dialer{
Timeout: time.Millisecond * time.Duration(10000),
}
return d.DialContext(ctx, network, "8.8.8.8:53")
},
}
ip, _ := r.LookupHost(context.Background(), "www.google.com")
print(ip[0])
}
这似乎有效 - 在我的防火墙上,这表明我的机器正在打开与 Google 的连接,而不是本地域 Controller
关于go - 指定用于在 Go 中查找的 DNS 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59889882/