cachefile.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package fakeip
  2. import (
  3. "net/netip"
  4. "github.com/metacubex/mihomo/component/profile/cachefile"
  5. )
  6. type cachefileStore struct {
  7. cache *cachefile.CacheFile
  8. }
  9. // GetByHost implements store.GetByHost
  10. func (c *cachefileStore) GetByHost(host string) (netip.Addr, bool) {
  11. elm := c.cache.GetFakeip([]byte(host))
  12. if elm == nil {
  13. return netip.Addr{}, false
  14. }
  15. if len(elm) == 4 {
  16. return netip.AddrFrom4(*(*[4]byte)(elm)), true
  17. } else {
  18. return netip.AddrFrom16(*(*[16]byte)(elm)), true
  19. }
  20. }
  21. // PutByHost implements store.PutByHost
  22. func (c *cachefileStore) PutByHost(host string, ip netip.Addr) {
  23. c.cache.PutFakeip([]byte(host), ip.AsSlice())
  24. }
  25. // GetByIP implements store.GetByIP
  26. func (c *cachefileStore) GetByIP(ip netip.Addr) (string, bool) {
  27. elm := c.cache.GetFakeip(ip.AsSlice())
  28. if elm == nil {
  29. return "", false
  30. }
  31. return string(elm), true
  32. }
  33. // PutByIP implements store.PutByIP
  34. func (c *cachefileStore) PutByIP(ip netip.Addr, host string) {
  35. c.cache.PutFakeip(ip.AsSlice(), []byte(host))
  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. // Exist implements store.Exist
  43. func (c *cachefileStore) Exist(ip netip.Addr) bool {
  44. _, exist := c.GetByIP(ip)
  45. return exist
  46. }
  47. // CloneTo implements store.CloneTo
  48. // already persistence
  49. func (c *cachefileStore) CloneTo(store store) {}
  50. // FlushFakeIP implements store.FlushFakeIP
  51. func (c *cachefileStore) FlushFakeIP() error {
  52. return c.cache.FlushFakeIP()
  53. }