nameserver_local.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. //go:build !confonly
  2. // +build !confonly
  3. package dns
  4. import (
  5. "context"
  6. "github.com/v2fly/v2ray-core/v5/common/net"
  7. "github.com/v2fly/v2ray-core/v5/features/dns"
  8. "github.com/v2fly/v2ray-core/v5/features/dns/localdns"
  9. )
  10. // LocalNameServer is an wrapper over local DNS feature.
  11. type LocalNameServer struct {
  12. client *localdns.Client
  13. }
  14. // QueryIP implements Server.
  15. func (s *LocalNameServer) QueryIP(_ context.Context, domain string, _ net.IP, option dns.IPOption, _ bool) ([]net.IP, error) {
  16. var ips []net.IP
  17. var err error
  18. switch {
  19. case option.IPv4Enable && option.IPv6Enable:
  20. ips, err = s.client.LookupIP(domain)
  21. case option.IPv4Enable:
  22. ips, err = s.client.LookupIPv4(domain)
  23. case option.IPv6Enable:
  24. ips, err = s.client.LookupIPv6(domain)
  25. }
  26. if len(ips) > 0 {
  27. newError("Localhost got answer: ", domain, " -> ", ips).AtInfo().WriteToLog()
  28. }
  29. return ips, err
  30. }
  31. // Name implements Server.
  32. func (s *LocalNameServer) Name() string {
  33. return "localhost"
  34. }
  35. // NewLocalNameServer creates localdns server object for directly lookup in system DNS.
  36. func NewLocalNameServer() *LocalNameServer {
  37. newError("DNS: created localhost client").AtInfo().WriteToLog()
  38. return &LocalNameServer{
  39. client: localdns.New(),
  40. }
  41. }
  42. // NewLocalDNSClient creates localdns client object for directly lookup in system DNS.
  43. func NewLocalDNSClient() *Client {
  44. return &Client{server: NewLocalNameServer()}
  45. }