登录
首页 >  Golang >  Go问答

获取特定域名的所有 TXT 记录

来源:stackoverflow

时间:2024-03-19 18:54:31 420浏览 收藏

在 Go 中获取特定域名 TXT 记录时,可以通过修改解析器配置解决超时问题。默认情况下,dig 命令可能无法处理大于 512 字节的响应或通过 TCP 进行查询。通过在 resolver.conf 中设置 options timeout:30 或使用 dig @8.8.8.8 +ignore +short +bufsize=1024 geckoboard.com txt 命令,可以将超时时间设置为更高值并指定更大的缓冲区大小,从而避免超时并成功获取 TXT 记录。

问题内容

我想在 go 中提取特定域的 txt 记录。我查看了一堆博客并尝试了以下代码:

package main

import (
        "fmt"
        "net"
)

func main() {
        txts, err := net.lookuptxt("google.com")
        if err != nil {
                panic(err)
        }
        if len(txts) == 0 {
                fmt.printf("no record")
        }
        for _, txt := range txts {
                fmt.printf("%s\n", txt)
        }

}

当我执行该程序时,我得到以下输出。

docusign=05958488-4752-4ef2-95eb-aa7ba8a3bd0e
facebook-domain-verification=22rm551cu4k0ab0bxsw536tlds4h95
globalsign-smime-dv=cdyx+xfhuw2wml6/gb8+59bsh31kzur6c1l2bpvqkx8=
v=spf1 include:_spf.google.com ~all

这是根据我的要求工作的,因为我按照 https://www.kitterman.com/spf/validate.html 来验证我是否获得了正确的输出。

现在,每当我将输入域更改为 geckoboard.com(比如说)时,我都会收到以下错误:

panic: lookup geckoboard.com on 127.0.0.53:53: read udp 127.0.0.1:38440->127.0.0.53:53: i/o timeout   
goroutine 1 [running]: 
main.main()     
          /home/maruthi/emailheader.go:11
+0x190 exit status 2

我知道这是一个超时异常。但是,当我在 https://www.kitterman.com/spf/validate.html 上运行相同的查询时,我会在几秒钟内得到预期的结果。

除了使用 net.lookuptxt("google.com") 之外,还有其他更好的方法来提取 txt 记录吗?如果没有,有人可以建议我一个好的重试机制,用于具有更高超时值的相同代码吗?

更新 1:尝试了 @florian weimer 提供的答案,但仍然超时。

$ dig +ignore +bufsize=512 geckoboard.com txt
; <<>> DiG 9.11.3-1ubuntu1.5-Ubuntu <<>> +ignore +bufsize=512 geckoboard.com txt
;; global options: +cmd
;; connection timed out; no servers could be reached

更新 2: 根据 @thundercat 的建议,我将超时设置为更高的值。我在resolver.conf中添加了 options timeout:30 。这两个查询、dig 和我的程序在超时之前都运行了 30 秒以上。


解决方案


谢谢@florian weimer 的帮助。我已经通过您的 dig 命令的小扩展来完成此操作。

$ dig @8.8.8.8 +ignore +short +bufsize=1024 geckoboard.com txt
"ms=ms20890953"
"facebook-domain-verification=uh1r0ebrc3sig0h2za0djoj4mhkn3g"
"google-site-verification=i6ouoqinhxpnud8ybb3-c8gqa7ykbehdx0xwueeglqi"
"google-site-verification=pwasmmjvce_daqc2-b7cz9uw4rft6y8zwq7yorbhmdw"
"google-site-verification=lsxvrgw-op91yihsz1knv57efgt97tmerxajv5hfi2q"
"spf2.0/pra include:spf.recurly.com ~all"
"status-page-domain-verification=8963fbq9nrjx"
"v=spf1 include:_spf.google.com include:sendgrid.net include:spf.recurly.com include:mail.zendesk.com include:servers.mcsv.net ~all"

我的 golang 代码是:

package main

import (
    "fmt"
    "os/exec"
)

func main() {
    out, err := exec.command("bash", "-c", "dig @8.8.8.8 +ignore +short +bufsize=1024 geckoboard.com txt").output()
    s := string(out[:])

    if err != nil {
        fmt.println("unexpected error occured ", err)
    }
    fmt.printf(s)
}

您的递归解析器可能配置错误或刚刚损坏。它可能无法正确处理 edns,或者根本不处理 tcp 查询。 (一些客户端虚拟化解决方案内置了存在这些问题的 dns 转发器。)

需要 tcp 的原因是响应大小大于 512 字节:

$ dig +ignore +bufsize=512 geckoboard.com txt

; <<>> DiG 9.10.3-P4-Debian <<>> +ignore +bufsize=512 geckoboard.com txt
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 60761
;; flags: qr tc rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1200
;; QUESTION SECTION:
;geckoboard.com.            IN  TXT

;; Query time: 0 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Wed Mar 06 20:39:31 CET 2019
;; MSG SIZE  rcvd: 43

tc 标志意味着客户端应该通过 tcp 重试。 (通常情况下,dig 会自动执行此操作,但 +ignore 标志会抑制此操作。)

这在您的环境中似乎失败了。也有可能递归解析器本身无法从全局 dns 获取数据。事实上,dig 查询会导致超时,而不是 tc 的响应,这表明是后者。进一步调试需要捕获数据包。

以上就是《获取特定域名的所有 TXT 记录》的详细内容,更多关于的资料请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>