github.com/yaling888/clash@v1.53.0/component/fakeip/cachefile.go (about)

     1  package fakeip
     2  
     3  import (
     4  	"net/netip"
     5  
     6  	"github.com/yaling888/clash/component/profile/cachefile"
     7  )
     8  
     9  type cachefileStore struct {
    10  	cache *cachefile.CacheFile
    11  }
    12  
    13  // GetByHost implements store.GetByHost
    14  func (c *cachefileStore) GetByHost(host string) (netip.Addr, bool) {
    15  	return netip.AddrFromSlice(c.cache.GetFakeip([]byte(host)))
    16  }
    17  
    18  // PutByHost implements store.PutByHost
    19  func (c *cachefileStore) PutByHost(host string, ip netip.Addr) {
    20  	_ = c.cache.PutFakeip([]byte(host), ip.AsSlice())
    21  }
    22  
    23  // GetByIP implements store.GetByIP
    24  func (c *cachefileStore) GetByIP(ip netip.Addr) (string, bool) {
    25  	elm := c.cache.GetFakeip(ip.AsSlice())
    26  	if elm == nil {
    27  		return "", false
    28  	}
    29  	return string(elm), true
    30  }
    31  
    32  // PutByIP implements store.PutByIP
    33  func (c *cachefileStore) PutByIP(ip netip.Addr, host string) {
    34  	_ = c.cache.PutFakeip(ip.AsSlice(), []byte(host))
    35  }
    36  
    37  // DelByIP implements store.DelByIP
    38  func (c *cachefileStore) DelByIP(ip netip.Addr) {
    39  	addr := ip.AsSlice()
    40  	_ = c.cache.DelFakeipPair(addr, c.cache.GetFakeip(addr))
    41  }
    42  
    43  // Exist implements store.Exist
    44  func (c *cachefileStore) Exist(ip netip.Addr) bool {
    45  	_, exist := c.GetByIP(ip)
    46  	return exist
    47  }
    48  
    49  // CloneTo implements store.CloneTo
    50  // already persistence
    51  func (c *cachefileStore) CloneTo(_ store) {}
    52  
    53  // FlushFakeIP implements store.FlushFakeIP
    54  func (c *cachefileStore) FlushFakeIP() error {
    55  	return c.cache.FlushFakeIP()
    56  }