http.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. M "github.com/sagernet/sing/common/metadata"
  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 *tls.Config
  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{C.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(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) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
  48. if h.tlsConfig != nil {
  49. conn = tls.Server(conn, h.tlsConfig)
  50. }
  51. return http.HandleConnection(ctx, conn, std_bufio.NewReader(conn), h.authenticator, h.upstreamUserHandler(metadata), M.Metadata{})
  52. }
  53. func (a *myInboundAdapter) upstreamUserHandler(metadata adapter.InboundContext) adapter.UpstreamHandlerAdapter {
  54. return adapter.NewUpstreamHandler(metadata, a.newUserConnection, a.streamUserPacketConnection, a)
  55. }
  56. func (a *myInboundAdapter) newUserConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
  57. user, loaded := auth.UserFromContext[string](ctx)
  58. if !loaded {
  59. a.logger.InfoContext(ctx, "inbound connection to ", metadata.Destination)
  60. return a.router.RouteConnection(ctx, conn, metadata)
  61. }
  62. metadata.User = user
  63. a.logger.InfoContext(ctx, "[", user, "] inbound connection to ", metadata.Destination)
  64. return a.router.RouteConnection(ctx, conn, metadata)
  65. }
  66. func (a *myInboundAdapter) streamUserPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
  67. a.logger.InfoContext(ctx, "inbound packet connection to ", metadata.Destination)
  68. return a.router.RoutePacketConnection(ctx, conn, metadata)
  69. }