device_system.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package wireguard
  2. import (
  3. "context"
  4. "errors"
  5. "net"
  6. "net/netip"
  7. "os"
  8. "runtime"
  9. "sync"
  10. "github.com/sagernet/sing-box/adapter"
  11. "github.com/sagernet/sing-tun"
  12. "github.com/sagernet/sing/common"
  13. E "github.com/sagernet/sing/common/exceptions"
  14. M "github.com/sagernet/sing/common/metadata"
  15. N "github.com/sagernet/sing/common/network"
  16. "github.com/sagernet/sing/service"
  17. "github.com/sagernet/wireguard-go/device"
  18. wgTun "github.com/sagernet/wireguard-go/tun"
  19. )
  20. var _ Device = (*systemDevice)(nil)
  21. type systemDevice struct {
  22. options DeviceOptions
  23. dialer N.Dialer
  24. device tun.Tun
  25. batchDevice tun.LinuxTUN
  26. events chan wgTun.Event
  27. closeOnce sync.Once
  28. }
  29. func newSystemDevice(options DeviceOptions) (*systemDevice, error) {
  30. if options.Name == "" {
  31. options.Name = tun.CalculateInterfaceName("wg")
  32. }
  33. return &systemDevice{
  34. options: options,
  35. dialer: options.CreateDialer(options.Name),
  36. events: make(chan wgTun.Event, 1),
  37. }, nil
  38. }
  39. func (w *systemDevice) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
  40. return w.dialer.DialContext(ctx, network, destination)
  41. }
  42. func (w *systemDevice) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
  43. return w.dialer.ListenPacket(ctx, destination)
  44. }
  45. func (w *systemDevice) SetDevice(device *device.Device) {
  46. }
  47. func (w *systemDevice) Start() error {
  48. networkManager := service.FromContext[adapter.NetworkManager](w.options.Context)
  49. tunOptions := tun.Options{
  50. Name: w.options.Name,
  51. Inet4Address: common.Filter(w.options.Address, func(it netip.Prefix) bool {
  52. return it.Addr().Is4()
  53. }),
  54. Inet6Address: common.Filter(w.options.Address, func(it netip.Prefix) bool {
  55. return it.Addr().Is6()
  56. }),
  57. MTU: w.options.MTU,
  58. GSO: w.options.GSO,
  59. InterfaceScope: true,
  60. Inet4RouteAddress: common.Filter(w.options.AllowedAddress, func(it netip.Prefix) bool {
  61. return it.Addr().Is4()
  62. }),
  63. Inet6RouteAddress: common.Filter(w.options.AllowedAddress, func(it netip.Prefix) bool { return it.Addr().Is6() }),
  64. InterfaceMonitor: networkManager.InterfaceMonitor(),
  65. InterfaceFinder: networkManager.InterfaceFinder(),
  66. Logger: w.options.Logger,
  67. }
  68. // works with Linux, macOS with IFSCOPE routes, not tested on Windows
  69. if runtime.GOOS == "darwin" {
  70. tunOptions.AutoRoute = true
  71. }
  72. tunInterface, err := tun.New(tunOptions)
  73. if err != nil {
  74. return err
  75. }
  76. err = tunInterface.Start()
  77. if err != nil {
  78. return err
  79. }
  80. w.options.Logger.Info("started at ", w.options.Name)
  81. w.device = tunInterface
  82. if w.options.GSO {
  83. batchTUN, isBatchTUN := tunInterface.(tun.LinuxTUN)
  84. if !isBatchTUN {
  85. tunInterface.Close()
  86. return E.New("GSO is not supported on current platform")
  87. }
  88. w.batchDevice = batchTUN
  89. }
  90. w.events <- wgTun.EventUp
  91. return nil
  92. }
  93. func (w *systemDevice) File() *os.File {
  94. return nil
  95. }
  96. func (w *systemDevice) Read(bufs [][]byte, sizes []int, offset int) (count int, err error) {
  97. if w.batchDevice != nil {
  98. count, err = w.batchDevice.BatchRead(bufs, offset-tun.PacketOffset, sizes)
  99. } else {
  100. sizes[0], err = w.device.Read(bufs[0][offset-tun.PacketOffset:])
  101. if err == nil {
  102. count = 1
  103. } else if errors.Is(err, tun.ErrTooManySegments) {
  104. err = wgTun.ErrTooManySegments
  105. }
  106. }
  107. return
  108. }
  109. func (w *systemDevice) Write(bufs [][]byte, offset int) (count int, err error) {
  110. if w.batchDevice != nil {
  111. return w.batchDevice.BatchWrite(bufs, offset)
  112. } else {
  113. for _, packet := range bufs {
  114. if tun.PacketOffset > 0 {
  115. common.ClearArray(packet[offset-tun.PacketOffset : offset])
  116. tun.PacketFillHeader(packet[offset-tun.PacketOffset:], tun.PacketIPVersion(packet[offset:]))
  117. }
  118. _, err = w.device.Write(packet[offset-tun.PacketOffset:])
  119. if err != nil {
  120. return
  121. }
  122. }
  123. }
  124. // WireGuard will not read count
  125. return
  126. }
  127. func (w *systemDevice) Flush() error {
  128. return nil
  129. }
  130. func (w *systemDevice) MTU() (int, error) {
  131. return int(w.options.MTU), nil
  132. }
  133. func (w *systemDevice) Name() (string, error) {
  134. return w.options.Name, nil
  135. }
  136. func (w *systemDevice) Events() <-chan wgTun.Event {
  137. return w.events
  138. }
  139. func (w *systemDevice) Close() error {
  140. close(w.events)
  141. return w.device.Close()
  142. }
  143. func (w *systemDevice) BatchSize() int {
  144. if w.batchDevice != nil {
  145. return w.batchDevice.BatchSize()
  146. }
  147. return 1
  148. }