device.go 946 B

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