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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package localdns
 
import (
    "github.com/v2fly/v2ray-core/v5/common/net"
    "github.com/v2fly/v2ray-core/v5/features/dns"
)
 
// Client is an implementation of dns.Client, which queries localhost for DNS.
type Client struct{}
 
// Type implements common.HasType.
func (*Client) Type() interface{} {
    return dns.ClientType()
}
 
// Start implements common.Runnable.
func (*Client) Start() error { return nil }
 
// Close implements common.Closable.
func (*Client) Close() error { return nil }
 
// LookupIP implements Client.
func (*Client) LookupIP(host string) ([]net.IP, error) {
    ips, err := net.LookupIP(host)
    if err != nil {
        return nil, err
    }
    parsedIPs := make([]net.IP, 0, len(ips))
    for _, ip := range ips {
        parsed := net.IPAddress(ip)
        if parsed != nil {
            parsedIPs = append(parsedIPs, parsed.IP())
        }
    }
    if len(parsedIPs) == 0 {
        return nil, dns.ErrEmptyResponse
    }
    return parsedIPs, nil
}
 
// LookupIPv4 implements IPv4Lookup.
func (c *Client) LookupIPv4(host string) ([]net.IP, error) {
    ips, err := c.LookupIP(host)
    if err != nil {
        return nil, err
    }
    ipv4 := make([]net.IP, 0, len(ips))
    for _, ip := range ips {
        if len(ip) == net.IPv4len {
            ipv4 = append(ipv4, ip)
        }
    }
    if len(ipv4) == 0 {
        return nil, dns.ErrEmptyResponse
    }
    return ipv4, nil
}
 
// LookupIPv6 implements IPv6Lookup.
func (c *Client) LookupIPv6(host string) ([]net.IP, error) {
    ips, err := c.LookupIP(host)
    if err != nil {
        return nil, err
    }
    ipv6 := make([]net.IP, 0, len(ips))
    for _, ip := range ips {
        if len(ip) == net.IPv6len {
            ipv6 = append(ipv6, ip)
        }
    }
    if len(ipv6) == 0 {
        return nil, dns.ErrEmptyResponse
    }
    return ipv6, nil
}
 
// New create a new dns.Client that queries localhost for DNS.
func New() *Client {
    return &Client{}
}