From 51964801ffbe1288097f0091fba6f9d39054b4b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Thu, 6 Jun 2024 20:51:21 +0800 Subject: [PATCH] Fix enforced power listener on windows --- route/router.go | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/route/router.go b/route/router.go index dfced502..73845a01 100644 --- a/route/router.go +++ b/route/router.go @@ -352,14 +352,6 @@ func NewRouter( 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 { ntpDialer, err := dialer.New(router, ntpOptions.DialerOptions) if err != nil { @@ -589,6 +581,15 @@ func (r *Router) Start() error { } } + if runtime.GOOS == "windows" { + powerListener, err := winpowrprof.NewEventListener(r.notifyWindowsPowerEvent) + if err == nil { + r.powerListener = powerListener + } else { + r.logger.Warn("initialize power listener: ", err) + } + } + if r.powerListener != nil { monitor.Start("start power listener") err := r.powerListener.Start()