提取特定域的所有 TXT 记录
Extracting all the TXT Records of a particular Domain
我想在 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 秒后超时。
您的递归解析器可能配置错误或损坏。它可能无法正确处理 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
的响应这一事实表明后者。进一步调试需要抓包。
谢谢@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)
}
我想在 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 秒后超时。
您的递归解析器可能配置错误或损坏。它可能无法正确处理 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
的响应这一事实表明后者。进一步调试需要抓包。
谢谢@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)
}