client.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package dns
  2. import (
  3. "github.com/v2fly/v2ray-core/v5/common/errors"
  4. "github.com/v2fly/v2ray-core/v5/common/net"
  5. "github.com/v2fly/v2ray-core/v5/common/serial"
  6. "github.com/v2fly/v2ray-core/v5/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 (opt IPOption) With(other IPOption) IPOption {
  15. return IPOption{
  16. IPv4Enable: opt.IPv4Enable && other.IPv4Enable,
  17. IPv6Enable: opt.IPv6Enable && other.IPv6Enable,
  18. FakeEnable: opt.FakeEnable && other.FakeEnable,
  19. }
  20. }
  21. func (opt IPOption) IsValid() bool {
  22. return opt.IPv4Enable || opt.IPv6Enable
  23. }
  24. // Client is a V2Ray feature for querying DNS information.
  25. //
  26. // v2ray:api:stable
  27. type Client interface {
  28. features.Feature
  29. // LookupIP returns IP address for the given domain. IPs may contain IPv4 and/or IPv6 addresses.
  30. LookupIP(domain string) ([]net.IP, error)
  31. }
  32. // IPv4Lookup is an optional feature for querying IPv4 addresses only.
  33. //
  34. // v2ray:api:beta
  35. type IPv4Lookup interface {
  36. LookupIPv4(domain string) ([]net.IP, error)
  37. }
  38. // IPv6Lookup is an optional feature for querying IPv6 addresses only.
  39. //
  40. // v2ray:api:beta
  41. type IPv6Lookup interface {
  42. LookupIPv6(domain string) ([]net.IP, error)
  43. }
  44. // ClientWithIPOption is an optional feature for querying DNS information.
  45. //
  46. // v2ray:api:beta
  47. type ClientWithIPOption interface {
  48. // GetIPOption returns IPOption for the DNS client.
  49. GetIPOption() *IPOption
  50. // SetQueryOption sets IPv4Enable and IPv6Enable for the DNS client.
  51. SetQueryOption(isIPv4Enable, isIPv6Enable bool)
  52. // SetFakeDNSOption sets FakeEnable option for DNS client.
  53. SetFakeDNSOption(isFakeEnable bool)
  54. }
  55. // ClientType returns the type of Client interface. Can be used for implementing common.HasType.
  56. //
  57. // v2ray:api:beta
  58. func ClientType() interface{} {
  59. return (*Client)(nil)
  60. }
  61. // ErrEmptyResponse indicates that DNS query succeeded but no answer was returned.
  62. var ErrEmptyResponse = errors.New("empty response")
  63. type RCodeError uint16
  64. func (e RCodeError) Error() string {
  65. return serial.Concat("rcode: ", uint16(e))
  66. }
  67. func RCodeFromError(err error) uint16 {
  68. if err == nil {
  69. return 0
  70. }
  71. cause := errors.Cause(err)
  72. if r, ok := cause.(RCodeError); ok {
  73. return uint16(r)
  74. }
  75. return 0
  76. }