2022-09-10 06:40:16 +00:00
|
|
|
package cachefile
|
|
|
|
|
|
|
|
import (
|
2023-07-08 08:08:46 +00:00
|
|
|
"net/netip"
|
2022-09-10 06:40:16 +00:00
|
|
|
"os"
|
2023-07-19 12:40:34 +00:00
|
|
|
"strings"
|
2023-07-08 08:08:46 +00:00
|
|
|
"sync"
|
2022-09-10 06:40:16 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/sagernet/sing-box/adapter"
|
|
|
|
|
|
|
|
"go.etcd.io/bbolt"
|
|
|
|
)
|
|
|
|
|
|
|
|
var bucketSelected = []byte("selected")
|
|
|
|
|
|
|
|
var _ adapter.ClashCacheFile = (*CacheFile)(nil)
|
|
|
|
|
|
|
|
type CacheFile struct {
|
2023-07-08 08:08:46 +00:00
|
|
|
DB *bbolt.DB
|
|
|
|
cacheID []byte
|
|
|
|
saveAccess sync.RWMutex
|
|
|
|
saveCache map[netip.Addr]string
|
2022-09-10 06:40:16 +00:00
|
|
|
}
|
|
|
|
|
2023-05-09 09:58:59 +00:00
|
|
|
func Open(path string, cacheID string) (*CacheFile, error) {
|
2022-09-10 06:40:16 +00:00
|
|
|
const fileMode = 0o666
|
|
|
|
options := bbolt.Options{Timeout: time.Second}
|
|
|
|
db, err := bbolt.Open(path, fileMode, &options)
|
|
|
|
switch err {
|
|
|
|
case bbolt.ErrInvalid, bbolt.ErrChecksum, bbolt.ErrVersionMismatch:
|
|
|
|
if err = os.Remove(path); err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
db, err = bbolt.Open(path, 0o666, &options)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-05-09 09:58:59 +00:00
|
|
|
var cacheIDBytes []byte
|
|
|
|
if cacheID != "" {
|
|
|
|
cacheIDBytes = append([]byte{0}, []byte(cacheID)...)
|
|
|
|
}
|
2023-07-19 12:40:34 +00:00
|
|
|
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
|
|
|
|
})
|
|
|
|
})
|
2023-07-08 08:08:46 +00:00
|
|
|
return &CacheFile{
|
|
|
|
DB: db,
|
|
|
|
cacheID: cacheIDBytes,
|
|
|
|
saveCache: make(map[netip.Addr]string),
|
|
|
|
}, nil
|
2023-05-09 09:58:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CacheFile) bucket(t *bbolt.Tx, key []byte) *bbolt.Bucket {
|
|
|
|
if c.cacheID == nil {
|
|
|
|
return t.Bucket(key)
|
|
|
|
}
|
|
|
|
bucket := t.Bucket(c.cacheID)
|
|
|
|
if bucket == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return bucket.Bucket(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CacheFile) createBucket(t *bbolt.Tx, key []byte) (*bbolt.Bucket, error) {
|
|
|
|
if c.cacheID == nil {
|
|
|
|
return t.CreateBucketIfNotExists(key)
|
|
|
|
}
|
|
|
|
bucket, err := t.CreateBucketIfNotExists(c.cacheID)
|
|
|
|
if bucket == nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return bucket.CreateBucketIfNotExists(key)
|
2022-09-10 06:40:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CacheFile) LoadSelected(group string) string {
|
|
|
|
var selected string
|
|
|
|
c.DB.View(func(t *bbolt.Tx) error {
|
2023-05-09 09:58:59 +00:00
|
|
|
bucket := c.bucket(t, bucketSelected)
|
2022-09-10 06:40:16 +00:00
|
|
|
if bucket == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
selectedBytes := bucket.Get([]byte(group))
|
|
|
|
if len(selectedBytes) > 0 {
|
|
|
|
selected = string(selectedBytes)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return selected
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CacheFile) StoreSelected(group, selected string) error {
|
|
|
|
return c.DB.Batch(func(t *bbolt.Tx) error {
|
2023-05-09 09:58:59 +00:00
|
|
|
bucket, err := c.createBucket(t, bucketSelected)
|
2022-09-10 06:40:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return bucket.Put([]byte(group), []byte(selected))
|
|
|
|
})
|
|
|
|
}
|
2022-12-02 13:57:10 +00:00
|
|
|
|
|
|
|
func (c *CacheFile) Close() error {
|
|
|
|
return c.DB.Close()
|
|
|
|
}
|