inbound.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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() error {
  47. return h.listener.Start()
  48. }
  49. func (h *Inbound) Close() error {
  50. return h.listener.Close()
  51. }
  52. func (h *Inbound) NewConnectionEx(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
  53. err := socks.HandleConnectionEx(ctx, conn, std_bufio.NewReader(conn), h.authenticator, nil, adapter.NewUpstreamHandlerEx(metadata, h.newUserConnection, h.streamUserPacketConnection), metadata.Source, metadata.Destination, onClose)
  54. N.CloseOnHandshakeFailure(conn, onClose, err)
  55. if err != nil {
  56. if E.IsClosedOrCanceled(err) {
  57. h.logger.DebugContext(ctx, "connection closed: ", err)
  58. } else {
  59. h.logger.ErrorContext(ctx, E.Cause(err, "process connection from ", metadata.Source))
  60. }
  61. }
  62. }
  63. func (h *Inbound) newUserConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
  64. metadata.Inbound = h.Tag()
  65. metadata.InboundType = h.Type()
  66. metadata.InboundDetour = h.listener.ListenOptions().Detour
  67. metadata.InboundOptions = h.listener.ListenOptions().InboundOptions
  68. user, loaded := auth.UserFromContext[string](ctx)
  69. if !loaded {
  70. h.logger.InfoContext(ctx, "inbound connection to ", metadata.Destination)
  71. h.router.RouteConnectionEx(ctx, conn, metadata, onClose)
  72. return
  73. }
  74. metadata.User = user
  75. h.logger.InfoContext(ctx, "[", user, "] inbound connection to ", metadata.Destination)
  76. h.router.RouteConnectionEx(ctx, conn, metadata, onClose)
  77. }
  78. func (h *Inbound) streamUserPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
  79. metadata.Inbound = h.Tag()
  80. metadata.InboundType = h.Type()
  81. metadata.InboundDetour = h.listener.ListenOptions().Detour
  82. metadata.InboundOptions = h.listener.ListenOptions().InboundOptions
  83. user, loaded := auth.UserFromContext[string](ctx)
  84. if !loaded {
  85. h.logger.InfoContext(ctx, "inbound packet connection to ", metadata.Destination)
  86. h.router.RoutePacketConnectionEx(ctx, conn, metadata, onClose)
  87. return
  88. }
  89. metadata.User = user
  90. h.logger.InfoContext(ctx, "[", user, "] inbound packet connection to ", metadata.Destination)
  91. h.router.RoutePacketConnectionEx(ctx, conn, metadata, onClose)
  92. }