outbound.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package hysteria
  2. import (
  3. "context"
  4. "net"
  5. "os"
  6. "github.com/sagernet/sing-box/adapter"
  7. "github.com/sagernet/sing-box/adapter/outbound"
  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-box/protocol/tuic"
  15. "github.com/sagernet/sing-quic/hysteria"
  16. "github.com/sagernet/sing/common"
  17. "github.com/sagernet/sing/common/bufio"
  18. E "github.com/sagernet/sing/common/exceptions"
  19. "github.com/sagernet/sing/common/logger"
  20. M "github.com/sagernet/sing/common/metadata"
  21. N "github.com/sagernet/sing/common/network"
  22. )
  23. func RegisterOutbound(registry *outbound.Registry) {
  24. outbound.Register[option.HysteriaOutboundOptions](registry, C.TypeHysteria, NewOutbound)
  25. }
  26. var (
  27. _ adapter.Outbound = (*tuic.Outbound)(nil)
  28. _ adapter.InterfaceUpdateListener = (*tuic.Outbound)(nil)
  29. )
  30. type Outbound struct {
  31. outbound.Adapter
  32. logger logger.ContextLogger
  33. client *hysteria.Client
  34. }
  35. func NewOutbound(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.HysteriaOutboundOptions) (adapter.Outbound, error) {
  36. options.UDPFragmentDefault = true
  37. if options.TLS == nil || !options.TLS.Enabled {
  38. return nil, C.ErrTLSRequired
  39. }
  40. tlsConfig, err := tls.NewClient(ctx, options.Server, common.PtrValueOrDefault(options.TLS))
  41. if err != nil {
  42. return nil, err
  43. }
  44. outboundDialer, err := dialer.New(router, options.DialerOptions)
  45. if err != nil {
  46. return nil, err
  47. }
  48. networkList := options.Network.Build()
  49. var password string
  50. if options.AuthString != "" {
  51. password = options.AuthString
  52. } else {
  53. password = string(options.Auth)
  54. }
  55. var sendBps, receiveBps uint64
  56. if len(options.Up) > 0 {
  57. sendBps, err = humanize.ParseBytes(options.Up)
  58. if err != nil {
  59. return nil, E.Cause(err, "invalid up speed format: ", options.Up)
  60. }
  61. } else {
  62. sendBps = uint64(options.UpMbps) * hysteria.MbpsToBps
  63. }
  64. if len(options.Down) > 0 {
  65. receiveBps, err = humanize.ParseBytes(options.Down)
  66. if err != nil {
  67. return nil, E.Cause(err, "invalid down speed format: ", options.Down)
  68. }
  69. } else {
  70. receiveBps = uint64(options.DownMbps) * hysteria.MbpsToBps
  71. }
  72. client, err := hysteria.NewClient(hysteria.ClientOptions{
  73. Context: ctx,
  74. Dialer: outboundDialer,
  75. Logger: logger,
  76. ServerAddress: options.ServerOptions.Build(),
  77. SendBPS: sendBps,
  78. ReceiveBPS: receiveBps,
  79. XPlusPassword: options.Obfs,
  80. Password: password,
  81. TLSConfig: tlsConfig,
  82. UDPDisabled: !common.Contains(networkList, N.NetworkUDP),
  83. ConnReceiveWindow: options.ReceiveWindowConn,
  84. StreamReceiveWindow: options.ReceiveWindow,
  85. DisableMTUDiscovery: options.DisableMTUDiscovery,
  86. })
  87. if err != nil {
  88. return nil, err
  89. }
  90. return &Outbound{
  91. Adapter: outbound.NewAdapterWithDialerOptions(C.TypeHysteria, networkList, tag, options.DialerOptions),
  92. logger: logger,
  93. client: client,
  94. }, nil
  95. }
  96. func (h *Outbound) 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 *Outbound) 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 *Outbound) InterfaceUpdated() {
  116. h.client.CloseWithError(E.New("network changed"))
  117. }
  118. func (h *Outbound) Close() error {
  119. return h.client.CloseWithError(os.ErrClosed)
  120. }