hysteria.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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/humanize"
  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. E "github.com/sagernet/sing/common/exceptions"
  17. N "github.com/sagernet/sing/common/network"
  18. )
  19. var _ adapter.Inbound = (*Hysteria)(nil)
  20. type Hysteria struct {
  21. myInboundAdapter
  22. tlsConfig tls.ServerConfig
  23. service *hysteria.Service[int]
  24. userNameList []string
  25. }
  26. func NewHysteria(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.HysteriaInboundOptions) (*Hysteria, error) {
  27. options.UDPFragmentDefault = true
  28. if options.TLS == nil || !options.TLS.Enabled {
  29. return nil, C.ErrTLSRequired
  30. }
  31. tlsConfig, err := tls.NewServer(ctx, logger, common.PtrValueOrDefault(options.TLS))
  32. if err != nil {
  33. return nil, err
  34. }
  35. inbound := &Hysteria{
  36. myInboundAdapter: myInboundAdapter{
  37. protocol: C.TypeHysteria,
  38. network: []string{N.NetworkUDP},
  39. ctx: ctx,
  40. router: router,
  41. logger: logger,
  42. tag: tag,
  43. listenOptions: options.ListenOptions,
  44. },
  45. tlsConfig: tlsConfig,
  46. }
  47. var sendBps, receiveBps uint64
  48. if len(options.Up) > 0 {
  49. sendBps, err = humanize.ParseBytes(options.Up)
  50. if err != nil {
  51. return nil, E.Cause(err, "invalid up speed format: ", options.Up)
  52. }
  53. } else {
  54. sendBps = uint64(options.UpMbps) * hysteria.MbpsToBps
  55. }
  56. if len(options.Down) > 0 {
  57. receiveBps, err = humanize.ParseBytes(options.Down)
  58. if receiveBps == 0 {
  59. return nil, E.New("invalid down speed format: ", options.Down)
  60. }
  61. } else {
  62. receiveBps = uint64(options.DownMbps) * hysteria.MbpsToBps
  63. }
  64. var udpTimeout time.Duration
  65. if options.UDPTimeout != 0 {
  66. udpTimeout = time.Duration(options.UDPTimeout)
  67. } else {
  68. udpTimeout = C.UDPTimeout
  69. }
  70. service, err := hysteria.NewService[int](hysteria.ServiceOptions{
  71. Context: ctx,
  72. Logger: logger,
  73. SendBPS: sendBps,
  74. ReceiveBPS: receiveBps,
  75. XPlusPassword: options.Obfs,
  76. TLSConfig: tlsConfig,
  77. UDPTimeout: udpTimeout,
  78. Handler: adapter.NewUpstreamHandler(adapter.InboundContext{}, inbound.newConnection, inbound.newPacketConnection, nil),
  79. // Legacy options
  80. ConnReceiveWindow: options.ReceiveWindowConn,
  81. StreamReceiveWindow: options.ReceiveWindowClient,
  82. MaxIncomingStreams: int64(options.MaxConnClient),
  83. DisableMTUDiscovery: options.DisableMTUDiscovery,
  84. })
  85. if err != nil {
  86. return nil, err
  87. }
  88. userList := make([]int, 0, len(options.Users))
  89. userNameList := make([]string, 0, len(options.Users))
  90. userPasswordList := make([]string, 0, len(options.Users))
  91. for index, user := range options.Users {
  92. userList = append(userList, index)
  93. userNameList = append(userNameList, user.Name)
  94. var password string
  95. if user.AuthString != "" {
  96. password = user.AuthString
  97. } else {
  98. password = string(user.Auth)
  99. }
  100. userPasswordList = append(userPasswordList, password)
  101. }
  102. service.UpdateUsers(userList, userPasswordList)
  103. inbound.service = service
  104. inbound.userNameList = userNameList
  105. return inbound, nil
  106. }
  107. func (h *Hysteria) newConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
  108. ctx = log.ContextWithNewID(ctx)
  109. metadata = h.createMetadata(conn, metadata)
  110. h.logger.InfoContext(ctx, "inbound connection from ", metadata.Source)
  111. userID, _ := auth.UserFromContext[int](ctx)
  112. if userName := h.userNameList[userID]; userName != "" {
  113. metadata.User = userName
  114. h.logger.InfoContext(ctx, "[", userName, "] inbound connection to ", metadata.Destination)
  115. } else {
  116. h.logger.InfoContext(ctx, "inbound connection to ", metadata.Destination)
  117. }
  118. return h.router.RouteConnection(ctx, conn, metadata)
  119. }
  120. func (h *Hysteria) newPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
  121. ctx = log.ContextWithNewID(ctx)
  122. metadata = h.createPacketMetadata(conn, metadata)
  123. h.logger.InfoContext(ctx, "inbound packet 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 packet connection to ", metadata.Destination)
  128. } else {
  129. h.logger.InfoContext(ctx, "inbound packet connection to ", metadata.Destination)
  130. }
  131. return h.router.RoutePacketConnection(ctx, conn, metadata)
  132. }
  133. func (h *Hysteria) Start() error {
  134. if h.tlsConfig != nil {
  135. err := h.tlsConfig.Start()
  136. if err != nil {
  137. return err
  138. }
  139. }
  140. packetConn, err := h.myInboundAdapter.ListenUDP()
  141. if err != nil {
  142. return err
  143. }
  144. return h.service.Start(packetConn)
  145. }
  146. func (h *Hysteria) Close() error {
  147. return common.Close(
  148. &h.myInboundAdapter,
  149. h.tlsConfig,
  150. common.PtrOrNil(h.service),
  151. )
  152. }