tuic.go 4.4 KB

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