inbound.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package tuic
  2. import (
  3. "context"
  4. "net"
  5. "time"
  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/tls"
  10. "github.com/sagernet/sing-box/common/uot"
  11. C "github.com/sagernet/sing-box/constant"
  12. "github.com/sagernet/sing-box/log"
  13. "github.com/sagernet/sing-box/option"
  14. "github.com/sagernet/sing-quic/tuic"
  15. "github.com/sagernet/sing/common"
  16. "github.com/sagernet/sing/common/auth"
  17. E "github.com/sagernet/sing/common/exceptions"
  18. N "github.com/sagernet/sing/common/network"
  19. "github.com/gofrs/uuid/v5"
  20. )
  21. func RegisterInbound(registry *inbound.Registry) {
  22. inbound.Register[option.TUICInboundOptions](registry, C.TypeTUIC, NewInbound)
  23. }
  24. type Inbound struct {
  25. inbound.Adapter
  26. router adapter.ConnectionRouterEx
  27. logger log.ContextLogger
  28. listener *listener.Listener
  29. tlsConfig tls.ServerConfig
  30. server *tuic.Service[int]
  31. userNameList []string
  32. }
  33. func NewInbound(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.TUICInboundOptions) (adapter.Inbound, error) {
  34. options.UDPFragmentDefault = true
  35. if options.TLS == nil || !options.TLS.Enabled {
  36. return nil, C.ErrTLSRequired
  37. }
  38. tlsConfig, err := tls.NewServer(ctx, logger, common.PtrValueOrDefault(options.TLS))
  39. if err != nil {
  40. return nil, err
  41. }
  42. inbound := &Inbound{
  43. Adapter: inbound.NewAdapter(C.TypeTUIC, tag),
  44. router: uot.NewRouter(router, logger),
  45. listener: listener.New(listener.Options{
  46. Context: ctx,
  47. Logger: logger,
  48. Listen: options.ListenOptions,
  49. }),
  50. tlsConfig: tlsConfig,
  51. }
  52. var udpTimeout time.Duration
  53. if options.UDPTimeout != 0 {
  54. udpTimeout = time.Duration(options.UDPTimeout)
  55. } else {
  56. udpTimeout = C.UDPTimeout
  57. }
  58. service, err := tuic.NewService[int](tuic.ServiceOptions{
  59. Context: ctx,
  60. Logger: logger,
  61. TLSConfig: tlsConfig,
  62. CongestionControl: options.CongestionControl,
  63. AuthTimeout: time.Duration(options.AuthTimeout),
  64. ZeroRTTHandshake: options.ZeroRTTHandshake,
  65. Heartbeat: time.Duration(options.Heartbeat),
  66. UDPTimeout: udpTimeout,
  67. Handler: adapter.NewUpstreamHandler(adapter.InboundContext{}, inbound.newConnection, inbound.newPacketConnection, nil),
  68. })
  69. if err != nil {
  70. return nil, err
  71. }
  72. var userList []int
  73. var userNameList []string
  74. var userUUIDList [][16]byte
  75. var userPasswordList []string
  76. for index, user := range options.Users {
  77. if user.UUID == "" {
  78. return nil, E.New("missing uuid for user ", index)
  79. }
  80. userUUID, err := uuid.FromString(user.UUID)
  81. if err != nil {
  82. return nil, E.Cause(err, "invalid uuid for user ", index)
  83. }
  84. userList = append(userList, index)
  85. userNameList = append(userNameList, user.Name)
  86. userUUIDList = append(userUUIDList, userUUID)
  87. userPasswordList = append(userPasswordList, user.Password)
  88. }
  89. service.UpdateUsers(userList, userUUIDList, userPasswordList)
  90. inbound.server = service
  91. inbound.userNameList = userNameList
  92. return inbound, nil
  93. }
  94. func (h *Inbound) newConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
  95. ctx = log.ContextWithNewID(ctx)
  96. metadata.Inbound = h.Tag()
  97. metadata.InboundType = h.Type()
  98. metadata.InboundDetour = h.listener.ListenOptions().Detour
  99. metadata.InboundOptions = h.listener.ListenOptions().InboundOptions
  100. h.logger.InfoContext(ctx, "inbound connection from ", metadata.Source)
  101. userID, _ := auth.UserFromContext[int](ctx)
  102. if userName := h.userNameList[userID]; userName != "" {
  103. metadata.User = userName
  104. h.logger.InfoContext(ctx, "[", userName, "] inbound connection to ", metadata.Destination)
  105. } else {
  106. h.logger.InfoContext(ctx, "inbound connection to ", metadata.Destination)
  107. }
  108. return h.router.RouteConnection(ctx, conn, metadata)
  109. }
  110. func (h *Inbound) newPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
  111. ctx = log.ContextWithNewID(ctx)
  112. metadata.Inbound = h.Tag()
  113. metadata.InboundType = h.Type()
  114. metadata.InboundDetour = h.listener.ListenOptions().Detour
  115. metadata.InboundOptions = h.listener.ListenOptions().InboundOptions
  116. metadata.OriginDestination = h.listener.UDPAddr()
  117. h.logger.InfoContext(ctx, "inbound packet connection from ", metadata.Source)
  118. userID, _ := auth.UserFromContext[int](ctx)
  119. if userName := h.userNameList[userID]; userName != "" {
  120. metadata.User = userName
  121. h.logger.InfoContext(ctx, "[", userName, "] inbound packet connection to ", metadata.Destination)
  122. } else {
  123. h.logger.InfoContext(ctx, "inbound packet connection to ", metadata.Destination)
  124. }
  125. return h.router.RoutePacketConnection(ctx, conn, metadata)
  126. }
  127. func (h *Inbound) Start() error {
  128. if h.tlsConfig != nil {
  129. err := h.tlsConfig.Start()
  130. if err != nil {
  131. return err
  132. }
  133. }
  134. packetConn, err := h.listener.ListenUDP()
  135. if err != nil {
  136. return err
  137. }
  138. return h.server.Start(packetConn)
  139. }
  140. func (h *Inbound) Close() error {
  141. return common.Close(
  142. &h.listener,
  143. h.tlsConfig,
  144. common.PtrOrNil(h.server),
  145. )
  146. }