hysteria.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //go:build with_quic
  2. package outbound
  3. import (
  4. "context"
  5. "net"
  6. "os"
  7. "github.com/sagernet/sing-box/adapter"
  8. "github.com/sagernet/sing-box/common/dialer"
  9. "github.com/sagernet/sing-box/common/humanize"
  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/bufio"
  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. var (
  22. _ adapter.Outbound = (*TUIC)(nil)
  23. _ adapter.InterfaceUpdateListener = (*TUIC)(nil)
  24. )
  25. type Hysteria struct {
  26. myOutboundAdapter
  27. client *hysteria.Client
  28. }
  29. func NewHysteria(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.HysteriaOutboundOptions) (*Hysteria, error) {
  30. options.UDPFragmentDefault = true
  31. if options.TLS == nil || !options.TLS.Enabled {
  32. return nil, C.ErrTLSRequired
  33. }
  34. tlsConfig, err := tls.NewClient(ctx, options.Server, common.PtrValueOrDefault(options.TLS))
  35. if err != nil {
  36. return nil, err
  37. }
  38. outboundDialer, err := dialer.New(router, options.DialerOptions)
  39. if err != nil {
  40. return nil, err
  41. }
  42. networkList := options.Network.Build()
  43. var password string
  44. if options.AuthString != "" {
  45. password = options.AuthString
  46. } else {
  47. password = string(options.Auth)
  48. }
  49. var sendBps, receiveBps uint64
  50. if len(options.Up) > 0 {
  51. sendBps, err = humanize.ParseBytes(options.Up)
  52. if err != nil {
  53. return nil, E.Cause(err, "invalid up speed format: ", options.Up)
  54. }
  55. } else {
  56. sendBps = uint64(options.UpMbps) * hysteria.MbpsToBps
  57. }
  58. if len(options.Down) > 0 {
  59. receiveBps, err = humanize.ParseBytes(options.Down)
  60. if receiveBps == 0 {
  61. return nil, E.New("invalid down speed format: ", options.Down)
  62. }
  63. } else {
  64. receiveBps = uint64(options.DownMbps) * hysteria.MbpsToBps
  65. }
  66. client, err := hysteria.NewClient(hysteria.ClientOptions{
  67. Context: ctx,
  68. Dialer: outboundDialer,
  69. Logger: logger,
  70. ServerAddress: options.ServerOptions.Build(),
  71. SendBPS: sendBps,
  72. ReceiveBPS: receiveBps,
  73. XPlusPassword: options.Obfs,
  74. Password: password,
  75. TLSConfig: tlsConfig,
  76. UDPDisabled: !common.Contains(networkList, N.NetworkUDP),
  77. ConnReceiveWindow: options.ReceiveWindowConn,
  78. StreamReceiveWindow: options.ReceiveWindow,
  79. DisableMTUDiscovery: options.DisableMTUDiscovery,
  80. })
  81. if err != nil {
  82. return nil, err
  83. }
  84. return &Hysteria{
  85. myOutboundAdapter: myOutboundAdapter{
  86. protocol: C.TypeHysteria,
  87. network: networkList,
  88. router: router,
  89. logger: logger,
  90. tag: tag,
  91. dependencies: withDialerDependency(options.DialerOptions),
  92. },
  93. client: client,
  94. }, nil
  95. }
  96. func (h *Hysteria) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
  97. switch N.NetworkName(network) {
  98. case N.NetworkTCP:
  99. h.logger.InfoContext(ctx, "outbound connection to ", destination)
  100. return h.client.DialConn(ctx, destination)
  101. case N.NetworkUDP:
  102. conn, err := h.ListenPacket(ctx, destination)
  103. if err != nil {
  104. return nil, err
  105. }
  106. return bufio.NewBindPacketConn(conn, destination), nil
  107. default:
  108. return nil, E.New("unsupported network: ", network)
  109. }
  110. }
  111. func (h *Hysteria) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
  112. h.logger.InfoContext(ctx, "outbound packet connection to ", destination)
  113. return h.client.ListenPacket(ctx, destination)
  114. }
  115. func (h *Hysteria) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
  116. return NewConnection(ctx, h, conn, metadata)
  117. }
  118. func (h *Hysteria) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
  119. return NewPacketConnection(ctx, h, conn, metadata)
  120. }
  121. func (h *Hysteria) InterfaceUpdated() error {
  122. return h.client.CloseWithError(E.New("network changed"))
  123. }
  124. func (h *Hysteria) Close() error {
  125. return h.client.CloseWithError(os.ErrClosed)
  126. }