go - 指定用于在 Go 中查找的 DNS 服务器

标签 go dns

有没有办法指定用于名称查找的 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/

相关文章:

amazon-web-services - 将 EC2 公共(public) IP 地址指向子域

php - 从 Linux 服务器发送邮件的问题

go - 如何在 Go 中读取打包的二进制数据?

date - Go 编程语言如何处理日期和时间?

ssl - 共享 SSL 证书作为子域

Linux 无法 ping 通 google.com

heroku - 如何将heroku应用程序DNS配置为Godaddy域?

go - 如何将gin模式设置为release模式?

docker - Docker容器中的服务器空响应

go - 使用 go.mod 创建和使用本地模块(go 1.15)