mirror of
https://github.com/SagerNet/sing-box.git
synced 2025-06-13 21:54:13 +08:00
platform: Add sleep support for NetworkExtension
This commit is contained in:
parent
8d629ef323
commit
1983f54907
43
common/sleep/manager.go
Normal file
43
common/sleep/manager.go
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
package sleep
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Manager struct {
|
||||||
|
access sync.Mutex
|
||||||
|
done chan struct{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewManager() *Manager {
|
||||||
|
closedChan := make(chan struct{})
|
||||||
|
close(closedChan)
|
||||||
|
return &Manager{
|
||||||
|
done: closedChan,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Manager) Sleep() {
|
||||||
|
m.access.Lock()
|
||||||
|
defer m.access.Unlock()
|
||||||
|
select {
|
||||||
|
case <-m.done:
|
||||||
|
default:
|
||||||
|
return
|
||||||
|
}
|
||||||
|
m.done = make(chan struct{})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Manager) Wake() {
|
||||||
|
m.access.Lock()
|
||||||
|
defer m.access.Unlock()
|
||||||
|
select {
|
||||||
|
case <-m.done:
|
||||||
|
default:
|
||||||
|
close(m.done)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Manager) Active() <-chan struct{} {
|
||||||
|
return m.done
|
||||||
|
}
|
@ -8,6 +8,7 @@ import (
|
|||||||
"github.com/sagernet/sing-box"
|
"github.com/sagernet/sing-box"
|
||||||
"github.com/sagernet/sing-box/adapter"
|
"github.com/sagernet/sing-box/adapter"
|
||||||
"github.com/sagernet/sing-box/common/process"
|
"github.com/sagernet/sing-box/common/process"
|
||||||
|
"github.com/sagernet/sing-box/common/sleep"
|
||||||
"github.com/sagernet/sing-box/common/urltest"
|
"github.com/sagernet/sing-box/common/urltest"
|
||||||
"github.com/sagernet/sing-box/experimental/libbox/internal/procfs"
|
"github.com/sagernet/sing-box/experimental/libbox/internal/procfs"
|
||||||
"github.com/sagernet/sing-box/experimental/libbox/platform"
|
"github.com/sagernet/sing-box/experimental/libbox/platform"
|
||||||
@ -25,6 +26,7 @@ type BoxService struct {
|
|||||||
ctx context.Context
|
ctx context.Context
|
||||||
cancel context.CancelFunc
|
cancel context.CancelFunc
|
||||||
instance *box.Box
|
instance *box.Box
|
||||||
|
sleepManager *sleep.Manager
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewService(configContent string, platformInterface PlatformInterface) (*BoxService, error) {
|
func NewService(configContent string, platformInterface PlatformInterface) (*BoxService, error) {
|
||||||
@ -35,6 +37,8 @@ func NewService(configContent string, platformInterface PlatformInterface) (*Box
|
|||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
ctx = filemanager.WithDefault(ctx, sWorkingPath, sTempPath, sUserID, sGroupID)
|
ctx = filemanager.WithDefault(ctx, sWorkingPath, sTempPath, sUserID, sGroupID)
|
||||||
ctx = service.ContextWithPtr(ctx, urltest.NewHistoryStorage())
|
ctx = service.ContextWithPtr(ctx, urltest.NewHistoryStorage())
|
||||||
|
sleepManager := sleep.NewManager()
|
||||||
|
ctx = service.ContextWithPtr(ctx, sleepManager)
|
||||||
instance, err := box.New(box.Options{
|
instance, err := box.New(box.Options{
|
||||||
Context: ctx,
|
Context: ctx,
|
||||||
Options: options,
|
Options: options,
|
||||||
@ -48,6 +52,7 @@ func NewService(configContent string, platformInterface PlatformInterface) (*Box
|
|||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
cancel: cancel,
|
cancel: cancel,
|
||||||
instance: instance,
|
instance: instance,
|
||||||
|
sleepManager: sleepManager,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -60,6 +65,15 @@ func (s *BoxService) Close() error {
|
|||||||
return s.instance.Close()
|
return s.instance.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *BoxService) Sleep() {
|
||||||
|
s.sleepManager.Sleep()
|
||||||
|
_ = s.instance.Router().ResetNetwork()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *BoxService) Wake() {
|
||||||
|
s.sleepManager.Wake()
|
||||||
|
}
|
||||||
|
|
||||||
var _ platform.Interface = (*platformInterfaceWrapper)(nil)
|
var _ platform.Interface = (*platformInterfaceWrapper)(nil)
|
||||||
|
|
||||||
type platformInterfaceWrapper struct {
|
type platformInterfaceWrapper struct {
|
||||||
|
@ -8,6 +8,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/sagernet/sing-box/adapter"
|
"github.com/sagernet/sing-box/adapter"
|
||||||
|
"github.com/sagernet/sing-box/common/sleep"
|
||||||
"github.com/sagernet/sing-box/common/urltest"
|
"github.com/sagernet/sing-box/common/urltest"
|
||||||
C "github.com/sagernet/sing-box/constant"
|
C "github.com/sagernet/sing-box/constant"
|
||||||
"github.com/sagernet/sing-box/log"
|
"github.com/sagernet/sing-box/log"
|
||||||
@ -152,6 +153,7 @@ type URLTestGroup struct {
|
|||||||
tolerance uint16
|
tolerance uint16
|
||||||
history *urltest.HistoryStorage
|
history *urltest.HistoryStorage
|
||||||
checking atomic.Bool
|
checking atomic.Bool
|
||||||
|
sleepManager *sleep.Manager
|
||||||
|
|
||||||
access sync.Mutex
|
access sync.Mutex
|
||||||
ticker *time.Ticker
|
ticker *time.Ticker
|
||||||
@ -182,6 +184,7 @@ func NewURLTestGroup(ctx context.Context, router adapter.Router, logger log.Logg
|
|||||||
tolerance: tolerance,
|
tolerance: tolerance,
|
||||||
history: history,
|
history: history,
|
||||||
close: make(chan struct{}),
|
close: make(chan struct{}),
|
||||||
|
sleepManager: service.PtrFromContext[sleep.Manager](ctx),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -263,6 +266,9 @@ func (g *URLTestGroup) Fallback(used adapter.Outbound) []adapter.Outbound {
|
|||||||
func (g *URLTestGroup) loopCheck() {
|
func (g *URLTestGroup) loopCheck() {
|
||||||
go g.CheckOutbounds(true)
|
go g.CheckOutbounds(true)
|
||||||
for {
|
for {
|
||||||
|
if g.sleepManager != nil {
|
||||||
|
<-g.sleepManager.Active()
|
||||||
|
}
|
||||||
select {
|
select {
|
||||||
case <-g.close:
|
case <-g.close:
|
||||||
return
|
return
|
||||||
|
Loading…
x
Reference in New Issue
Block a user