mirror of
https://github.com/shtorm-7/sing-box-extended.git
synced 2026-06-26 04:13:13 +03:00
platform: Add sleep support for NetworkExtension
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"github.com/sagernet/sing-box"
|
||||
"github.com/sagernet/sing-box/adapter"
|
||||
"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/experimental/libbox/internal/procfs"
|
||||
"github.com/sagernet/sing-box/experimental/libbox/platform"
|
||||
@@ -22,9 +23,10 @@ import (
|
||||
)
|
||||
|
||||
type BoxService struct {
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
instance *box.Box
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
instance *box.Box
|
||||
sleepManager *sleep.Manager
|
||||
}
|
||||
|
||||
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 = filemanager.WithDefault(ctx, sWorkingPath, sTempPath, sUserID, sGroupID)
|
||||
ctx = service.ContextWithPtr(ctx, urltest.NewHistoryStorage())
|
||||
sleepManager := sleep.NewManager()
|
||||
ctx = service.ContextWithPtr(ctx, sleepManager)
|
||||
instance, err := box.New(box.Options{
|
||||
Context: ctx,
|
||||
Options: options,
|
||||
@@ -45,9 +49,10 @@ func NewService(configContent string, platformInterface PlatformInterface) (*Box
|
||||
return nil, E.Cause(err, "create service")
|
||||
}
|
||||
return &BoxService{
|
||||
ctx: ctx,
|
||||
cancel: cancel,
|
||||
instance: instance,
|
||||
ctx: ctx,
|
||||
cancel: cancel,
|
||||
instance: instance,
|
||||
sleepManager: sleepManager,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -60,6 +65,15 @@ func (s *BoxService) Close() error {
|
||||
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)
|
||||
|
||||
type platformInterfaceWrapper struct {
|
||||
|
||||
Reference in New Issue
Block a user