Fix WireGuard pause and sleep handling

This commit is contained in:
世界
2026-10-23 19:57:07 +08:00
parent af9dc0a51f
commit cc022098fd
4 changed files with 28 additions and 11 deletions
+2 -2
View File
@@ -45,7 +45,7 @@ require (
github.com/sagernet/netlink v0.0.0-20260814022025-64455d367bbf
github.com/sagernet/nftables v0.3.0-mod.4
github.com/sagernet/quic-go v0.61.0-sing-box-mod.12
github.com/sagernet/sing v0.9.7-0.20261023115459-0678b21952a5
github.com/sagernet/sing v0.9.7-0.20261023115459-5dc452e537bf
github.com/sagernet/sing-cloudflared v0.1.4-0.20261021152903-2d40d788fcf6
github.com/sagernet/sing-mux v0.3.10-0.20260929204512-caf09fe32475
github.com/sagernet/sing-openconnect v0.1.1-0.20260929151225-cbd68c45de58
@@ -60,7 +60,7 @@ require (
github.com/sagernet/sing-vmess v0.2.9-0.20261019223527-a0c58542bd9d
github.com/sagernet/smux v1.5.50-sing-box-mod.1
github.com/sagernet/tailscale v1.102.1-sing-box-1.14-mod.5.0.20261019070639-79c1732442a9
github.com/sagernet/wireguard-go v0.0.8-0.20261019230631-34ff6c5e8c94
github.com/sagernet/wireguard-go v0.0.8-0.20261022215841-e4d85ef2ed38
github.com/sagernet/ws v0.0.0-20231204124109-acfe8907c854
github.com/spf13/cobra v1.10.2
github.com/stretchr/testify v1.12.0
+4 -4
View File
@@ -318,8 +318,8 @@ github.com/sagernet/nftables v0.3.0-mod.4 h1:vnOtcDYeSXv2e5RoRuGH0lrpttQFJ8iC4IC
github.com/sagernet/nftables v0.3.0-mod.4/go.mod h1:8kslHG4VvYNihcco+i6uxIX7qbT8A56T0y5q7U44ZaQ=
github.com/sagernet/quic-go v0.61.0-sing-box-mod.12 h1:9/USaaiONHFeqBePb+4C/A0MBM2dmxED49UGBpHLHS4=
github.com/sagernet/quic-go v0.61.0-sing-box-mod.12/go.mod h1:hmLC8GJPp+BrpBgrgJnacvI4fZGIhasuy1ROJJ8GR4E=
github.com/sagernet/sing v0.9.7-0.20261023115459-0678b21952a5 h1:CWi0GHLvGQlSAGPoSLJYhiTNN/2tjAdTBHHozl4p3gw=
github.com/sagernet/sing v0.9.7-0.20261023115459-0678b21952a5/go.mod h1:K3Owt3xPhHugvlnlPPxZJ/exXdaJfEPOTNorGk4AXjo=
github.com/sagernet/sing v0.9.7-0.20261023115459-5dc452e537bf h1:qWlGERjsI0F1I9e1W5Z/ARk/9KEN7xIas6e2QHEypUM=
github.com/sagernet/sing v0.9.7-0.20261023115459-5dc452e537bf/go.mod h1:K3Owt3xPhHugvlnlPPxZJ/exXdaJfEPOTNorGk4AXjo=
github.com/sagernet/sing-cloudflared v0.1.4-0.20261021152903-2d40d788fcf6 h1:cedQ/AnIVZXNS0RbV5E6xXbLan7Qu8rOj7+B1kvb7fs=
github.com/sagernet/sing-cloudflared v0.1.4-0.20261021152903-2d40d788fcf6/go.mod h1:XEqEDYRCAYLaoPjZ1ifVWJg5iWAJHL2gOAXe/PM28Cg=
github.com/sagernet/sing-mux v0.3.10-0.20260929204512-caf09fe32475 h1:aG0DqHEOjax7DyfE1GTAXZotOg3QGVCz3/8rCSua2kg=
@@ -348,8 +348,8 @@ github.com/sagernet/smux v1.5.50-sing-box-mod.1 h1:XkJcivBC9V4wBjiGXIXZ229aZCU1h
github.com/sagernet/smux v1.5.50-sing-box-mod.1/go.mod h1:NjhsCEWedJm7eFLyhuBgIEzwfhRmytrUoiLluxs5Sk8=
github.com/sagernet/tailscale v1.102.1-sing-box-1.14-mod.5.0.20261019070639-79c1732442a9 h1:Phb4IDqGdP2Wg8VDComdDy2uMg1ZN3gs0kpJHp6C8O8=
github.com/sagernet/tailscale v1.102.1-sing-box-1.14-mod.5.0.20261019070639-79c1732442a9/go.mod h1:WLUSOPmTcf7VN9gLCe01qUSIvD+/cKC177neENyZPkI=
github.com/sagernet/wireguard-go v0.0.8-0.20261019230631-34ff6c5e8c94 h1:mgBlBilXpYZ7Qu15mrCDTEm5wwBjrdRHCF5gIhLLYbY=
github.com/sagernet/wireguard-go v0.0.8-0.20261019230631-34ff6c5e8c94/go.mod h1:er10sELpmzLXq7S7Pbc1Zsbyapcr+/gxNAHKTo6fzVA=
github.com/sagernet/wireguard-go v0.0.8-0.20261022215841-e4d85ef2ed38 h1:VruOdZAjG+nQi//y9YYL8Pf18+SymIkzBoxNt5gUzuY=
github.com/sagernet/wireguard-go v0.0.8-0.20261022215841-e4d85ef2ed38/go.mod h1:er10sELpmzLXq7S7Pbc1Zsbyapcr+/gxNAHKTo6fzVA=
github.com/sagernet/ws v0.0.0-20231204124109-acfe8907c854 h1:6uUiZcDRnZSAegryaUGwPC/Fj13JSHwiTftrXhMmYOc=
github.com/sagernet/ws v0.0.0-20231204124109-acfe8907c854/go.mod h1:LtfoSK3+NG57tvnVEHgcuBW9ujgE8enPSgzgwStwCAA=
github.com/smallstep/pkcs7 v0.1.1 h1:x+rPdt2W088V9Vkjho4KtoggyktZJlMduZAtRHm68LU=
+5 -3
View File
@@ -119,8 +119,11 @@ func (c *ClientBind) receive(packets [][]byte, sizes []int, eps []conn.Endpoint)
}
c.logger.Error(E.Cause(err, "connect to server"))
err = nil
c.pauseManager.WaitActive()
time.Sleep(time.Second)
c.pauseManager.WaitActiveContext(c.bindCtx)
select {
case <-c.done:
case <-time.After(time.Second):
}
return
}
n, addr, err := udpConn.ReadFrom(packets[0])
@@ -166,7 +169,6 @@ func (c *ClientBind) SetMark(mark uint32) error {
func (c *ClientBind) Send(bufs [][]byte, ep conn.Endpoint, offset int) error {
udpConn, err := c.connect()
if err != nil {
c.pauseManager.WaitActive()
time.Sleep(time.Second)
return err
}
+17 -2
View File
@@ -9,6 +9,7 @@ import (
"net/netip"
"os"
"strings"
"sync"
"github.com/sagernet/sing-box/common/dialer"
"github.com/sagernet/sing-box/service/powerreport"
@@ -38,6 +39,7 @@ type Endpoint struct {
egressPool *tun.UDPEgressPool
pause pause.Manager
pauseCallback *list.Element[pause.Callback]
stateAccess sync.Mutex
}
func NewEndpoint(options EndpointOptions) (*Endpoint, error) {
@@ -285,6 +287,8 @@ func (e *Endpoint) Close() error {
e.egressPool.Close()
e.egressPool = nil
}
e.stateAccess.Lock()
defer e.stateAccess.Unlock()
if e.device != nil {
e.device.Down()
e.device.Close()
@@ -310,9 +314,20 @@ func (e *Endpoint) BindUpdate() error {
func (e *Endpoint) onPauseUpdated(event int) {
switch event {
case pause.EventNetworkPause:
case pause.EventNetworkPause, pause.EventNetworkWake:
go e.updateNetworkPause()
}
}
func (e *Endpoint) updateNetworkPause() {
e.stateAccess.Lock()
defer e.stateAccess.Unlock()
if e.device == nil {
return
}
if e.pause.IsNetworkPaused() {
e.device.Down()
case pause.EventNetworkWake:
} else {
e.device.Up()
}
}