sing-box/transport/wireguard/device_system.go

170 lines
3.9 KiB
Go
Raw Normal View History

2022-09-06 00:15:09 +08:00
package wireguard
import (
"context"
"errors"
2022-09-06 00:15:09 +08:00
"net"
"net/netip"
"os"
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-box/common/dialer"
"github.com/sagernet/sing-box/option"
"github.com/sagernet/sing-tun"
2023-08-08 16:14:03 +08:00
"github.com/sagernet/sing/common"
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"
wgTun "github.com/sagernet/wireguard-go/tun"
2022-09-06 00:15:09 +08:00
)
var _ Device = (*SystemDevice)(nil)
type SystemDevice struct {
2024-08-26 14:01:32 +08:00
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
2022-09-06 00:15:09 +08:00
}
func NewSystemDevice(router adapter.Router, interfaceName string, localPrefixes []netip.Prefix, mtu uint32, gso bool) (*SystemDevice, error) {
2022-09-06 00:15:09 +08:00
var inet4Addresses []netip.Prefix
var inet6Addresses []netip.Prefix
for _, prefixes := range localPrefixes {
if prefixes.Addr().Is4() {
inet4Addresses = append(inet4Addresses, prefixes)
} else {
inet6Addresses = append(inet6Addresses, prefixes)
}
}
if interfaceName == "" {
interfaceName = tun.CalculateInterfaceName("wg")
}
2024-08-26 14:01:32 +08:00
2022-09-06 00:15:09 +08:00
return &SystemDevice{
2023-08-08 16:14:03 +08:00
dialer: common.Must1(dialer.NewDefault(router, option.DialerOptions{
2022-09-06 00:15:09 +08:00
BindInterface: interfaceName,
2023-08-08 16:14:03 +08:00
})),
2024-08-26 14:01:32 +08:00
name: interfaceName,
mtu: mtu,
inet4Addresses: inet4Addresses,
inet6Addresses: inet6Addresses,
gso: gso,
2024-09-20 20:12:12 +08:00
events: make(chan wgTun.Event, 1),
2022-09-06 00:15:09 +08:00
}, nil
}
func (w *SystemDevice) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
return w.dialer.DialContext(ctx, network, destination)
}
func (w *SystemDevice) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
return w.dialer.ListenPacket(ctx, destination)
}
2023-04-20 13:16:31 +08:00
func (w *SystemDevice) Inet4Address() netip.Addr {
2024-08-26 14:01:32 +08:00
if len(w.inet4Addresses) == 0 {
return netip.Addr{}
}
return w.inet4Addresses[0].Addr()
2023-04-20 13:16:31 +08:00
}
func (w *SystemDevice) Inet6Address() netip.Addr {
2024-08-26 14:01:32 +08:00
if len(w.inet6Addresses) == 0 {
return netip.Addr{}
}
return w.inet6Addresses[0].Addr()
2023-04-20 13:16:31 +08:00
}
2022-09-06 00:15:09 +08:00
func (w *SystemDevice) Start() error {
2024-08-26 14:01:32 +08:00
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
}
2022-09-06 00:15:09 +08:00
w.events <- wgTun.EventUp
return nil
}
func (w *SystemDevice) File() *os.File {
return nil
}
2023-04-20 13:16:31 +08:00
func (w *SystemDevice) Read(bufs [][]byte, sizes []int, offset int) (count int, err error) {
if w.batchDevice != nil {
count, err = w.batchDevice.BatchRead(bufs, offset, sizes)
} else {
sizes[0], err = w.device.Read(bufs[0][offset:])
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
}
2023-04-20 13:16:31 +08: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 {
for _, b := range bufs {
_, err = w.device.Write(b[offset:])
if err != nil {
return
}
2023-04-20 13:16:31 +08:00
}
}
// WireGuard will not read count
2023-04-20 13:16:31 +08:00
return
2022-09-06 00:15:09 +08:00
}
func (w *SystemDevice) Flush() error {
return nil
}
func (w *SystemDevice) MTU() (int, error) {
2024-08-26 14:01:32 +08:00
return int(w.mtu), nil
2022-09-06 00:15:09 +08:00
}
func (w *SystemDevice) Name() (string, error) {
return w.name, nil
}
2023-04-20 13:16:31 +08:00
func (w *SystemDevice) Events() <-chan wgTun.Event {
2022-09-06 00:15:09 +08:00
return w.events
}
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
func (w *SystemDevice) BatchSize() int {
if w.batchDevice != nil {
return w.batchDevice.BatchSize()
}
2023-04-20 13:16:31 +08:00
return 1
}