client.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. // Client is a V2Ray feature for querying DNS information.
  15. //
  16. // v2ray: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) ([]net.IP, error)
  21. }
  22. // IPv4Lookup is an optional feature for querying IPv4 addresses only.
  23. //
  24. // v2ray:api:beta
  25. type IPv4Lookup interface {
  26. LookupIPv4(domain string) ([]net.IP, error)
  27. }
  28. // IPv6Lookup is an optional feature for querying IPv6 addresses only.
  29. //
  30. // v2ray:api:beta
  31. type IPv6Lookup interface {
  32. LookupIPv6(domain string) ([]net.IP, error)
  33. }
  34. // ClientWithIPOption is an optional feature for querying DNS information.
  35. //
  36. // v2ray:api:beta
  37. type ClientWithIPOption interface {
  38. // GetIPOption returns IPOption for the DNS client.
  39. GetIPOption() *IPOption
  40. // SetQueryOption sets IPv4Enable and IPv6Enable for the DNS client.
  41. SetQueryOption(isIPv4Enable, isIPv6Enable bool)
  42. // SetFakeDNSOption sets FakeEnable option for DNS client.
  43. SetFakeDNSOption(isFakeEnable bool)
  44. }
  45. // ClientType returns the type of Client interface. Can be used for implementing common.HasType.
  46. //
  47. // v2ray:api:beta
  48. func ClientType() interface{} {
  49. return (*Client)(nil)
  50. }
  51. // ErrEmptyResponse indicates that DNS query succeeded but no answer was returned.
  52. var ErrEmptyResponse = errors.New("empty response")
  53. type RCodeError uint16
  54. func (e RCodeError) Error() string {
  55. return serial.Concat("rcode: ", uint16(e))
  56. }
  57. func RCodeFromError(err error) uint16 {
  58. if err == nil {
  59. return 0
  60. }
  61. cause := errors.Cause(err)
  62. if r, ok := cause.(RCodeError); ok {
  63. return uint16(r)
  64. }
  65. return 0
  66. }