inbound.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. if h.tlsConfig != nil {
  77. tlsConn, err := tls.ServerHandshake(ctx, conn, h.tlsConfig)
  78. if err != nil {
  79. N.CloseOnHandshakeFailure(conn, onClose, err)
  80. h.logger.ErrorContext(ctx, E.Cause(err, "process connection from ", metadata.Source, ": TLS handshake"))
  81. return
  82. }
  83. conn = tlsConn
  84. }
  85. err := http.HandleConnectionEx(ctx, conn, std_bufio.NewReader(conn), h.authenticator, adapter.NewUpstreamHandlerEx(metadata, h.newUserConnection, h.streamUserPacketConnection), metadata.Source, onClose)
  86. if err != nil {
  87. N.CloseOnHandshakeFailure(conn, onClose, err)
  88. h.logger.ErrorContext(ctx, E.Cause(err, "process connection from ", metadata.Source))
  89. }
  90. }
  91. func (h *Inbound) newUserConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
  92. metadata.Inbound = h.Tag()
  93. metadata.InboundType = h.Type()
  94. user, loaded := auth.UserFromContext[string](ctx)
  95. if !loaded {
  96. h.logger.InfoContext(ctx, "inbound connection to ", metadata.Destination)
  97. h.router.RouteConnectionEx(ctx, conn, metadata, onClose)
  98. return
  99. }
  100. metadata.User = user
  101. h.logger.InfoContext(ctx, "[", user, "] inbound connection to ", metadata.Destination)
  102. h.router.RouteConnectionEx(ctx, conn, metadata, onClose)
  103. }
  104. func (h *Inbound) streamUserPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
  105. metadata.Inbound = h.Tag()
  106. metadata.InboundType = h.Type()
  107. user, loaded := auth.UserFromContext[string](ctx)
  108. if !loaded {
  109. h.logger.InfoContext(ctx, "inbound packet connection to ", metadata.Destination)
  110. h.router.RoutePacketConnectionEx(ctx, conn, metadata, onClose)
  111. return
  112. }
  113. metadata.User = user
  114. h.logger.InfoContext(ctx, "[", user, "] inbound packet connection to ", metadata.Destination)
  115. h.router.RoutePacketConnectionEx(ctx, conn, metadata, onClose)
  116. }