-
Notifications
You must be signed in to change notification settings - Fork 0
/
nameserver.go
51 lines (39 loc) · 963 Bytes
/
nameserver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// +build !confonly
package dns
import (
"context"
"v2ray.com/core/common/net"
"v2ray.com/core/features/dns/localdns"
)
// IPOption is an object for IP query options.
type IPOption struct {
IPv4Enable bool
IPv6Enable bool
}
type Client interface {
Name() string
QueryIP(ctx context.Context, domain string, option IPOption) ([]net.IP, error)
}
type localNameServer struct {
client *localdns.Client
}
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)
}
if option.IPv6Enable {
return s.client.LookupIPv6(domain)
}
return nil, newError("neither IPv4 nor IPv6 is enabled")
}
func (s *localNameServer) Name() string {
return "localhost"
}
func NewLocalNameServer() *localNameServer {
return &localNameServer{
client: localdns.New(),
}
}