v2ray-core/app/dns/nameserver.go

44 lines
828 B
Go
Raw Normal View History

2017-12-19 23:55:09 +01:00
package dns
2016-05-15 23:09:28 -07:00
import (
"context"
2016-05-15 23:09:28 -07:00
"v2ray.com/core/common/net"
"v2ray.com/core/features/dns/localdns"
2016-05-15 23:09:28 -07:00
)
type IPOption struct {
IPv4Enable bool
IPv6Enable bool
}
type NameServerInterface interface {
QueryIP(ctx context.Context, domain string, option IPOption) ([]net.IP, error)
2016-05-15 23:09:28 -07:00
}
2016-05-16 09:05:01 -07:00
2018-06-27 11:23:39 +02:00
type localNameServer struct {
client *localdns.Client
2016-05-16 09:05:01 -07: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 15:16:45 +02:00
}
if option.IPv6Enable {
return s.client.LookupIPv6(domain)
2018-06-26 15:16:45 +02:00
}
return nil, newError("neither IPv4 nor IPv6 is enabled")
2018-06-26 15:16:45 +02:00
}
2018-06-27 11:23:39 +02:00
func NewLocalNameServer() *localNameServer {
return &localNameServer{
client: localdns.New(),
2018-06-26 15:16:45 +02:00
}
2016-05-16 09:05:01 -07:00
}