2019-02-01 14:08:21 -05:00
|
|
|
// +build !confonly
|
|
|
|
|
2017-12-19 17:55:09 -05:00
|
|
|
package dns
|
2016-05-16 02:09:28 -04:00
|
|
|
|
|
|
|
import (
|
2017-01-26 14:46:44 -05:00
|
|
|
"context"
|
2016-05-16 02:09:28 -04:00
|
|
|
|
2017-08-29 06:56:57 -04:00
|
|
|
"v2ray.com/core/common/net"
|
2018-11-19 14:42:02 -05:00
|
|
|
"v2ray.com/core/features/dns/localdns"
|
2016-05-16 02:09:28 -04:00
|
|
|
)
|
|
|
|
|
2018-12-28 14:28:31 -05:00
|
|
|
// IPOption is an object for IP query options.
|
2018-11-19 14:42:02 -05:00
|
|
|
type IPOption struct {
|
|
|
|
IPv4Enable bool
|
|
|
|
IPv6Enable bool
|
|
|
|
}
|
|
|
|
|
2019-02-22 18:01:23 -05:00
|
|
|
// Client is the interface for DNS client.
|
2018-12-28 14:28:31 -05:00
|
|
|
type Client interface {
|
2019-02-22 18:01:23 -05:00
|
|
|
// Name of the Client.
|
2018-11-22 10:29:09 -05:00
|
|
|
Name() string
|
2019-02-22 18:01:23 -05:00
|
|
|
|
|
|
|
// QueryIP sends IP queries to its configured server.
|
2018-11-19 14:42:02 -05:00
|
|
|
QueryIP(ctx context.Context, domain string, option IPOption) ([]net.IP, error)
|
2016-05-16 02:09:28 -04:00
|
|
|
}
|
2016-05-16 12:05:01 -04:00
|
|
|
|
2020-10-11 07:22:46 -04:00
|
|
|
type LocalNameServer struct {
|
2018-11-19 14:42:02 -05:00
|
|
|
client *localdns.Client
|
2016-05-16 12:05:01 -04:00
|
|
|
}
|
|
|
|
|
2020-10-11 07:22:46 -04:00
|
|
|
func (s *LocalNameServer) QueryIP(ctx context.Context, domain string, option IPOption) ([]net.IP, error) {
|
2018-11-19 14:42:02 -05:00
|
|
|
if option.IPv4Enable && option.IPv6Enable {
|
|
|
|
return s.client.LookupIP(domain)
|
|
|
|
}
|
|
|
|
|
|
|
|
if option.IPv4Enable {
|
|
|
|
return s.client.LookupIPv4(domain)
|
2018-06-26 09:16:45 -04:00
|
|
|
}
|
2018-11-19 14:42:02 -05:00
|
|
|
|
|
|
|
if option.IPv6Enable {
|
|
|
|
return s.client.LookupIPv6(domain)
|
2018-06-26 09:16:45 -04:00
|
|
|
}
|
2018-11-19 14:42:02 -05:00
|
|
|
|
|
|
|
return nil, newError("neither IPv4 nor IPv6 is enabled")
|
2018-06-26 09:16:45 -04:00
|
|
|
}
|
|
|
|
|
2020-10-11 07:22:46 -04:00
|
|
|
func (s *LocalNameServer) Name() string {
|
2018-11-22 10:29:09 -05:00
|
|
|
return "localhost"
|
|
|
|
}
|
|
|
|
|
2020-10-11 07:22:46 -04:00
|
|
|
func NewLocalNameServer() *LocalNameServer {
|
2019-11-25 03:14:39 -05:00
|
|
|
newError("DNS: created localhost client").AtInfo().WriteToLog()
|
2020-10-11 07:22:46 -04:00
|
|
|
return &LocalNameServer{
|
2018-11-19 14:42:02 -05:00
|
|
|
client: localdns.New(),
|
2018-06-26 09:16:45 -04:00
|
|
|
}
|
2016-05-16 12:05:01 -04:00
|
|
|
}
|