gpt4 book ai didi

go - 通过 Cisco IPSec 连接时,应用程序不解析内部 DNS 条目

转载 作者:IT王子 更新时间:2023-10-29 01:09:46 26 4
gpt4 key购买 nike

我使用 Cisco IPsec 连接到我工作场所的 VPN。我使用 OS X 的 native Cisco IPSec 客户端进行连接。我们有一个内部 DNS 服务器,用于保存内部站点的记录,例如 scotty.infinidat.com。使用 curl 联系内部站点按预期工作。使用以下 Python 代码也可以:

import requests

resp = requests.get("http://www.google.com")
resp.raise_for_status()

resp = requests.get("http://scotty.infinidat.com")
resp.raise_for_status()

但是,尝试在 Go 中实现等价物失败了:

主要包

import (
"fmt"
"net/http"
)

func main() {
_, err := http.Get("http://google.com/") // This works
if err != nil {
panic(fmt.Sprintf("Error contacting Google: %s", err))
}

_, err = http.Get("http://scotty.infinidat.com/") // This doesn't
if err != nil {
panic(fmt.Sprintf("Error contacting an internal site: %s", err))
}
}

在连接到 VPN 时运行上面的程序会产生以下输出:

panic: Error contacting internal site: Get http://scotty.infinidat.com/: dial tcp: lookup scotty.infinidat.com on 10.135.1.1:53: no such host

goroutine 1 [running]:
panic(0x290ca0, 0xc82010a490)
/usr/local/Cellar/go/1.6.2/libexec/src/runtime/panic.go:481 +0x3e6
main.main()
/Users/roeyd/src/go/src/webtest/main.go:16 +0x2af

其中 10.135.1.1 是我本地网络的 DNS 服务器。据我了解,纯 Go DNS 解析器在 OS X 上不可用。通过设置 GODEBUG=netdns=cgo 强制 Go 使用 cgo DNS 解析器没有任何区别。

最佳答案

或许你可以使用一个完整的DNS库来先解析IP:

 package main

import (
"log"

"github.com/miekg/dns"
)

func main() {
c := dns.Client{}
m := dns.Msg{}
m.SetQuestion("scotty.infinidat.com.", dns.TypeA)
r, t, err := c.Exchange(&m, "10.135.1.1:53")
if err != nil {
log.Fatal(err)
}
log.Printf("Took %v", t)
for _, ans := range r.Answer {
Arecord := ans.(*dns.A)
log.Printf("%s", Arecord.A)
}
}

关于go - 通过 Cisco IPSec 连接时,应用程序不解析内部 DNS 条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36997731/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com