1
0

inbound.go 3.7 KB

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