outbound.go 3.6 KB

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