nameserver.go 971 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package dns
  2. import (
  3. "context"
  4. "github.com/xtls/xray-core/common/net"
  5. "github.com/xtls/xray-core/features/dns"
  6. "github.com/xtls/xray-core/features/dns/localdns"
  7. )
  8. // Client is the interface for DNS client.
  9. type Client interface {
  10. // Name of the Client.
  11. Name() string
  12. // QueryIP sends IP queries to its configured server.
  13. QueryIP(ctx context.Context, domain string, option dns.IPOption) ([]net.IP, error)
  14. }
  15. type LocalNameServer struct {
  16. client *localdns.Client
  17. }
  18. func (s *LocalNameServer) QueryIP(_ context.Context, domain string, option dns.IPOption) ([]net.IP, error) {
  19. if option.IPv4Enable || option.IPv6Enable {
  20. return s.client.LookupIP(domain, option)
  21. }
  22. return nil, newError("neither IPv4 nor IPv6 is enabled")
  23. }
  24. func (s *LocalNameServer) Name() string {
  25. return "localhost"
  26. }
  27. func NewLocalNameServer() *LocalNameServer {
  28. newError("DNS: created localhost client").AtInfo().WriteToLog()
  29. return &LocalNameServer{
  30. client: localdns.New(),
  31. }
  32. }