dns.go 861 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package main
  2. import (
  3. "context"
  4. "net"
  5. "strings"
  6. "time"
  7. )
  8. type dnsSmartClient struct {
  9. dialer *net.Dialer
  10. }
  11. func newDnsSmartClient() *dnsSmartClient {
  12. return &dnsSmartClient{
  13. dialer: &net.Dialer{
  14. Timeout: 3 * time.Second,
  15. KeepAlive: 30 * time.Second,
  16. DualStack: true,
  17. },
  18. }
  19. }
  20. func (c *dnsSmartClient) Dial(ctx context.Context, network, address string) (conn net.Conn, err error) {
  21. // net.dns1 might be ipv6, Issue https://github.com/openatx/atx-agent/issues/39
  22. dns1 := getProperty("net.dns1")
  23. if dns1 == "" || strings.Contains(dns1, ":") {
  24. // 国内DNS列表: https://www.zhihu.com/question/32229915
  25. dns1 = "114.114.114.114"
  26. }
  27. log.Println("dns resolve", dns1)
  28. return c.dialer.DialContext(ctx, "udp", dns1+":53")
  29. }
  30. func init() {
  31. net.DefaultResolver = &net.Resolver{
  32. PreferGo: true,
  33. Dial: newDnsSmartClient().Dial,
  34. }
  35. }