12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- package fakeip
- import (
- "net/netip"
- "github.com/metacubex/mihomo/common/lru"
- )
- type memoryStore struct {
- cacheIP *lru.LruCache[string, netip.Addr]
- cacheHost *lru.LruCache[netip.Addr, string]
- }
- func (m *memoryStore) GetByHost(host string) (netip.Addr, bool) {
- if ip, exist := m.cacheIP.Get(host); exist {
-
- m.cacheHost.Get(ip)
- return ip, true
- }
- return netip.Addr{}, false
- }
- func (m *memoryStore) PutByHost(host string, ip netip.Addr) {
- m.cacheIP.Set(host, ip)
- }
- func (m *memoryStore) GetByIP(ip netip.Addr) (string, bool) {
- if host, exist := m.cacheHost.Get(ip); exist {
-
- m.cacheIP.Get(host)
- return host, true
- }
- return "", false
- }
- func (m *memoryStore) PutByIP(ip netip.Addr, host string) {
- m.cacheHost.Set(ip, host)
- }
- func (m *memoryStore) DelByIP(ip netip.Addr) {
- if host, exist := m.cacheHost.Get(ip); exist {
- m.cacheIP.Delete(host)
- }
- m.cacheHost.Delete(ip)
- }
- func (m *memoryStore) Exist(ip netip.Addr) bool {
- return m.cacheHost.Exist(ip)
- }
- func (m *memoryStore) CloneTo(store store) {
- if ms, ok := store.(*memoryStore); ok {
- m.cacheIP.CloneTo(ms.cacheIP)
- m.cacheHost.CloneTo(ms.cacheHost)
- }
- }
- func (m *memoryStore) FlushFakeIP() error {
- _ = m.cacheIP.Clear()
- return m.cacheHost.Clear()
- }
- func newMemoryStore(size int) *memoryStore {
- return &memoryStore{
- cacheIP: lru.New[string, netip.Addr](lru.WithSize[string, netip.Addr](size)),
- cacheHost: lru.New[netip.Addr, string](lru.WithSize[netip.Addr, string](size)),
- }
- }
|