outbound.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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/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, logger, options.Server, common.PtrValueOrDefault(options.TLS))
  41. if err != nil {
  42. return nil, err
  43. }
  44. outboundDialer, err := dialer.New(ctx, options.DialerOptions, options.ServerIsDomain())
  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 options.Up.Value() > 0 {
  57. sendBps = options.Up.Value()
  58. } else {
  59. sendBps = uint64(options.UpMbps) * hysteria.MbpsToBps
  60. }
  61. if options.Down.Value() > 0 {
  62. receiveBps = options.Down.Value()
  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. ServerPorts: options.ServerPorts,
  72. HopInterval: time.Duration(options.HopInterval),
  73. SendBPS: sendBps,
  74. ReceiveBPS: receiveBps,
  75. XPlusPassword: options.Obfs,
  76. Password: password,
  77. TLSConfig: tlsConfig,
  78. UDPDisabled: !common.Contains(networkList, N.NetworkUDP),
  79. ConnReceiveWindow: options.ReceiveWindowConn,
  80. StreamReceiveWindow: options.ReceiveWindow,
  81. DisableMTUDiscovery: options.DisableMTUDiscovery,
  82. })
  83. if err != nil {
  84. return nil, err
  85. }
  86. return &Outbound{
  87. Adapter: outbound.NewAdapterWithDialerOptions(C.TypeHysteria, tag, networkList, options.DialerOptions),
  88. logger: logger,
  89. client: client,
  90. }, nil
  91. }
  92. func (h *Outbound) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
  93. switch N.NetworkName(network) {
  94. case N.NetworkTCP:
  95. h.logger.InfoContext(ctx, "outbound connection to ", destination)
  96. return h.client.DialConn(ctx, destination)
  97. case N.NetworkUDP:
  98. conn, err := h.ListenPacket(ctx, destination)
  99. if err != nil {
  100. return nil, err
  101. }
  102. return bufio.NewBindPacketConn(conn, destination), nil
  103. default:
  104. return nil, E.New("unsupported network: ", network)
  105. }
  106. }
  107. func (h *Outbound) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
  108. h.logger.InfoContext(ctx, "outbound packet connection to ", destination)
  109. return h.client.ListenPacket(ctx, destination)
  110. }
  111. func (h *Outbound) InterfaceUpdated() {
  112. h.client.CloseWithError(E.New("network changed"))
  113. }
  114. func (h *Outbound) Close() error {
  115. return h.client.CloseWithError(os.ErrClosed)
  116. }