http.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package inbound
  2. import (
  3. std_bufio "bufio"
  4. "context"
  5. "crypto/tls"
  6. "net"
  7. "github.com/sagernet/sing-box/adapter"
  8. C "github.com/sagernet/sing-box/constant"
  9. "github.com/sagernet/sing-box/log"
  10. "github.com/sagernet/sing-box/option"
  11. "github.com/sagernet/sing/common"
  12. "github.com/sagernet/sing/common/auth"
  13. E "github.com/sagernet/sing/common/exceptions"
  14. N "github.com/sagernet/sing/common/network"
  15. "github.com/sagernet/sing/protocol/http"
  16. )
  17. var _ adapter.Inbound = (*HTTP)(nil)
  18. type HTTP struct {
  19. myInboundAdapter
  20. authenticator auth.Authenticator
  21. tlsConfig *TLSConfig
  22. }
  23. func NewHTTP(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.HTTPMixedInboundOptions) (*HTTP, error) {
  24. inbound := &HTTP{
  25. myInboundAdapter: myInboundAdapter{
  26. protocol: C.TypeHTTP,
  27. network: []string{N.NetworkTCP},
  28. ctx: ctx,
  29. router: router,
  30. logger: logger,
  31. tag: tag,
  32. listenOptions: options.ListenOptions,
  33. setSystemProxy: options.SetSystemProxy,
  34. },
  35. authenticator: auth.NewAuthenticator(options.Users),
  36. }
  37. if options.TLS != nil {
  38. tlsConfig, err := NewTLSConfig(ctx, logger, common.PtrValueOrDefault(options.TLS))
  39. if err != nil {
  40. return nil, err
  41. }
  42. inbound.tlsConfig = tlsConfig
  43. }
  44. inbound.connHandler = inbound
  45. return inbound, nil
  46. }
  47. func (h *HTTP) Start() error {
  48. if h.tlsConfig != nil {
  49. err := h.tlsConfig.Start()
  50. if err != nil {
  51. return E.Cause(err, "create TLS config")
  52. }
  53. }
  54. return h.myInboundAdapter.Start()
  55. }
  56. func (h *HTTP) Close() error {
  57. return common.Close(
  58. &h.myInboundAdapter,
  59. common.PtrOrNil(h.tlsConfig),
  60. )
  61. }
  62. func (h *HTTP) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
  63. if h.tlsConfig != nil {
  64. conn = tls.Server(conn, h.tlsConfig.Config())
  65. }
  66. return http.HandleConnection(ctx, conn, std_bufio.NewReader(conn), h.authenticator, h.upstreamUserHandler(metadata), adapter.UpstreamMetadata(metadata))
  67. }
  68. func (a *myInboundAdapter) upstreamUserHandler(metadata adapter.InboundContext) adapter.UpstreamHandlerAdapter {
  69. return adapter.NewUpstreamHandler(metadata, a.newUserConnection, a.streamUserPacketConnection, a)
  70. }
  71. func (a *myInboundAdapter) newUserConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
  72. user, loaded := auth.UserFromContext[string](ctx)
  73. if !loaded {
  74. a.logger.InfoContext(ctx, "inbound connection to ", metadata.Destination)
  75. return a.router.RouteConnection(ctx, conn, metadata)
  76. }
  77. metadata.User = user
  78. a.logger.InfoContext(ctx, "[", user, "] inbound connection to ", metadata.Destination)
  79. return a.router.RouteConnection(ctx, conn, metadata)
  80. }
  81. func (a *myInboundAdapter) streamUserPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
  82. a.logger.InfoContext(ctx, "inbound packet connection to ", metadata.Destination)
  83. return a.router.RoutePacketConnection(ctx, conn, metadata)
  84. }