cache.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. package cachefile
  2. import (
  3. "context"
  4. "errors"
  5. "net/netip"
  6. "os"
  7. "strings"
  8. "sync"
  9. "time"
  10. "github.com/sagernet/bbolt"
  11. bboltErrors "github.com/sagernet/bbolt/errors"
  12. "github.com/sagernet/sing-box/adapter"
  13. "github.com/sagernet/sing-box/option"
  14. "github.com/sagernet/sing/common"
  15. E "github.com/sagernet/sing/common/exceptions"
  16. "github.com/sagernet/sing/service/filemanager"
  17. )
  18. var (
  19. bucketSelected = []byte("selected")
  20. bucketExpand = []byte("group_expand")
  21. bucketMode = []byte("clash_mode")
  22. bucketRuleSet = []byte("rule_set")
  23. bucketNameList = []string{
  24. string(bucketSelected),
  25. string(bucketExpand),
  26. string(bucketMode),
  27. string(bucketRuleSet),
  28. string(bucketRDRC),
  29. }
  30. cacheIDDefault = []byte("default")
  31. )
  32. var _ adapter.CacheFile = (*CacheFile)(nil)
  33. type CacheFile struct {
  34. ctx context.Context
  35. path string
  36. cacheID []byte
  37. storeFakeIP bool
  38. storeRDRC bool
  39. rdrcTimeout time.Duration
  40. DB *bbolt.DB
  41. resetAccess sync.Mutex
  42. saveMetadataTimer *time.Timer
  43. saveFakeIPAccess sync.RWMutex
  44. saveDomain map[netip.Addr]string
  45. saveAddress4 map[string]netip.Addr
  46. saveAddress6 map[string]netip.Addr
  47. saveRDRCAccess sync.RWMutex
  48. saveRDRC map[saveRDRCCacheKey]bool
  49. }
  50. type saveRDRCCacheKey struct {
  51. TransportName string
  52. QuestionName string
  53. QType uint16
  54. }
  55. func New(ctx context.Context, options option.CacheFileOptions) *CacheFile {
  56. var path string
  57. if options.Path != "" {
  58. path = options.Path
  59. } else {
  60. path = "cache.db"
  61. }
  62. var cacheIDBytes []byte
  63. if options.CacheID != "" {
  64. cacheIDBytes = append([]byte{0}, []byte(options.CacheID)...)
  65. }
  66. var rdrcTimeout time.Duration
  67. if options.StoreRDRC {
  68. if options.RDRCTimeout > 0 {
  69. rdrcTimeout = time.Duration(options.RDRCTimeout)
  70. } else {
  71. rdrcTimeout = 7 * 24 * time.Hour
  72. }
  73. }
  74. return &CacheFile{
  75. ctx: ctx,
  76. path: filemanager.BasePath(ctx, path),
  77. cacheID: cacheIDBytes,
  78. storeFakeIP: options.StoreFakeIP,
  79. storeRDRC: options.StoreRDRC,
  80. rdrcTimeout: rdrcTimeout,
  81. saveDomain: make(map[netip.Addr]string),
  82. saveAddress4: make(map[string]netip.Addr),
  83. saveAddress6: make(map[string]netip.Addr),
  84. saveRDRC: make(map[saveRDRCCacheKey]bool),
  85. }
  86. }
  87. func (c *CacheFile) Name() string {
  88. return "cache-file"
  89. }
  90. func (c *CacheFile) Dependencies() []string {
  91. return nil
  92. }
  93. func (c *CacheFile) Start(stage adapter.StartStage) error {
  94. if stage != adapter.StartStateInitialize {
  95. return nil
  96. }
  97. const fileMode = 0o666
  98. options := bbolt.Options{Timeout: time.Second}
  99. var (
  100. db *bbolt.DB
  101. err error
  102. )
  103. for i := 0; i < 10; i++ {
  104. db, err = bbolt.Open(c.path, fileMode, &options)
  105. if err == nil {
  106. break
  107. }
  108. if errors.Is(err, bboltErrors.ErrTimeout) {
  109. continue
  110. }
  111. if E.IsMulti(err, bboltErrors.ErrInvalid, bboltErrors.ErrChecksum, bboltErrors.ErrVersionMismatch) {
  112. rmErr := os.Remove(c.path)
  113. if rmErr != nil {
  114. return err
  115. }
  116. }
  117. time.Sleep(100 * time.Millisecond)
  118. }
  119. if err != nil {
  120. return err
  121. }
  122. err = filemanager.Chown(c.ctx, c.path)
  123. if err != nil {
  124. db.Close()
  125. return E.Cause(err, "platform chown")
  126. }
  127. err = db.Batch(func(tx *bbolt.Tx) error {
  128. return tx.ForEach(func(name []byte, b *bbolt.Bucket) error {
  129. if name[0] == 0 {
  130. return b.ForEachBucket(func(k []byte) error {
  131. bucketName := string(k)
  132. if !(common.Contains(bucketNameList, bucketName)) {
  133. _ = b.DeleteBucket(name)
  134. }
  135. return nil
  136. })
  137. } else {
  138. bucketName := string(name)
  139. if !(common.Contains(bucketNameList, bucketName) || strings.HasPrefix(bucketName, fakeipBucketPrefix)) {
  140. _ = tx.DeleteBucket(name)
  141. }
  142. }
  143. return nil
  144. })
  145. })
  146. if err != nil {
  147. db.Close()
  148. return err
  149. }
  150. c.DB = db
  151. return nil
  152. }
  153. func (c *CacheFile) Close() error {
  154. if c.DB == nil {
  155. return nil
  156. }
  157. return c.DB.Close()
  158. }
  159. func (c *CacheFile) view(fn func(tx *bbolt.Tx) error) (err error) {
  160. defer func() {
  161. if r := recover(); r != nil {
  162. c.resetDB()
  163. err = E.New("database corrupted: ", r)
  164. }
  165. }()
  166. return c.DB.View(fn)
  167. }
  168. func (c *CacheFile) batch(fn func(tx *bbolt.Tx) error) (err error) {
  169. defer func() {
  170. if r := recover(); r != nil {
  171. c.resetDB()
  172. err = E.New("database corrupted: ", r)
  173. }
  174. }()
  175. return c.DB.Batch(fn)
  176. }
  177. func (c *CacheFile) update(fn func(tx *bbolt.Tx) error) (err error) {
  178. defer func() {
  179. if r := recover(); r != nil {
  180. c.resetDB()
  181. err = E.New("database corrupted: ", r)
  182. }
  183. }()
  184. return c.DB.Update(fn)
  185. }
  186. func (c *CacheFile) resetDB() {
  187. c.resetAccess.Lock()
  188. defer c.resetAccess.Unlock()
  189. c.DB.Close()
  190. os.Remove(c.path)
  191. db, err := bbolt.Open(c.path, 0o666, &bbolt.Options{Timeout: time.Second})
  192. if err == nil {
  193. _ = filemanager.Chown(c.ctx, c.path)
  194. c.DB = db
  195. }
  196. }
  197. func (c *CacheFile) StoreFakeIP() bool {
  198. return c.storeFakeIP
  199. }
  200. func (c *CacheFile) LoadMode() string {
  201. var mode string
  202. c.view(func(t *bbolt.Tx) error {
  203. bucket := t.Bucket(bucketMode)
  204. if bucket == nil {
  205. return nil
  206. }
  207. var modeBytes []byte
  208. if len(c.cacheID) > 0 {
  209. modeBytes = bucket.Get(c.cacheID)
  210. } else {
  211. modeBytes = bucket.Get(cacheIDDefault)
  212. }
  213. mode = string(modeBytes)
  214. return nil
  215. })
  216. return mode
  217. }
  218. func (c *CacheFile) StoreMode(mode string) error {
  219. return c.batch(func(t *bbolt.Tx) error {
  220. bucket, err := t.CreateBucketIfNotExists(bucketMode)
  221. if err != nil {
  222. return err
  223. }
  224. if len(c.cacheID) > 0 {
  225. return bucket.Put(c.cacheID, []byte(mode))
  226. } else {
  227. return bucket.Put(cacheIDDefault, []byte(mode))
  228. }
  229. })
  230. }
  231. func (c *CacheFile) bucket(t *bbolt.Tx, key []byte) *bbolt.Bucket {
  232. if c.cacheID == nil {
  233. return t.Bucket(key)
  234. }
  235. bucket := t.Bucket(c.cacheID)
  236. if bucket == nil {
  237. return nil
  238. }
  239. return bucket.Bucket(key)
  240. }
  241. func (c *CacheFile) createBucket(t *bbolt.Tx, key []byte) (*bbolt.Bucket, error) {
  242. if c.cacheID == nil {
  243. return t.CreateBucketIfNotExists(key)
  244. }
  245. bucket, err := t.CreateBucketIfNotExists(c.cacheID)
  246. if bucket == nil {
  247. return nil, err
  248. }
  249. return bucket.CreateBucketIfNotExists(key)
  250. }
  251. func (c *CacheFile) LoadSelected(group string) string {
  252. var selected string
  253. c.view(func(t *bbolt.Tx) error {
  254. bucket := c.bucket(t, bucketSelected)
  255. if bucket == nil {
  256. return nil
  257. }
  258. selectedBytes := bucket.Get([]byte(group))
  259. if len(selectedBytes) > 0 {
  260. selected = string(selectedBytes)
  261. }
  262. return nil
  263. })
  264. return selected
  265. }
  266. func (c *CacheFile) StoreSelected(group, selected string) error {
  267. return c.batch(func(t *bbolt.Tx) error {
  268. bucket, err := c.createBucket(t, bucketSelected)
  269. if err != nil {
  270. return err
  271. }
  272. return bucket.Put([]byte(group), []byte(selected))
  273. })
  274. }
  275. func (c *CacheFile) LoadGroupExpand(group string) (isExpand bool, loaded bool) {
  276. c.view(func(t *bbolt.Tx) error {
  277. bucket := c.bucket(t, bucketExpand)
  278. if bucket == nil {
  279. return nil
  280. }
  281. expandBytes := bucket.Get([]byte(group))
  282. if len(expandBytes) == 1 {
  283. isExpand = expandBytes[0] == 1
  284. loaded = true
  285. }
  286. return nil
  287. })
  288. return
  289. }
  290. func (c *CacheFile) StoreGroupExpand(group string, isExpand bool) error {
  291. return c.batch(func(t *bbolt.Tx) error {
  292. bucket, err := c.createBucket(t, bucketExpand)
  293. if err != nil {
  294. return err
  295. }
  296. if isExpand {
  297. return bucket.Put([]byte(group), []byte{1})
  298. } else {
  299. return bucket.Put([]byte(group), []byte{0})
  300. }
  301. })
  302. }
  303. func (c *CacheFile) LoadRuleSet(tag string) *adapter.SavedBinary {
  304. var savedSet adapter.SavedBinary
  305. err := c.view(func(t *bbolt.Tx) error {
  306. bucket := c.bucket(t, bucketRuleSet)
  307. if bucket == nil {
  308. return os.ErrNotExist
  309. }
  310. setBinary := bucket.Get([]byte(tag))
  311. if len(setBinary) == 0 {
  312. return os.ErrInvalid
  313. }
  314. return savedSet.UnmarshalBinary(setBinary)
  315. })
  316. if err != nil {
  317. return nil
  318. }
  319. return &savedSet
  320. }
  321. func (c *CacheFile) SaveRuleSet(tag string, set *adapter.SavedBinary) error {
  322. return c.batch(func(t *bbolt.Tx) error {
  323. bucket, err := c.createBucket(t, bucketRuleSet)
  324. if err != nil {
  325. return err
  326. }
  327. setBinary, err := set.MarshalBinary()
  328. if err != nil {
  329. return err
  330. }
  331. return bucket.Put([]byte(tag), setBinary)
  332. })
  333. }