nameserver_local.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // +build !confonly
  2. package dns
  3. import (
  4. "context"
  5. "github.com/v2fly/v2ray-core/v4/common/net"
  6. "github.com/v2fly/v2ray-core/v4/features/dns"
  7. "github.com/v2fly/v2ray-core/v4/features/dns/localdns"
  8. )
  9. // LocalNameServer is an wrapper over local DNS feature.
  10. type LocalNameServer struct {
  11. client *localdns.Client
  12. }
  13. // QueryIP implements Server.
  14. func (s *LocalNameServer) QueryIP(_ context.Context, domain string, _ net.IP, option dns.IPOption) ([]net.IP, error) {
  15. if option.IPv4Enable || option.IPv6Enable {
  16. return s.client.LookupIP(domain, option)
  17. }
  18. return nil, newError("neither IPv4 nor IPv6 is enabled")
  19. }
  20. // Name implements Server.
  21. func (s *LocalNameServer) Name() string {
  22. return "localhost"
  23. }
  24. // NewLocalNameServer creates localdns server object for directly lookup in system DNS.
  25. func NewLocalNameServer() *LocalNameServer {
  26. newError("DNS: created localhost client").AtInfo().WriteToLog()
  27. return &LocalNameServer{
  28. client: localdns.New(),
  29. }
  30. }
  31. // NewLocalDNSClient creates localdns client object for directly lookup in system DNS.
  32. func NewLocalDNSClient() *Client {
  33. return &Client{server: NewLocalNameServer()}
  34. }