wireguard: Fix events chan leak

This commit is contained in:
世界 2024-08-26 14:01:32 +08:00
parent ceda5cc95d
commit 9bac18bcd1
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
3 changed files with 54 additions and 52 deletions

View file

@ -152,6 +152,10 @@ func (w *WireGuard) start() error {
} }
bind = wireguard.NewClientBind(w.ctx, w, w.listener, isConnect, connectAddr, reserved) bind = wireguard.NewClientBind(w.ctx, w, w.listener, isConnect, connectAddr, reserved)
} }
err = w.tunDevice.Start()
if err != nil {
return err
}
wgDevice := device.NewDevice(w.tunDevice, bind, &device.Logger{ wgDevice := device.NewDevice(w.tunDevice, bind, &device.Logger{
Verbosef: func(format string, args ...interface{}) { Verbosef: func(format string, args ...interface{}) {
w.logger.Debug(fmt.Sprintf(strings.ToLower(format), args...)) w.logger.Debug(fmt.Sprintf(strings.ToLower(format), args...))
@ -170,7 +174,7 @@ func (w *WireGuard) start() error {
} }
w.device = wgDevice w.device = wgDevice
w.pauseCallback = w.pauseManager.RegisterCallback(w.onPauseUpdated) w.pauseCallback = w.pauseManager.RegisterCallback(w.onPauseUpdated)
return w.tunDevice.Start() return nil
} }
func (w *WireGuard) Close() error { func (w *WireGuard) Close() error {
@ -180,7 +184,6 @@ func (w *WireGuard) Close() error {
if w.pauseCallback != nil { if w.pauseCallback != nil {
w.pauseManager.UnregisterCallback(w.pauseCallback) w.pauseManager.UnregisterCallback(w.pauseCallback)
} }
w.tunDevice.Close()
return nil return nil
} }

View file

@ -230,17 +230,13 @@ func (w *StackDevice) Events() <-chan wgTun.Event {
} }
func (w *StackDevice) Close() error { func (w *StackDevice) Close() error {
select { close(w.done)
case <-w.done: close(w.events)
return os.ErrClosed
default:
}
w.stack.Close() w.stack.Close()
for _, endpoint := range w.stack.CleanupEndpoints() { for _, endpoint := range w.stack.CleanupEndpoints() {
endpoint.Abort() endpoint.Abort()
} }
w.stack.Wait() w.stack.Wait()
close(w.done)
return nil return nil
} }

View file

@ -6,6 +6,7 @@ import (
"net" "net"
"net/netip" "net/netip"
"os" "os"
"sync"
"github.com/sagernet/sing-box/adapter" "github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/common/dialer" "github.com/sagernet/sing-box/common/dialer"
@ -25,10 +26,12 @@ type SystemDevice struct {
device tun.Tun device tun.Tun
batchDevice tun.LinuxTUN batchDevice tun.LinuxTUN
name string name string
mtu int mtu uint32
inet4Addresses []netip.Prefix
inet6Addresses []netip.Prefix
gso bool
events chan wgTun.Event events chan wgTun.Event
addr4 netip.Addr closeOnce sync.Once
addr6 netip.Addr
} }
func NewSystemDevice(router adapter.Router, interfaceName string, localPrefixes []netip.Prefix, mtu uint32, gso bool) (*SystemDevice, error) { func NewSystemDevice(router adapter.Router, interfaceName string, localPrefixes []netip.Prefix, mtu uint32, gso bool) (*SystemDevice, error) {
@ -44,43 +47,17 @@ func NewSystemDevice(router adapter.Router, interfaceName string, localPrefixes
if interfaceName == "" { if interfaceName == "" {
interfaceName = tun.CalculateInterfaceName("wg") interfaceName = tun.CalculateInterfaceName("wg")
} }
tunInterface, err := tun.New(tun.Options{
Name: interfaceName,
Inet4Address: inet4Addresses,
Inet6Address: inet6Addresses,
MTU: mtu,
GSO: gso,
})
if err != nil {
return nil, err
}
var inet4Address netip.Addr
var inet6Address netip.Addr
if len(inet4Addresses) > 0 {
inet4Address = inet4Addresses[0].Addr()
}
if len(inet6Addresses) > 0 {
inet6Address = inet6Addresses[0].Addr()
}
var batchDevice tun.LinuxTUN
if gso {
batchTUN, isBatchTUN := tunInterface.(tun.LinuxTUN)
if !isBatchTUN {
return nil, E.New("GSO is not supported on current platform")
}
batchDevice = batchTUN
}
return &SystemDevice{ return &SystemDevice{
dialer: common.Must1(dialer.NewDefault(router, option.DialerOptions{ dialer: common.Must1(dialer.NewDefault(router, option.DialerOptions{
BindInterface: interfaceName, BindInterface: interfaceName,
})), })),
device: tunInterface,
batchDevice: batchDevice,
name: interfaceName, name: interfaceName,
mtu: int(mtu), mtu: mtu,
inet4Addresses: inet4Addresses,
inet6Addresses: inet6Addresses,
gso: gso,
events: make(chan wgTun.Event), events: make(chan wgTun.Event),
addr4: inet4Address,
addr6: inet6Address,
}, nil }, nil
} }
@ -93,14 +70,39 @@ func (w *SystemDevice) ListenPacket(ctx context.Context, destination M.Socksaddr
} }
func (w *SystemDevice) Inet4Address() netip.Addr { func (w *SystemDevice) Inet4Address() netip.Addr {
return w.addr4 if len(w.inet4Addresses) == 0 {
return netip.Addr{}
}
return w.inet4Addresses[0].Addr()
} }
func (w *SystemDevice) Inet6Address() netip.Addr { func (w *SystemDevice) Inet6Address() netip.Addr {
return w.addr6 if len(w.inet6Addresses) == 0 {
return netip.Addr{}
}
return w.inet6Addresses[0].Addr()
} }
func (w *SystemDevice) Start() error { func (w *SystemDevice) Start() error {
tunInterface, err := tun.New(tun.Options{
Name: w.name,
Inet4Address: w.inet4Addresses,
Inet6Address: w.inet6Addresses,
MTU: w.mtu,
GSO: w.gso,
})
if err != nil {
return err
}
w.device = tunInterface
if w.gso {
batchTUN, isBatchTUN := tunInterface.(tun.LinuxTUN)
if !isBatchTUN {
tunInterface.Close()
return E.New("GSO is not supported on current platform")
}
w.batchDevice = batchTUN
}
w.events <- wgTun.EventUp w.events <- wgTun.EventUp
return nil return nil
} }
@ -143,7 +145,7 @@ func (w *SystemDevice) Flush() error {
} }
func (w *SystemDevice) MTU() (int, error) { func (w *SystemDevice) MTU() (int, error) {
return w.mtu, nil return int(w.mtu), nil
} }
func (w *SystemDevice) Name() (string, error) { func (w *SystemDevice) Name() (string, error) {
@ -155,6 +157,7 @@ func (w *SystemDevice) Events() <-chan wgTun.Event {
} }
func (w *SystemDevice) Close() error { func (w *SystemDevice) Close() error {
close(w.events)
return w.device.Close() return w.device.Close()
} }