github.com/metacubex/mihomo@v1.18.5/component/fakeip/cachefile.go (about) 1 package fakeip 2 3 import ( 4 "net/netip" 5 6 "github.com/metacubex/mihomo/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 elm := c.cache.GetFakeip([]byte(host)) 16 if elm == nil { 17 return netip.Addr{}, false 18 } 19 20 if len(elm) == 4 { 21 return netip.AddrFrom4(*(*[4]byte)(elm)), true 22 } else { 23 return netip.AddrFrom16(*(*[16]byte)(elm)), true 24 } 25 } 26 27 // PutByHost implements store.PutByHost 28 func (c *cachefileStore) PutByHost(host string, ip netip.Addr) { 29 c.cache.PutFakeip([]byte(host), ip.AsSlice()) 30 } 31 32 // GetByIP implements store.GetByIP 33 func (c *cachefileStore) GetByIP(ip netip.Addr) (string, bool) { 34 elm := c.cache.GetFakeip(ip.AsSlice()) 35 if elm == nil { 36 return "", false 37 } 38 return string(elm), true 39 } 40 41 // PutByIP implements store.PutByIP 42 func (c *cachefileStore) PutByIP(ip netip.Addr, host string) { 43 c.cache.PutFakeip(ip.AsSlice(), []byte(host)) 44 } 45 46 // DelByIP implements store.DelByIP 47 func (c *cachefileStore) DelByIP(ip netip.Addr) { 48 addr := ip.AsSlice() 49 c.cache.DelFakeipPair(addr, c.cache.GetFakeip(addr)) 50 } 51 52 // Exist implements store.Exist 53 func (c *cachefileStore) Exist(ip netip.Addr) bool { 54 _, exist := c.GetByIP(ip) 55 return exist 56 } 57 58 // CloneTo implements store.CloneTo 59 // already persistence 60 func (c *cachefileStore) CloneTo(store store) {} 61 62 // FlushFakeIP implements store.FlushFakeIP 63 func (c *cachefileStore) FlushFakeIP() error { 64 return c.cache.FlushFakeIP() 65 }