mirror of
https://github.com/shtorm-7/sing-box-extended.git
synced 2026-12-04 15:48:38 +00:00
Reserve FakeIP addresses before allocation
This commit is contained in:
@@ -54,9 +54,6 @@ type CacheFile struct {
|
||||
optimisticTimeout time.Duration
|
||||
DB *bbolt.DB
|
||||
dbAccess sync.RWMutex
|
||||
saveMetadataAccess sync.Mutex
|
||||
saveMetadata *adapter.FakeIPMetadata
|
||||
saveMetadataTimer *time.Timer
|
||||
saveFakeIPAccess sync.RWMutex
|
||||
saveDomain map[netip.Addr]string
|
||||
saveAddress4 map[string]netip.Addr
|
||||
|
||||
@@ -4,11 +4,9 @@ import (
|
||||
"bytes"
|
||||
"net/netip"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/sagernet/bbolt"
|
||||
"github.com/sagernet/sing-box/adapter"
|
||||
C "github.com/sagernet/sing-box/constant"
|
||||
"github.com/sagernet/sing/common/logger"
|
||||
M "github.com/sagernet/sing/common/metadata"
|
||||
)
|
||||
@@ -24,7 +22,7 @@ var (
|
||||
|
||||
func (c *CacheFile) FakeIPMetadata() *adapter.FakeIPMetadata {
|
||||
var metadata adapter.FakeIPMetadata
|
||||
err := c.batch(func(tx *bbolt.Tx) error {
|
||||
err := c.view(func(tx *bbolt.Tx) error {
|
||||
bucket := tx.Bucket(bucketFakeIP)
|
||||
if bucket == nil {
|
||||
return os.ErrNotExist
|
||||
@@ -33,10 +31,6 @@ func (c *CacheFile) FakeIPMetadata() *adapter.FakeIPMetadata {
|
||||
if len(metadataBinary) == 0 {
|
||||
return os.ErrInvalid
|
||||
}
|
||||
err := bucket.Delete(keyMetadata)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return metadata.UnmarshalBinary(metadataBinary)
|
||||
})
|
||||
if err != nil {
|
||||
@@ -47,34 +41,78 @@ func (c *CacheFile) FakeIPMetadata() *adapter.FakeIPMetadata {
|
||||
|
||||
func (c *CacheFile) FakeIPSaveMetadata(metadata *adapter.FakeIPMetadata) error {
|
||||
return c.batch(func(tx *bbolt.Tx) error {
|
||||
bucket, err := tx.CreateBucketIfNotExists(bucketFakeIP)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
metadataBinary, err := metadata.MarshalBinary()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return bucket.Put(keyMetadata, metadataBinary)
|
||||
return putFakeIPMetadata(tx, metadata)
|
||||
})
|
||||
}
|
||||
|
||||
func (c *CacheFile) FakeIPSaveMetadataAsync(metadata *adapter.FakeIPMetadata) {
|
||||
c.saveMetadataAccess.Lock()
|
||||
defer c.saveMetadataAccess.Unlock()
|
||||
c.saveMetadata = metadata
|
||||
if c.saveMetadataTimer == nil {
|
||||
c.saveMetadataTimer = time.AfterFunc(C.FakeIPMetadataSaveInterval, func() {
|
||||
c.saveMetadataAccess.Lock()
|
||||
savedMetadata := c.saveMetadata
|
||||
c.saveMetadataAccess.Unlock()
|
||||
if savedMetadata != nil {
|
||||
_ = c.FakeIPSaveMetadata(savedMetadata)
|
||||
}
|
||||
})
|
||||
} else {
|
||||
c.saveMetadataTimer.Reset(C.FakeIPMetadataSaveInterval)
|
||||
func (c *CacheFile) FakeIPReserve(metadata *adapter.FakeIPMetadata, current netip.Addr, reservedEnd netip.Addr) error {
|
||||
return c.batch(func(tx *bbolt.Tx) error {
|
||||
err := putFakeIPMetadata(tx, metadata)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return deleteFakeIPRange(tx, current, reservedEnd)
|
||||
})
|
||||
}
|
||||
|
||||
func putFakeIPMetadata(tx *bbolt.Tx, metadata *adapter.FakeIPMetadata) error {
|
||||
bucket, err := tx.CreateBucketIfNotExists(bucketFakeIP)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
metadataBinary, err := metadata.MarshalBinary()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return bucket.Put(keyMetadata, metadataBinary)
|
||||
}
|
||||
|
||||
func deleteFakeIPRange(tx *bbolt.Tx, current netip.Addr, reservedEnd netip.Addr) error {
|
||||
bucket := tx.Bucket(bucketFakeIP)
|
||||
if bucket == nil {
|
||||
return nil
|
||||
}
|
||||
var domainBucket *bbolt.Bucket
|
||||
if current.Is4() {
|
||||
domainBucket = tx.Bucket(bucketFakeIPDomain4)
|
||||
} else {
|
||||
domainBucket = tx.Bucket(bucketFakeIPDomain6)
|
||||
}
|
||||
from := current.AsSlice()
|
||||
to := reservedEnd.AsSlice()
|
||||
wrapped := bytes.Compare(to, from) <= 0
|
||||
var addresses, domains [][]byte
|
||||
cursor := bucket.Cursor()
|
||||
for key, value := cursor.Seek(from); key != nil; key, value = cursor.Next() {
|
||||
if !wrapped && bytes.Compare(key, to) > 0 {
|
||||
break
|
||||
}
|
||||
if len(key) == len(from) && !bytes.Equal(key, from) {
|
||||
addresses = append(addresses, key)
|
||||
domains = append(domains, value)
|
||||
}
|
||||
}
|
||||
if wrapped {
|
||||
for key, value := cursor.First(); key != nil && bytes.Compare(key, to) <= 0; key, value = cursor.Next() {
|
||||
if len(key) == len(from) {
|
||||
addresses = append(addresses, key)
|
||||
domains = append(domains, value)
|
||||
}
|
||||
}
|
||||
}
|
||||
for i, address := range addresses {
|
||||
err := bucket.Delete(address)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if domainBucket != nil && bytes.Equal(domainBucket.Get(domains[i]), address) {
|
||||
err = domainBucket.Delete(domains[i])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *CacheFile) FakeIPStore(address netip.Addr, domain string) error {
|
||||
|
||||
Reference in New Issue
Block a user