Fix cache file

This commit is contained in:
世界 2023-07-19 20:40:34 +08:00
parent e075bb5c8d
commit 98bf696d01
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
3 changed files with 20 additions and 4 deletions

View file

@ -3,6 +3,7 @@ package cachefile
import ( import (
"net/netip" "net/netip"
"os" "os"
"strings"
"sync" "sync"
"time" "time"
@ -40,6 +41,18 @@ func Open(path string, cacheID string) (*CacheFile, error) {
if cacheID != "" { if cacheID != "" {
cacheIDBytes = append([]byte{0}, []byte(cacheID)...) cacheIDBytes = append([]byte{0}, []byte(cacheID)...)
} }
err = db.Batch(func(tx *bbolt.Tx) error {
return tx.ForEach(func(name []byte, b *bbolt.Bucket) error {
bucketName := string(name)
if !(bucketName == string(bucketSelected) || strings.HasPrefix(bucketName, fakeipBucketPrefix)) {
delErr := tx.DeleteBucket(name)
if delErr != nil {
return delErr
}
}
return nil
})
})
return &CacheFile{ return &CacheFile{
DB: db, DB: db,
cacheID: cacheIDBytes, cacheID: cacheIDBytes,

View file

@ -11,11 +11,13 @@ import (
"go.etcd.io/bbolt" "go.etcd.io/bbolt"
) )
const fakeipBucketPrefix = "fakeip_"
var ( var (
bucketFakeIP = []byte("fakeip") bucketFakeIP = []byte(fakeipBucketPrefix + "address")
bucketFakeIPDomain4 = []byte("fakeip_domain4") bucketFakeIPDomain4 = []byte(fakeipBucketPrefix + "domain4")
bucketFakeIPDomain6 = []byte("fakeip_domain6") bucketFakeIPDomain6 = []byte(fakeipBucketPrefix + "domain6")
keyMetadata = []byte("metadata") keyMetadata = []byte(fakeipBucketPrefix + "metadata")
) )
func (c *CacheFile) FakeIPMetadata() *adapter.FakeIPMetadata { func (c *CacheFile) FakeIPMetadata() *adapter.FakeIPMetadata {

View file

@ -50,6 +50,7 @@ func (s *Store) Start() error {
if s.inet6Range.IsValid() { if s.inet6Range.IsValid() {
s.inet6Current = s.inet6Range.Addr().Next().Next() s.inet6Current = s.inet6Range.Addr().Next().Next()
} }
_ = storage.FakeIPReset()
} }
s.storage = storage s.storage = storage
return nil return nil