2021-08-21 01:20:40 -04:00
|
|
|
//go:build !confonly
|
2020-12-18 04:24:33 -05:00
|
|
|
// +build !confonly
|
|
|
|
|
|
|
|
package dns
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2021-02-16 15:31:50 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v4/common/net"
|
2021-02-22 21:17:20 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v4/features/dns"
|
2021-02-16 15:31:50 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v4/features/dns/localdns"
|
2020-12-18 04:24:33 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// LocalNameServer is an wrapper over local DNS feature.
|
|
|
|
type LocalNameServer struct {
|
|
|
|
client *localdns.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
// QueryIP implements Server.
|
2021-02-24 03:03:50 -05:00
|
|
|
func (s *LocalNameServer) QueryIP(_ context.Context, domain string, _ net.IP, option dns.IPOption, _ bool) ([]net.IP, error) {
|
2021-03-19 03:55:18 -04:00
|
|
|
var ips []net.IP
|
|
|
|
var err error
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case option.IPv4Enable && option.IPv6Enable:
|
|
|
|
ips, err = s.client.LookupIP(domain)
|
|
|
|
case option.IPv4Enable:
|
|
|
|
ips, err = s.client.LookupIPv4(domain)
|
|
|
|
case option.IPv6Enable:
|
|
|
|
ips, err = s.client.LookupIPv6(domain)
|
2020-12-18 04:24:33 -05:00
|
|
|
}
|
|
|
|
|
2021-03-19 03:55:18 -04:00
|
|
|
if len(ips) > 0 {
|
|
|
|
newError("Localhost got answer: ", domain, " -> ", ips).AtInfo().WriteToLog()
|
|
|
|
}
|
|
|
|
|
|
|
|
return ips, err
|
2020-12-18 04:24:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Name implements Server.
|
|
|
|
func (s *LocalNameServer) Name() string {
|
|
|
|
return "localhost"
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewLocalNameServer creates localdns server object for directly lookup in system DNS.
|
|
|
|
func NewLocalNameServer() *LocalNameServer {
|
|
|
|
newError("DNS: created localhost client").AtInfo().WriteToLog()
|
|
|
|
return &LocalNameServer{
|
|
|
|
client: localdns.New(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewLocalDNSClient creates localdns client object for directly lookup in system DNS.
|
|
|
|
func NewLocalDNSClient() *Client {
|
|
|
|
return &Client{server: NewLocalNameServer()}
|
|
|
|
}
|