mirror of
https://github.com/shtorm-7/sing-box-extended.git
synced 2026-12-01 14:18:36 +00:00
Fix WireGuard detour bind stalling until device wake
This commit is contained in:
@@ -60,7 +60,7 @@ require (
|
||||
github.com/sagernet/sing-vmess v0.2.9-0.20261124075649-4acc92a6f6c6
|
||||
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.20261022215841-e4d85ef2ed38
|
||||
github.com/sagernet/wireguard-go v0.0.8-0.20261125120826-afd160a056dc
|
||||
github.com/sagernet/ws v0.0.0-20231204124109-acfe8907c854
|
||||
github.com/spf13/cobra v1.10.2
|
||||
github.com/stretchr/testify v1.12.0
|
||||
|
||||
@@ -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.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/wireguard-go v0.0.8-0.20261125120826-afd160a056dc h1:ZRkrL6O1dSoCQxYY1URAMyyNZEuomM7DpAYJtpenG18=
|
||||
github.com/sagernet/wireguard-go v0.0.8-0.20261125120826-afd160a056dc/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,6 +5,7 @@ import (
|
||||
"net"
|
||||
"net/netip"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/sagernet/sing/common"
|
||||
@@ -13,8 +14,6 @@ import (
|
||||
"github.com/sagernet/sing/common/logger"
|
||||
M "github.com/sagernet/sing/common/metadata"
|
||||
N "github.com/sagernet/sing/common/network"
|
||||
"github.com/sagernet/sing/service"
|
||||
"github.com/sagernet/sing/service/pause"
|
||||
"github.com/sagernet/wireguard-go/conn"
|
||||
)
|
||||
|
||||
@@ -23,14 +22,13 @@ var _ conn.Bind = (*ClientBind)(nil)
|
||||
type ClientBind struct {
|
||||
ctx context.Context
|
||||
logger logger.Logger
|
||||
pauseManager pause.Manager
|
||||
bindCtx context.Context
|
||||
bindDone context.CancelFunc
|
||||
dialer N.Dialer
|
||||
reservedAccess sync.RWMutex
|
||||
reservedForEndpoint map[netip.AddrPort][3]uint8
|
||||
connAccess sync.Mutex
|
||||
conn *wireConn
|
||||
conn atomic.Pointer[wireConn]
|
||||
done chan struct{}
|
||||
isConnect bool
|
||||
connectAddr netip.AddrPort
|
||||
@@ -41,7 +39,6 @@ func NewClientBind(ctx context.Context, logger logger.Logger, dialer N.Dialer, i
|
||||
return &ClientBind{
|
||||
ctx: ctx,
|
||||
logger: logger,
|
||||
pauseManager: service.FromContext[pause.Manager](ctx),
|
||||
dialer: dialer,
|
||||
reservedForEndpoint: make(map[netip.AddrPort][3]uint8),
|
||||
done: make(chan struct{}),
|
||||
@@ -52,11 +49,10 @@ func NewClientBind(ctx context.Context, logger logger.Logger, dialer N.Dialer, i
|
||||
}
|
||||
|
||||
func (c *ClientBind) connect() (*wireConn, error) {
|
||||
serverConn := c.conn
|
||||
serverConn := c.conn.Load()
|
||||
if serverConn != nil {
|
||||
select {
|
||||
case <-serverConn.done:
|
||||
serverConn = nil
|
||||
default:
|
||||
return serverConn, nil
|
||||
}
|
||||
@@ -68,11 +64,10 @@ func (c *ClientBind) connect() (*wireConn, error) {
|
||||
return nil, net.ErrClosed
|
||||
default:
|
||||
}
|
||||
serverConn = c.conn
|
||||
serverConn = c.conn.Load()
|
||||
if serverConn != nil {
|
||||
select {
|
||||
case <-serverConn.done:
|
||||
serverConn = nil
|
||||
default:
|
||||
return serverConn, nil
|
||||
}
|
||||
@@ -82,7 +77,7 @@ func (c *ClientBind) connect() (*wireConn, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c.conn = &wireConn{
|
||||
serverConn = &wireConn{
|
||||
PacketConn: bufio.NewUnbindPacketConn(udpConn),
|
||||
done: make(chan struct{}),
|
||||
}
|
||||
@@ -91,12 +86,13 @@ func (c *ClientBind) connect() (*wireConn, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c.conn = &wireConn{
|
||||
serverConn = &wireConn{
|
||||
PacketConn: bufio.NewPacketConn(udpConn),
|
||||
done: make(chan struct{}),
|
||||
}
|
||||
}
|
||||
return c.conn, nil
|
||||
c.conn.Store(serverConn)
|
||||
return serverConn, nil
|
||||
}
|
||||
|
||||
func (c *ClientBind) Open(port uint16) (fns []conn.ReceiveFunc, actualPort uint16, err error) {
|
||||
@@ -119,7 +115,6 @@ func (c *ClientBind) receive(packets [][]byte, sizes []int, eps []conn.Endpoint)
|
||||
}
|
||||
c.logger.Error(E.Cause(err, "connect to server"))
|
||||
err = nil
|
||||
c.pauseManager.WaitActiveContext(c.bindCtx)
|
||||
select {
|
||||
case <-c.done:
|
||||
case <-time.After(time.Second):
|
||||
@@ -158,7 +153,7 @@ func (c *ClientBind) Close() error {
|
||||
}
|
||||
c.connAccess.Lock()
|
||||
defer c.connAccess.Unlock()
|
||||
common.Close(common.PtrOrNil(c.conn))
|
||||
common.Close(common.PtrOrNil(c.conn.Load()))
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -186,7 +181,7 @@ func (c *ClientBind) Send(bufs [][]byte, ep conn.Endpoint, offset int) error {
|
||||
}
|
||||
copy(buf[1:4], reserved[:])
|
||||
}
|
||||
_, err = udpConn.WriteToUDPAddrPort(buf, destination)
|
||||
_, err = udpConn.WriteTo(buf, M.SocksaddrFromNetIP(destination).UDPAddr())
|
||||
if err != nil {
|
||||
udpConn.Close()
|
||||
return err
|
||||
@@ -215,18 +210,10 @@ func (c *ClientBind) SetReservedForEndpoint(destination netip.AddrPort, reserved
|
||||
|
||||
type wireConn struct {
|
||||
net.PacketConn
|
||||
conn net.Conn
|
||||
access sync.Mutex
|
||||
done chan struct{}
|
||||
}
|
||||
|
||||
func (w *wireConn) WriteToUDPAddrPort(b []byte, addr netip.AddrPort) (int, error) {
|
||||
if w.conn != nil {
|
||||
return w.conn.Write(b)
|
||||
}
|
||||
return w.PacketConn.WriteTo(b, M.SocksaddrFromNetIP(addr).UDPAddr())
|
||||
}
|
||||
|
||||
func (w *wireConn) Close() error {
|
||||
w.access.Lock()
|
||||
defer w.access.Unlock()
|
||||
|
||||
Reference in New Issue
Block a user