device.go 967 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. GSO bool
  28. Address []netip.Prefix
  29. AllowedAddress []netip.Prefix
  30. }
  31. func NewDevice(options DeviceOptions) (Device, error) {
  32. if !options.System {
  33. return newStackDevice(options)
  34. } else if options.Handler == nil {
  35. return newSystemDevice(options)
  36. } else {
  37. return newSystemStackDevice(options)
  38. }
  39. }