client.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. func (p *IPOption) Copy() *IPOption {
  15. return &IPOption{p.IPv4Enable, p.IPv6Enable, p.FakeEnable}
  16. }
  17. type Option func(dopt *IPOption) *IPOption
  18. // Client is a Xray feature for querying DNS information.
  19. //
  20. // xray:api:stable
  21. type Client interface {
  22. features.Feature
  23. // LookupIP returns IP address for the given domain. IPs may contain IPv4 and/or IPv6 addresses.
  24. LookupIP(domain string) ([]net.IP, error)
  25. // LookupOptions query IP address for domain with *IPOption.
  26. LookupOptions(domain string, opt ...Option) ([]net.IP, error)
  27. }
  28. // IPv4Lookup is an optional feature for querying IPv4 addresses only.
  29. //
  30. // xray:api:beta
  31. type IPv4Lookup interface {
  32. LookupIPv4(domain string) ([]net.IP, error)
  33. }
  34. // IPv6Lookup is an optional feature for querying IPv6 addresses only.
  35. //
  36. // xray:api:beta
  37. type IPv6Lookup interface {
  38. LookupIPv6(domain string) ([]net.IP, error)
  39. }
  40. // ClientType returns the type of Client interface. Can be used for implementing common.HasType.
  41. //
  42. // xray:api:beta
  43. func ClientType() interface{} {
  44. return (*Client)(nil)
  45. }
  46. // ErrEmptyResponse indicates that DNS query succeeded but no answer was returned.
  47. var ErrEmptyResponse = errors.New("empty response")
  48. type RCodeError uint16
  49. func (e RCodeError) Error() string {
  50. return serial.Concat("rcode: ", uint16(e))
  51. }
  52. func RCodeFromError(err error) uint16 {
  53. if err == nil {
  54. return 0
  55. }
  56. cause := errors.Cause(err)
  57. if r, ok := cause.(RCodeError); ok {
  58. return uint16(r)
  59. }
  60. return 0
  61. }
  62. var (
  63. LookupIPv4Only = func(d *IPOption) *IPOption {
  64. d.IPv4Enable = true
  65. d.IPv6Enable = false
  66. return d
  67. }
  68. LookupIPv6Only = func(d *IPOption) *IPOption {
  69. d.IPv4Enable = false
  70. d.IPv6Enable = true
  71. return d
  72. }
  73. LookupIP = func(d *IPOption) *IPOption {
  74. d.IPv4Enable = true
  75. d.IPv6Enable = true
  76. return d
  77. }
  78. LookupFake = func(d *IPOption) *IPOption {
  79. d.FakeEnable = true
  80. return d
  81. }
  82. LookupNoFake = func(d *IPOption) *IPOption {
  83. d.FakeEnable = false
  84. return d
  85. }
  86. LookupAll = func(d *IPOption) *IPOption {
  87. LookupIP(d)
  88. LookupFake(d)
  89. return d
  90. }
  91. )