2022-09-06 00:15:09 +08:00
|
|
|
package wireguard
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-12-20 20:00:00 +08:00
|
|
|
"errors"
|
2022-09-06 00:15:09 +08:00
|
|
|
"net"
|
|
|
|
"net/netip"
|
|
|
|
"os"
|
2024-11-21 18:10:41 +08:00
|
|
|
"runtime"
|
2024-08-26 14:01:32 +08:00
|
|
|
"sync"
|
2022-09-06 00:15:09 +08:00
|
|
|
|
|
|
|
"github.com/sagernet/sing-box/adapter"
|
|
|
|
"github.com/sagernet/sing-tun"
|
2023-08-08 16:14:03 +08:00
|
|
|
"github.com/sagernet/sing/common"
|
2023-12-20 20:00:00 +08:00
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
2022-09-06 00:15:09 +08:00
|
|
|
M "github.com/sagernet/sing/common/metadata"
|
|
|
|
N "github.com/sagernet/sing/common/network"
|
2024-11-21 18:10:41 +08:00
|
|
|
"github.com/sagernet/sing/service"
|
|
|
|
"github.com/sagernet/wireguard-go/device"
|
2022-11-07 16:35:15 +08:00
|
|
|
wgTun "github.com/sagernet/wireguard-go/tun"
|
2022-09-06 00:15:09 +08:00
|
|
|
)
|
|
|
|
|
2024-11-21 18:10:41 +08: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-06 00:15:09 +08:00
|
|
|
}
|
|
|
|
|
2024-11-21 18:10:41 +08:00
|
|
|
func newSystemDevice(options DeviceOptions) (*systemDevice, error) {
|
|
|
|
if options.Name == "" {
|
|
|
|
options.Name = tun.CalculateInterfaceName("wg")
|
2022-09-06 00:15:09 +08:00
|
|
|
}
|
2024-11-21 18:10:41 +08:00
|
|
|
return &systemDevice{
|
|
|
|
options: options,
|
|
|
|
dialer: options.CreateDialer(options.Name),
|
|
|
|
events: make(chan wgTun.Event, 1),
|
2022-09-06 00:15:09 +08:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-11-21 18:10:41 +08:00
|
|
|
func (w *systemDevice) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
|
2022-09-06 00:15:09 +08:00
|
|
|
return w.dialer.DialContext(ctx, network, destination)
|
|
|
|
}
|
|
|
|
|
2024-11-21 18:10:41 +08:00
|
|
|
func (w *systemDevice) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
|
2022-09-06 00:15:09 +08:00
|
|
|
return w.dialer.ListenPacket(ctx, destination)
|
|
|
|
}
|
|
|
|
|
2024-11-21 18:10:41 +08: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(),
|
|
|
|
Logger: w.options.Logger,
|
2024-08-26 14:01:32 +08:00
|
|
|
}
|
2024-11-21 18:10:41 +08:00
|
|
|
// works with Linux, macOS with IFSCOPE routes, not tested on Windows
|
|
|
|
if runtime.GOOS == "darwin" {
|
|
|
|
tunOptions.AutoRoute = true
|
2024-08-26 14:01:32 +08:00
|
|
|
}
|
2024-11-21 18:10:41 +08:00
|
|
|
tunInterface, err := tun.New(tunOptions)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = tunInterface.Start()
|
2024-08-26 14:01:32 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-11-21 18:10:41 +08:00
|
|
|
w.options.Logger.Info("started at ", w.options.Name)
|
2024-08-26 14:01:32 +08:00
|
|
|
w.device = tunInterface
|
2024-11-21 18:10:41 +08:00
|
|
|
if w.options.GSO {
|
2024-08-26 14:01:32 +08: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-06 00:15:09 +08:00
|
|
|
w.events <- wgTun.EventUp
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-11-21 18:10:41 +08:00
|
|
|
func (w *systemDevice) File() *os.File {
|
2022-09-06 00:15:09 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-11-21 18:10:41 +08:00
|
|
|
func (w *systemDevice) Read(bufs [][]byte, sizes []int, offset int) (count int, err error) {
|
2023-12-20 20:00:00 +08:00
|
|
|
if w.batchDevice != nil {
|
2024-11-21 18:10:41 +08:00
|
|
|
count, err = w.batchDevice.BatchRead(bufs, offset-tun.PacketOffset, sizes)
|
2023-12-20 20:00:00 +08:00
|
|
|
} else {
|
2024-11-21 18:10:41 +08:00
|
|
|
sizes[0], err = w.device.Read(bufs[0][offset-tun.PacketOffset:])
|
2023-12-20 20:00:00 +08:00
|
|
|
if err == nil {
|
|
|
|
count = 1
|
|
|
|
} else if errors.Is(err, tun.ErrTooManySegments) {
|
|
|
|
err = wgTun.ErrTooManySegments
|
|
|
|
}
|
2023-04-20 13:16:31 +08:00
|
|
|
}
|
|
|
|
return
|
2022-09-06 00:15:09 +08:00
|
|
|
}
|
|
|
|
|
2024-11-21 18:10:41 +08:00
|
|
|
func (w *systemDevice) Write(bufs [][]byte, offset int) (count int, err error) {
|
2023-12-20 20:00:00 +08:00
|
|
|
if w.batchDevice != nil {
|
2024-11-21 18:10:41 +08:00
|
|
|
return w.batchDevice.BatchWrite(bufs, offset)
|
2023-12-20 20:00:00 +08:00
|
|
|
} else {
|
2024-11-21 18:10:41 +08: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:])
|
2023-12-20 20:00:00 +08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2023-04-20 13:16:31 +08:00
|
|
|
}
|
|
|
|
}
|
2023-12-20 20:00:00 +08:00
|
|
|
// WireGuard will not read count
|
2023-04-20 13:16:31 +08:00
|
|
|
return
|
2022-09-06 00:15:09 +08:00
|
|
|
}
|
|
|
|
|
2024-11-21 18:10:41 +08:00
|
|
|
func (w *systemDevice) Flush() error {
|
2022-09-06 00:15:09 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-11-21 18:10:41 +08:00
|
|
|
func (w *systemDevice) MTU() (int, error) {
|
|
|
|
return int(w.options.MTU), nil
|
2022-09-06 00:15:09 +08:00
|
|
|
}
|
|
|
|
|
2024-11-21 18:10:41 +08:00
|
|
|
func (w *systemDevice) Name() (string, error) {
|
|
|
|
return w.options.Name, nil
|
2022-09-06 00:15:09 +08:00
|
|
|
}
|
|
|
|
|
2024-11-21 18:10:41 +08:00
|
|
|
func (w *systemDevice) Events() <-chan wgTun.Event {
|
2022-09-06 00:15:09 +08:00
|
|
|
return w.events
|
|
|
|
}
|
|
|
|
|
2024-11-21 18:10:41 +08:00
|
|
|
func (w *systemDevice) Close() error {
|
2024-08-26 14:01:32 +08:00
|
|
|
close(w.events)
|
2022-09-06 00:15:09 +08:00
|
|
|
return w.device.Close()
|
|
|
|
}
|
2023-04-20 13:16:31 +08:00
|
|
|
|
2024-11-21 18:10:41 +08:00
|
|
|
func (w *systemDevice) BatchSize() int {
|
2023-12-20 20:00:00 +08:00
|
|
|
if w.batchDevice != nil {
|
|
|
|
return w.batchDevice.BatchSize()
|
|
|
|
}
|
2023-04-20 13:16:31 +08:00
|
|
|
return 1
|
|
|
|
}
|