github.com/xmplusdev/xmcore@v1.8.11-0.20240412132628-5518b55526af/app/dns/nameserver_local.go (about) 1 package dns 2 3 import ( 4 "context" 5 "strings" 6 "time" 7 8 "github.com/xmplusdev/xmcore/common/log" 9 "github.com/xmplusdev/xmcore/common/net" 10 "github.com/xmplusdev/xmcore/features/dns" 11 "github.com/xmplusdev/xmcore/features/dns/localdns" 12 ) 13 14 // LocalNameServer is an wrapper over local DNS feature. 15 type LocalNameServer struct { 16 client *localdns.Client 17 } 18 19 const errEmptyResponse = "No address associated with hostname" 20 21 // QueryIP implements Server. 22 func (s *LocalNameServer) QueryIP(_ context.Context, domain string, _ net.IP, option dns.IPOption, _ bool) (ips []net.IP, err error) { 23 start := time.Now() 24 ips, err = s.client.LookupIP(domain, option) 25 26 if err != nil && strings.HasSuffix(err.Error(), errEmptyResponse) { 27 err = dns.ErrEmptyResponse 28 } 29 30 if len(ips) > 0 { 31 newError("Localhost got answer: ", domain, " -> ", ips).AtInfo().WriteToLog() 32 log.Record(&log.DNSLog{Server: s.Name(), Domain: domain, Result: ips, Status: log.DNSQueried, Elapsed: time.Since(start), Error: err}) 33 } 34 35 return 36 } 37 38 // Name implements Server. 39 func (s *LocalNameServer) Name() string { 40 return "localhost" 41 } 42 43 // NewLocalNameServer creates localdns server object for directly lookup in system DNS. 44 func NewLocalNameServer() *LocalNameServer { 45 newError("DNS: created localhost client").AtInfo().WriteToLog() 46 return &LocalNameServer{ 47 client: localdns.New(), 48 } 49 } 50 51 // NewLocalDNSClient creates localdns client object for directly lookup in system DNS. 52 func NewLocalDNSClient() *Client { 53 return &Client{server: NewLocalNameServer()} 54 }