加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
dns.go 868 Bytes
一键复制 编辑 原始数据 按行查看 历史
codeskyblue 提交于 2019-03-25 16:30 . fix dns error, close #39
package main
import (
"context"
"log"
"net"
"strings"
"time"
)
type dnsSmartClient struct {
dialer *net.Dialer
}
func newDnsSmartClient() *dnsSmartClient {
return &dnsSmartClient{
dialer: &net.Dialer{
Timeout: 3 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
},
}
}
func (c *dnsSmartClient) Dial(ctx context.Context, network, address string) (conn net.Conn, err error) {
// net.dns1 might be ipv6, Issue https://github.com/openatx/atx-agent/issues/39
dns1 := getProperty("net.dns1")
if dns1 == "" || strings.Contains(dns1, ":") {
// 国内DNS列表: https://www.zhihu.com/question/32229915
dns1 = "114.114.114.114"
}
log.Println("dns resolve", dns1)
return c.dialer.DialContext(ctx, "udp", dns1+":53")
}
func init() {
net.DefaultResolver = &net.Resolver{
PreferGo: true,
Dial: newDnsSmartClient().Dial,
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化