2
0

ServiceFactory.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. namespace Renci.SshNet
  10. {
  11. /// <summary>
  12. /// Basic factory for creating new services.
  13. /// </summary>
  14. internal partial class ServiceFactory : IServiceFactory
  15. {
  16. /// <summary>
  17. /// Creates a <see cref="IClientAuthentication"/>.
  18. /// </summary>
  19. /// <returns>
  20. /// A <see cref="IClientAuthentication"/>.
  21. /// </returns>
  22. public IClientAuthentication CreateClientAuthentication()
  23. {
  24. return new ClientAuthentication();
  25. }
  26. /// <summary>
  27. /// Creates a new <see cref="ISession"/> with the specified <see cref="ConnectionInfo"/>.
  28. /// </summary>
  29. /// <param name="connectionInfo">The <see cref="ConnectionInfo"/> to use for creating a new session.</param>
  30. /// <returns>
  31. /// An <see cref="ISession"/> for the specified <see cref="ConnectionInfo"/>.
  32. /// </returns>
  33. /// <exception cref="ArgumentNullException"><paramref name="connectionInfo"/> is <c>null</c>.</exception>
  34. public ISession CreateSession(ConnectionInfo connectionInfo)
  35. {
  36. return new Session(connectionInfo, this);
  37. }
  38. /// <summary>
  39. /// Creates a new <see cref="ISftpSession"/> in a given <see cref="ISession"/> and with
  40. /// the specified operation timeout and encoding.
  41. /// </summary>
  42. /// <param name="session">The <see cref="ISession"/> to create the <see cref="ISftpSession"/> in.</param>
  43. /// <param name="operationTimeout">The operation timeout.</param>
  44. /// <param name="encoding">The encoding.</param>
  45. /// <returns>
  46. /// An <see cref="ISftpSession"/>.
  47. /// </returns>
  48. public ISftpSession CreateSftpSession(ISession session, TimeSpan operationTimeout, Encoding encoding)
  49. {
  50. return new SftpSession(session, operationTimeout, encoding);
  51. }
  52. /// <summary>
  53. /// Create a new <see cref="PipeStream"/>.
  54. /// </summary>
  55. /// <returns>
  56. /// A <see cref="PipeStream"/>.
  57. /// </returns>
  58. public PipeStream CreatePipeStream()
  59. {
  60. return new PipeStream();
  61. }
  62. /// <summary>
  63. /// Negotiates a key exchange algorithm, and creates a <see cref="IKeyExchange" /> for the negotiated
  64. /// algorithm.
  65. /// </summary>
  66. /// <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>
  67. /// <param name="serverAlgorithms">The names of the key exchange algorithms supported by the SSH server.</param>
  68. /// <returns>
  69. /// A <see cref="IKeyExchange"/> that was negotiated between client and server.
  70. /// </returns>
  71. /// <exception cref="ArgumentNullException"><paramref name="clientAlgorithms"/> is <c>null</c>.</exception>
  72. /// <exception cref="ArgumentNullException"><paramref name="serverAlgorithms"/> is <c>null</c>.</exception>
  73. /// <exception cref="SshConnectionException">No key exchange algorithms are supported by both client and server.</exception>
  74. public IKeyExchange CreateKeyExchange(IDictionary<string, Type> clientAlgorithms, string[] serverAlgorithms)
  75. {
  76. if (clientAlgorithms == null)
  77. throw new ArgumentNullException("clientAlgorithms");
  78. if (serverAlgorithms == null)
  79. throw new ArgumentNullException("serverAlgorithms");
  80. // find an algorithm that is supported by both client and server
  81. var keyExchangeAlgorithmType = (from c in clientAlgorithms
  82. from s in serverAlgorithms
  83. where s == c.Key
  84. select c.Value).FirstOrDefault();
  85. if (keyExchangeAlgorithmType == null)
  86. {
  87. throw new SshConnectionException("Failed to negotiate key exchange algorithm.", DisconnectReason.KeyExchangeFailed);
  88. }
  89. return keyExchangeAlgorithmType.CreateInstance<KeyExchange>();
  90. }
  91. }
  92. }