http.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package inbound
  2. import (
  3. std_bufio "bufio"
  4. "context"
  5. "net"
  6. "github.com/sagernet/sing-box/adapter"
  7. "github.com/sagernet/sing-box/common/tls"
  8. "github.com/sagernet/sing-box/common/uot"
  9. C "github.com/sagernet/sing-box/constant"
  10. "github.com/sagernet/sing-box/log"
  11. "github.com/sagernet/sing-box/option"
  12. "github.com/sagernet/sing/common"
  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. )
  18. var (
  19. _ adapter.Inbound = (*HTTP)(nil)
  20. _ adapter.TCPInjectableInbound = (*HTTP)(nil)
  21. )
  22. type HTTP struct {
  23. myInboundAdapter
  24. authenticator *auth.Authenticator
  25. tlsConfig tls.ServerConfig
  26. }
  27. func NewHTTP(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.HTTPMixedInboundOptions) (*HTTP, error) {
  28. inbound := &HTTP{
  29. myInboundAdapter: myInboundAdapter{
  30. protocol: C.TypeHTTP,
  31. network: []string{N.NetworkTCP},
  32. ctx: ctx,
  33. router: uot.NewRouter(router, logger),
  34. logger: logger,
  35. tag: tag,
  36. listenOptions: options.ListenOptions,
  37. setSystemProxy: options.SetSystemProxy,
  38. },
  39. authenticator: auth.NewAuthenticator(options.Users),
  40. }
  41. if options.TLS != nil {
  42. tlsConfig, err := tls.NewServer(ctx, logger, common.PtrValueOrDefault(options.TLS))
  43. if err != nil {
  44. return nil, err
  45. }
  46. inbound.tlsConfig = tlsConfig
  47. }
  48. inbound.connHandler = inbound
  49. return inbound, nil
  50. }
  51. func (h *HTTP) Start() error {
  52. if h.tlsConfig != nil {
  53. err := h.tlsConfig.Start()
  54. if err != nil {
  55. return E.Cause(err, "create TLS config")
  56. }
  57. }
  58. return h.myInboundAdapter.Start()
  59. }
  60. func (h *HTTP) Close() error {
  61. return common.Close(
  62. &h.myInboundAdapter,
  63. h.tlsConfig,
  64. )
  65. }
  66. func (h *HTTP) NewConnectionEx(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
  67. err := h.newConnection(ctx, conn, metadata, onClose)
  68. N.CloseOnHandshakeFailure(conn, onClose, err)
  69. if err != nil {
  70. h.logger.ErrorContext(ctx, E.Cause(err, "process connection from ", metadata.Source))
  71. }
  72. }
  73. func (h *HTTP) newConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) error {
  74. var err error
  75. if h.tlsConfig != nil {
  76. conn, err = tls.ServerHandshake(ctx, conn, h.tlsConfig)
  77. if err != nil {
  78. return err
  79. }
  80. }
  81. return http.HandleConnectionEx(ctx, conn, std_bufio.NewReader(conn), h.authenticator, nil, h.upstreamUserHandlerEx(metadata), metadata.Source, onClose)
  82. }
  83. func (a *myInboundAdapter) upstreamUserHandlerEx(metadata adapter.InboundContext) adapter.UpstreamHandlerAdapterEx {
  84. return adapter.NewUpstreamHandlerEx(metadata, a.newUserConnection, a.streamUserPacketConnection)
  85. }
  86. func (a *myInboundAdapter) newUserConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
  87. user, loaded := auth.UserFromContext[string](ctx)
  88. if !loaded {
  89. a.logger.InfoContext(ctx, "inbound connection to ", metadata.Destination)
  90. a.router.RouteConnectionEx(ctx, conn, metadata, onClose)
  91. return
  92. }
  93. metadata.User = user
  94. a.logger.InfoContext(ctx, "[", user, "] inbound connection to ", metadata.Destination)
  95. a.router.RouteConnectionEx(ctx, conn, metadata, onClose)
  96. }
  97. func (a *myInboundAdapter) streamUserPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
  98. user, loaded := auth.UserFromContext[string](ctx)
  99. if !loaded {
  100. a.logger.InfoContext(ctx, "inbound packet connection to ", metadata.Destination)
  101. a.router.RoutePacketConnectionEx(ctx, conn, metadata, onClose)
  102. return
  103. }
  104. metadata.User = user
  105. a.logger.InfoContext(ctx, "[", user, "] inbound packet connection to ", metadata.Destination)
  106. a.router.RoutePacketConnectionEx(ctx, conn, metadata, onClose)
  107. }