inbound.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. metadata.InboundDetour = h.listener.ListenOptions().Detour
  119. metadata.InboundOptions = h.listener.ListenOptions().InboundOptions
  120. metadata.OriginDestination = h.listener.UDPAddr()
  121. metadata.Source = source
  122. metadata.Destination = destination
  123. h.logger.InfoContext(ctx, "inbound connection from ", metadata.Source)
  124. userID, _ := auth.UserFromContext[int](ctx)
  125. if userName := h.userNameList[userID]; userName != "" {
  126. metadata.User = userName
  127. h.logger.InfoContext(ctx, "[", userName, "] inbound connection to ", metadata.Destination)
  128. } else {
  129. h.logger.InfoContext(ctx, "inbound connection to ", metadata.Destination)
  130. }
  131. h.router.RouteConnectionEx(ctx, conn, metadata, onClose)
  132. }
  133. func (h *Inbound) NewPacketConnectionEx(ctx context.Context, conn N.PacketConn, source M.Socksaddr, destination M.Socksaddr, onClose N.CloseHandlerFunc) {
  134. ctx = log.ContextWithNewID(ctx)
  135. var metadata adapter.InboundContext
  136. metadata.Inbound = h.Tag()
  137. metadata.InboundType = h.Type()
  138. metadata.InboundDetour = h.listener.ListenOptions().Detour
  139. metadata.InboundOptions = h.listener.ListenOptions().InboundOptions
  140. metadata.OriginDestination = h.listener.UDPAddr()
  141. metadata.Source = source
  142. metadata.Destination = destination
  143. h.logger.InfoContext(ctx, "inbound packet connection from ", metadata.Source)
  144. userID, _ := auth.UserFromContext[int](ctx)
  145. if userName := h.userNameList[userID]; userName != "" {
  146. metadata.User = userName
  147. h.logger.InfoContext(ctx, "[", userName, "] inbound packet connection to ", metadata.Destination)
  148. } else {
  149. h.logger.InfoContext(ctx, "inbound packet connection to ", metadata.Destination)
  150. }
  151. h.router.RoutePacketConnectionEx(ctx, conn, metadata, onClose)
  152. }
  153. func (h *Inbound) Start(stage adapter.StartStage) error {
  154. if stage != adapter.StartStateStart {
  155. return nil
  156. }
  157. if h.tlsConfig != nil {
  158. err := h.tlsConfig.Start()
  159. if err != nil {
  160. return err
  161. }
  162. }
  163. packetConn, err := h.listener.ListenUDP()
  164. if err != nil {
  165. return err
  166. }
  167. return h.service.Start(packetConn)
  168. }
  169. func (h *Inbound) Close() error {
  170. return common.Close(
  171. h.listener,
  172. h.tlsConfig,
  173. common.PtrOrNil(h.service),
  174. )
  175. }