inbound.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. i.udpNat.NewPacket([][]byte{buffer.Bytes()}, source, M.Socksaddr{}, nil)
  76. }
  77. func (i *Inbound) NewConnectionEx(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
  78. metadata.Inbound = i.Tag()
  79. metadata.InboundType = i.Type()
  80. destination := metadata.OriginDestination
  81. switch i.overrideOption {
  82. case 1:
  83. destination = i.overrideDestination
  84. case 2:
  85. destination.Addr = i.overrideDestination.Addr
  86. case 3:
  87. destination.Port = metadata.Destination.Port
  88. }
  89. metadata.Destination = destination
  90. if i.overrideOption != 0 {
  91. i.logger.InfoContext(ctx, "inbound connection to ", metadata.Destination)
  92. }
  93. i.router.RouteConnectionEx(ctx, conn, metadata, onClose)
  94. }
  95. func (i *Inbound) NewPacketConnectionEx(ctx context.Context, conn N.PacketConn, source M.Socksaddr, destination M.Socksaddr, onClose N.CloseHandlerFunc) {
  96. i.logger.InfoContext(ctx, "inbound packet connection from ", source)
  97. i.logger.InfoContext(ctx, "inbound packet connection to ", destination)
  98. var metadata adapter.InboundContext
  99. metadata.Inbound = i.Tag()
  100. metadata.InboundType = i.Type()
  101. //nolint:staticcheck
  102. metadata.InboundDetour = i.listener.ListenOptions().Detour
  103. //nolint:staticcheck
  104. metadata.InboundOptions = i.listener.ListenOptions().InboundOptions
  105. metadata.Source = source
  106. destination = i.listener.UDPAddr()
  107. switch i.overrideOption {
  108. case 1:
  109. destination = i.overrideDestination
  110. case 2:
  111. destination.Addr = i.overrideDestination.Addr
  112. case 3:
  113. destination.Port = i.overrideDestination.Port
  114. default:
  115. }
  116. metadata.Destination = destination
  117. metadata.OriginDestination = i.listener.UDPAddr()
  118. i.router.RoutePacketConnectionEx(ctx, conn, metadata, onClose)
  119. }
  120. func (i *Inbound) preparePacketConnection(source M.Socksaddr, destination M.Socksaddr, userData any) (bool, context.Context, N.PacketWriter, N.CloseHandlerFunc) {
  121. return true, log.ContextWithNewID(i.ctx), &directPacketWriter{i.listener.PacketWriter(), source}, nil
  122. }
  123. type directPacketWriter struct {
  124. writer N.PacketWriter
  125. source M.Socksaddr
  126. }
  127. func (w *directPacketWriter) WritePacket(buffer *buf.Buffer, addr M.Socksaddr) error {
  128. return w.writer.WritePacket(buffer, w.source)
  129. }