2021-02-18 23:41:50 +08:00
|
|
|
package cachefile
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/gob"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/Dreamacro/clash/component/profile"
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
"github.com/Dreamacro/clash/log"
|
2021-10-04 19:20:11 +08:00
|
|
|
|
|
|
|
bolt "go.etcd.io/bbolt"
|
2021-02-18 23:41:50 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
initOnce sync.Once
|
|
|
|
fileMode os.FileMode = 0666
|
|
|
|
defaultCache *CacheFile
|
|
|
|
|
2021-10-04 19:20:11 +08:00
|
|
|
bucketSelected = []byte("selected")
|
|
|
|
)
|
2021-02-18 23:41:50 +08:00
|
|
|
|
|
|
|
// CacheFile store and update the cache file
|
|
|
|
type CacheFile struct {
|
2021-10-04 19:20:11 +08:00
|
|
|
db *bolt.DB
|
2021-02-18 23:41:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CacheFile) SetSelected(group, selected string) {
|
|
|
|
if !profile.StoreSelected.Load() {
|
|
|
|
return
|
2021-10-04 19:20:11 +08:00
|
|
|
} else if c.db == nil {
|
2021-02-18 23:41:50 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-04 19:20:11 +08:00
|
|
|
err := c.db.Batch(func(t *bolt.Tx) error {
|
|
|
|
bucket, err := t.CreateBucketIfNotExists(bucketSelected)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return bucket.Put([]byte(group), []byte(selected))
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Warnln("[CacheFile] write cache to %s failed: %s", c.db.Path(), err.Error())
|
2021-02-18 23:41:50 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CacheFile) SelectedMap() map[string]string {
|
|
|
|
if !profile.StoreSelected.Load() {
|
|
|
|
return nil
|
2021-10-04 19:20:11 +08:00
|
|
|
} else if c.db == nil {
|
|
|
|
return nil
|
2021-02-18 23:41:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
mapping := map[string]string{}
|
2021-10-04 19:20:11 +08:00
|
|
|
c.db.View(func(t *bolt.Tx) error {
|
|
|
|
bucket := t.Bucket(bucketSelected)
|
|
|
|
if bucket == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
c := bucket.Cursor()
|
|
|
|
for k, v := c.First(); k != nil; k, v = c.Next() {
|
|
|
|
mapping[string(k)] = string(v)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2021-02-18 23:41:50 +08:00
|
|
|
return mapping
|
|
|
|
}
|
|
|
|
|
2021-10-04 19:20:11 +08:00
|
|
|
func (c *CacheFile) Close() error {
|
|
|
|
return c.db.Close()
|
|
|
|
}
|
|
|
|
|
2021-10-05 12:42:21 +08:00
|
|
|
// TODO: remove migrateCache until 2022
|
2021-10-04 19:20:11 +08:00
|
|
|
func migrateCache() {
|
|
|
|
defer func() {
|
|
|
|
db, err := bolt.Open(C.Path.Cache(), fileMode, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnln("[CacheFile] can't open cache file: %s", err.Error())
|
|
|
|
}
|
|
|
|
defaultCache = &CacheFile{
|
|
|
|
db: db,
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
buf, err := ioutil.ReadFile(C.Path.OldCache())
|
|
|
|
if err != nil {
|
|
|
|
return
|
2021-02-18 23:41:50 +08:00
|
|
|
}
|
2021-10-04 19:20:11 +08:00
|
|
|
defer os.Remove(C.Path.OldCache())
|
2021-02-18 23:41:50 +08:00
|
|
|
|
2021-10-04 19:20:11 +08:00
|
|
|
// read old cache file
|
|
|
|
type cache struct {
|
|
|
|
Selected map[string]string
|
|
|
|
}
|
2021-02-18 23:41:50 +08:00
|
|
|
model := &cache{
|
|
|
|
Selected: map[string]string{},
|
|
|
|
}
|
2021-10-04 19:20:11 +08:00
|
|
|
bufReader := bytes.NewBuffer(buf)
|
|
|
|
gob.NewDecoder(bufReader).Decode(model)
|
2021-02-18 23:41:50 +08:00
|
|
|
|
2021-10-04 19:20:11 +08:00
|
|
|
// write to new cache file
|
|
|
|
db, err := bolt.Open(C.Path.Cache(), fileMode, nil)
|
|
|
|
if err != nil {
|
|
|
|
return
|
2021-02-18 23:41:50 +08:00
|
|
|
}
|
2021-10-04 19:20:11 +08:00
|
|
|
defer db.Close()
|
|
|
|
db.Batch(func(t *bolt.Tx) error {
|
|
|
|
bucket, err := t.CreateBucketIfNotExists(bucketSelected)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for group, selected := range model.Selected {
|
|
|
|
if err := bucket.Put([]byte(group), []byte(selected)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2021-02-18 23:41:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cache return singleton of CacheFile
|
|
|
|
func Cache() *CacheFile {
|
2021-10-04 19:20:11 +08:00
|
|
|
initOnce.Do(migrateCache)
|
2021-02-18 23:41:50 +08:00
|
|
|
|
|
|
|
return defaultCache
|
|
|
|
}
|