forked from fossabot/clash
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
366 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package fakeip | ||
|
||
import ( | ||
"net" | ||
|
||
"github.com/Dreamacro/clash/component/profile/cachefile" | ||
) | ||
|
||
type cachefileStore struct { | ||
cache *cachefile.CacheFile | ||
} | ||
|
||
// GetByHost implements store.GetByHost | ||
func (c *cachefileStore) GetByHost(host string) (net.IP, bool) { | ||
elm := c.cache.GetFakeip([]byte(host)) | ||
if elm == nil { | ||
return nil, false | ||
} | ||
return net.IP(elm), true | ||
} | ||
|
||
// PutByHost implements store.PutByHost | ||
func (c *cachefileStore) PutByHost(host string, ip net.IP) { | ||
c.cache.PutFakeip([]byte(host), ip) | ||
} | ||
|
||
// GetByIP implements store.GetByIP | ||
func (c *cachefileStore) GetByIP(ip net.IP) (string, bool) { | ||
elm := c.cache.GetFakeip(ip.To4()) | ||
if elm == nil { | ||
return "", false | ||
} | ||
return string(elm), true | ||
} | ||
|
||
// PutByIP implements store.PutByIP | ||
func (c *cachefileStore) PutByIP(ip net.IP, host string) { | ||
c.cache.PutFakeip(ip.To4(), []byte(host)) | ||
} | ||
|
||
// Exist implements store.Exist | ||
func (c *cachefileStore) Exist(ip net.IP) bool { | ||
_, exist := c.GetByIP(ip) | ||
return exist | ||
} | ||
|
||
// CloneTo implements store.CloneTo | ||
// already persistence | ||
func (c *cachefileStore) CloneTo(store store) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package fakeip | ||
|
||
import ( | ||
"net" | ||
|
||
"github.com/Dreamacro/clash/common/cache" | ||
) | ||
|
||
type memoryStore struct { | ||
cache *cache.LruCache | ||
} | ||
|
||
// GetByHost implements store.GetByHost | ||
func (m *memoryStore) GetByHost(host string) (net.IP, bool) { | ||
if elm, exist := m.cache.Get(host); exist { | ||
ip := elm.(net.IP) | ||
|
||
// ensure ip --> host on head of linked list | ||
m.cache.Get(ipToUint(ip.To4())) | ||
return ip, true | ||
} | ||
|
||
return nil, false | ||
} | ||
|
||
// PutByHost implements store.PutByHost | ||
func (m *memoryStore) PutByHost(host string, ip net.IP) { | ||
m.cache.Set(host, ip) | ||
} | ||
|
||
// GetByIP implements store.GetByIP | ||
func (m *memoryStore) GetByIP(ip net.IP) (string, bool) { | ||
if elm, exist := m.cache.Get(ipToUint(ip.To4())); exist { | ||
host := elm.(string) | ||
|
||
// ensure host --> ip on head of linked list | ||
m.cache.Get(host) | ||
return host, true | ||
} | ||
|
||
return "", false | ||
} | ||
|
||
// PutByIP implements store.PutByIP | ||
func (m *memoryStore) PutByIP(ip net.IP, host string) { | ||
m.cache.Set(ipToUint(ip.To4()), host) | ||
} | ||
|
||
// Exist implements store.Exist | ||
func (m *memoryStore) Exist(ip net.IP) bool { | ||
return m.cache.Exist(ipToUint(ip.To4())) | ||
} | ||
|
||
// CloneTo implements store.CloneTo | ||
// only for memoryStore to memoryStore | ||
func (m *memoryStore) CloneTo(store store) { | ||
if ms, ok := store.(*memoryStore); ok { | ||
m.cache.CloneTo(ms.cache) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.