github.com/Uhtred009/v2ray-core-1@v4.31.2+incompatible/app/dns/nameserver.go (about)

     1  // +build !confonly
     2  
     3  package dns
     4  
     5  import (
     6  	"context"
     7  
     8  	"v2ray.com/core/common/net"
     9  	"v2ray.com/core/features/dns/localdns"
    10  )
    11  
    12  // IPOption is an object for IP query options.
    13  type IPOption struct {
    14  	IPv4Enable bool
    15  	IPv6Enable bool
    16  }
    17  
    18  // Client is the interface for DNS client.
    19  type Client interface {
    20  	// Name of the Client.
    21  	Name() string
    22  
    23  	// QueryIP sends IP queries to its configured server.
    24  	QueryIP(ctx context.Context, domain string, option IPOption) ([]net.IP, error)
    25  }
    26  
    27  type localNameServer struct {
    28  	client *localdns.Client
    29  }
    30  
    31  func (s *localNameServer) QueryIP(ctx context.Context, domain string, option IPOption) ([]net.IP, error) {
    32  	if option.IPv4Enable && option.IPv6Enable {
    33  		return s.client.LookupIP(domain)
    34  	}
    35  
    36  	if option.IPv4Enable {
    37  		return s.client.LookupIPv4(domain)
    38  	}
    39  
    40  	if option.IPv6Enable {
    41  		return s.client.LookupIPv6(domain)
    42  	}
    43  
    44  	return nil, newError("neither IPv4 nor IPv6 is enabled")
    45  }
    46  
    47  func (s *localNameServer) Name() string {
    48  	return "localhost"
    49  }
    50  
    51  func NewLocalNameServer() *localNameServer {
    52  	newError("DNS: created localhost client").AtInfo().WriteToLog()
    53  	return &localNameServer{
    54  		client: localdns.New(),
    55  	}
    56  }