2020-11-25 19:01:53 +08:00
|
|
|
package dns_test
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
2020-12-04 09:36:16 +08:00
|
|
|
. "github.com/xtls/xray-core/app/dns"
|
|
|
|
|
"github.com/xtls/xray-core/common"
|
2026-04-14 00:42:29 +08:00
|
|
|
"github.com/xtls/xray-core/common/geodata"
|
2020-12-04 09:36:16 +08:00
|
|
|
"github.com/xtls/xray-core/common/net"
|
2021-03-06 23:39:50 -05:00
|
|
|
"github.com/xtls/xray-core/features/dns"
|
2020-11-25 19:01:53 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestStaticHosts(t *testing.T) {
|
|
|
|
|
pb := []*Config_HostMapping{
|
2025-07-23 18:11:43 +08:00
|
|
|
{
|
2026-04-14 00:42:29 +08:00
|
|
|
Domain: &geodata.DomainRule{Value: &geodata.DomainRule_Custom{Custom: &geodata.Domain{Type: geodata.Domain_Domain, Value: "lan"}}},
|
2025-07-23 18:11:43 +08:00
|
|
|
ProxiedDomain: "#3",
|
|
|
|
|
},
|
2020-11-25 19:01:53 +08:00
|
|
|
{
|
2026-04-14 00:42:29 +08:00
|
|
|
Domain: &geodata.DomainRule{Value: &geodata.DomainRule_Custom{Custom: &geodata.Domain{Type: geodata.Domain_Full, Value: "example.com"}}},
|
2020-11-25 19:01:53 +08:00
|
|
|
Ip: [][]byte{
|
|
|
|
|
{1, 1, 1, 1},
|
|
|
|
|
},
|
|
|
|
|
},
|
2021-10-16 21:02:51 +08:00
|
|
|
{
|
2026-04-14 00:42:29 +08:00
|
|
|
Domain: &geodata.DomainRule{Value: &geodata.DomainRule_Custom{Custom: &geodata.Domain{Type: geodata.Domain_Full, Value: "proxy.xray.com"}}},
|
2021-10-16 21:02:51 +08:00
|
|
|
Ip: [][]byte{
|
|
|
|
|
{1, 2, 3, 4},
|
|
|
|
|
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
|
|
|
|
|
},
|
2021-10-20 00:52:49 +08:00
|
|
|
ProxiedDomain: "another-proxy.xray.com",
|
2021-10-16 21:02:51 +08:00
|
|
|
},
|
|
|
|
|
{
|
2026-04-14 00:42:29 +08:00
|
|
|
Domain: &geodata.DomainRule{Value: &geodata.DomainRule_Custom{Custom: &geodata.Domain{Type: geodata.Domain_Full, Value: "proxy2.xray.com"}}},
|
2021-10-20 00:52:49 +08:00
|
|
|
ProxiedDomain: "proxy.xray.com",
|
2021-10-16 21:02:51 +08:00
|
|
|
},
|
2020-11-25 19:01:53 +08:00
|
|
|
{
|
2026-04-14 00:42:29 +08:00
|
|
|
Domain: &geodata.DomainRule{Value: &geodata.DomainRule_Custom{Custom: &geodata.Domain{Type: geodata.Domain_Domain, Value: "example.cn"}}},
|
2020-11-25 19:01:53 +08:00
|
|
|
Ip: [][]byte{
|
|
|
|
|
{2, 2, 2, 2},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
2026-04-14 00:42:29 +08:00
|
|
|
Domain: &geodata.DomainRule{Value: &geodata.DomainRule_Custom{Custom: &geodata.Domain{Type: geodata.Domain_Domain, Value: "baidu.com"}}},
|
2020-11-25 19:01:53 +08:00
|
|
|
Ip: [][]byte{
|
|
|
|
|
{127, 0, 0, 1},
|
2021-10-16 21:02:51 +08:00
|
|
|
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
|
2020-11-25 19:01:53 +08:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-19 09:05:59 +08:00
|
|
|
hosts, err := NewStaticHosts(pb)
|
2020-11-25 19:01:53 +08:00
|
|
|
common.Must(err)
|
|
|
|
|
|
|
|
|
|
{
|
2025-07-23 18:11:43 +08:00
|
|
|
_, err := hosts.Lookup("example.com.lan", dns.IPOption{})
|
|
|
|
|
if dns.RCodeFromError(err) != 3 {
|
|
|
|
|
t.Error(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
ips, _ := hosts.Lookup("example.com", dns.IPOption{
|
2020-11-25 19:01:53 +08: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)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
2025-07-23 18:11:43 +08:00
|
|
|
domain, _ := hosts.Lookup("proxy.xray.com", dns.IPOption{
|
2021-10-16 21:02:51 +08:00
|
|
|
IPv4Enable: true,
|
|
|
|
|
IPv6Enable: false,
|
|
|
|
|
})
|
|
|
|
|
if len(domain) != 1 {
|
|
|
|
|
t.Error("expect 1 domain, but got ", len(domain))
|
|
|
|
|
}
|
2021-10-20 00:52:49 +08:00
|
|
|
if diff := cmp.Diff(domain[0].Domain(), "another-proxy.xray.com"); diff != "" {
|
2021-10-16 21:02:51 +08:00
|
|
|
t.Error(diff)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
2025-07-23 18:11:43 +08:00
|
|
|
domain, _ := hosts.Lookup("proxy2.xray.com", dns.IPOption{
|
2021-10-16 21:02:51 +08:00
|
|
|
IPv4Enable: true,
|
|
|
|
|
IPv6Enable: false,
|
|
|
|
|
})
|
|
|
|
|
if len(domain) != 1 {
|
|
|
|
|
t.Error("expect 1 domain, but got ", len(domain))
|
|
|
|
|
}
|
2021-10-20 00:52:49 +08:00
|
|
|
if diff := cmp.Diff(domain[0].Domain(), "another-proxy.xray.com"); diff != "" {
|
2021-10-16 21:02:51 +08:00
|
|
|
t.Error(diff)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
2025-07-23 18:11:43 +08:00
|
|
|
ips, _ := hosts.Lookup("www.example.cn", dns.IPOption{
|
2020-11-25 19:01:53 +08: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)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
2025-07-23 18:11:43 +08:00
|
|
|
ips, _ := hosts.Lookup("baidu.com", dns.IPOption{
|
2020-11-25 19:01:53 +08: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)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|