outbound.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package hysteria2
  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-quic/hysteria2"
  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.Hysteria2OutboundOptions](registry, C.TypeHysteria2, 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 *hysteria2.Client
  35. }
  36. func NewOutbound(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.Hysteria2OutboundOptions) (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, logger, options.Server, common.PtrValueOrDefault(options.TLS))
  42. if err != nil {
  43. return nil, err
  44. }
  45. var salamanderPassword string
  46. if options.Obfs != nil {
  47. if options.Obfs.Password == "" {
  48. return nil, E.New("missing obfs password")
  49. }
  50. switch options.Obfs.Type {
  51. case hysteria2.ObfsTypeSalamander:
  52. salamanderPassword = options.Obfs.Password
  53. default:
  54. return nil, E.New("unknown obfs type: ", options.Obfs.Type)
  55. }
  56. }
  57. outboundDialer, err := dialer.New(ctx, options.DialerOptions, options.ServerIsDomain())
  58. if err != nil {
  59. return nil, err
  60. }
  61. networkList := options.Network.Build()
  62. client, err := hysteria2.NewClient(hysteria2.ClientOptions{
  63. Context: ctx,
  64. Dialer: outboundDialer,
  65. Logger: logger,
  66. BrutalDebug: options.BrutalDebug,
  67. ServerAddress: options.ServerOptions.Build(),
  68. ServerPorts: options.ServerPorts,
  69. HopInterval: time.Duration(options.HopInterval),
  70. SendBPS: uint64(options.UpMbps * hysteria.MbpsToBps),
  71. ReceiveBPS: uint64(options.DownMbps * hysteria.MbpsToBps),
  72. SalamanderPassword: salamanderPassword,
  73. Password: options.Password,
  74. TLSConfig: tlsConfig,
  75. UDPDisabled: !common.Contains(networkList, N.NetworkUDP),
  76. })
  77. if err != nil {
  78. return nil, err
  79. }
  80. return &Outbound{
  81. Adapter: outbound.NewAdapterWithDialerOptions(C.TypeHysteria2, tag, networkList, options.DialerOptions),
  82. logger: logger,
  83. client: client,
  84. }, nil
  85. }
  86. func (h *Outbound) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
  87. switch N.NetworkName(network) {
  88. case N.NetworkTCP:
  89. h.logger.InfoContext(ctx, "outbound connection to ", destination)
  90. return h.client.DialConn(ctx, destination)
  91. case N.NetworkUDP:
  92. conn, err := h.ListenPacket(ctx, destination)
  93. if err != nil {
  94. return nil, err
  95. }
  96. return bufio.NewBindPacketConn(conn, destination), nil
  97. default:
  98. return nil, E.New("unsupported network: ", network)
  99. }
  100. }
  101. func (h *Outbound) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
  102. h.logger.InfoContext(ctx, "outbound packet connection to ", destination)
  103. return h.client.ListenPacket(ctx)
  104. }
  105. func (h *Outbound) InterfaceUpdated() {
  106. h.client.CloseWithError(E.New("network changed"))
  107. }
  108. func (h *Outbound) Close() error {
  109. return h.client.CloseWithError(os.ErrClosed)
  110. }