Skip to content

Commit

Permalink
Fix async FakeIP save
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Jul 8, 2023
1 parent 1c526fe commit cbf6133
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 10 deletions.
2 changes: 2 additions & 0 deletions adapter/fakeip.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/netip"

"github.com/sagernet/sing-dns"
"github.com/sagernet/sing/common/logger"
)

type FakeIPStore interface {
Expand All @@ -18,6 +19,7 @@ type FakeIPStorage interface {
FakeIPMetadata() *FakeIPMetadata
FakeIPSaveMetadata(metadata *FakeIPMetadata) error
FakeIPStore(address netip.Addr, domain string) error
FakeIPStoreAsync(address netip.Addr, domain string, logger logger.Logger)
FakeIPLoad(address netip.Addr) (string, bool)
FakeIPReset() error
}
Expand Down
14 changes: 11 additions & 3 deletions experimental/clashapi/cachefile/cache.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package cachefile

import (
"net/netip"
"os"
"sync"
"time"

"github.com/sagernet/sing-box/adapter"
Expand All @@ -14,8 +16,10 @@ var bucketSelected = []byte("selected")
var _ adapter.ClashCacheFile = (*CacheFile)(nil)

type CacheFile struct {
DB *bbolt.DB
cacheID []byte
DB *bbolt.DB
cacheID []byte
saveAccess sync.RWMutex
saveCache map[netip.Addr]string
}

func Open(path string, cacheID string) (*CacheFile, error) {
Expand All @@ -36,7 +40,11 @@ func Open(path string, cacheID string) (*CacheFile, error) {
if cacheID != "" {
cacheIDBytes = append([]byte{0}, []byte(cacheID)...)
}
return &CacheFile{db, cacheIDBytes}, nil
return &CacheFile{
DB: db,
cacheID: cacheIDBytes,
saveCache: make(map[netip.Addr]string),
}, nil
}

func (c *CacheFile) bucket(t *bbolt.Tx, key []byte) *bbolt.Bucket {
Expand Down
24 changes: 24 additions & 0 deletions experimental/clashapi/cachefile/fakeip.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"

"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing/common/logger"

"go.etcd.io/bbolt"
)
Expand All @@ -14,6 +15,8 @@ var (
keyMetadata = []byte("metadata")
)

var _ adapter.ClashCacheFile = (*CacheFile)(nil)

func (c *CacheFile) FakeIPMetadata() *adapter.FakeIPMetadata {
var metadata adapter.FakeIPMetadata
err := c.DB.View(func(tx *bbolt.Tx) error {
Expand Down Expand Up @@ -57,7 +60,28 @@ func (c *CacheFile) FakeIPStore(address netip.Addr, domain string) error {
})
}

func (c *CacheFile) FakeIPStoreAsync(address netip.Addr, domain string, logger logger.Logger) {
c.saveAccess.Lock()
c.saveCache[address] = domain
c.saveAccess.Unlock()
go func() {
err := c.FakeIPStore(address, domain)
if err != nil {
logger.Warn("save FakeIP address pair: ", err)
}
c.saveAccess.Lock()
delete(c.saveCache, address)
c.saveAccess.Unlock()
}()
}

func (c *CacheFile) FakeIPLoad(address netip.Addr) (string, bool) {
c.saveAccess.RLock()
cachedDomain, cached := c.saveCache[address]
c.saveAccess.RUnlock()
if cached {
return cachedDomain, true
}
var domain string
_ = c.DB.View(func(tx *bbolt.Tx) error {
bucket := tx.Bucket(bucketFakeIP)
Expand Down
2 changes: 1 addition & 1 deletion route/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ func NewRouter(
if fakeIPOptions.Inet6Range != nil {
inet6Range = fakeIPOptions.Inet6Range.Build()
}
router.fakeIPStore = fakeip.NewStore(router, router.logger, inet4Range, inet6Range)
router.fakeIPStore = fakeip.NewStore(router, inet4Range, inet6Range)

Check failure on line 255 in route/router.go

View workflow job for this annotation

GitHub Actions / cross (darwin-arm64, darwin, arm64)

not enough arguments in call to fakeip.NewStore

Check failure on line 255 in route/router.go

View workflow job for this annotation

GitHub Actions / cross (linux-armv7, linux, arm, 7)

not enough arguments in call to fakeip.NewStore

Check failure on line 255 in route/router.go

View workflow job for this annotation

GitHub Actions / cross (freebsd-amd64, freebsd, amd64, v1)

not enough arguments in call to fakeip.NewStore

Check failure on line 255 in route/router.go

View workflow job for this annotation

GitHub Actions / cross (freebsd-386, freebsd, 386)

not enough arguments in call to fakeip.NewStore

Check failure on line 255 in route/router.go

View workflow job for this annotation

GitHub Actions / cross (linux-mips-hardfloat, linux, mips, hardfloat)

not enough arguments in call to fakeip.NewStore

Check failure on line 255 in route/router.go

View workflow job for this annotation

GitHub Actions / cross (darwin-amd64, darwin, amd64, v1)

not enough arguments in call to fakeip.NewStore

Check failure on line 255 in route/router.go

View workflow job for this annotation

GitHub Actions / cross (linux-mipsel-hardfloat, linux, mipsle, hardfloat)

not enough arguments in call to fakeip.NewStore

Check failure on line 255 in route/router.go

View workflow job for this annotation

GitHub Actions / cross (linux-mips64el, linux, mips64le)

not enough arguments in call to fakeip.NewStore

Check failure on line 255 in route/router.go

View workflow job for this annotation

GitHub Actions / cross (linux-amd64, linux, amd64, v1)

not enough arguments in call to fakeip.NewStore

Check failure on line 255 in route/router.go

View workflow job for this annotation

GitHub Actions / Debug build

not enough arguments in call to fakeip.NewStore

Check failure on line 255 in route/router.go

View workflow job for this annotation

GitHub Actions / cross (linux-mips64, linux, mips64)

not enough arguments in call to fakeip.NewStore

Check failure on line 255 in route/router.go

View workflow job for this annotation

GitHub Actions / cross (linux-arm64, linux, arm64)

not enough arguments in call to fakeip.NewStore

Check failure on line 255 in route/router.go

View workflow job for this annotation

GitHub Actions / cross (linux-386, linux, 386)

not enough arguments in call to fakeip.NewStore

Check failure on line 255 in route/router.go

View workflow job for this annotation

GitHub Actions / cross (linux-armv6, linux, arm, 6)

not enough arguments in call to fakeip.NewStore

Check failure on line 255 in route/router.go

View workflow job for this annotation

GitHub Actions / cross (freebsd-arm64, freebsd, arm64)

not enough arguments in call to fakeip.NewStore

Check failure on line 255 in route/router.go

View workflow job for this annotation

GitHub Actions / cross (darwin-amd64-v3, darwin, amd64, v3)

not enough arguments in call to fakeip.NewStore

Check failure on line 255 in route/router.go

View workflow job for this annotation

GitHub Actions / cross (linux-amd64-v3, linux, amd64, v3)

not enough arguments in call to fakeip.NewStore

Check failure on line 255 in route/router.go

View workflow job for this annotation

GitHub Actions / cross (freebsd-amd64-v3, freebsd, amd64, v3)

not enough arguments in call to fakeip.NewStore

Check failure on line 255 in route/router.go

View workflow job for this annotation

GitHub Actions / cross (linux-armv5, linux, arm, 5)

not enough arguments in call to fakeip.NewStore

Check failure on line 255 in route/router.go

View workflow job for this annotation

GitHub Actions / Debug build (Go 1.18)

not enough arguments in call to fakeip.NewStore

Check failure on line 255 in route/router.go

View workflow job for this annotation

GitHub Actions / cross (linux-mipsel-softfloat, linux, mipsle, softfloat)

not enough arguments in call to fakeip.NewStore

Check failure on line 255 in route/router.go

View workflow job for this annotation

GitHub Actions / cross (windows-386, windows, 386)

not enough arguments in call to fakeip.NewStore

Check failure on line 255 in route/router.go

View workflow job for this annotation

GitHub Actions / cross (windows-arm32v7, windows, arm, 7)

not enough arguments in call to fakeip.NewStore

Check failure on line 255 in route/router.go

View workflow job for this annotation

GitHub Actions / cross (linux-s390x, linux, s390x)

not enough arguments in call to fakeip.NewStore

Check failure on line 255 in route/router.go

View workflow job for this annotation

GitHub Actions / cross (linux-mips-softfloat, linux, mips, softfloat)

not enough arguments in call to fakeip.NewStore

Check failure on line 255 in route/router.go

View workflow job for this annotation

GitHub Actions / cross (windows-arm64, windows, arm64)

not enough arguments in call to fakeip.NewStore

Check failure on line 255 in route/router.go

View workflow job for this annotation

GitHub Actions / cross (windows-amd64, windows, amd64, v1)

not enough arguments in call to fakeip.NewStore

Check failure on line 255 in route/router.go

View workflow job for this annotation

GitHub Actions / cross (windows-amd64-v3, windows, amd64, v3)

not enough arguments in call to fakeip.NewStore
}

usePlatformDefaultInterfaceMonitor := platformInterface != nil && platformInterface.UsePlatformDefaultInterfaceMonitor()
Expand Down
5 changes: 5 additions & 0 deletions transport/fakeip/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing/common/cache"
"github.com/sagernet/sing/common/logger"
)

var _ adapter.FakeIPStorage = (*MemoryStorage)(nil)
Expand Down Expand Up @@ -34,6 +35,10 @@ func (s *MemoryStorage) FakeIPStore(address netip.Addr, domain string) error {
return nil
}

func (s *MemoryStorage) FakeIPStoreAsync(address netip.Addr, domain string, logger logger.Logger) {
s.domainCache.Store(address, domain)
}

func (s *MemoryStorage) FakeIPLoad(address netip.Addr) (string, bool) {
return s.domainCache.Load(address)
}
Expand Down
7 changes: 1 addition & 6 deletions transport/fakeip/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,7 @@ func (s *Store) Create(domain string, strategy dns.DomainStrategy) (netip.Addr,
s.inet6Current = nextAddress
address = nextAddress
}
go func() {
err := s.storage.FakeIPStore(address, domain)
if err != nil {
s.logger.Warn("save FakeIP address pair: ", err)
}
}()
s.storage.FakeIPStoreAsync(address, domain, s.logger)
return address, nil
}

Expand Down

0 comments on commit cbf6133

Please sign in to comment.