device_system.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package wireguard
  2. import (
  3. "context"
  4. "net"
  5. "net/netip"
  6. "os"
  7. "github.com/sagernet/sing-box/adapter"
  8. "github.com/sagernet/sing-box/common/dialer"
  9. "github.com/sagernet/sing-box/option"
  10. "github.com/sagernet/sing-tun"
  11. "github.com/sagernet/sing/common"
  12. M "github.com/sagernet/sing/common/metadata"
  13. N "github.com/sagernet/sing/common/network"
  14. wgTun "github.com/sagernet/wireguard-go/tun"
  15. )
  16. var _ Device = (*SystemDevice)(nil)
  17. type SystemDevice struct {
  18. dialer N.Dialer
  19. device tun.Tun
  20. name string
  21. mtu int
  22. events chan wgTun.Event
  23. addr4 netip.Addr
  24. addr6 netip.Addr
  25. }
  26. func NewSystemDevice(router adapter.Router, interfaceName string, localPrefixes []netip.Prefix, mtu uint32) (*SystemDevice, error) {
  27. var inet4Addresses []netip.Prefix
  28. var inet6Addresses []netip.Prefix
  29. for _, prefixes := range localPrefixes {
  30. if prefixes.Addr().Is4() {
  31. inet4Addresses = append(inet4Addresses, prefixes)
  32. } else {
  33. inet6Addresses = append(inet6Addresses, prefixes)
  34. }
  35. }
  36. if interfaceName == "" {
  37. interfaceName = tun.CalculateInterfaceName("wg")
  38. }
  39. tunInterface, err := tun.New(tun.Options{
  40. Name: interfaceName,
  41. Inet4Address: inet4Addresses,
  42. Inet6Address: inet6Addresses,
  43. MTU: mtu,
  44. })
  45. if err != nil {
  46. return nil, err
  47. }
  48. var inet4Address netip.Addr
  49. var inet6Address netip.Addr
  50. if len(inet4Addresses) > 0 {
  51. inet4Address = inet4Addresses[0].Addr()
  52. }
  53. if len(inet6Addresses) > 0 {
  54. inet6Address = inet6Addresses[0].Addr()
  55. }
  56. return &SystemDevice{
  57. dialer: common.Must1(dialer.NewDefault(router, option.DialerOptions{
  58. BindInterface: interfaceName,
  59. })),
  60. device: tunInterface,
  61. name: interfaceName,
  62. mtu: int(mtu),
  63. events: make(chan wgTun.Event),
  64. addr4: inet4Address,
  65. addr6: inet6Address,
  66. }, nil
  67. }
  68. func (w *SystemDevice) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
  69. return w.dialer.DialContext(ctx, network, destination)
  70. }
  71. func (w *SystemDevice) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
  72. return w.dialer.ListenPacket(ctx, destination)
  73. }
  74. func (w *SystemDevice) Inet4Address() netip.Addr {
  75. return w.addr4
  76. }
  77. func (w *SystemDevice) Inet6Address() netip.Addr {
  78. return w.addr6
  79. }
  80. func (w *SystemDevice) Start() error {
  81. w.events <- wgTun.EventUp
  82. return nil
  83. }
  84. func (w *SystemDevice) File() *os.File {
  85. return nil
  86. }
  87. func (w *SystemDevice) Read(bufs [][]byte, sizes []int, offset int) (count int, err error) {
  88. sizes[0], err = w.device.Read(bufs[0][offset-tun.PacketOffset:])
  89. if err == nil {
  90. count = 1
  91. }
  92. return
  93. }
  94. func (w *SystemDevice) Write(bufs [][]byte, offset int) (count int, err error) {
  95. for _, b := range bufs {
  96. _, err = w.device.Write(b[offset:])
  97. if err != nil {
  98. return
  99. }
  100. count++
  101. }
  102. return
  103. }
  104. func (w *SystemDevice) Flush() error {
  105. return nil
  106. }
  107. func (w *SystemDevice) MTU() (int, error) {
  108. return w.mtu, nil
  109. }
  110. func (w *SystemDevice) Name() (string, error) {
  111. return w.name, nil
  112. }
  113. func (w *SystemDevice) Events() <-chan wgTun.Event {
  114. return w.events
  115. }
  116. func (w *SystemDevice) Close() error {
  117. return w.device.Close()
  118. }
  119. func (w *SystemDevice) BatchSize() int {
  120. return 1
  121. }