-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: rewrite bbolt cachefile implements
never use returned byte slices outside the transaction, ref: https://pkg.go.dev/go.etcd.io/bbolt#hdr-Caveats
- Loading branch information
Showing
12 changed files
with
212 additions
and
168 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,45 @@ | ||
package utils | ||
|
||
import ( | ||
"crypto/md5" | ||
"encoding/hex" | ||
) | ||
|
||
// HashType warps hash array inside struct | ||
// someday can change to other hash algorithm simply | ||
type HashType struct { | ||
md5 [md5.Size]byte // MD5 | ||
} | ||
|
||
func MakeHash(data []byte) HashType { | ||
return HashType{md5.Sum(data)} | ||
} | ||
|
||
func MakeHashFromBytes(hashBytes []byte) (h HashType) { | ||
if len(hashBytes) != md5.Size { | ||
return | ||
} | ||
copy(h.md5[:], hashBytes) | ||
return | ||
} | ||
|
||
func (h HashType) Equal(hash HashType) bool { | ||
return h.md5 == hash.md5 | ||
} | ||
|
||
func (h HashType) Bytes() []byte { | ||
return h.md5[:] | ||
} | ||
|
||
func (h HashType) String() string { | ||
return hex.EncodeToString(h.Bytes()) | ||
} | ||
|
||
func (h HashType) Len() int { | ||
return len(h.md5) | ||
} | ||
|
||
func (h HashType) IsValid() bool { | ||
var zero HashType | ||
return h != zero | ||
} |
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
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
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
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
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,115 @@ | ||
package cachefile | ||
|
||
import ( | ||
"net/netip" | ||
|
||
"github.com/metacubex/mihomo/log" | ||
|
||
"github.com/metacubex/bbolt" | ||
) | ||
|
||
type FakeIpStore struct { | ||
*CacheFile | ||
} | ||
|
||
func (c *CacheFile) FakeIpStore() *FakeIpStore { | ||
return &FakeIpStore{c} | ||
} | ||
|
||
func (c *FakeIpStore) GetByHost(host string) (ip netip.Addr, exist bool) { | ||
if c.DB == nil { | ||
return | ||
} | ||
c.DB.View(func(t *bbolt.Tx) error { | ||
if bucket := t.Bucket(bucketFakeip); bucket != nil { | ||
if v := bucket.Get([]byte(host)); v != nil { | ||
ip, exist = netip.AddrFromSlice(v) | ||
} | ||
} | ||
return nil | ||
}) | ||
return | ||
} | ||
|
||
func (c *FakeIpStore) PutByHost(host string, ip netip.Addr) { | ||
if c.DB == nil { | ||
return | ||
} | ||
err := c.DB.Batch(func(t *bbolt.Tx) error { | ||
bucket, err := t.CreateBucketIfNotExists(bucketFakeip) | ||
if err != nil { | ||
return err | ||
} | ||
return bucket.Put([]byte(host), ip.AsSlice()) | ||
}) | ||
if err != nil { | ||
log.Warnln("[CacheFile] write cache to %s failed: %s", c.DB.Path(), err.Error()) | ||
} | ||
} | ||
|
||
func (c *FakeIpStore) GetByIP(ip netip.Addr) (host string, exist bool) { | ||
if c.DB == nil { | ||
return | ||
} | ||
c.DB.View(func(t *bbolt.Tx) error { | ||
if bucket := t.Bucket(bucketFakeip); bucket != nil { | ||
if v := bucket.Get(ip.AsSlice()); v != nil { | ||
host, exist = string(v), true | ||
} | ||
} | ||
return nil | ||
}) | ||
return | ||
} | ||
|
||
func (c *FakeIpStore) PutByIP(ip netip.Addr, host string) { | ||
if c.DB == nil { | ||
return | ||
} | ||
err := c.DB.Batch(func(t *bbolt.Tx) error { | ||
bucket, err := t.CreateBucketIfNotExists(bucketFakeip) | ||
if err != nil { | ||
return err | ||
} | ||
return bucket.Put(ip.AsSlice(), []byte(host)) | ||
}) | ||
if err != nil { | ||
log.Warnln("[CacheFile] write cache to %s failed: %s", c.DB.Path(), err.Error()) | ||
} | ||
} | ||
|
||
func (c *FakeIpStore) DelByIP(ip netip.Addr) { | ||
if c.DB == nil { | ||
return | ||
} | ||
|
||
addr := ip.AsSlice() | ||
err := c.DB.Batch(func(t *bbolt.Tx) error { | ||
bucket, err := t.CreateBucketIfNotExists(bucketFakeip) | ||
if err != nil { | ||
return err | ||
} | ||
host := bucket.Get(addr) | ||
err = bucket.Delete(addr) | ||
if len(host) > 0 { | ||
if err = bucket.Delete(host); err != nil { | ||
return err | ||
} | ||
} | ||
return err | ||
}) | ||
if err != nil { | ||
log.Warnln("[CacheFile] write cache to %s failed: %s", c.DB.Path(), err.Error()) | ||
} | ||
} | ||
|
||
func (c *FakeIpStore) FlushFakeIP() error { | ||
err := c.DB.Batch(func(t *bbolt.Tx) error { | ||
bucket := t.Bucket(bucketFakeip) | ||
if bucket == nil { | ||
return nil | ||
} | ||
return t.DeleteBucket(bucketFakeip) | ||
}) | ||
return err | ||
} |
Oops, something went wrong.