inbound.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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() error {
  51. return h.listener.Start()
  52. }
  53. func (h *Inbound) Close() error {
  54. return h.listener.Close()
  55. }
  56. func (h *Inbound) NewConnectionEx(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
  57. err := h.newConnection(ctx, conn, metadata, onClose)
  58. N.CloseOnHandshakeFailure(conn, onClose, err)
  59. if err != nil {
  60. h.logger.ErrorContext(ctx, E.Cause(err, "process connection from ", metadata.Source))
  61. }
  62. }
  63. func (h *Inbound) newConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) error {
  64. reader := std_bufio.NewReader(conn)
  65. headerBytes, err := reader.Peek(1)
  66. if err != nil {
  67. return E.Cause(err, "peek first byte")
  68. }
  69. switch headerBytes[0] {
  70. case socks4.Version, socks5.Version:
  71. return socks.HandleConnectionEx(ctx, conn, reader, h.authenticator, nil, adapter.NewUpstreamHandlerEx(metadata, h.newUserConnection, h.streamUserPacketConnection), metadata.Source, metadata.Destination, onClose)
  72. default:
  73. return http.HandleConnectionEx(ctx, conn, reader, h.authenticator, nil, adapter.NewUpstreamHandlerEx(metadata, h.newUserConnection, h.streamUserPacketConnection), metadata.Source, onClose)
  74. }
  75. }
  76. func (h *Inbound) newUserConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
  77. user, loaded := auth.UserFromContext[string](ctx)
  78. if !loaded {
  79. h.logger.InfoContext(ctx, "inbound connection to ", metadata.Destination)
  80. h.router.RouteConnectionEx(ctx, conn, metadata, onClose)
  81. return
  82. }
  83. metadata.User = user
  84. h.logger.InfoContext(ctx, "[", user, "] inbound connection to ", metadata.Destination)
  85. h.router.RouteConnectionEx(ctx, conn, metadata, onClose)
  86. }
  87. func (h *Inbound) streamUserPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
  88. user, loaded := auth.UserFromContext[string](ctx)
  89. if !loaded {
  90. h.logger.InfoContext(ctx, "inbound packet connection to ", metadata.Destination)
  91. h.router.RoutePacketConnectionEx(ctx, conn, metadata, onClose)
  92. return
  93. }
  94. metadata.User = user
  95. h.logger.InfoContext(ctx, "[", user, "] inbound packet connection to ", metadata.Destination)
  96. h.router.RoutePacketConnectionEx(ctx, conn, metadata, onClose)
  97. }