2020-11-25 11:01:53 +00:00
|
|
|
package dns_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
|
2020-12-04 01:36:16 +00:00
|
|
|
. "github.com/xtls/xray-core/app/dns"
|
|
|
|
"github.com/xtls/xray-core/common"
|
2021-03-24 15:01:20 +00:00
|
|
|
"github.com/xtls/xray-core/common/matcher/domain"
|
2020-12-04 01:36:16 +00:00
|
|
|
"github.com/xtls/xray-core/common/net"
|
2021-03-07 04:39:50 +00:00
|
|
|
"github.com/xtls/xray-core/features/dns"
|
2020-11-25 11:01:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestStaticHosts(t *testing.T) {
|
|
|
|
pb := []*Config_HostMapping{
|
|
|
|
{
|
2021-03-24 15:01:20 +00:00
|
|
|
Type: domain.MatchingType_Full,
|
2020-11-25 11:01:53 +00:00
|
|
|
Domain: "example.com",
|
|
|
|
Ip: [][]byte{
|
|
|
|
{1, 1, 1, 1},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-03-24 15:01:20 +00:00
|
|
|
Type: domain.MatchingType_Subdomain,
|
2020-11-25 11:01:53 +00:00
|
|
|
Domain: "example.cn",
|
|
|
|
Ip: [][]byte{
|
|
|
|
{2, 2, 2, 2},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-03-24 15:01:20 +00:00
|
|
|
Type: domain.MatchingType_Subdomain,
|
2020-11-25 11:01:53 +00:00
|
|
|
Domain: "baidu.com",
|
|
|
|
Ip: [][]byte{
|
|
|
|
{127, 0, 0, 1},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
hosts, err := NewStaticHosts(pb, nil)
|
|
|
|
common.Must(err)
|
|
|
|
|
|
|
|
{
|
2021-04-10 03:36:58 +00:00
|
|
|
ips := hosts.Lookup("example.com", &dns.IPOption{
|
2020-11-25 11:01:53 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-04-10 03:36:58 +00:00
|
|
|
ips := hosts.Lookup("www.example.cn", &dns.IPOption{
|
2020-11-25 11:01:53 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-04-10 03:36:58 +00:00
|
|
|
ips := hosts.Lookup("baidu.com", &dns.IPOption{
|
2020-11-25 11:01:53 +00: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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|