2021-10-16 13:02:51 +00:00
|
|
|
package dns
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"strings"
|
2022-07-25 03:44:16 +00:00
|
|
|
"time"
|
2021-10-16 13:02:51 +00:00
|
|
|
|
2024-06-29 18:32:57 +00:00
|
|
|
"github.com/xtls/xray-core/common/errors"
|
2022-07-25 03:44:16 +00:00
|
|
|
"github.com/xtls/xray-core/common/log"
|
2021-10-16 13:02:51 +00:00
|
|
|
"github.com/xtls/xray-core/common/net"
|
2021-12-15 00:28:47 +00:00
|
|
|
"github.com/xtls/xray-core/features/dns"
|
2021-10-16 13:02:51 +00:00
|
|
|
"github.com/xtls/xray-core/features/dns/localdns"
|
|
|
|
)
|
|
|
|
|
|
|
|
// LocalNameServer is an wrapper over local DNS feature.
|
|
|
|
type LocalNameServer struct {
|
|
|
|
client *localdns.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
const errEmptyResponse = "No address associated with hostname"
|
|
|
|
|
|
|
|
// QueryIP implements Server.
|
2024-06-29 18:32:57 +00:00
|
|
|
func (s *LocalNameServer) QueryIP(ctx context.Context, domain string, _ net.IP, option dns.IPOption, _ bool) (ips []net.IP, err error) {
|
2022-07-25 03:44:16 +00:00
|
|
|
start := time.Now()
|
2021-10-16 13:02:51 +00:00
|
|
|
ips, err = s.client.LookupIP(domain, option)
|
|
|
|
|
|
|
|
if err != nil && strings.HasSuffix(err.Error(), errEmptyResponse) {
|
|
|
|
err = dns.ErrEmptyResponse
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ips) > 0 {
|
2024-06-29 18:32:57 +00:00
|
|
|
errors.LogInfo(ctx, "Localhost got answer: ", domain, " -> ", ips)
|
2022-07-25 03:44:16 +00:00
|
|
|
log.Record(&log.DNSLog{Server: s.Name(), Domain: domain, Result: ips, Status: log.DNSQueried, Elapsed: time.Since(start), Error: err})
|
2021-10-16 13:02:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name implements Server.
|
|
|
|
func (s *LocalNameServer) Name() string {
|
|
|
|
return "localhost"
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewLocalNameServer creates localdns server object for directly lookup in system DNS.
|
|
|
|
func NewLocalNameServer() *LocalNameServer {
|
2024-06-29 18:32:57 +00:00
|
|
|
errors.LogInfo(context.Background(), "DNS: created localhost client")
|
2021-10-16 13:02:51 +00:00
|
|
|
return &LocalNameServer{
|
|
|
|
client: localdns.New(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewLocalDNSClient creates localdns client object for directly lookup in system DNS.
|
|
|
|
func NewLocalDNSClient() *Client {
|
|
|
|
return &Client{server: NewLocalNameServer()}
|
|
|
|
}
|