Files
sing-box-extended-mirror/transport/wireguard/device.go
T

53 lines
1.2 KiB
Go
Raw Normal View History

2022-09-06 00:15:09 +08:00
package wireguard
import (
2024-11-21 18:10:41 +08:00
"context"
"net/netip"
"time"
2025-02-17 22:00:57 +08:00
"github.com/sagernet/sing-box/adapter"
2024-11-21 18:10:41 +08:00
"github.com/sagernet/sing-tun"
"github.com/sagernet/sing/common/logger"
2022-09-06 00:15:09 +08:00
N "github.com/sagernet/sing/common/network"
2024-11-21 18:10:41 +08:00
"github.com/sagernet/wireguard-go/device"
wgTun "github.com/sagernet/wireguard-go/tun"
2022-09-06 00:15:09 +08:00
)
type Device interface {
2024-11-21 18:10:41 +08:00
wgTun.Device
2022-09-06 00:15:09 +08:00
N.Dialer
Start() error
2024-11-21 18:10:41 +08:00
SetDevice(device *device.Device)
2025-02-17 22:00:57 +08:00
Inet4Address() netip.Addr
Inet6Address() netip.Addr
2024-11-21 18:10:41 +08:00
}
type DeviceOptions struct {
Context context.Context
Logger logger.ContextLogger
System bool
Handler tun.Handler
UDPTimeout time.Duration
2026-06-01 17:51:02 +08:00
ICMPTimeout time.Duration
2024-11-21 18:10:41 +08:00
CreateDialer func(interfaceName string) N.Dialer
Name string
MTU uint32
Address []netip.Prefix
AllowedAddress []netip.Prefix
}
func NewDevice(options DeviceOptions) (Device, error) {
if !options.System {
return newStackDevice(options)
2025-02-17 22:00:57 +08:00
} else if !tun.WithGVisor {
2024-11-21 18:10:41 +08:00
return newSystemDevice(options)
} else {
return newSystemStackDevice(options)
}
2022-09-06 00:15:09 +08:00
}
2025-02-17 22:00:57 +08:00
type NatDevice interface {
Device
CreateDestination(metadata adapter.InboundContext, routeContext tun.DirectRouteContext, timeout time.Duration) (tun.DirectRouteDestination, error)
}