Files
Xray-core/app/dns/nameserver_local.go
T

55 lines
1.5 KiB
Go
Raw Normal View History

2021-10-16 21:02:51 +08:00
package dns
import (
"context"
2022-07-24 23:44:16 -04:00
"time"
2021-10-16 21:02:51 +08:00
2024-06-29 14:32:57 -04:00
"github.com/xtls/xray-core/common/errors"
2022-07-24 23:44:16 -04:00
"github.com/xtls/xray-core/common/log"
2021-10-16 21:02:51 +08:00
"github.com/xtls/xray-core/common/net"
2021-12-14 19:28:47 -05:00
"github.com/xtls/xray-core/features/dns"
2021-10-16 21:02:51 +08:00
"github.com/xtls/xray-core/features/dns/localdns"
)
// LocalNameServer is an wrapper over local DNS feature.
type LocalNameServer struct {
client *localdns.Client
2021-10-16 21:02:51 +08:00
}
// QueryIP implements Server.
func (s *LocalNameServer) QueryIP(ctx context.Context, domain string, option dns.IPOption) (ips []net.IP, ttl uint32, err error) {
2022-07-24 23:44:16 -04:00
start := time.Now()
ips, ttl, err = s.client.LookupIP(domain, option)
2021-10-16 21:02:51 +08:00
if len(ips) > 0 {
2024-06-29 14:32:57 -04:00
errors.LogInfo(ctx, "Localhost got answer: ", domain, " -> ", ips)
2022-07-24 23:44:16 -04:00
log.Record(&log.DNSLog{Server: s.Name(), Domain: domain, Result: ips, Status: log.DNSQueried, Elapsed: time.Since(start), Error: err})
2021-10-16 21:02:51 +08:00
}
return
}
// Name implements Server.
func (s *LocalNameServer) Name() string {
return "localhost"
}
2025-11-21 13:45:42 +08:00
// IsDisableCache implements Server.
func (s *LocalNameServer) IsDisableCache() bool {
return true
}
2021-10-16 21:02:51 +08:00
// NewLocalNameServer creates localdns server object for directly lookup in system DNS.
func NewLocalNameServer() *LocalNameServer {
2024-06-29 14:32:57 -04:00
errors.LogInfo(context.Background(), "DNS: created localhost client")
2021-10-16 21:02:51 +08:00
return &LocalNameServer{
client: localdns.New(),
2021-10-16 21:02:51 +08:00
}
}
// NewLocalDNSClient creates localdns client object for directly lookup in system DNS.
func NewLocalDNSClient(ipOption dns.IPOption) *Client {
return &Client{server: NewLocalNameServer(), ipOption: &ipOption}
2021-10-16 21:02:51 +08:00
}