device_system.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. M "github.com/sagernet/sing/common/metadata"
  12. N "github.com/sagernet/sing/common/network"
  13. wgTun "github.com/sagernet/wireguard-go/tun"
  14. )
  15. var _ Device = (*SystemDevice)(nil)
  16. type SystemDevice struct {
  17. dialer N.Dialer
  18. device tun.Tun
  19. name string
  20. mtu int
  21. events chan wgTun.Event
  22. }
  23. /*func (w *SystemDevice) NewEndpoint() (stack.LinkEndpoint, error) {
  24. gTun, isGTun := w.device.(tun.GVisorTun)
  25. if !isGTun {
  26. return nil, tun.ErrGVisorUnsupported
  27. }
  28. return gTun.NewEndpoint()
  29. }*/
  30. func NewSystemDevice(router adapter.Router, interfaceName string, localPrefixes []netip.Prefix, mtu uint32) (*SystemDevice, error) {
  31. var inet4Addresses []netip.Prefix
  32. var inet6Addresses []netip.Prefix
  33. for _, prefixes := range localPrefixes {
  34. if prefixes.Addr().Is4() {
  35. inet4Addresses = append(inet4Addresses, prefixes)
  36. } else {
  37. inet6Addresses = append(inet6Addresses, prefixes)
  38. }
  39. }
  40. if interfaceName == "" {
  41. interfaceName = tun.CalculateInterfaceName("wg")
  42. }
  43. tunInterface, err := tun.New(tun.Options{
  44. Name: interfaceName,
  45. Inet4Address: inet4Addresses,
  46. Inet6Address: inet6Addresses,
  47. MTU: mtu,
  48. })
  49. if err != nil {
  50. return nil, err
  51. }
  52. return &SystemDevice{
  53. dialer.NewDefault(router, option.DialerOptions{
  54. BindInterface: interfaceName,
  55. }),
  56. tunInterface, interfaceName, int(mtu), make(chan wgTun.Event),
  57. }, nil
  58. }
  59. func (w *SystemDevice) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
  60. return w.dialer.DialContext(ctx, network, destination)
  61. }
  62. func (w *SystemDevice) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
  63. return w.dialer.ListenPacket(ctx, destination)
  64. }
  65. func (w *SystemDevice) Start() error {
  66. w.events <- wgTun.EventUp
  67. return nil
  68. }
  69. func (w *SystemDevice) File() *os.File {
  70. return nil
  71. }
  72. func (w *SystemDevice) Read(bytes []byte, index int) (int, error) {
  73. return w.device.Read(bytes[index-tun.PacketOffset:])
  74. }
  75. func (w *SystemDevice) Write(bytes []byte, index int) (int, error) {
  76. return w.device.Write(bytes[index:])
  77. }
  78. func (w *SystemDevice) Flush() error {
  79. return nil
  80. }
  81. func (w *SystemDevice) MTU() (int, error) {
  82. return w.mtu, nil
  83. }
  84. func (w *SystemDevice) Name() (string, error) {
  85. return w.name, nil
  86. }
  87. func (w *SystemDevice) Events() chan wgTun.Event {
  88. return w.events
  89. }
  90. func (w *SystemDevice) Close() error {
  91. return w.device.Close()
  92. }