有没有办法指定用于名称查找的 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.
Run Code Online (Sandbox Code Playgroud)
也早在那个链接上
It can use a pure Go resolver that sends DNS requests directly to
the servers listed in /etc/resolv.conf,
Run Code Online (Sandbox Code Playgroud)
如何像使用 dig 那样对任意服务器进行查找?
dig @8.8.8.8 google.com
Run Code Online (Sandbox Code Playgroud)
Fra*_*844 17
如果我正确地阅读了该文档(也许不是)...
使用您要使用的 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])
}
Run Code Online (Sandbox Code Playgroud)
这似乎有效 - 在我的防火墙上,这表明我的机器正在打开与 Google 的连接,而不是本地域控制器
| 归档时间: |
|
| 查看次数: |
7944 次 |
| 最近记录: |