From 3c85b8bc488231c9190aeba5b523fe2778f79271 Mon Sep 17 00:00:00 2001 From: dyhkwong <50692134+dyhkwong@users.noreply.github.com> Date: Sat, 27 Apr 2024 22:31:12 +0800 Subject: [PATCH] Fix fake-ip mapping --- experimental/cachefile/fakeip.go | 13 +++++++++++++ transport/fakeip/memory.go | 7 +++++++ 2 files changed, 20 insertions(+) diff --git a/experimental/cachefile/fakeip.go b/experimental/cachefile/fakeip.go index 690b0d91..41c1dee6 100644 --- a/experimental/cachefile/fakeip.go +++ b/experimental/cachefile/fakeip.go @@ -74,6 +74,7 @@ func (c *CacheFile) FakeIPStore(address netip.Addr, domain string) error { if err != nil { return err } + oldDomain := bucket.Get(address.AsSlice()) err = bucket.Put(address.AsSlice(), []byte(domain)) if err != nil { return err @@ -86,12 +87,24 @@ func (c *CacheFile) FakeIPStore(address netip.Addr, domain string) error { if err != nil { return err } + if oldDomain != nil { + if err := bucket.Delete(oldDomain); err != nil { + return err + } + } return bucket.Put([]byte(domain), address.AsSlice()) }) } func (c *CacheFile) FakeIPStoreAsync(address netip.Addr, domain string, logger logger.Logger) { c.saveAccess.Lock() + if oldDomain, loaded := c.saveDomain[address]; loaded { + if address.Is4() { + delete(c.saveAddress4, oldDomain) + } else { + delete(c.saveAddress6, oldDomain) + } + } c.saveDomain[address] = domain if address.Is4() { c.saveAddress4[domain] = address diff --git a/transport/fakeip/memory.go b/transport/fakeip/memory.go index 0bca4910..1640ab34 100644 --- a/transport/fakeip/memory.go +++ b/transport/fakeip/memory.go @@ -40,6 +40,13 @@ func (s *MemoryStorage) FakeIPSaveMetadataAsync(metadata *adapter.FakeIPMetadata func (s *MemoryStorage) FakeIPStore(address netip.Addr, domain string) error { s.addressAccess.Lock() s.domainAccess.Lock() + if oldDomain, loaded := s.addressCache[address]; loaded { + if address.Is4() { + delete(s.domainCache4, oldDomain) + } else { + delete(s.domainCache6, oldDomain) + } + } s.addressCache[address] = domain if address.Is4() { s.domainCache4[domain] = address