github.com/nxtrace/NTrace-core@v1.3.1-0.20240513132635-39169291e8c9/util/dot.go (about)

     1  package util
     2  
     3  import (
     4  	"context"
     5  	"crypto/tls"
     6  	"net"
     7  	"time"
     8  )
     9  
    10  func newDoTResolver(serverName string, addrs string) *net.Resolver {
    11  
    12  	d := &net.Dialer{
    13  		// 设置超时时间
    14  		Timeout: 1000 * time.Millisecond,
    15  	}
    16  
    17  	tlsConfig := &tls.Config{
    18  		// 设置 TLS Server Name 以确保证书能和域名对应
    19  		ServerName: serverName,
    20  	}
    21  	return &net.Resolver{
    22  		// 指定使用 Go Build-in 的 DNS Resolver 来解析
    23  		PreferGo: true,
    24  		Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
    25  			conn, err := d.DialContext(ctx, "tcp", addrs)
    26  			if err != nil {
    27  				return nil, err
    28  			}
    29  			return tls.Client(conn, tlsConfig), nil
    30  		},
    31  	}
    32  }