2020-11-25 11:01:53 +00:00
|
|
|
package dns
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2020-12-04 01:36:16 +00:00
|
|
|
"github.com/xtls/xray-core/common/net"
|
2021-03-07 04:39:50 +00:00
|
|
|
"github.com/xtls/xray-core/features/dns"
|
2020-12-04 01:36:16 +00:00
|
|
|
"github.com/xtls/xray-core/features/dns/localdns"
|
2020-11-25 11:01:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Client is the interface for DNS client.
|
|
|
|
type Client interface {
|
|
|
|
// Name of the Client.
|
|
|
|
Name() string
|
|
|
|
|
|
|
|
// QueryIP sends IP queries to its configured server.
|
2021-03-07 04:39:50 +00:00
|
|
|
QueryIP(ctx context.Context, domain string, option dns.IPOption) ([]net.IP, error)
|
2020-11-25 11:01:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type LocalNameServer struct {
|
|
|
|
client *localdns.Client
|
|
|
|
}
|
|
|
|
|
2021-03-07 04:39:50 +00:00
|
|
|
func (s *LocalNameServer) QueryIP(_ context.Context, domain string, option dns.IPOption) ([]net.IP, error) {
|
|
|
|
if option.IPv4Enable || option.IPv6Enable {
|
|
|
|
return s.client.LookupIP(domain, option)
|
2020-11-25 11:01:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil, newError("neither IPv4 nor IPv6 is enabled")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *LocalNameServer) Name() string {
|
|
|
|
return "localhost"
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewLocalNameServer() *LocalNameServer {
|
|
|
|
newError("DNS: created localhost client").AtInfo().WriteToLog()
|
|
|
|
return &LocalNameServer{
|
|
|
|
client: localdns.New(),
|
|
|
|
}
|
|
|
|
}
|