context.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package dns
  2. import (
  3. "context"
  4. "github.com/xtls/xray-core/common/errors"
  5. "github.com/xtls/xray-core/common/net"
  6. "github.com/xtls/xray-core/features/dns"
  7. "github.com/xtls/xray-core/features/routing"
  8. )
  9. // ResolvableContext is an implementation of routing.Context, with domain resolving capability.
  10. type ResolvableContext struct {
  11. routing.Context
  12. dnsClient dns.Client
  13. cacheIPs []net.IP
  14. hasError bool
  15. }
  16. // GetTargetIPs overrides original routing.Context's implementation.
  17. func (ctx *ResolvableContext) GetTargetIPs() []net.IP {
  18. if len(ctx.cacheIPs) > 0 {
  19. return ctx.cacheIPs
  20. }
  21. if ctx.hasError {
  22. return nil
  23. }
  24. if domain := ctx.GetTargetDomain(); len(domain) != 0 {
  25. ips, _, err := ctx.dnsClient.LookupIP(domain, dns.IPOption{
  26. IPv4Enable: true,
  27. IPv6Enable: true,
  28. FakeEnable: false,
  29. })
  30. if err == nil {
  31. ctx.cacheIPs = ips
  32. return ips
  33. }
  34. errors.LogInfoInner(context.Background(), err, "resolve ip for ", domain)
  35. }
  36. if ips := ctx.Context.GetTargetIPs(); len(ips) != 0 {
  37. ctx.cacheIPs = ips
  38. return ips
  39. }
  40. ctx.hasError = true
  41. return nil
  42. }
  43. // ContextWithDNSClient creates a new routing context with domain resolving capability.
  44. // Resolved domain IPs can be retrieved by GetTargetIPs().
  45. func ContextWithDNSClient(ctx routing.Context, client dns.Client) routing.Context {
  46. return &ResolvableContext{Context: ctx, dnsClient: client}
  47. }