Skip to content

Commit

Permalink
wireguard: Fix events chan leak
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Aug 26, 2024
1 parent 9832932 commit 2ebd1c2
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 51 deletions.
1 change: 0 additions & 1 deletion outbound/wireguard.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,6 @@ func (w *WireGuard) Close() error {
if w.pauseCallback != nil {
w.pauseManager.UnregisterCallback(w.pauseCallback)
}
w.tunDevice.Close()
return nil
}

Expand Down
8 changes: 2 additions & 6 deletions transport/wireguard/device_stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,17 +230,13 @@ func (w *StackDevice) Events() <-chan wgTun.Event {
}

func (w *StackDevice) Close() error {
select {
case <-w.done:
return os.ErrClosed
default:
}
close(w.done)
close(w.events)
w.stack.Close()
for _, endpoint := range w.stack.CleanupEndpoints() {
endpoint.Abort()
}
w.stack.Wait()
close(w.done)
return nil
}

Expand Down
91 changes: 47 additions & 44 deletions transport/wireguard/device_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net"
"net/netip"
"os"
"sync"

"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/common/dialer"
Expand All @@ -21,14 +22,16 @@ import (
var _ Device = (*SystemDevice)(nil)

type SystemDevice struct {
dialer N.Dialer
device tun.Tun
batchDevice tun.LinuxTUN
name string
mtu int
events chan wgTun.Event
addr4 netip.Addr
addr6 netip.Addr
dialer N.Dialer
device tun.Tun
batchDevice tun.LinuxTUN
name string
mtu uint32
inet4Addresses []netip.Prefix
inet6Addresses []netip.Prefix
gso bool
events chan wgTun.Event
closeOnce sync.Once
}

func NewSystemDevice(router adapter.Router, interfaceName string, localPrefixes []netip.Prefix, mtu uint32, gso bool) (*SystemDevice, error) {
Expand All @@ -44,43 +47,17 @@ func NewSystemDevice(router adapter.Router, interfaceName string, localPrefixes
if interfaceName == "" {
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{
dialer: common.Must1(dialer.NewDefault(router, option.DialerOptions{
BindInterface: interfaceName,
})),
device: tunInterface,
batchDevice: batchDevice,
name: interfaceName,
mtu: int(mtu),
events: make(chan wgTun.Event),
addr4: inet4Address,
addr6: inet6Address,
name: interfaceName,
mtu: mtu,
inet4Addresses: inet4Addresses,
inet6Addresses: inet6Addresses,
gso: gso,
events: make(chan wgTun.Event),
}, nil
}

Expand All @@ -93,14 +70,39 @@ func (w *SystemDevice) ListenPacket(ctx context.Context, destination M.Socksaddr
}

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 {
return w.addr6
if len(w.inet6Addresses) == 0 {
return netip.Addr{}
}
return w.inet6Addresses[0].Addr()
}

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
return nil
}
Expand Down Expand Up @@ -143,7 +145,7 @@ func (w *SystemDevice) Flush() error {
}

func (w *SystemDevice) MTU() (int, error) {
return w.mtu, nil
return int(w.mtu), nil
}

func (w *SystemDevice) Name() (string, error) {
Expand All @@ -155,6 +157,7 @@ func (w *SystemDevice) Events() <-chan wgTun.Event {
}

func (w *SystemDevice) Close() error {
close(w.events)
return w.device.Close()
}

Expand Down

0 comments on commit 2ebd1c2

Please sign in to comment.