forked from openatx/atx-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdns.go
38 lines (34 loc) · 745 Bytes
/
dns.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package main
import (
"context"
"log"
"net"
"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) {
dns1 := getProperty("net.dns1")
if 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,
}
}