nameserver_local.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package dns
  2. import (
  3. "context"
  4. "strings"
  5. "github.com/xtls/xray-core/features/dns"
  6. "github.com/xtls/xray-core/common/net"
  7. "github.com/xtls/xray-core/features/dns/localdns"
  8. )
  9. // LocalNameServer is an wrapper over local DNS feature.
  10. type LocalNameServer struct {
  11. client *localdns.Client
  12. }
  13. const errEmptyResponse = "No address associated with hostname"
  14. // QueryIP implements Server.
  15. func (s *LocalNameServer) QueryIP(_ context.Context, domain string, _ net.IP, option dns.IPOption, _ bool) (ips []net.IP, err error) {
  16. ips, err = s.client.LookupIP(domain, option)
  17. if err != nil && strings.HasSuffix(err.Error(), errEmptyResponse) {
  18. err = dns.ErrEmptyResponse
  19. }
  20. if len(ips) > 0 {
  21. newError("Localhost got answer: ", domain, " -> ", ips).AtInfo().WriteToLog()
  22. }
  23. return
  24. }
  25. // Name implements Server.
  26. func (s *LocalNameServer) Name() string {
  27. return "localhost"
  28. }
  29. // NewLocalNameServer creates localdns server object for directly lookup in system DNS.
  30. func NewLocalNameServer() *LocalNameServer {
  31. newError("DNS: created localhost client").AtInfo().WriteToLog()
  32. return &LocalNameServer{
  33. client: localdns.New(),
  34. }
  35. }
  36. // NewLocalDNSClient creates localdns client object for directly lookup in system DNS.
  37. func NewLocalDNSClient() *Client {
  38. return &Client{server: NewLocalNameServer()}
  39. }