github.com/metacubex/mihomo@v1.18.5/component/dialer/resolver.go (about)

     1  package dialer
     2  
     3  import (
     4  	"context"
     5  	"net"
     6  	"net/netip"
     7  )
     8  
     9  func init() {
    10  	// We must use this DialContext to query DNS
    11  	// when using net default resolver.
    12  	net.DefaultResolver.PreferGo = true
    13  	net.DefaultResolver.Dial = resolverDialContext
    14  }
    15  
    16  func resolverDialContext(ctx context.Context, network, address string) (net.Conn, error) {
    17  	d := &net.Dialer{}
    18  
    19  	interfaceName := DefaultInterface.Load()
    20  
    21  	if interfaceName != "" {
    22  		dstIP, err := netip.ParseAddr(address)
    23  		if err == nil {
    24  			_ = bindIfaceToDialer(interfaceName, d, network, dstIP)
    25  		}
    26  	}
    27  
    28  	return d.DialContext(ctx, network, address)
    29  }