inbound.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package mixed
  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. N "github.com/sagernet/sing/common/network"
  16. "github.com/sagernet/sing/protocol/http"
  17. "github.com/sagernet/sing/protocol/socks"
  18. "github.com/sagernet/sing/protocol/socks/socks4"
  19. "github.com/sagernet/sing/protocol/socks/socks5"
  20. )
  21. func RegisterInbound(registry *inbound.Registry) {
  22. inbound.Register[option.HTTPMixedInboundOptions](registry, C.TypeMixed, NewInbound)
  23. }
  24. var _ adapter.TCPInjectableInbound = (*Inbound)(nil)
  25. type Inbound struct {
  26. inbound.Adapter
  27. router adapter.ConnectionRouterEx
  28. logger log.ContextLogger
  29. listener *listener.Listener
  30. authenticator *auth.Authenticator
  31. }
  32. func NewInbound(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.HTTPMixedInboundOptions) (adapter.Inbound, error) {
  33. inbound := &Inbound{
  34. Adapter: inbound.NewAdapter(C.TypeMixed, tag),
  35. router: uot.NewRouter(router, logger),
  36. logger: logger,
  37. authenticator: auth.NewAuthenticator(options.Users),
  38. }
  39. inbound.listener = listener.New(listener.Options{
  40. Context: ctx,
  41. Logger: logger,
  42. Network: []string{N.NetworkTCP},
  43. Listen: options.ListenOptions,
  44. ConnectionHandler: inbound,
  45. SetSystemProxy: options.SetSystemProxy,
  46. SystemProxySOCKS: true,
  47. })
  48. return inbound, nil
  49. }
  50. func (h *Inbound) Start() error {
  51. return h.listener.Start()
  52. }
  53. func (h *Inbound) Close() error {
  54. return h.listener.Close()
  55. }
  56. func (h *Inbound) NewConnectionEx(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
  57. err := h.newConnection(ctx, conn, metadata, onClose)
  58. N.CloseOnHandshakeFailure(conn, onClose, err)
  59. if err != nil {
  60. if E.IsClosedOrCanceled(err) {
  61. h.logger.DebugContext(ctx, "connection closed: ", err)
  62. } else {
  63. h.logger.ErrorContext(ctx, E.Cause(err, "process connection from ", metadata.Source))
  64. }
  65. }
  66. }
  67. func (h *Inbound) newConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) error {
  68. reader := std_bufio.NewReader(conn)
  69. headerBytes, err := reader.Peek(1)
  70. if err != nil {
  71. return E.Cause(err, "peek first byte")
  72. }
  73. switch headerBytes[0] {
  74. case socks4.Version, socks5.Version:
  75. return socks.HandleConnectionEx(ctx, conn, reader, h.authenticator, nil, adapter.NewUpstreamHandlerEx(metadata, h.newUserConnection, h.streamUserPacketConnection), metadata.Source, metadata.Destination, onClose)
  76. default:
  77. return http.HandleConnectionEx(ctx, conn, reader, h.authenticator, nil, adapter.NewUpstreamHandlerEx(metadata, h.newUserConnection, h.streamUserPacketConnection), metadata.Source, onClose)
  78. }
  79. }
  80. func (h *Inbound) newUserConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
  81. metadata.Inbound = h.Tag()
  82. metadata.InboundType = h.Type()
  83. metadata.InboundDetour = h.listener.ListenOptions().Detour
  84. metadata.InboundOptions = h.listener.ListenOptions().InboundOptions
  85. user, loaded := auth.UserFromContext[string](ctx)
  86. if !loaded {
  87. h.logger.InfoContext(ctx, "inbound connection to ", metadata.Destination)
  88. h.router.RouteConnectionEx(ctx, conn, metadata, onClose)
  89. return
  90. }
  91. metadata.User = user
  92. h.logger.InfoContext(ctx, "[", user, "] inbound connection to ", metadata.Destination)
  93. h.router.RouteConnectionEx(ctx, conn, metadata, onClose)
  94. }
  95. func (h *Inbound) streamUserPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
  96. metadata.Inbound = h.Tag()
  97. metadata.InboundType = h.Type()
  98. metadata.InboundDetour = h.listener.ListenOptions().Detour
  99. metadata.InboundOptions = h.listener.ListenOptions().InboundOptions
  100. user, loaded := auth.UserFromContext[string](ctx)
  101. if !loaded {
  102. h.logger.InfoContext(ctx, "inbound packet connection to ", metadata.Destination)
  103. h.router.RoutePacketConnectionEx(ctx, conn, metadata, onClose)
  104. return
  105. }
  106. metadata.User = user
  107. h.logger.InfoContext(ctx, "[", user, "] inbound packet connection to ", metadata.Destination)
  108. h.router.RoutePacketConnectionEx(ctx, conn, metadata, onClose)
  109. }