outbound.go 4.1 KB

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