inbound.go 5.3 KB

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