inbound.go 5.9 KB

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