outbound.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package tuic
  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-quic/tuic"
  15. "github.com/sagernet/sing/common"
  16. "github.com/sagernet/sing/common/bufio"
  17. E "github.com/sagernet/sing/common/exceptions"
  18. "github.com/sagernet/sing/common/logger"
  19. M "github.com/sagernet/sing/common/metadata"
  20. N "github.com/sagernet/sing/common/network"
  21. "github.com/sagernet/sing/common/uot"
  22. "github.com/gofrs/uuid/v5"
  23. )
  24. func RegisterOutbound(registry *outbound.Registry) {
  25. outbound.Register[option.TUICOutboundOptions](registry, C.TypeTUIC, NewOutbound)
  26. }
  27. var _ adapter.InterfaceUpdateListener = (*Outbound)(nil)
  28. type Outbound struct {
  29. outbound.Adapter
  30. logger logger.ContextLogger
  31. client *tuic.Client
  32. udpStream bool
  33. }
  34. func NewOutbound(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.TUICOutboundOptions) (adapter.Outbound, error) {
  35. options.UDPFragmentDefault = true
  36. if options.TLS == nil || !options.TLS.Enabled {
  37. return nil, C.ErrTLSRequired
  38. }
  39. tlsConfig, err := tls.NewClient(ctx, options.Server, common.PtrValueOrDefault(options.TLS))
  40. if err != nil {
  41. return nil, err
  42. }
  43. userUUID, err := uuid.FromString(options.UUID)
  44. if err != nil {
  45. return nil, E.Cause(err, "invalid uuid")
  46. }
  47. var tuicUDPStream bool
  48. if options.UDPOverStream && options.UDPRelayMode != "" {
  49. return nil, E.New("udp_over_stream is conflict with udp_relay_mode")
  50. }
  51. switch options.UDPRelayMode {
  52. case "native":
  53. case "quic":
  54. tuicUDPStream = true
  55. }
  56. outboundDialer, err := dialer.New(ctx, options.DialerOptions, options.ServerIsDomain())
  57. if err != nil {
  58. return nil, err
  59. }
  60. client, err := tuic.NewClient(tuic.ClientOptions{
  61. Context: ctx,
  62. Dialer: outboundDialer,
  63. ServerAddress: options.ServerOptions.Build(),
  64. TLSConfig: tlsConfig,
  65. UUID: userUUID,
  66. Password: options.Password,
  67. CongestionControl: options.CongestionControl,
  68. UDPStream: tuicUDPStream,
  69. ZeroRTTHandshake: options.ZeroRTTHandshake,
  70. Heartbeat: time.Duration(options.Heartbeat),
  71. })
  72. if err != nil {
  73. return nil, err
  74. }
  75. return &Outbound{
  76. Adapter: outbound.NewAdapterWithDialerOptions(C.TypeTUIC, tag, options.Network.Build(), options.DialerOptions),
  77. logger: logger,
  78. client: client,
  79. udpStream: options.UDPOverStream,
  80. }, nil
  81. }
  82. func (h *Outbound) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
  83. switch N.NetworkName(network) {
  84. case N.NetworkTCP:
  85. h.logger.InfoContext(ctx, "outbound connection to ", destination)
  86. return h.client.DialConn(ctx, destination)
  87. case N.NetworkUDP:
  88. if h.udpStream {
  89. h.logger.InfoContext(ctx, "outbound stream packet connection to ", destination)
  90. streamConn, err := h.client.DialConn(ctx, uot.RequestDestination(uot.Version))
  91. if err != nil {
  92. return nil, err
  93. }
  94. return uot.NewLazyConn(streamConn, uot.Request{
  95. IsConnect: true,
  96. Destination: destination,
  97. }), nil
  98. } else {
  99. conn, err := h.ListenPacket(ctx, destination)
  100. if err != nil {
  101. return nil, err
  102. }
  103. return bufio.NewBindPacketConn(conn, destination), nil
  104. }
  105. default:
  106. return nil, E.New("unsupported network: ", network)
  107. }
  108. }
  109. func (h *Outbound) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
  110. if h.udpStream {
  111. h.logger.InfoContext(ctx, "outbound stream packet connection to ", destination)
  112. streamConn, err := h.client.DialConn(ctx, uot.RequestDestination(uot.Version))
  113. if err != nil {
  114. return nil, err
  115. }
  116. return uot.NewLazyConn(streamConn, uot.Request{
  117. IsConnect: false,
  118. Destination: destination,
  119. }), nil
  120. } else {
  121. h.logger.InfoContext(ctx, "outbound packet connection to ", destination)
  122. return h.client.ListenPacket(ctx)
  123. }
  124. }
  125. func (h *Outbound) InterfaceUpdated() {
  126. _ = h.client.CloseWithError(E.New("network changed"))
  127. }
  128. func (h *Outbound) Close() error {
  129. return h.client.CloseWithError(os.ErrClosed)
  130. }