sing-box/transport/wireguard/device_system.go

166 lines
4.2 KiB
Go
Raw Normal View History

2022-09-05 16:15:09 +00:00
package wireguard
import (
"context"
"errors"
2022-09-05 16:15:09 +00:00
"net"
"net/netip"
"os"
2024-11-21 10:10:41 +00:00
"runtime"
2024-08-26 06:01:32 +00:00
"sync"
2022-09-05 16:15:09 +00:00
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-tun"
2023-08-08 08:14:03 +00:00
"github.com/sagernet/sing/common"
E "github.com/sagernet/sing/common/exceptions"
2022-09-05 16:15:09 +00:00
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
2024-11-21 10:10:41 +00:00
"github.com/sagernet/sing/service"
"github.com/sagernet/wireguard-go/device"
wgTun "github.com/sagernet/wireguard-go/tun"
2022-09-05 16:15:09 +00:00
)
2024-11-21 10:10:41 +00:00
var _ Device = (*systemDevice)(nil)
type systemDevice struct {
options DeviceOptions
dialer N.Dialer
device tun.Tun
batchDevice tun.LinuxTUN
events chan wgTun.Event
closeOnce sync.Once
2022-09-05 16:15:09 +00:00
}
2024-11-21 10:10:41 +00:00
func newSystemDevice(options DeviceOptions) (*systemDevice, error) {
if options.Name == "" {
options.Name = tun.CalculateInterfaceName("wg")
2022-09-05 16:15:09 +00:00
}
2024-11-21 10:10:41 +00:00
return &systemDevice{
options: options,
dialer: options.CreateDialer(options.Name),
events: make(chan wgTun.Event, 1),
2022-09-05 16:15:09 +00:00
}, nil
}
2024-11-21 10:10:41 +00:00
func (w *systemDevice) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
2022-09-05 16:15:09 +00:00
return w.dialer.DialContext(ctx, network, destination)
}
2024-11-21 10:10:41 +00:00
func (w *systemDevice) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
2022-09-05 16:15:09 +00:00
return w.dialer.ListenPacket(ctx, destination)
}
2024-11-21 10:10:41 +00:00
func (w *systemDevice) SetDevice(device *device.Device) {
}
func (w *systemDevice) Start() error {
networkManager := service.FromContext[adapter.NetworkManager](w.options.Context)
tunOptions := tun.Options{
Name: w.options.Name,
Inet4Address: common.Filter(w.options.Address, func(it netip.Prefix) bool {
return it.Addr().Is4()
}),
Inet6Address: common.Filter(w.options.Address, func(it netip.Prefix) bool {
return it.Addr().Is6()
}),
MTU: w.options.MTU,
GSO: w.options.GSO,
InterfaceScope: true,
Inet4RouteAddress: common.Filter(w.options.AllowedAddress, func(it netip.Prefix) bool {
return it.Addr().Is4()
}),
Inet6RouteAddress: common.Filter(w.options.AllowedAddress, func(it netip.Prefix) bool { return it.Addr().Is6() }),
InterfaceMonitor: networkManager.InterfaceMonitor(),
InterfaceFinder: networkManager.InterfaceFinder(),
2024-08-26 06:01:32 +00:00
}
2024-11-21 10:10:41 +00:00
// works with Linux, macOS with IFSCOPE routes, not tested on Windows
if runtime.GOOS == "darwin" {
tunOptions.AutoRoute = true
2024-08-26 06:01:32 +00:00
}
2024-11-21 10:10:41 +00:00
tunInterface, err := tun.New(tunOptions)
if err != nil {
return err
}
err = tunInterface.Start()
2024-08-26 06:01:32 +00:00
if err != nil {
return err
}
2024-11-21 10:10:41 +00:00
w.options.Logger.Info("started at ", w.options.Name)
2024-08-26 06:01:32 +00:00
w.device = tunInterface
2024-11-21 10:10:41 +00:00
if w.options.GSO {
2024-08-26 06:01:32 +00:00
batchTUN, isBatchTUN := tunInterface.(tun.LinuxTUN)
if !isBatchTUN {
tunInterface.Close()
return E.New("GSO is not supported on current platform")
}
w.batchDevice = batchTUN
}
2022-09-05 16:15:09 +00:00
w.events <- wgTun.EventUp
return nil
}
2024-11-21 10:10:41 +00:00
func (w *systemDevice) File() *os.File {
2022-09-05 16:15:09 +00:00
return nil
}
2024-11-21 10:10:41 +00:00
func (w *systemDevice) Read(bufs [][]byte, sizes []int, offset int) (count int, err error) {
if w.batchDevice != nil {
2024-11-21 10:10:41 +00:00
count, err = w.batchDevice.BatchRead(bufs, offset-tun.PacketOffset, sizes)
} else {
2024-11-21 10:10:41 +00:00
sizes[0], err = w.device.Read(bufs[0][offset-tun.PacketOffset:])
if err == nil {
count = 1
} else if errors.Is(err, tun.ErrTooManySegments) {
err = wgTun.ErrTooManySegments
}
2023-04-20 05:16:31 +00:00
}
return
2022-09-05 16:15:09 +00:00
}
2024-11-21 10:10:41 +00:00
func (w *systemDevice) Write(bufs [][]byte, offset int) (count int, err error) {
if w.batchDevice != nil {
return 0, w.batchDevice.BatchWrite(bufs, offset)
} else {
2024-11-21 10:10:41 +00:00
for _, packet := range bufs {
if tun.PacketOffset > 0 {
common.ClearArray(packet[offset-tun.PacketOffset : offset])
tun.PacketFillHeader(packet[offset-tun.PacketOffset:], tun.PacketIPVersion(packet[offset:]))
}
_, err = w.device.Write(packet[offset-tun.PacketOffset:])
if err != nil {
return
}
2023-04-20 05:16:31 +00:00
}
}
// WireGuard will not read count
2023-04-20 05:16:31 +00:00
return
2022-09-05 16:15:09 +00:00
}
2024-11-21 10:10:41 +00:00
func (w *systemDevice) Flush() error {
2022-09-05 16:15:09 +00:00
return nil
}
2024-11-21 10:10:41 +00:00
func (w *systemDevice) MTU() (int, error) {
return int(w.options.MTU), nil
2022-09-05 16:15:09 +00:00
}
2024-11-21 10:10:41 +00:00
func (w *systemDevice) Name() (string, error) {
return w.options.Name, nil
2022-09-05 16:15:09 +00:00
}
2024-11-21 10:10:41 +00:00
func (w *systemDevice) Events() <-chan wgTun.Event {
2022-09-05 16:15:09 +00:00
return w.events
}
2024-11-21 10:10:41 +00:00
func (w *systemDevice) Close() error {
2024-08-26 06:01:32 +00:00
close(w.events)
2022-09-05 16:15:09 +00:00
return w.device.Close()
}
2023-04-20 05:16:31 +00:00
2024-11-21 10:10:41 +00:00
func (w *systemDevice) BatchSize() int {
if w.batchDevice != nil {
return w.batchDevice.BatchSize()
}
2023-04-20 05:16:31 +00:00
return 1
}