ServiceFactory.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Renci.SshNet.Common;
  6. using Renci.SshNet.Messages.Transport;
  7. using Renci.SshNet.Security;
  8. using Renci.SshNet.Sftp;
  9. using Renci.SshNet.Abstractions;
  10. namespace Renci.SshNet
  11. {
  12. /// <summary>
  13. /// Basic factory for creating new services.
  14. /// </summary>
  15. internal partial class ServiceFactory : IServiceFactory
  16. {
  17. /// <summary>
  18. /// Creates a <see cref="IClientAuthentication"/>.
  19. /// </summary>
  20. /// <returns>
  21. /// A <see cref="IClientAuthentication"/>.
  22. /// </returns>
  23. public IClientAuthentication CreateClientAuthentication()
  24. {
  25. return new ClientAuthentication();
  26. }
  27. /// <summary>
  28. /// Creates a new <see cref="ISession"/> with the specified <see cref="ConnectionInfo"/>.
  29. /// </summary>
  30. /// <param name="connectionInfo">The <see cref="ConnectionInfo"/> to use for creating a new session.</param>
  31. /// <returns>
  32. /// An <see cref="ISession"/> for the specified <see cref="ConnectionInfo"/>.
  33. /// </returns>
  34. /// <exception cref="ArgumentNullException"><paramref name="connectionInfo"/> is <c>null</c>.</exception>
  35. public ISession CreateSession(ConnectionInfo connectionInfo)
  36. {
  37. return new Session(connectionInfo, this);
  38. }
  39. /// <summary>
  40. /// Creates a new <see cref="ISftpSession"/> in a given <see cref="ISession"/> and with
  41. /// the specified operation timeout and encoding.
  42. /// </summary>
  43. /// <param name="session">The <see cref="ISession"/> to create the <see cref="ISftpSession"/> in.</param>
  44. /// <param name="operationTimeout">The number of milliseconds to wait for an operation to complete, or -1 to wait indefinitely.</param>
  45. /// <param name="encoding">The encoding.</param>
  46. /// <param name="sftpMessageFactory">The factory to use for creating SFTP messages.</param>
  47. /// <returns>
  48. /// An <see cref="ISftpSession"/>.
  49. /// </returns>
  50. public ISftpSession CreateSftpSession(ISession session, int operationTimeout, Encoding encoding, ISftpResponseFactory sftpMessageFactory)
  51. {
  52. return new SftpSession(session, operationTimeout, encoding, sftpMessageFactory);
  53. }
  54. /// <summary>
  55. /// Create a new <see cref="PipeStream"/>.
  56. /// </summary>
  57. /// <returns>
  58. /// A <see cref="PipeStream"/>.
  59. /// </returns>
  60. public PipeStream CreatePipeStream()
  61. {
  62. return new PipeStream();
  63. }
  64. /// <summary>
  65. /// Negotiates a key exchange algorithm, and creates a <see cref="IKeyExchange" /> for the negotiated
  66. /// algorithm.
  67. /// </summary>
  68. /// <param name="clientAlgorithms">A <see cref="IDictionary{String, Type}"/> of the key exchange algorithms supported by the client where key is the name of the algorithm, and value is the type implementing this algorithm.</param>
  69. /// <param name="serverAlgorithms">The names of the key exchange algorithms supported by the SSH server.</param>
  70. /// <returns>
  71. /// A <see cref="IKeyExchange"/> that was negotiated between client and server.
  72. /// </returns>
  73. /// <exception cref="ArgumentNullException"><paramref name="clientAlgorithms"/> is <c>null</c>.</exception>
  74. /// <exception cref="ArgumentNullException"><paramref name="serverAlgorithms"/> is <c>null</c>.</exception>
  75. /// <exception cref="SshConnectionException">No key exchange algorithms are supported by both client and server.</exception>
  76. public IKeyExchange CreateKeyExchange(IDictionary<string, Type> clientAlgorithms, string[] serverAlgorithms)
  77. {
  78. if (clientAlgorithms == null)
  79. throw new ArgumentNullException("clientAlgorithms");
  80. if (serverAlgorithms == null)
  81. throw new ArgumentNullException("serverAlgorithms");
  82. // find an algorithm that is supported by both client and server
  83. var keyExchangeAlgorithmType = (from c in clientAlgorithms
  84. from s in serverAlgorithms
  85. where s == c.Key
  86. select c.Value).FirstOrDefault();
  87. if (keyExchangeAlgorithmType == null)
  88. {
  89. throw new SshConnectionException("Failed to negotiate key exchange algorithm.", DisconnectReason.KeyExchangeFailed);
  90. }
  91. return keyExchangeAlgorithmType.CreateInstance<IKeyExchange>();
  92. }
  93. public ISftpFileReader CreateSftpFileReader(string fileName, ISftpSession sftpSession, uint bufferSize)
  94. {
  95. const int DefaultMaxPendingReads = 3;
  96. var openAsyncResult = sftpSession.BeginOpen(fileName, Flags.Read, null, null);
  97. var statAsyncResult = sftpSession.BeginLStat(fileName, null, null);
  98. long? fileSize;
  99. int maxPendingReads;
  100. var chunkSize = sftpSession.CalculateOptimalReadLength(bufferSize);
  101. // fallback to a default maximum of pending reads when remote server does not allow us to obtain
  102. // the attributes of the file
  103. try
  104. {
  105. var fileAttributes = sftpSession.EndLStat(statAsyncResult);
  106. fileSize = fileAttributes.Size;
  107. maxPendingReads = Math.Min(10, (int) Math.Ceiling((double) fileAttributes.Size / chunkSize) + 1);
  108. }
  109. catch (SshException ex)
  110. {
  111. fileSize = null;
  112. maxPendingReads = DefaultMaxPendingReads;
  113. DiagnosticAbstraction.Log(string.Format("Failed to obtain size of file. Allowing maximum {0} pending reads: {1}", maxPendingReads, ex));
  114. }
  115. var handle = sftpSession.EndOpen(openAsyncResult);
  116. return sftpSession.CreateFileReader(handle, sftpSession, chunkSize, maxPendingReads, fileSize);
  117. }
  118. public ISftpResponseFactory CreateSftpMessageFactory()
  119. {
  120. return new SftpResponseFactory();
  121. }
  122. }
  123. }