2017-12-19 22:55:09 +00:00
|
|
|
package dns
|
2016-05-16 06:09:28 +00:00
|
|
|
|
|
|
|
import (
|
2017-01-26 19:46:44 +00:00
|
|
|
"context"
|
2016-05-16 06:09:28 +00:00
|
|
|
|
2017-08-29 10:56:57 +00:00
|
|
|
"v2ray.com/core/common/net"
|
2018-11-19 19:42:02 +00:00
|
|
|
"v2ray.com/core/features/dns/localdns"
|
2016-05-16 06:09:28 +00:00
|
|
|
)
|
|
|
|
|
2018-11-19 19:42:02 +00:00
|
|
|
type IPOption struct {
|
|
|
|
IPv4Enable bool
|
|
|
|
IPv6Enable bool
|
|
|
|
}
|
|
|
|
|
2018-08-24 19:51:03 +00:00
|
|
|
type NameServerInterface interface {
|
2018-11-19 19:42:02 +00:00
|
|
|
QueryIP(ctx context.Context, domain string, option IPOption) ([]net.IP, error)
|
2016-05-16 06:09:28 +00:00
|
|
|
}
|
2016-05-16 16:05:01 +00:00
|
|
|
|
2018-06-27 09:23:39 +00:00
|
|
|
type localNameServer struct {
|
2018-11-19 19:42:02 +00:00
|
|
|
client *localdns.Client
|
2016-05-16 16:05:01 +00:00
|
|
|
}
|
|
|
|
|
2018-11-19 19:42:02 +00:00
|
|
|
func (s *localNameServer) QueryIP(ctx context.Context, domain string, option IPOption) ([]net.IP, error) {
|
|
|
|
if option.IPv4Enable && option.IPv6Enable {
|
|
|
|
return s.client.LookupIP(domain)
|
|
|
|
}
|
|
|
|
|
|
|
|
if option.IPv4Enable {
|
|
|
|
return s.client.LookupIPv4(domain)
|
2018-06-26 13:16:45 +00:00
|
|
|
}
|
2018-11-19 19:42:02 +00:00
|
|
|
|
|
|
|
if option.IPv6Enable {
|
|
|
|
return s.client.LookupIPv6(domain)
|
2018-06-26 13:16:45 +00:00
|
|
|
}
|
2018-11-19 19:42:02 +00:00
|
|
|
|
|
|
|
return nil, newError("neither IPv4 nor IPv6 is enabled")
|
2018-06-26 13:16:45 +00:00
|
|
|
}
|
|
|
|
|
2018-06-27 09:23:39 +00:00
|
|
|
func NewLocalNameServer() *localNameServer {
|
|
|
|
return &localNameServer{
|
2018-11-19 19:42:02 +00:00
|
|
|
client: localdns.New(),
|
2018-06-26 13:16:45 +00:00
|
|
|
}
|
2016-05-16 16:05:01 +00:00
|
|
|
}
|