2022-09-10 06:40:16 +00:00
|
|
|
package cachefile
|
|
|
|
|
|
|
|
import (
|
2023-10-16 04:00:00 +00:00
|
|
|
"errors"
|
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"
|
|
|
|
|
2023-10-08 14:29:31 +00:00
|
|
|
"github.com/sagernet/bbolt"
|
|
|
|
bboltErrors "github.com/sagernet/bbolt/errors"
|
2022-09-10 06:40:16 +00:00
|
|
|
"github.com/sagernet/sing-box/adapter"
|
2023-08-24 13:52:38 +00:00
|
|
|
"github.com/sagernet/sing/common"
|
2023-10-16 04:00:00 +00:00
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
2022-09-10 06:40:16 +00:00
|
|
|
)
|
|
|
|
|
2023-08-12 11:33:43 +00:00
|
|
|
var (
|
|
|
|
bucketSelected = []byte("selected")
|
|
|
|
bucketExpand = []byte("group_expand")
|
2023-08-24 13:52:38 +00:00
|
|
|
bucketMode = []byte("clash_mode")
|
|
|
|
|
|
|
|
bucketNameList = []string{
|
|
|
|
string(bucketSelected),
|
|
|
|
string(bucketExpand),
|
|
|
|
string(bucketMode),
|
|
|
|
}
|
|
|
|
|
|
|
|
cacheIDDefault = []byte("default")
|
2023-08-12 11:33:43 +00:00
|
|
|
)
|
2022-09-10 06:40:16 +00:00
|
|
|
|
|
|
|
var _ adapter.ClashCacheFile = (*CacheFile)(nil)
|
|
|
|
|
|
|
|
type CacheFile struct {
|
2023-08-07 13:53:19 +00:00
|
|
|
DB *bbolt.DB
|
|
|
|
cacheID []byte
|
|
|
|
saveAccess sync.RWMutex
|
|
|
|
saveDomain map[netip.Addr]string
|
|
|
|
saveAddress4 map[string]netip.Addr
|
|
|
|
saveAddress6 map[string]netip.Addr
|
|
|
|
saveMetadataTimer *time.Timer
|
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}
|
2023-10-16 04:00:00 +00:00
|
|
|
var (
|
|
|
|
db *bbolt.DB
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
db, err = bbolt.Open(path, fileMode, &options)
|
|
|
|
if err == nil {
|
2022-09-10 06:40:16 +00:00
|
|
|
break
|
|
|
|
}
|
2023-10-16 04:00:00 +00:00
|
|
|
if errors.Is(err, bboltErrors.ErrTimeout) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if E.IsMulti(err, bboltErrors.ErrInvalid, bboltErrors.ErrChecksum, bboltErrors.ErrVersionMismatch) {
|
|
|
|
rmErr := os.Remove(path)
|
|
|
|
if rmErr != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
2022-09-10 06:40:16 +00:00
|
|
|
}
|
|
|
|
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 {
|
2023-07-24 08:50:26 +00:00
|
|
|
if name[0] == 0 {
|
|
|
|
return b.ForEachBucket(func(k []byte) error {
|
|
|
|
bucketName := string(k)
|
2023-08-24 13:52:38 +00:00
|
|
|
if !(common.Contains(bucketNameList, bucketName)) {
|
2023-08-12 11:33:43 +00:00
|
|
|
_ = b.DeleteBucket(name)
|
2023-07-24 08:50:26 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
bucketName := string(name)
|
2023-08-24 13:52:38 +00:00
|
|
|
if !(common.Contains(bucketNameList, bucketName) || strings.HasPrefix(bucketName, fakeipBucketPrefix)) {
|
2023-08-12 11:33:43 +00:00
|
|
|
_ = tx.DeleteBucket(name)
|
2023-07-19 12:40:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
})
|
2023-07-20 11:51:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-07-08 08:08:46 +00:00
|
|
|
return &CacheFile{
|
2023-07-20 11:51:04 +00:00
|
|
|
DB: db,
|
|
|
|
cacheID: cacheIDBytes,
|
|
|
|
saveDomain: make(map[netip.Addr]string),
|
|
|
|
saveAddress4: make(map[string]netip.Addr),
|
|
|
|
saveAddress6: make(map[string]netip.Addr),
|
2023-07-08 08:08:46 +00:00
|
|
|
}, nil
|
2023-05-09 09:58:59 +00:00
|
|
|
}
|
|
|
|
|
2023-08-24 13:52:38 +00:00
|
|
|
func (c *CacheFile) LoadMode() string {
|
|
|
|
var mode string
|
|
|
|
c.DB.View(func(t *bbolt.Tx) error {
|
|
|
|
bucket := t.Bucket(bucketMode)
|
|
|
|
if bucket == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var modeBytes []byte
|
|
|
|
if len(c.cacheID) > 0 {
|
|
|
|
modeBytes = bucket.Get(c.cacheID)
|
|
|
|
} else {
|
|
|
|
modeBytes = bucket.Get(cacheIDDefault)
|
|
|
|
}
|
|
|
|
mode = string(modeBytes)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return mode
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CacheFile) StoreMode(mode string) error {
|
|
|
|
return c.DB.Batch(func(t *bbolt.Tx) error {
|
|
|
|
bucket, err := t.CreateBucketIfNotExists(bucketMode)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(c.cacheID) > 0 {
|
|
|
|
return bucket.Put(c.cacheID, []byte(mode))
|
|
|
|
} else {
|
|
|
|
return bucket.Put(cacheIDDefault, []byte(mode))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2023-08-12 11:33:43 +00:00
|
|
|
func (c *CacheFile) LoadGroupExpand(group string) (isExpand bool, loaded bool) {
|
|
|
|
c.DB.View(func(t *bbolt.Tx) error {
|
|
|
|
bucket := c.bucket(t, bucketExpand)
|
|
|
|
if bucket == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
expandBytes := bucket.Get([]byte(group))
|
|
|
|
if len(expandBytes) == 1 {
|
|
|
|
isExpand = expandBytes[0] == 1
|
|
|
|
loaded = true
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CacheFile) StoreGroupExpand(group string, isExpand bool) error {
|
|
|
|
return c.DB.Batch(func(t *bbolt.Tx) error {
|
|
|
|
bucket, err := c.createBucket(t, bucketExpand)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if isExpand {
|
|
|
|
return bucket.Put([]byte(group), []byte{1})
|
|
|
|
} else {
|
|
|
|
return bucket.Put([]byte(group), []byte{0})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-12-02 13:57:10 +00:00
|
|
|
func (c *CacheFile) Close() error {
|
|
|
|
return c.DB.Close()
|
|
|
|
}
|