inbound.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package mixed
  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/uot"
  10. C "github.com/sagernet/sing-box/constant"
  11. "github.com/sagernet/sing-box/log"
  12. "github.com/sagernet/sing-box/option"
  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. "github.com/sagernet/sing/protocol/socks"
  18. "github.com/sagernet/sing/protocol/socks/socks4"
  19. "github.com/sagernet/sing/protocol/socks/socks5"
  20. )
  21. func RegisterInbound(registry *inbound.Registry) {
  22. inbound.Register[option.HTTPMixedInboundOptions](registry, C.TypeMixed, NewInbound)
  23. }
  24. var _ adapter.TCPInjectableInbound = (*Inbound)(nil)
  25. type Inbound struct {
  26. inbound.Adapter
  27. router adapter.ConnectionRouterEx
  28. logger log.ContextLogger
  29. listener *listener.Listener
  30. authenticator *auth.Authenticator
  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.TypeMixed, tag),
  35. router: uot.NewRouter(router, logger),
  36. logger: logger,
  37. authenticator: auth.NewAuthenticator(options.Users),
  38. }
  39. inbound.listener = listener.New(listener.Options{
  40. Context: ctx,
  41. Logger: logger,
  42. Network: []string{N.NetworkTCP},
  43. Listen: options.ListenOptions,
  44. ConnectionHandler: inbound,
  45. SetSystemProxy: options.SetSystemProxy,
  46. SystemProxySOCKS: true,
  47. })
  48. return inbound, nil
  49. }
  50. func (h *Inbound) Start(stage adapter.StartStage) error {
  51. if stage != adapter.StartStateStart {
  52. return nil
  53. }
  54. return h.listener.Start()
  55. }
  56. func (h *Inbound) Close() error {
  57. return h.listener.Close()
  58. }
  59. func (h *Inbound) NewConnectionEx(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
  60. err := h.newConnection(ctx, conn, metadata, onClose)
  61. N.CloseOnHandshakeFailure(conn, onClose, err)
  62. if err != nil {
  63. if E.IsClosedOrCanceled(err) {
  64. h.logger.DebugContext(ctx, "connection closed: ", err)
  65. } else {
  66. h.logger.ErrorContext(ctx, E.Cause(err, "process connection from ", metadata.Source))
  67. }
  68. }
  69. }
  70. func (h *Inbound) newConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) error {
  71. reader := std_bufio.NewReader(conn)
  72. headerBytes, err := reader.Peek(1)
  73. if err != nil {
  74. return E.Cause(err, "peek first byte")
  75. }
  76. switch headerBytes[0] {
  77. case socks4.Version, socks5.Version:
  78. return socks.HandleConnectionEx(ctx, conn, reader, h.authenticator, nil, adapter.NewUpstreamHandlerEx(metadata, h.newUserConnection, h.streamUserPacketConnection), metadata.Source, metadata.Destination, onClose)
  79. default:
  80. return http.HandleConnectionEx(ctx, conn, reader, h.authenticator, nil, adapter.NewUpstreamHandlerEx(metadata, h.newUserConnection, h.streamUserPacketConnection), metadata.Source, onClose)
  81. }
  82. }
  83. func (h *Inbound) newUserConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
  84. metadata.Inbound = h.Tag()
  85. metadata.InboundType = h.Type()
  86. user, loaded := auth.UserFromContext[string](ctx)
  87. if !loaded {
  88. h.logger.InfoContext(ctx, "inbound connection to ", metadata.Destination)
  89. h.router.RouteConnectionEx(ctx, conn, metadata, onClose)
  90. return
  91. }
  92. metadata.User = user
  93. h.logger.InfoContext(ctx, "[", user, "] inbound connection to ", metadata.Destination)
  94. h.router.RouteConnectionEx(ctx, conn, metadata, onClose)
  95. }
  96. func (h *Inbound) streamUserPacketConnection(ctx context.Context, conn N.PacketConn, 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 packet connection to ", metadata.Destination)
  102. h.router.RoutePacketConnectionEx(ctx, conn, metadata, onClose)
  103. return
  104. }
  105. metadata.User = user
  106. h.logger.InfoContext(ctx, "[", user, "] inbound packet connection to ", metadata.Destination)
  107. h.router.RoutePacketConnectionEx(ctx, conn, metadata, onClose)
  108. }