inbound.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package hysteria
  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. 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/hysteria"
  14. "github.com/sagernet/sing/common"
  15. "github.com/sagernet/sing/common/auth"
  16. M "github.com/sagernet/sing/common/metadata"
  17. N "github.com/sagernet/sing/common/network"
  18. )
  19. func RegisterInbound(registry *inbound.Registry) {
  20. inbound.Register[option.HysteriaInboundOptions](registry, C.TypeHysteria, NewInbound)
  21. }
  22. type Inbound struct {
  23. inbound.Adapter
  24. router adapter.Router
  25. logger log.ContextLogger
  26. listener *listener.Listener
  27. tlsConfig tls.ServerConfig
  28. service *hysteria.Service[int]
  29. userNameList []string
  30. }
  31. func NewInbound(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.HysteriaInboundOptions) (adapter.Inbound, error) {
  32. options.UDPFragmentDefault = true
  33. if options.TLS == nil || !options.TLS.Enabled {
  34. return nil, C.ErrTLSRequired
  35. }
  36. tlsConfig, err := tls.NewServer(ctx, logger, common.PtrValueOrDefault(options.TLS))
  37. if err != nil {
  38. return nil, err
  39. }
  40. inbound := &Inbound{
  41. Adapter: inbound.NewAdapter(C.TypeHysteria, tag),
  42. router: router,
  43. logger: logger,
  44. listener: listener.New(listener.Options{
  45. Context: ctx,
  46. Logger: logger,
  47. Listen: options.ListenOptions,
  48. }),
  49. tlsConfig: tlsConfig,
  50. }
  51. var sendBps, receiveBps uint64
  52. if options.Up.Value() > 0 {
  53. sendBps = options.Up.Value()
  54. } else {
  55. sendBps = uint64(options.UpMbps) * hysteria.MbpsToBps
  56. }
  57. if options.Down.Value() > 0 {
  58. receiveBps = options.Down.Value()
  59. } else {
  60. receiveBps = uint64(options.DownMbps) * hysteria.MbpsToBps
  61. }
  62. var udpTimeout time.Duration
  63. if options.UDPTimeout != 0 {
  64. udpTimeout = time.Duration(options.UDPTimeout)
  65. } else {
  66. udpTimeout = C.UDPTimeout
  67. }
  68. service, err := hysteria.NewService[int](hysteria.ServiceOptions{
  69. Context: ctx,
  70. Logger: logger,
  71. SendBPS: sendBps,
  72. ReceiveBPS: receiveBps,
  73. XPlusPassword: options.Obfs,
  74. TLSConfig: tlsConfig,
  75. UDPTimeout: udpTimeout,
  76. Handler: inbound,
  77. // Legacy options
  78. ConnReceiveWindow: options.ReceiveWindowConn,
  79. StreamReceiveWindow: options.ReceiveWindowClient,
  80. MaxIncomingStreams: int64(options.MaxConnClient),
  81. DisableMTUDiscovery: options.DisableMTUDiscovery,
  82. })
  83. if err != nil {
  84. return nil, err
  85. }
  86. userList := make([]int, 0, len(options.Users))
  87. userNameList := make([]string, 0, len(options.Users))
  88. userPasswordList := make([]string, 0, len(options.Users))
  89. for index, user := range options.Users {
  90. userList = append(userList, index)
  91. userNameList = append(userNameList, user.Name)
  92. var password string
  93. if user.AuthString != "" {
  94. password = user.AuthString
  95. } else {
  96. password = string(user.Auth)
  97. }
  98. userPasswordList = append(userPasswordList, password)
  99. }
  100. service.UpdateUsers(userList, userPasswordList)
  101. inbound.service = service
  102. inbound.userNameList = userNameList
  103. return inbound, nil
  104. }
  105. func (h *Inbound) NewConnectionEx(ctx context.Context, conn net.Conn, source M.Socksaddr, destination M.Socksaddr, onClose N.CloseHandlerFunc) {
  106. ctx = log.ContextWithNewID(ctx)
  107. var metadata adapter.InboundContext
  108. metadata.Inbound = h.Tag()
  109. metadata.InboundType = h.Type()
  110. //nolint:staticcheck
  111. metadata.InboundDetour = h.listener.ListenOptions().Detour
  112. //nolint:staticcheck
  113. metadata.InboundOptions = h.listener.ListenOptions().InboundOptions
  114. metadata.OriginDestination = h.listener.UDPAddr()
  115. metadata.Source = source
  116. metadata.Destination = destination
  117. h.logger.InfoContext(ctx, "inbound 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 connection to ", metadata.Destination)
  122. } else {
  123. h.logger.InfoContext(ctx, "inbound connection to ", metadata.Destination)
  124. }
  125. h.router.RouteConnectionEx(ctx, conn, metadata, onClose)
  126. }
  127. func (h *Inbound) NewPacketConnectionEx(ctx context.Context, conn N.PacketConn, source M.Socksaddr, destination M.Socksaddr, onClose N.CloseHandlerFunc) {
  128. ctx = log.ContextWithNewID(ctx)
  129. var metadata adapter.InboundContext
  130. metadata.Inbound = h.Tag()
  131. metadata.InboundType = h.Type()
  132. //nolint:staticcheck
  133. metadata.InboundDetour = h.listener.ListenOptions().Detour
  134. //nolint:staticcheck
  135. metadata.InboundOptions = h.listener.ListenOptions().InboundOptions
  136. metadata.OriginDestination = h.listener.UDPAddr()
  137. metadata.Source = source
  138. metadata.Destination = destination
  139. h.logger.InfoContext(ctx, "inbound packet connection from ", metadata.Source)
  140. userID, _ := auth.UserFromContext[int](ctx)
  141. if userName := h.userNameList[userID]; userName != "" {
  142. metadata.User = userName
  143. h.logger.InfoContext(ctx, "[", userName, "] inbound packet connection to ", metadata.Destination)
  144. } else {
  145. h.logger.InfoContext(ctx, "inbound packet connection to ", metadata.Destination)
  146. }
  147. h.router.RoutePacketConnectionEx(ctx, conn, metadata, onClose)
  148. }
  149. func (h *Inbound) Start(stage adapter.StartStage) error {
  150. if stage != adapter.StartStateStart {
  151. return nil
  152. }
  153. if h.tlsConfig != nil {
  154. err := h.tlsConfig.Start()
  155. if err != nil {
  156. return err
  157. }
  158. }
  159. packetConn, err := h.listener.ListenUDP()
  160. if err != nil {
  161. return err
  162. }
  163. return h.service.Start(packetConn)
  164. }
  165. func (h *Inbound) Close() error {
  166. return common.Close(
  167. h.listener,
  168. h.tlsConfig,
  169. common.PtrOrNil(h.service),
  170. )
  171. }