1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-07-01 03:25:23 +00:00
v2fly/app/dns/nameserver.go

50 lines
942 B
Go
Raw Normal View History

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