2021-03-07 04:39:50 +00:00
|
|
|
package fakedns
|
|
|
|
|
|
|
|
import (
|
2021-10-20 05:15:49 +00:00
|
|
|
gonet "net"
|
2022-03-12 15:35:06 +00:00
|
|
|
"strconv"
|
2021-12-15 00:28:47 +00:00
|
|
|
"testing"
|
2021-10-20 05:15:49 +00:00
|
|
|
|
2021-03-07 04:39:50 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2021-03-10 14:55:51 +00:00
|
|
|
|
2022-03-12 15:35:06 +00:00
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
|
2021-03-07 04:39:50 +00:00
|
|
|
"github.com/xtls/xray-core/common"
|
|
|
|
"github.com/xtls/xray-core/common/net"
|
|
|
|
"github.com/xtls/xray-core/common/uuid"
|
2021-03-14 15:58:27 +00:00
|
|
|
"github.com/xtls/xray-core/features/dns"
|
|
|
|
)
|
|
|
|
|
2021-10-20 05:15:49 +00:00
|
|
|
var ipPrefix = "198.1"
|
2021-03-07 04:39:50 +00:00
|
|
|
|
|
|
|
func TestNewFakeDnsHolder(_ *testing.T) {
|
|
|
|
_, err := NewFakeDNSHolder()
|
|
|
|
common.Must(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFakeDnsHolderCreateMapping(t *testing.T) {
|
|
|
|
fkdns, err := NewFakeDNSHolder()
|
|
|
|
common.Must(err)
|
|
|
|
|
2021-03-10 14:55:51 +00:00
|
|
|
addr := fkdns.GetFakeIPForDomain("fakednstest.example.com")
|
2021-03-14 15:58:27 +00:00
|
|
|
assert.Equal(t, ipPrefix, addr[0].IP().String()[0:len(ipPrefix)])
|
2021-03-07 04:39:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFakeDnsHolderCreateMappingMany(t *testing.T) {
|
|
|
|
fkdns, err := NewFakeDNSHolder()
|
|
|
|
common.Must(err)
|
|
|
|
|
2021-03-10 14:55:51 +00:00
|
|
|
addr := fkdns.GetFakeIPForDomain("fakednstest.example.com")
|
2021-03-14 15:58:27 +00:00
|
|
|
assert.Equal(t, ipPrefix, addr[0].IP().String()[0:len(ipPrefix)])
|
2021-03-07 04:39:50 +00:00
|
|
|
|
2021-03-10 14:55:51 +00:00
|
|
|
addr2 := fkdns.GetFakeIPForDomain("fakednstest2.example.com")
|
2021-03-14 15:58:27 +00:00
|
|
|
assert.Equal(t, ipPrefix, addr2[0].IP().String()[0:len(ipPrefix)])
|
2021-03-07 04:39:50 +00:00
|
|
|
assert.NotEqual(t, addr[0].IP().String(), addr2[0].IP().String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFakeDnsHolderCreateMappingManyAndResolve(t *testing.T) {
|
|
|
|
fkdns, err := NewFakeDNSHolder()
|
|
|
|
common.Must(err)
|
|
|
|
|
2021-03-10 14:55:51 +00:00
|
|
|
addr := fkdns.GetFakeIPForDomain("fakednstest.example.com")
|
|
|
|
addr2 := fkdns.GetFakeIPForDomain("fakednstest2.example.com")
|
2021-03-07 04:39:50 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
result := fkdns.GetDomainFromFakeDNS(addr[0])
|
2021-03-10 14:55:51 +00:00
|
|
|
assert.Equal(t, "fakednstest.example.com", result)
|
2021-03-07 04:39:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
result := fkdns.GetDomainFromFakeDNS(addr2[0])
|
2021-03-10 14:55:51 +00:00
|
|
|
assert.Equal(t, "fakednstest2.example.com", result)
|
2021-03-07 04:39:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFakeDnsHolderCreateMappingManySingleDomain(t *testing.T) {
|
|
|
|
fkdns, err := NewFakeDNSHolder()
|
|
|
|
common.Must(err)
|
|
|
|
|
2021-03-10 14:55:51 +00:00
|
|
|
addr := fkdns.GetFakeIPForDomain("fakednstest.example.com")
|
|
|
|
addr2 := fkdns.GetFakeIPForDomain("fakednstest.example.com")
|
2021-03-07 04:39:50 +00:00
|
|
|
assert.Equal(t, addr[0].IP().String(), addr2[0].IP().String())
|
|
|
|
}
|
|
|
|
|
2022-03-12 15:35:06 +00:00
|
|
|
func TestGetFakeIPForDomainConcurrently(t *testing.T) {
|
|
|
|
fkdns, err := NewFakeDNSHolder()
|
|
|
|
common.Must(err)
|
|
|
|
|
|
|
|
total := 200
|
|
|
|
addr := make([][]net.Address, total)
|
|
|
|
var errg errgroup.Group
|
|
|
|
for i := 0; i < total; i++ {
|
|
|
|
errg.Go(testGetFakeIP(i, addr, fkdns))
|
|
|
|
}
|
|
|
|
errg.Wait()
|
|
|
|
for i := 0; i < total; i++ {
|
|
|
|
for j := i + 1; j < total; j++ {
|
|
|
|
assert.NotEqual(t, addr[i][0].IP().String(), addr[j][0].IP().String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testGetFakeIP(index int, addr [][]net.Address, fkdns *Holder) func() error {
|
|
|
|
return func() error {
|
|
|
|
addr[index] = fkdns.GetFakeIPForDomain("fakednstest" + strconv.Itoa(index) + ".example.com")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-07 04:39:50 +00:00
|
|
|
func TestFakeDnsHolderCreateMappingAndRollOver(t *testing.T) {
|
|
|
|
fkdns, err := NewFakeDNSHolderConfigOnly(&FakeDnsPool{
|
2021-10-20 05:15:49 +00:00
|
|
|
IpPool: dns.FakeIPv4Pool,
|
2021-03-07 04:39:50 +00:00
|
|
|
LruSize: 256,
|
|
|
|
})
|
|
|
|
common.Must(err)
|
|
|
|
|
|
|
|
err = fkdns.Start()
|
|
|
|
|
|
|
|
common.Must(err)
|
|
|
|
|
2021-03-10 14:55:51 +00:00
|
|
|
addr := fkdns.GetFakeIPForDomain("fakednstest.example.com")
|
|
|
|
addr2 := fkdns.GetFakeIPForDomain("fakednstest2.example.com")
|
2021-03-07 04:39:50 +00:00
|
|
|
|
|
|
|
for i := 0; i <= 8192; i++ {
|
|
|
|
{
|
|
|
|
result := fkdns.GetDomainFromFakeDNS(addr[0])
|
2021-03-10 14:55:51 +00:00
|
|
|
assert.Equal(t, "fakednstest.example.com", result)
|
2021-03-07 04:39:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
result := fkdns.GetDomainFromFakeDNS(addr2[0])
|
2021-03-10 14:55:51 +00:00
|
|
|
assert.Equal(t, "fakednstest2.example.com", result)
|
2021-03-07 04:39:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
uuid := uuid.New()
|
2021-03-10 14:55:51 +00:00
|
|
|
domain := uuid.String() + ".fakednstest.example.com"
|
2021-03-07 04:39:50 +00:00
|
|
|
tempAddr := fkdns.GetFakeIPForDomain(domain)
|
|
|
|
rsaddr := tempAddr[0].IP().String()
|
|
|
|
|
|
|
|
result := fkdns.GetDomainFromFakeDNS(net.ParseAddress(rsaddr))
|
|
|
|
assert.Equal(t, domain, result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-20 05:15:49 +00:00
|
|
|
|
|
|
|
func TestFakeDNSMulti(t *testing.T) {
|
|
|
|
fakeMulti, err := NewFakeDNSHolderMulti(&FakeDnsPoolMulti{
|
|
|
|
Pools: []*FakeDnsPool{{
|
|
|
|
IpPool: "240.0.0.0/12",
|
|
|
|
LruSize: 256,
|
|
|
|
}, {
|
|
|
|
IpPool: "fddd:c5b4:ff5f:f4f0::/64",
|
|
|
|
LruSize: 256,
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
common.Must(err)
|
|
|
|
|
|
|
|
err = fakeMulti.Start()
|
|
|
|
|
|
|
|
common.Must(err)
|
|
|
|
|
|
|
|
assert.Nil(t, err, "Should not throw error")
|
|
|
|
_ = fakeMulti
|
|
|
|
|
|
|
|
t.Run("checkInRange", func(t *testing.T) {
|
|
|
|
t.Run("ipv4", func(t *testing.T) {
|
|
|
|
inPool := fakeMulti.IsIPInIPPool(net.IPAddress([]byte{240, 0, 0, 5}))
|
|
|
|
assert.True(t, inPool)
|
|
|
|
})
|
|
|
|
t.Run("ipv6", func(t *testing.T) {
|
|
|
|
ip, err := gonet.ResolveIPAddr("ip", "fddd:c5b4:ff5f:f4f0::5")
|
|
|
|
assert.Nil(t, err)
|
|
|
|
inPool := fakeMulti.IsIPInIPPool(net.IPAddress(ip.IP))
|
|
|
|
assert.True(t, inPool)
|
|
|
|
})
|
|
|
|
t.Run("ipv4_inverse", func(t *testing.T) {
|
|
|
|
inPool := fakeMulti.IsIPInIPPool(net.IPAddress([]byte{241, 0, 0, 5}))
|
|
|
|
assert.False(t, inPool)
|
|
|
|
})
|
|
|
|
t.Run("ipv6_inverse", func(t *testing.T) {
|
|
|
|
ip, err := gonet.ResolveIPAddr("ip", "fcdd:c5b4:ff5f:f4f0::5")
|
|
|
|
assert.Nil(t, err)
|
|
|
|
inPool := fakeMulti.IsIPInIPPool(net.IPAddress(ip.IP))
|
|
|
|
assert.False(t, inPool)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("allocateTwoAddressForTwoPool", func(t *testing.T) {
|
2022-02-05 02:59:50 +00:00
|
|
|
address := fakeMulti.GetFakeIPForDomain("fakednstest.example.com")
|
2021-10-20 05:15:49 +00:00
|
|
|
assert.Len(t, address, 2, "should be 2 address one for each pool")
|
|
|
|
t.Run("eachOfThemShouldResolve:0", func(t *testing.T) {
|
|
|
|
domain := fakeMulti.GetDomainFromFakeDNS(address[0])
|
2022-02-05 02:59:50 +00:00
|
|
|
assert.Equal(t, "fakednstest.example.com", domain)
|
2021-10-20 05:15:49 +00:00
|
|
|
})
|
|
|
|
t.Run("eachOfThemShouldResolve:1", func(t *testing.T) {
|
|
|
|
domain := fakeMulti.GetDomainFromFakeDNS(address[1])
|
2022-02-05 02:59:50 +00:00
|
|
|
assert.Equal(t, "fakednstest.example.com", domain)
|
2021-10-20 05:15:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("understandIPTypeSelector", func(t *testing.T) {
|
|
|
|
t.Run("ipv4", func(t *testing.T) {
|
2022-02-05 02:59:50 +00:00
|
|
|
address := fakeMulti.GetFakeIPForDomain3("fakednstestipv4.example.com", true, false)
|
2021-10-20 05:15:49 +00:00
|
|
|
assert.Len(t, address, 1, "should be 1 address")
|
|
|
|
assert.True(t, address[0].Family().IsIPv4())
|
|
|
|
})
|
|
|
|
t.Run("ipv6", func(t *testing.T) {
|
2022-02-05 02:59:50 +00:00
|
|
|
address := fakeMulti.GetFakeIPForDomain3("fakednstestipv6.example.com", false, true)
|
2021-10-20 05:15:49 +00:00
|
|
|
assert.Len(t, address, 1, "should be 1 address")
|
|
|
|
assert.True(t, address[0].Family().IsIPv6())
|
|
|
|
})
|
|
|
|
t.Run("ipv46", func(t *testing.T) {
|
2022-02-05 02:59:50 +00:00
|
|
|
address := fakeMulti.GetFakeIPForDomain3("fakednstestipv46.example.com", true, true)
|
2021-10-20 05:15:49 +00:00
|
|
|
assert.Len(t, address, 2, "should be 2 address")
|
|
|
|
assert.True(t, address[0].Family().IsIPv4())
|
|
|
|
assert.True(t, address[1].Family().IsIPv6())
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|