inbound.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package socks
  2. import (
  3. std_bufio "bufio"
  4. "context"
  5. "net"
  6. "time"
  7. "github.com/sagernet/sing-box/adapter"
  8. "github.com/sagernet/sing-box/adapter/inbound"
  9. "github.com/sagernet/sing-box/common/listener"
  10. "github.com/sagernet/sing-box/common/uot"
  11. C "github.com/sagernet/sing-box/constant"
  12. "github.com/sagernet/sing-box/log"
  13. "github.com/sagernet/sing-box/option"
  14. "github.com/sagernet/sing/common/auth"
  15. E "github.com/sagernet/sing/common/exceptions"
  16. "github.com/sagernet/sing/common/logger"
  17. N "github.com/sagernet/sing/common/network"
  18. "github.com/sagernet/sing/protocol/socks"
  19. )
  20. func RegisterInbound(registry *inbound.Registry) {
  21. inbound.Register[option.SocksInboundOptions](registry, C.TypeSOCKS, NewInbound)
  22. }
  23. var _ adapter.TCPInjectableInbound = (*Inbound)(nil)
  24. type Inbound struct {
  25. inbound.Adapter
  26. router adapter.ConnectionRouterEx
  27. logger logger.ContextLogger
  28. listener *listener.Listener
  29. authenticator *auth.Authenticator
  30. udpTimeout time.Duration
  31. }
  32. func NewInbound(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.SocksInboundOptions) (adapter.Inbound, error) {
  33. var udpTimeout time.Duration
  34. if options.UDPTimeout != 0 {
  35. udpTimeout = time.Duration(options.UDPTimeout)
  36. } else {
  37. udpTimeout = C.UDPTimeout
  38. }
  39. inbound := &Inbound{
  40. Adapter: inbound.NewAdapter(C.TypeSOCKS, tag),
  41. router: uot.NewRouter(router, logger),
  42. logger: logger,
  43. authenticator: auth.NewAuthenticator(options.Users),
  44. udpTimeout: udpTimeout,
  45. }
  46. inbound.listener = listener.New(listener.Options{
  47. Context: ctx,
  48. Logger: logger,
  49. Network: []string{N.NetworkTCP},
  50. Listen: options.ListenOptions,
  51. ConnectionHandler: inbound,
  52. })
  53. return inbound, nil
  54. }
  55. func (h *Inbound) Start(stage adapter.StartStage) error {
  56. if stage != adapter.StartStateStart {
  57. return nil
  58. }
  59. return h.listener.Start()
  60. }
  61. func (h *Inbound) Close() error {
  62. return h.listener.Close()
  63. }
  64. func (h *Inbound) NewConnectionEx(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
  65. err := socks.HandleConnectionEx(ctx, conn, std_bufio.NewReader(conn), h.authenticator, adapter.NewUpstreamHandlerEx(metadata, h.newUserConnection, h.streamUserPacketConnection), h.listener, h.udpTimeout, metadata.Source, onClose)
  66. N.CloseOnHandshakeFailure(conn, onClose, err)
  67. if err != nil {
  68. if E.IsClosedOrCanceled(err) {
  69. h.logger.DebugContext(ctx, "connection closed: ", err)
  70. } else {
  71. h.logger.ErrorContext(ctx, E.Cause(err, "process connection from ", metadata.Source))
  72. }
  73. }
  74. }
  75. func (h *Inbound) newUserConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
  76. metadata.Inbound = h.Tag()
  77. metadata.InboundType = h.Type()
  78. user, loaded := auth.UserFromContext[string](ctx)
  79. if !loaded {
  80. h.logger.InfoContext(ctx, "inbound connection to ", metadata.Destination)
  81. h.router.RouteConnectionEx(ctx, conn, metadata, onClose)
  82. return
  83. }
  84. metadata.User = user
  85. h.logger.InfoContext(ctx, "[", user, "] inbound connection to ", metadata.Destination)
  86. h.router.RouteConnectionEx(ctx, conn, metadata, onClose)
  87. }
  88. func (h *Inbound) streamUserPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
  89. metadata.Inbound = h.Tag()
  90. metadata.InboundType = h.Type()
  91. user, loaded := auth.UserFromContext[string](ctx)
  92. if !loaded {
  93. if !metadata.Destination.IsValid() {
  94. h.logger.InfoContext(ctx, "inbound packet connection")
  95. } else {
  96. h.logger.InfoContext(ctx, "inbound packet connection to ", metadata.Destination)
  97. }
  98. h.router.RoutePacketConnectionEx(ctx, conn, metadata, onClose)
  99. return
  100. }
  101. metadata.User = user
  102. if !metadata.Destination.IsValid() {
  103. h.logger.InfoContext(ctx, "[", user, "] inbound packet connection")
  104. } else {
  105. h.logger.InfoContext(ctx, "[", user, "] inbound packet connection to ", metadata.Destination)
  106. }
  107. h.router.RoutePacketConnectionEx(ctx, conn, metadata, onClose)
  108. }