1
0

inbound.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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.NewServerWithOptions(tls.ServerOptions{
  41. Context: ctx,
  42. Logger: logger,
  43. Options: common.PtrValueOrDefault(options.TLS),
  44. KTLSCompatible: true,
  45. })
  46. if err != nil {
  47. return nil, err
  48. }
  49. inbound.tlsConfig = tlsConfig
  50. }
  51. inbound.listener = listener.New(listener.Options{
  52. Context: ctx,
  53. Logger: logger,
  54. Network: []string{N.NetworkTCP},
  55. Listen: options.ListenOptions,
  56. ConnectionHandler: inbound,
  57. SetSystemProxy: options.SetSystemProxy,
  58. SystemProxySOCKS: false,
  59. })
  60. return inbound, nil
  61. }
  62. func (h *Inbound) Start(stage adapter.StartStage) error {
  63. if stage != adapter.StartStateStart {
  64. return nil
  65. }
  66. if h.tlsConfig != nil {
  67. err := h.tlsConfig.Start()
  68. if err != nil {
  69. return E.Cause(err, "create TLS config")
  70. }
  71. }
  72. return h.listener.Start()
  73. }
  74. func (h *Inbound) Close() error {
  75. return common.Close(
  76. h.listener,
  77. h.tlsConfig,
  78. )
  79. }
  80. func (h *Inbound) NewConnectionEx(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
  81. if h.tlsConfig != nil {
  82. tlsConn, err := tls.ServerHandshake(ctx, conn, h.tlsConfig)
  83. if err != nil {
  84. N.CloseOnHandshakeFailure(conn, onClose, err)
  85. h.logger.ErrorContext(ctx, E.Cause(err, "process connection from ", metadata.Source, ": TLS handshake"))
  86. return
  87. }
  88. conn = tlsConn
  89. }
  90. err := http.HandleConnectionEx(ctx, conn, std_bufio.NewReader(conn), h.authenticator, adapter.NewUpstreamHandlerEx(metadata, h.newUserConnection, h.streamUserPacketConnection), metadata.Source, onClose)
  91. if err != nil {
  92. N.CloseOnHandshakeFailure(conn, onClose, err)
  93. h.logger.ErrorContext(ctx, E.Cause(err, "process connection from ", metadata.Source))
  94. }
  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. user, loaded := auth.UserFromContext[string](ctx)
  100. if !loaded {
  101. h.logger.InfoContext(ctx, "inbound connection to ", metadata.Destination)
  102. h.router.RouteConnectionEx(ctx, conn, metadata, onClose)
  103. return
  104. }
  105. metadata.User = user
  106. h.logger.InfoContext(ctx, "[", user, "] inbound connection to ", metadata.Destination)
  107. h.router.RouteConnectionEx(ctx, conn, metadata, onClose)
  108. }
  109. func (h *Inbound) streamUserPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
  110. metadata.Inbound = h.Tag()
  111. metadata.InboundType = h.Type()
  112. user, loaded := auth.UserFromContext[string](ctx)
  113. if !loaded {
  114. h.logger.InfoContext(ctx, "inbound packet connection to ", metadata.Destination)
  115. h.router.RoutePacketConnectionEx(ctx, conn, metadata, onClose)
  116. return
  117. }
  118. metadata.User = user
  119. h.logger.InfoContext(ctx, "[", user, "] inbound packet connection to ", metadata.Destination)
  120. h.router.RoutePacketConnectionEx(ctx, conn, metadata, onClose)
  121. }