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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package dns_test
 
import (
    "testing"
 
    "github.com/google/go-cmp/cmp"
 
    . "github.com/v2fly/v2ray-core/v5/app/dns"
    "github.com/v2fly/v2ray-core/v5/common"
    "github.com/v2fly/v2ray-core/v5/common/net"
    "github.com/v2fly/v2ray-core/v5/features/dns"
)
 
func TestStaticHosts(t *testing.T) {
    pb := []*HostMapping{
        {
            Type:   DomainMatchingType_Full,
            Domain: "v2fly.org",
            Ip: [][]byte{
                {1, 1, 1, 1},
            },
        },
        {
            Type:   DomainMatchingType_Full,
            Domain: "proxy.v2fly.org",
            Ip: [][]byte{
                {1, 2, 3, 4},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
            },
            ProxiedDomain: "another-proxy.v2fly.org",
        },
        {
            Type:          DomainMatchingType_Full,
            Domain:        "proxy2.v2fly.org",
            ProxiedDomain: "proxy.v2fly.org",
        },
        {
            Type:   DomainMatchingType_Subdomain,
            Domain: "v2ray.cn",
            Ip: [][]byte{
                {2, 2, 2, 2},
            },
        },
        {
            Type:   DomainMatchingType_Subdomain,
            Domain: "baidu.com",
            Ip: [][]byte{
                {127, 0, 0, 1},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
            },
        },
    }
 
    hosts, err := NewStaticHosts(pb, nil)
    common.Must(err)
 
    {
        ips := hosts.Lookup("v2fly.org", dns.IPOption{
            IPv4Enable: true,
            IPv6Enable: true,
        })
        if len(ips) != 1 {
            t.Error("expect 1 IP, but got ", len(ips))
        }
        if diff := cmp.Diff([]byte(ips[0].IP()), []byte{1, 1, 1, 1}); diff != "" {
            t.Error(diff)
        }
    }
 
    {
        domain := hosts.Lookup("proxy.v2fly.org", dns.IPOption{
            IPv4Enable: true,
            IPv6Enable: false,
        })
        if len(domain) != 1 {
            t.Error("expect 1 domain, but got ", len(domain))
        }
        if diff := cmp.Diff(domain[0].Domain(), "another-proxy.v2fly.org"); diff != "" {
            t.Error(diff)
        }
    }
 
    {
        domain := hosts.Lookup("proxy2.v2fly.org", dns.IPOption{
            IPv4Enable: true,
            IPv6Enable: false,
        })
        if len(domain) != 1 {
            t.Error("expect 1 domain, but got ", len(domain))
        }
        if diff := cmp.Diff(domain[0].Domain(), "another-proxy.v2fly.org"); diff != "" {
            t.Error(diff)
        }
    }
 
    {
        ips := hosts.Lookup("www.v2ray.cn", dns.IPOption{
            IPv4Enable: true,
            IPv6Enable: true,
        })
        if len(ips) != 1 {
            t.Error("expect 1 IP, but got ", len(ips))
        }
        if diff := cmp.Diff([]byte(ips[0].IP()), []byte{2, 2, 2, 2}); diff != "" {
            t.Error(diff)
        }
    }
 
    {
        ips := hosts.Lookup("baidu.com", dns.IPOption{
            IPv4Enable: false,
            IPv6Enable: true,
        })
        if len(ips) != 1 {
            t.Error("expect 1 IP, but got ", len(ips))
        }
        if diff := cmp.Diff([]byte(ips[0].IP()), []byte(net.LocalHostIPv6.IP())); diff != "" {
            t.Error(diff)
        }
    }
}