refactor: WARP

This commit is contained in:
Sergei Maklagin
2025-06-08 19:51:17 +03:00
parent 5c911c97d8
commit 4e74a4108d
12 changed files with 346 additions and 253 deletions

View File

@@ -43,6 +43,7 @@ type CacheFile struct {
cacheID []byte
storeFakeIP bool
storeRDRC bool
storeWARPConfig bool
rdrcTimeout time.Duration
DB *bbolt.DB
saveMetadataTimer *time.Timer
@@ -80,16 +81,17 @@ func New(ctx context.Context, options option.CacheFileOptions) *CacheFile {
}
}
return &CacheFile{
ctx: ctx,
path: filemanager.BasePath(ctx, path),
cacheID: cacheIDBytes,
storeFakeIP: options.StoreFakeIP,
storeRDRC: options.StoreRDRC,
rdrcTimeout: rdrcTimeout,
saveDomain: make(map[netip.Addr]string),
saveAddress4: make(map[string]netip.Addr),
saveAddress6: make(map[string]netip.Addr),
saveRDRC: make(map[saveRDRCCacheKey]bool),
ctx: ctx,
path: filemanager.BasePath(ctx, path),
cacheID: cacheIDBytes,
storeFakeIP: options.StoreFakeIP,
storeRDRC: options.StoreRDRC,
storeWARPConfig: options.StoreWARPConfig,
rdrcTimeout: rdrcTimeout,
saveDomain: make(map[netip.Addr]string),
saveAddress4: make(map[string]netip.Addr),
saveAddress6: make(map[string]netip.Addr),
saveRDRC: make(map[saveRDRCCacheKey]bool),
}
}
@@ -317,35 +319,39 @@ func (c *CacheFile) SaveRuleSet(tag string, set *adapter.SavedBinary) error {
})
}
func (c *CacheFile) LoadCloudflareProfile(tag string) *adapter.SavedBinary {
var savedProfile adapter.SavedBinary
func (c *CacheFile) StoreWARPConfig() bool {
return c.storeWARPConfig
}
func (c *CacheFile) LoadWARPConfig(tag string) *adapter.SavedBinary {
var savedConfig adapter.SavedBinary
err := c.DB.View(func(t *bbolt.Tx) error {
bucket := c.bucket(t, bucketRuleSet)
if bucket == nil {
return os.ErrNotExist
}
profileBinary := bucket.Get([]byte(tag))
if len(profileBinary) == 0 {
configBinary := bucket.Get([]byte(tag))
if len(configBinary) == 0 {
return os.ErrInvalid
}
return savedProfile.UnmarshalBinary(profileBinary)
return savedConfig.UnmarshalBinary(configBinary)
})
if err != nil {
return nil
}
return &savedProfile
return &savedConfig
}
func (c *CacheFile) SaveCloudflareProfile(tag string, set *adapter.SavedBinary) error {
func (c *CacheFile) SaveWARPConfig(tag string, set *adapter.SavedBinary) error {
return c.DB.Batch(func(t *bbolt.Tx) error {
bucket, err := c.createBucket(t, bucketRuleSet)
if err != nil {
return err
}
profileBinary, err := set.MarshalBinary()
configBinary, err := set.MarshalBinary()
if err != nil {
return err
}
return bucket.Put([]byte(tag), profileBinary)
return bucket.Put([]byte(tag), configBinary)
})
}