From 71cad51e8f95f9e6b1906a60574c591d5776db7b Mon Sep 17 00:00:00 2001 From: bobo liu <7552030+fakeboboliu@users.noreply.github.com> Date: Fri, 12 Aug 2022 13:47:51 +0800 Subject: [PATCH] Fix: satisfy RFC4343 - DNS case insensitivity (#2260) --- component/fakeip/pool.go | 4 ++++ component/fakeip/pool_test.go | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/component/fakeip/pool.go b/component/fakeip/pool.go index 925882bb..e6730a05 100644 --- a/component/fakeip/pool.go +++ b/component/fakeip/pool.go @@ -3,6 +3,7 @@ package fakeip import ( "errors" "net" + "strings" "sync" "github.com/Dreamacro/clash/common/cache" @@ -36,6 +37,9 @@ type Pool struct { func (p *Pool) Lookup(host string) net.IP { p.mux.Lock() defer p.mux.Unlock() + + // RFC4343: DNS Case Insensitive, we SHOULD return result with all cases. + host = strings.ToLower(host) if ip, exist := p.store.GetByHost(host); exist { return ip } diff --git a/component/fakeip/pool_test.go b/component/fakeip/pool_test.go index bd034636..7e161d03 100644 --- a/component/fakeip/pool_test.go +++ b/component/fakeip/pool_test.go @@ -75,6 +75,27 @@ func TestPool_Basic(t *testing.T) { } } +func TestPool_Case_Insensitive(t *testing.T) { + _, ipnet, _ := net.ParseCIDR("192.168.0.1/29") + pools, tempfile, err := createPools(Options{ + IPNet: ipnet, + Size: 10, + }) + assert.Nil(t, err) + defer os.Remove(tempfile) + + for _, pool := range pools { + first := pool.Lookup("foo.com") + last := pool.Lookup("Foo.Com") + foo, exist := pool.LookBack(last) + + assert.True(t, first.Equal(pool.Lookup("Foo.Com"))) + assert.Equal(t, pool.Lookup("fOo.cOM"), first) + assert.True(t, exist) + assert.Equal(t, foo, "foo.com") + } +} + func TestPool_CycleUsed(t *testing.T) { _, ipnet, _ := net.ParseCIDR("192.168.0.1/29") pools, tempfile, err := createPools(Options{