github.com/xtls/xray-core@v1.8.12-0.20240518155711-3168d27b0bdb/features/dns/client.go (about)

     1  package dns
     2  
     3  import (
     4  	"github.com/xtls/xray-core/common/errors"
     5  	"github.com/xtls/xray-core/common/net"
     6  	"github.com/xtls/xray-core/common/serial"
     7  	"github.com/xtls/xray-core/features"
     8  )
     9  
    10  // IPOption is an object for IP query options.
    11  type IPOption struct {
    12  	IPv4Enable bool
    13  	IPv6Enable bool
    14  	FakeEnable bool
    15  }
    16  
    17  // Client is a Xray feature for querying DNS information.
    18  //
    19  // xray:api:stable
    20  type Client interface {
    21  	features.Feature
    22  
    23  	// LookupIP returns IP address for the given domain. IPs may contain IPv4 and/or IPv6 addresses.
    24  	LookupIP(domain string, option IPOption) ([]net.IP, error)
    25  }
    26  
    27  type HostsLookup interface {
    28  	LookupHosts(domain string) *net.Address
    29  }
    30  
    31  // ClientType returns the type of Client interface. Can be used for implementing common.HasType.
    32  //
    33  // xray:api:beta
    34  func ClientType() interface{} {
    35  	return (*Client)(nil)
    36  }
    37  
    38  // ErrEmptyResponse indicates that DNS query succeeded but no answer was returned.
    39  var ErrEmptyResponse = errors.New("empty response")
    40  
    41  type RCodeError uint16
    42  
    43  func (e RCodeError) Error() string {
    44  	return serial.Concat("rcode: ", uint16(e))
    45  }
    46  
    47  func RCodeFromError(err error) uint16 {
    48  	if err == nil {
    49  		return 0
    50  	}
    51  	cause := errors.Cause(err)
    52  	if r, ok := cause.(RCodeError); ok {
    53  		return uint16(r)
    54  	}
    55  	return 0
    56  }