Hunter0x7c7
2022-08-11 a82f9cb69f63aaeba40c024960deda7d75b9fece
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
52
53
54
55
56
//go:build !confonly
// +build !confonly
 
package dns
 
import (
    "context"
 
    "github.com/v2fly/v2ray-core/v5/common/net"
    "github.com/v2fly/v2ray-core/v5/features/dns"
    "github.com/v2fly/v2ray-core/v5/features/dns/localdns"
)
 
// LocalNameServer is an wrapper over local DNS feature.
type LocalNameServer struct {
    client *localdns.Client
}
 
// QueryIP implements Server.
func (s *LocalNameServer) QueryIP(_ context.Context, domain string, _ net.IP, option dns.IPOption, _ bool) ([]net.IP, error) {
    var ips []net.IP
    var err error
 
    switch {
    case option.IPv4Enable && option.IPv6Enable:
        ips, err = s.client.LookupIP(domain)
    case option.IPv4Enable:
        ips, err = s.client.LookupIPv4(domain)
    case option.IPv6Enable:
        ips, err = s.client.LookupIPv6(domain)
    }
 
    if len(ips) > 0 {
        newError("Localhost got answer: ", domain, " -> ", ips).AtInfo().WriteToLog()
    }
 
    return ips, err
}
 
// Name implements Server.
func (s *LocalNameServer) Name() string {
    return "localhost"
}
 
// NewLocalNameServer creates localdns server object for directly lookup in system DNS.
func NewLocalNameServer() *LocalNameServer {
    newError("DNS: created localhost client").AtInfo().WriteToLog()
    return &LocalNameServer{
        client: localdns.New(),
    }
}
 
// NewLocalDNSClient creates localdns client object for directly lookup in system DNS.
func NewLocalDNSClient() *Client {
    return &Client{server: NewLocalNameServer()}
}