mirror of
https://github.com/SagerNet/sing-box.git
synced 2024-11-22 08:31:30 +00:00
Handle Windows power events
This commit is contained in:
parent
ee14135298
commit
11c7b4a866
|
@ -8,6 +8,7 @@ import (
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"os/user"
|
"os/user"
|
||||||
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -42,6 +43,7 @@ import (
|
||||||
serviceNTP "github.com/sagernet/sing/common/ntp"
|
serviceNTP "github.com/sagernet/sing/common/ntp"
|
||||||
"github.com/sagernet/sing/common/task"
|
"github.com/sagernet/sing/common/task"
|
||||||
"github.com/sagernet/sing/common/uot"
|
"github.com/sagernet/sing/common/uot"
|
||||||
|
"github.com/sagernet/sing/common/winpowrprof"
|
||||||
"github.com/sagernet/sing/service"
|
"github.com/sagernet/sing/service"
|
||||||
"github.com/sagernet/sing/service/pause"
|
"github.com/sagernet/sing/service/pause"
|
||||||
)
|
)
|
||||||
|
@ -85,6 +87,7 @@ type Router struct {
|
||||||
networkMonitor tun.NetworkUpdateMonitor
|
networkMonitor tun.NetworkUpdateMonitor
|
||||||
interfaceMonitor tun.DefaultInterfaceMonitor
|
interfaceMonitor tun.DefaultInterfaceMonitor
|
||||||
packageManager tun.PackageManager
|
packageManager tun.PackageManager
|
||||||
|
powerListener winpowrprof.EventListener
|
||||||
processSearcher process.Searcher
|
processSearcher process.Searcher
|
||||||
timeService *ntp.Service
|
timeService *ntp.Service
|
||||||
pauseManager pause.Manager
|
pauseManager pause.Manager
|
||||||
|
@ -321,6 +324,14 @@ func NewRouter(
|
||||||
router.interfaceMonitor = interfaceMonitor
|
router.interfaceMonitor = interfaceMonitor
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
powerListener, err := winpowrprof.NewEventListener(router.notifyWindowsPowerEvent)
|
||||||
|
if err != nil {
|
||||||
|
return nil, E.Cause(err, "initialize power listener")
|
||||||
|
}
|
||||||
|
router.powerListener = powerListener
|
||||||
|
}
|
||||||
|
|
||||||
if ntpOptions.Enabled {
|
if ntpOptions.Enabled {
|
||||||
timeService, err := ntp.NewService(ctx, router, logFactory.NewLogger("ntp"), ntpOptions)
|
timeService, err := ntp.NewService(ctx, router, logFactory.NewLogger("ntp"), ntpOptions)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -560,6 +571,16 @@ func (r *Router) Start() error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if r.powerListener != nil {
|
||||||
|
monitor.Start("start power listener")
|
||||||
|
err := r.powerListener.Start()
|
||||||
|
monitor.Finish()
|
||||||
|
if err != nil {
|
||||||
|
return E.Cause(err, "start power listener")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (needWIFIStateFromRuleSet || r.needWIFIState) && r.platformInterface != nil {
|
if (needWIFIStateFromRuleSet || r.needWIFIState) && r.platformInterface != nil {
|
||||||
monitor.Start("initialize WIFI state")
|
monitor.Start("initialize WIFI state")
|
||||||
r.needWIFIState = true
|
r.needWIFIState = true
|
||||||
|
@ -657,6 +678,13 @@ func (r *Router) Close() error {
|
||||||
})
|
})
|
||||||
monitor.Finish()
|
monitor.Finish()
|
||||||
}
|
}
|
||||||
|
if r.powerListener != nil {
|
||||||
|
monitor.Start("close power listener")
|
||||||
|
err = E.Append(err, r.powerListener.Close(), func(err error) error {
|
||||||
|
return E.Cause(err, "close power listener")
|
||||||
|
})
|
||||||
|
monitor.Finish()
|
||||||
|
}
|
||||||
if r.timeService != nil {
|
if r.timeService != nil {
|
||||||
monitor.Start("close time service")
|
monitor.Start("close time service")
|
||||||
err = E.Append(err, r.timeService.Close(), func(err error) error {
|
err = E.Append(err, r.timeService.Close(), func(err error) error {
|
||||||
|
@ -1189,3 +1217,19 @@ func (r *Router) updateWIFIState() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *Router) notifyWindowsPowerEvent(event int) {
|
||||||
|
switch event {
|
||||||
|
case winpowrprof.EVENT_SUSPEND:
|
||||||
|
r.pauseManager.DevicePause()
|
||||||
|
_ = r.ResetNetwork()
|
||||||
|
case winpowrprof.EVENT_RESUME:
|
||||||
|
if !r.pauseManager.IsDevicePaused() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fallthrough
|
||||||
|
case winpowrprof.EVENT_RESUME_AUTOMATIC:
|
||||||
|
r.pauseManager.DeviceWake()
|
||||||
|
_ = r.ResetNetwork()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue