client.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. // ClientType returns the type of Client interface. Can be used for implementing common.HasType.
  23. //
  24. // xray:api:beta
  25. func ClientType() interface{} {
  26. return (*Client)(nil)
  27. }
  28. // ErrEmptyResponse indicates that DNS query succeeded but no answer was returned.
  29. var ErrEmptyResponse = errors.New("empty response")
  30. type RCodeError uint16
  31. func (e RCodeError) Error() string {
  32. return serial.Concat("rcode: ", uint16(e))
  33. }
  34. func RCodeFromError(err error) uint16 {
  35. if err == nil {
  36. return 0
  37. }
  38. cause := errors.Cause(err)
  39. if r, ok := cause.(RCodeError); ok {
  40. return uint16(r)
  41. }
  42. return 0
  43. }