inbound.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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() error {
  58. if h.tlsConfig != nil {
  59. err := h.tlsConfig.Start()
  60. if err != nil {
  61. return E.Cause(err, "create TLS config")
  62. }
  63. }
  64. return h.listener.Start()
  65. }
  66. func (h *Inbound) Close() error {
  67. return common.Close(
  68. &h.listener,
  69. h.tlsConfig,
  70. )
  71. }
  72. func (h *Inbound) NewConnectionEx(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
  73. err := h.newConnection(ctx, conn, metadata, onClose)
  74. N.CloseOnHandshakeFailure(conn, onClose, err)
  75. if err != nil {
  76. h.logger.ErrorContext(ctx, E.Cause(err, "process connection from ", metadata.Source))
  77. }
  78. }
  79. func (h *Inbound) newConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) error {
  80. var err error
  81. if h.tlsConfig != nil {
  82. conn, err = tls.ServerHandshake(ctx, conn, h.tlsConfig)
  83. if err != nil {
  84. return err
  85. }
  86. }
  87. return http.HandleConnectionEx(ctx, conn, std_bufio.NewReader(conn), h.authenticator, nil, adapter.NewUpstreamHandlerEx(metadata, h.newUserConnection, h.streamUserPacketConnection), metadata.Source, onClose)
  88. }
  89. func (h *Inbound) newUserConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
  90. user, loaded := auth.UserFromContext[string](ctx)
  91. if !loaded {
  92. h.logger.InfoContext(ctx, "inbound connection to ", metadata.Destination)
  93. h.router.RouteConnectionEx(ctx, conn, metadata, onClose)
  94. return
  95. }
  96. metadata.User = user
  97. h.logger.InfoContext(ctx, "[", user, "] inbound connection to ", metadata.Destination)
  98. h.router.RouteConnectionEx(ctx, conn, metadata, onClose)
  99. }
  100. func (h *Inbound) streamUserPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
  101. user, loaded := auth.UserFromContext[string](ctx)
  102. if !loaded {
  103. h.logger.InfoContext(ctx, "inbound packet connection to ", metadata.Destination)
  104. h.router.RoutePacketConnectionEx(ctx, conn, metadata, onClose)
  105. return
  106. }
  107. metadata.User = user
  108. h.logger.InfoContext(ctx, "[", user, "] inbound packet connection to ", metadata.Destination)
  109. h.router.RoutePacketConnectionEx(ctx, conn, metadata, onClose)
  110. }