client.go 1.2 KB

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