device.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package wireguard
  2. import (
  3. "context"
  4. "net/netip"
  5. "time"
  6. "github.com/sagernet/sing-box/adapter"
  7. "github.com/sagernet/sing-tun"
  8. "github.com/sagernet/sing/common/logger"
  9. N "github.com/sagernet/sing/common/network"
  10. "github.com/sagernet/wireguard-go/device"
  11. wgTun "github.com/sagernet/wireguard-go/tun"
  12. )
  13. type Device interface {
  14. wgTun.Device
  15. N.Dialer
  16. Start() error
  17. SetDevice(device *device.Device)
  18. Inet4Address() netip.Addr
  19. Inet6Address() netip.Addr
  20. }
  21. type DeviceOptions struct {
  22. Context context.Context
  23. Logger logger.ContextLogger
  24. System bool
  25. Handler tun.Handler
  26. UDPTimeout time.Duration
  27. CreateDialer func(interfaceName string) N.Dialer
  28. Name string
  29. MTU uint32
  30. Address []netip.Prefix
  31. AllowedAddress []netip.Prefix
  32. }
  33. func NewDevice(options DeviceOptions) (Device, error) {
  34. if !options.System {
  35. return newStackDevice(options)
  36. } else if !tun.WithGVisor {
  37. return newSystemDevice(options)
  38. } else {
  39. return newSystemStackDevice(options)
  40. }
  41. }
  42. type NatDevice interface {
  43. Device
  44. CreateDestination(metadata adapter.InboundContext, routeContext tun.DirectRouteContext, timeout time.Duration) (tun.DirectRouteDestination, error)
  45. }