nameserver_local.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package dns
  2. import (
  3. "context"
  4. "time"
  5. "github.com/xtls/xray-core/common/errors"
  6. "github.com/xtls/xray-core/common/log"
  7. "github.com/xtls/xray-core/common/net"
  8. "github.com/xtls/xray-core/features/dns"
  9. "github.com/xtls/xray-core/features/dns/localdns"
  10. )
  11. // LocalNameServer is an wrapper over local DNS feature.
  12. type LocalNameServer struct {
  13. client *localdns.Client
  14. }
  15. // QueryIP implements Server.
  16. func (s *LocalNameServer) QueryIP(ctx context.Context, domain string, option dns.IPOption) (ips []net.IP, ttl uint32, err error) {
  17. start := time.Now()
  18. ips, ttl, err = s.client.LookupIP(domain, option)
  19. if len(ips) > 0 {
  20. errors.LogInfo(ctx, "Localhost got answer: ", domain, " -> ", ips)
  21. log.Record(&log.DNSLog{Server: s.Name(), Domain: domain, Result: ips, Status: log.DNSQueried, Elapsed: time.Since(start), Error: err})
  22. }
  23. return
  24. }
  25. // Name implements Server.
  26. func (s *LocalNameServer) Name() string {
  27. return "localhost"
  28. }
  29. // IsDisableCache implements Server.
  30. func (s *LocalNameServer) IsDisableCache() bool {
  31. return true
  32. }
  33. // NewLocalNameServer creates localdns server object for directly lookup in system DNS.
  34. func NewLocalNameServer() *LocalNameServer {
  35. errors.LogInfo(context.Background(), "DNS: created localhost client")
  36. return &LocalNameServer{
  37. client: localdns.New(),
  38. }
  39. }
  40. // NewLocalDNSClient creates localdns client object for directly lookup in system DNS.
  41. func NewLocalDNSClient(ipOption dns.IPOption) *Client {
  42. return &Client{server: NewLocalNameServer(), ipOption: &ipOption}
  43. }