device_system.go 4.8 KB

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