client.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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, uint32, 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. const DefaultTTL = 300
  31. type RCodeError uint16
  32. func (e RCodeError) Error() string {
  33. return serial.Concat("rcode: ", uint16(e))
  34. }
  35. func (RCodeError) IP() net.IP {
  36. panic("Calling IP() on a RCodeError.")
  37. }
  38. func (RCodeError) Domain() string {
  39. panic("Calling Domain() on a RCodeError.")
  40. }
  41. func (RCodeError) Family() net.AddressFamily {
  42. panic("Calling Family() on a RCodeError.")
  43. }
  44. func (e RCodeError) String() string {
  45. return e.Error()
  46. }
  47. var _ net.Address = (*RCodeError)(nil)
  48. func RCodeFromError(err error) uint16 {
  49. if err == nil {
  50. return 0
  51. }
  52. cause := errors.Cause(err)
  53. if r, ok := cause.(RCodeError); ok {
  54. return uint16(r)
  55. }
  56. return 0
  57. }