inbound.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package http
  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/tls"
  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"
  15. "github.com/sagernet/sing/common/auth"
  16. E "github.com/sagernet/sing/common/exceptions"
  17. N "github.com/sagernet/sing/common/network"
  18. "github.com/sagernet/sing/protocol/http"
  19. )
  20. func RegisterInbound(registry *inbound.Registry) {
  21. inbound.Register[option.HTTPMixedInboundOptions](registry, C.TypeHTTP, NewInbound)
  22. }
  23. var _ adapter.TCPInjectableInbound = (*Inbound)(nil)
  24. type Inbound struct {
  25. inbound.Adapter
  26. router adapter.ConnectionRouterEx
  27. logger log.ContextLogger
  28. listener *listener.Listener
  29. authenticator *auth.Authenticator
  30. tlsConfig tls.ServerConfig
  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.TypeHTTP, tag),
  35. router: uot.NewRouter(router, logger),
  36. logger: logger,
  37. authenticator: auth.NewAuthenticator(options.Users),
  38. }
  39. if options.TLS != nil {
  40. tlsConfig, err := tls.NewServer(ctx, logger, common.PtrValueOrDefault(options.TLS))
  41. if err != nil {
  42. return nil, err
  43. }
  44. inbound.tlsConfig = tlsConfig
  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. SetSystemProxy: options.SetSystemProxy,
  53. SystemProxySOCKS: false,
  54. })
  55. return inbound, nil
  56. }
  57. func (h *Inbound) Start(stage adapter.StartStage) error {
  58. if stage != adapter.StartStateStart {
  59. return nil
  60. }
  61. if h.tlsConfig != nil {
  62. err := h.tlsConfig.Start()
  63. if err != nil {
  64. return E.Cause(err, "create TLS config")
  65. }
  66. }
  67. return h.listener.Start()
  68. }
  69. func (h *Inbound) Close() error {
  70. return common.Close(
  71. h.listener,
  72. h.tlsConfig,
  73. )
  74. }
  75. func (h *Inbound) NewConnectionEx(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
  76. err := h.newConnection(ctx, conn, metadata, onClose)
  77. N.CloseOnHandshakeFailure(conn, onClose, err)
  78. if err != nil {
  79. if E.IsClosedOrCanceled(err) {
  80. h.logger.DebugContext(ctx, "connection closed: ", err)
  81. } else {
  82. h.logger.ErrorContext(ctx, E.Cause(err, "process connection from ", metadata.Source))
  83. }
  84. }
  85. }
  86. func (h *Inbound) newConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) error {
  87. var err error
  88. if h.tlsConfig != nil {
  89. conn, err = tls.ServerHandshake(ctx, conn, h.tlsConfig)
  90. if err != nil {
  91. return err
  92. }
  93. }
  94. return http.HandleConnectionEx(ctx, conn, std_bufio.NewReader(conn), h.authenticator, nil, adapter.NewUpstreamHandlerEx(metadata, h.newUserConnection, h.streamUserPacketConnection), metadata.Source, onClose)
  95. }
  96. func (h *Inbound) newUserConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
  97. metadata.Inbound = h.Tag()
  98. metadata.InboundType = h.Type()
  99. metadata.InboundDetour = h.listener.ListenOptions().Detour
  100. metadata.InboundOptions = h.listener.ListenOptions().InboundOptions
  101. user, loaded := auth.UserFromContext[string](ctx)
  102. if !loaded {
  103. h.logger.InfoContext(ctx, "inbound connection to ", metadata.Destination)
  104. h.router.RouteConnectionEx(ctx, conn, metadata, onClose)
  105. return
  106. }
  107. metadata.User = user
  108. h.logger.InfoContext(ctx, "[", user, "] inbound connection to ", metadata.Destination)
  109. h.router.RouteConnectionEx(ctx, conn, metadata, onClose)
  110. }
  111. func (h *Inbound) streamUserPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
  112. metadata.Inbound = h.Tag()
  113. metadata.InboundType = h.Type()
  114. metadata.InboundDetour = h.listener.ListenOptions().Detour
  115. metadata.InboundOptions = h.listener.ListenOptions().InboundOptions
  116. user, loaded := auth.UserFromContext[string](ctx)
  117. if !loaded {
  118. h.logger.InfoContext(ctx, "inbound packet connection to ", metadata.Destination)
  119. h.router.RoutePacketConnectionEx(ctx, conn, metadata, onClose)
  120. return
  121. }
  122. metadata.User = user
  123. h.logger.InfoContext(ctx, "[", user, "] inbound packet connection to ", metadata.Destination)
  124. h.router.RoutePacketConnectionEx(ctx, conn, metadata, onClose)
  125. }