inbound.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package direct
  2. import (
  3. "context"
  4. "net"
  5. "time"
  6. "github.com/sagernet/sing-box/adapter"
  7. "github.com/sagernet/sing-box/adapter/inbound"
  8. "github.com/sagernet/sing-box/common/listener"
  9. C "github.com/sagernet/sing-box/constant"
  10. "github.com/sagernet/sing-box/log"
  11. "github.com/sagernet/sing-box/option"
  12. "github.com/sagernet/sing/common/buf"
  13. M "github.com/sagernet/sing/common/metadata"
  14. N "github.com/sagernet/sing/common/network"
  15. "github.com/sagernet/sing/common/udpnat2"
  16. )
  17. func RegisterInbound(registry *inbound.Registry) {
  18. inbound.Register[option.DirectInboundOptions](registry, C.TypeDirect, NewInbound)
  19. }
  20. type Inbound struct {
  21. inbound.Adapter
  22. ctx context.Context
  23. router adapter.ConnectionRouterEx
  24. logger log.ContextLogger
  25. listener *listener.Listener
  26. udpNat *udpnat.Service
  27. overrideOption int
  28. overrideDestination M.Socksaddr
  29. }
  30. func NewInbound(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.DirectInboundOptions) (adapter.Inbound, error) {
  31. options.UDPFragmentDefault = true
  32. inbound := &Inbound{
  33. Adapter: inbound.NewAdapter(C.TypeDirect, tag),
  34. ctx: ctx,
  35. router: router,
  36. logger: logger,
  37. }
  38. if options.OverrideAddress != "" && options.OverridePort != 0 {
  39. inbound.overrideOption = 1
  40. inbound.overrideDestination = M.ParseSocksaddrHostPort(options.OverrideAddress, options.OverridePort)
  41. } else if options.OverrideAddress != "" {
  42. inbound.overrideOption = 2
  43. inbound.overrideDestination = M.ParseSocksaddrHostPort(options.OverrideAddress, options.OverridePort)
  44. } else if options.OverridePort != 0 {
  45. inbound.overrideOption = 3
  46. inbound.overrideDestination = M.Socksaddr{Port: options.OverridePort}
  47. }
  48. var udpTimeout time.Duration
  49. if options.UDPTimeout != 0 {
  50. udpTimeout = time.Duration(options.UDPTimeout)
  51. } else {
  52. udpTimeout = C.UDPTimeout
  53. }
  54. inbound.udpNat = udpnat.New(inbound, inbound.preparePacketConnection, udpTimeout, false)
  55. inbound.listener = listener.New(listener.Options{
  56. Context: ctx,
  57. Logger: logger,
  58. Network: options.Network.Build(),
  59. Listen: options.ListenOptions,
  60. ConnectionHandler: inbound,
  61. PacketHandler: inbound,
  62. })
  63. return inbound, nil
  64. }
  65. func (i *Inbound) Start(stage adapter.StartStage) error {
  66. if stage != adapter.StartStateStart {
  67. return nil
  68. }
  69. return i.listener.Start()
  70. }
  71. func (i *Inbound) Close() error {
  72. return i.listener.Close()
  73. }
  74. func (i *Inbound) NewPacketEx(buffer *buf.Buffer, source M.Socksaddr) {
  75. var destination M.Socksaddr
  76. switch i.overrideOption {
  77. case 1:
  78. destination = i.overrideDestination
  79. case 2:
  80. destination = i.overrideDestination
  81. destination.Port = i.listener.UDPAddr().Port
  82. case 3:
  83. destination = source
  84. destination.Port = i.overrideDestination.Port
  85. }
  86. i.udpNat.NewPacket([][]byte{buffer.Bytes()}, source, destination, nil)
  87. }
  88. func (i *Inbound) NewConnectionEx(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
  89. metadata.Inbound = i.Tag()
  90. metadata.InboundType = i.Type()
  91. metadata.Destination = M.SocksaddrFromNet(conn.LocalAddr())
  92. switch i.overrideOption {
  93. case 1:
  94. metadata.Destination = i.overrideDestination
  95. case 2:
  96. destination := i.overrideDestination
  97. destination.Port = metadata.Destination.Port
  98. metadata.Destination = destination
  99. case 3:
  100. metadata.Destination.Port = i.overrideDestination.Port
  101. }
  102. if i.overrideOption != 0 {
  103. i.logger.InfoContext(ctx, "inbound connection to ", metadata.Destination)
  104. }
  105. i.router.RouteConnectionEx(ctx, conn, metadata, onClose)
  106. }
  107. func (i *Inbound) NewPacketConnectionEx(ctx context.Context, conn N.PacketConn, source M.Socksaddr, destination M.Socksaddr, onClose N.CloseHandlerFunc) {
  108. i.logger.InfoContext(ctx, "inbound packet connection from ", source)
  109. i.logger.InfoContext(ctx, "inbound packet connection to ", destination)
  110. var metadata adapter.InboundContext
  111. metadata.Inbound = i.Tag()
  112. metadata.InboundType = i.Type()
  113. //nolint:staticcheck
  114. metadata.InboundDetour = i.listener.ListenOptions().Detour
  115. //nolint:staticcheck
  116. metadata.InboundOptions = i.listener.ListenOptions().InboundOptions
  117. metadata.Source = source
  118. metadata.Destination = destination
  119. metadata.OriginDestination = i.listener.UDPAddr()
  120. i.router.RoutePacketConnectionEx(ctx, conn, metadata, onClose)
  121. }
  122. func (i *Inbound) preparePacketConnection(source M.Socksaddr, destination M.Socksaddr, userData any) (bool, context.Context, N.PacketWriter, N.CloseHandlerFunc) {
  123. return true, log.ContextWithNewID(i.ctx), &directPacketWriter{i.listener.PacketWriter(), source}, nil
  124. }
  125. type directPacketWriter struct {
  126. writer N.PacketWriter
  127. source M.Socksaddr
  128. }
  129. func (w *directPacketWriter) WritePacket(buffer *buf.Buffer, addr M.Socksaddr) error {
  130. return w.writer.WritePacket(buffer, w.source)
  131. }