2018-06-26 16:34:05 -04:00
|
|
|
package dns_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2018-11-15 05:17:20 -05:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2021-02-16 15:31:50 -05:00
|
|
|
|
2022-01-02 10:16:23 -05:00
|
|
|
. "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"
|
2018-06-26 16:34:05 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestStaticHosts(t *testing.T) {
|
2021-09-07 08:43:28 -04:00
|
|
|
pb := []*HostMapping{
|
2018-06-26 16:34:05 -04:00
|
|
|
{
|
2018-08-24 15:51:03 -04:00
|
|
|
Type: DomainMatchingType_Full,
|
2021-02-16 15:31:50 -05:00
|
|
|
Domain: "v2fly.org",
|
2018-06-26 16:34:05 -04:00
|
|
|
Ip: [][]byte{
|
|
|
|
{1, 1, 1, 1},
|
|
|
|
},
|
|
|
|
},
|
2021-04-10 23:52:12 -04:00
|
|
|
{
|
|
|
|
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",
|
|
|
|
},
|
2018-06-26 16:34:05 -04:00
|
|
|
{
|
2018-08-24 15:51:03 -04:00
|
|
|
Type: DomainMatchingType_Subdomain,
|
2018-06-26 16:34:05 -04:00
|
|
|
Domain: "v2ray.cn",
|
|
|
|
Ip: [][]byte{
|
|
|
|
{2, 2, 2, 2},
|
|
|
|
},
|
|
|
|
},
|
2019-02-20 18:03:31 -05:00
|
|
|
{
|
|
|
|
Type: DomainMatchingType_Subdomain,
|
|
|
|
Domain: "baidu.com",
|
|
|
|
Ip: [][]byte{
|
|
|
|
{127, 0, 0, 1},
|
2021-04-10 09:56:08 -04:00
|
|
|
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
|
2019-02-20 18:03:31 -05:00
|
|
|
},
|
|
|
|
},
|
2018-06-26 16:34:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
hosts, err := NewStaticHosts(pb, nil)
|
2018-11-15 05:17:20 -05:00
|
|
|
common.Must(err)
|
2018-06-26 16:34:05 -04:00
|
|
|
|
|
|
|
{
|
2021-02-22 21:17:20 -05:00
|
|
|
ips := hosts.Lookup("v2fly.org", dns.IPOption{
|
2018-11-19 14:42:02 -05:00
|
|
|
IPv4Enable: true,
|
|
|
|
IPv6Enable: true,
|
|
|
|
})
|
2018-11-15 05:17:20 -05:00
|
|
|
if len(ips) != 1 {
|
|
|
|
t.Error("expect 1 IP, but got ", len(ips))
|
|
|
|
}
|
2019-01-30 15:04:29 -05:00
|
|
|
if diff := cmp.Diff([]byte(ips[0].IP()), []byte{1, 1, 1, 1}); diff != "" {
|
2018-11-15 05:17:20 -05:00
|
|
|
t.Error(diff)
|
|
|
|
}
|
2018-06-26 16:34:05 -04:00
|
|
|
}
|
|
|
|
|
2021-04-10 23:52:12 -04:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-26 16:34:05 -04:00
|
|
|
{
|
2021-02-22 21:17:20 -05:00
|
|
|
ips := hosts.Lookup("www.v2ray.cn", dns.IPOption{
|
2018-11-19 14:42:02 -05:00
|
|
|
IPv4Enable: true,
|
|
|
|
IPv6Enable: true,
|
|
|
|
})
|
2018-11-15 05:17:20 -05:00
|
|
|
if len(ips) != 1 {
|
|
|
|
t.Error("expect 1 IP, but got ", len(ips))
|
|
|
|
}
|
2019-01-30 15:04:29 -05:00
|
|
|
if diff := cmp.Diff([]byte(ips[0].IP()), []byte{2, 2, 2, 2}); diff != "" {
|
2018-11-15 05:17:20 -05:00
|
|
|
t.Error(diff)
|
|
|
|
}
|
2018-06-26 16:34:05 -04:00
|
|
|
}
|
2019-02-20 18:03:31 -05:00
|
|
|
|
|
|
|
{
|
2021-02-22 21:17:20 -05:00
|
|
|
ips := hosts.Lookup("baidu.com", dns.IPOption{
|
2019-02-20 18:03:31 -05:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2018-06-26 16:34:05 -04:00
|
|
|
}
|