inbound.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. logger logger.ContextLogger
  26. router adapter.ConnectionRouterEx
  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. authenticator: auth.NewAuthenticator(options.Users),
  35. }
  36. inbound.listener = listener.New(listener.Options{
  37. Context: ctx,
  38. Logger: logger,
  39. Network: []string{N.NetworkTCP},
  40. Listen: options.ListenOptions,
  41. ConnectionHandler: inbound,
  42. })
  43. return inbound, nil
  44. }
  45. func (h *Inbound) Start() error {
  46. return h.listener.Start()
  47. }
  48. func (h *Inbound) Close() error {
  49. return h.listener.Close()
  50. }
  51. func (h *Inbound) NewConnectionEx(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
  52. err := socks.HandleConnectionEx(ctx, conn, std_bufio.NewReader(conn), h.authenticator, nil, adapter.NewUpstreamHandlerEx(metadata, h.newUserConnection, h.streamUserPacketConnection), metadata.Source, metadata.Destination, onClose)
  53. N.CloseOnHandshakeFailure(conn, onClose, err)
  54. if err != nil {
  55. h.logger.ErrorContext(ctx, E.Cause(err, "process connection from ", metadata.Source))
  56. }
  57. }
  58. func (h *Inbound) newUserConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
  59. user, loaded := auth.UserFromContext[string](ctx)
  60. if !loaded {
  61. h.logger.InfoContext(ctx, "inbound connection to ", metadata.Destination)
  62. h.router.RouteConnectionEx(ctx, conn, metadata, onClose)
  63. return
  64. }
  65. metadata.User = user
  66. h.logger.InfoContext(ctx, "[", user, "] inbound connection to ", metadata.Destination)
  67. h.router.RouteConnectionEx(ctx, conn, metadata, onClose)
  68. }
  69. func (h *Inbound) streamUserPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
  70. user, loaded := auth.UserFromContext[string](ctx)
  71. if !loaded {
  72. h.logger.InfoContext(ctx, "inbound packet connection to ", metadata.Destination)
  73. h.router.RoutePacketConnectionEx(ctx, conn, metadata, onClose)
  74. return
  75. }
  76. metadata.User = user
  77. h.logger.InfoContext(ctx, "[", user, "] inbound packet connection to ", metadata.Destination)
  78. h.router.RoutePacketConnectionEx(ctx, conn, metadata, onClose)
  79. }