2021-02-18 23:41:50 +08:00
|
|
|
package cachefile
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"sync"
|
2021-10-05 22:47:26 +08:00
|
|
|
"time"
|
2021-02-18 23:41:50 +08:00
|
|
|
|
2023-11-03 21:01:45 +08:00
|
|
|
"github.com/metacubex/mihomo/component/profile"
|
|
|
|
C "github.com/metacubex/mihomo/constant"
|
|
|
|
"github.com/metacubex/mihomo/log"
|
2021-10-04 19:20:11 +08:00
|
|
|
|
2021-10-11 20:48:58 +08:00
|
|
|
"go.etcd.io/bbolt"
|
2021-02-18 23:41:50 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
initOnce sync.Once
|
2021-10-10 23:44:09 +08:00
|
|
|
fileMode os.FileMode = 0o666
|
2021-02-18 23:41:50 +08:00
|
|
|
defaultCache *CacheFile
|
|
|
|
|
2021-10-04 19:20:11 +08:00
|
|
|
bucketSelected = []byte("selected")
|
2021-10-11 20:48:58 +08:00
|
|
|
bucketFakeip = []byte("fakeip")
|
2021-10-04 19:20:11 +08:00
|
|
|
)
|
2021-02-18 23:41:50 +08:00
|
|
|
|
|
|
|
// CacheFile store and update the cache file
|
|
|
|
type CacheFile struct {
|
2021-10-11 20:48:58 +08:00
|
|
|
DB *bbolt.DB
|
2021-02-18 23:41:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CacheFile) SetSelected(group, selected string) {
|
|
|
|
if !profile.StoreSelected.Load() {
|
|
|
|
return
|
2021-10-11 20:48:58 +08:00
|
|
|
} else if c.DB == nil {
|
2021-02-18 23:41:50 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-11 20:48:58 +08:00
|
|
|
err := c.DB.Batch(func(t *bbolt.Tx) error {
|
2021-10-04 19:20:11 +08:00
|
|
|
bucket, err := t.CreateBucketIfNotExists(bucketSelected)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return bucket.Put([]byte(group), []byte(selected))
|
|
|
|
})
|
|
|
|
if err != nil {
|
2021-10-11 20:48:58 +08:00
|
|
|
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-11 20:48:58 +08:00
|
|
|
} else if c.DB == nil {
|
2021-10-04 19:20:11 +08:00
|
|
|
return nil
|
2021-02-18 23:41:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
mapping := map[string]string{}
|
2021-10-11 20:48:58 +08:00
|
|
|
c.DB.View(func(t *bbolt.Tx) error {
|
2021-10-04 19:20:11 +08:00
|
|
|
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-11 20:48:58 +08:00
|
|
|
func (c *CacheFile) PutFakeip(key, value []byte) error {
|
|
|
|
if c.DB == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err := c.DB.Batch(func(t *bbolt.Tx) error {
|
|
|
|
bucket, err := t.CreateBucketIfNotExists(bucketFakeip)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return bucket.Put(key, value)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Warnln("[CacheFile] write cache to %s failed: %s", c.DB.Path(), err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-23 22:01:49 +08:00
|
|
|
func (c *CacheFile) DelFakeipPair(ip, host []byte) error {
|
|
|
|
if c.DB == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err := c.DB.Batch(func(t *bbolt.Tx) error {
|
|
|
|
bucket, err := t.CreateBucketIfNotExists(bucketFakeip)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = bucket.Delete(ip)
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-11 20:48:58 +08:00
|
|
|
func (c *CacheFile) GetFakeip(key []byte) []byte {
|
|
|
|
if c.DB == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
tx, err := c.DB.Begin(false)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
bucket := tx.Bucket(bucketFakeip)
|
|
|
|
if bucket == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return bucket.Get(key)
|
|
|
|
}
|
|
|
|
|
2022-03-23 01:05:43 +08:00
|
|
|
func (c *CacheFile) 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
|
|
|
|
}
|
|
|
|
|
2021-10-04 19:20:11 +08:00
|
|
|
func (c *CacheFile) Close() error {
|
2021-10-11 20:48:58 +08:00
|
|
|
return c.DB.Close()
|
2021-10-04 19:20:11 +08:00
|
|
|
}
|
|
|
|
|
2022-01-02 00:48:57 +08:00
|
|
|
func initCache() {
|
|
|
|
options := bbolt.Options{Timeout: time.Second}
|
|
|
|
db, err := bbolt.Open(C.Path.Cache(), fileMode, &options)
|
|
|
|
switch err {
|
|
|
|
case bbolt.ErrInvalid, bbolt.ErrChecksum, bbolt.ErrVersionMismatch:
|
|
|
|
if err = os.Remove(C.Path.Cache()); err != nil {
|
|
|
|
log.Warnln("[CacheFile] remove invalid cache file error: %s", err.Error())
|
|
|
|
break
|
2021-10-04 19:20:11 +08:00
|
|
|
}
|
2022-01-02 00:48:57 +08:00
|
|
|
log.Infoln("[CacheFile] remove invalid cache file and create new one")
|
|
|
|
db, err = bbolt.Open(C.Path.Cache(), fileMode, &options)
|
2021-02-18 23:41:50 +08:00
|
|
|
}
|
2021-10-04 19:20:11 +08:00
|
|
|
if err != nil {
|
2022-01-02 00:48:57 +08:00
|
|
|
log.Warnln("[CacheFile] can't open cache file: %s", err.Error())
|
2021-02-18 23:41:50 +08:00
|
|
|
}
|
2021-10-11 20:48:58 +08:00
|
|
|
|
2022-01-02 00:48:57 +08:00
|
|
|
defaultCache = &CacheFile{
|
|
|
|
DB: db,
|
|
|
|
}
|
2021-02-18 23:41:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cache return singleton of CacheFile
|
|
|
|
func Cache() *CacheFile {
|
2022-01-02 00:48:57 +08:00
|
|
|
initOnce.Do(initCache)
|
2021-02-18 23:41:50 +08:00
|
|
|
|
|
|
|
return defaultCache
|
|
|
|
}
|