PrivateKeyConnectionInfo.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Collections.ObjectModel;
  5. namespace Renci.SshNet
  6. {
  7. /// <summary>
  8. /// Provides connection information when private key authentication method is used
  9. /// </summary>
  10. /// <example>
  11. /// <code source="..\..\Renci.SshNet.Tests\Classes\PrivateKeyConnectionInfoTest.cs" region="Example PrivateKeyConnectionInfo PrivateKeyFile" language="C#" title="Connect using private key" />
  12. /// </example>
  13. public class PrivateKeyConnectionInfo : ConnectionInfo, IDisposable
  14. {
  15. /// <summary>
  16. /// Gets the key files used for authentication.
  17. /// </summary>
  18. public ICollection<PrivateKeyFile> KeyFiles { get; private set; }
  19. /// <summary>
  20. /// Initializes a new instance of the <see cref="PrivateKeyConnectionInfo"/> class.
  21. /// </summary>
  22. /// <param name="host">Connection host.</param>
  23. /// <param name="username">Connection username.</param>
  24. /// <param name="keyFiles">Connection key files.</param>
  25. /// <example>
  26. /// <code source="..\..\Renci.SshNet.Tests\Classes\PrivateKeyConnectionInfoTest.cs" region="Example PrivateKeyConnectionInfo PrivateKeyFile" language="C#" title="Connect using private key" />
  27. /// <code source="..\..\Renci.SshNet.Tests\Classes\PrivateKeyConnectionInfoTest.cs" region="Example PrivateKeyConnectionInfo PrivateKeyFile Multiple" language="C#" title="Connect using multiple private key" />
  28. /// </example>
  29. public PrivateKeyConnectionInfo(string host, string username, params PrivateKeyFile[] keyFiles)
  30. : this(host, ConnectionInfo.DefaultPort, username, ProxyTypes.None, string.Empty, 0, string.Empty, string.Empty, keyFiles)
  31. {
  32. }
  33. /// <summary>
  34. /// Initializes a new instance of the <see cref="PrivateKeyConnectionInfo"/> class.
  35. /// </summary>
  36. /// <param name="host">Connection host.</param>
  37. /// <param name="port">Connection port.</param>
  38. /// <param name="username">Connection username.</param>
  39. /// <param name="keyFiles">Connection key files.</param>
  40. public PrivateKeyConnectionInfo(string host, int port, string username, params PrivateKeyFile[] keyFiles)
  41. : this(host, port, username, ProxyTypes.None, string.Empty, 0, string.Empty, string.Empty, keyFiles)
  42. {
  43. }
  44. /// <summary>
  45. /// Initializes a new instance of the <see cref="PasswordConnectionInfo"/> class.
  46. /// </summary>
  47. /// <param name="host">Connection host.</param>
  48. /// <param name="port">The port.</param>
  49. /// <param name="username">Connection username.</param>
  50. /// <param name="proxyType">Type of the proxy.</param>
  51. /// <param name="proxyHost">The proxy host.</param>
  52. /// <param name="proxyPort">The proxy port.</param>
  53. /// <param name="keyFiles">The key files.</param>
  54. public PrivateKeyConnectionInfo(string host, int port, string username, ProxyTypes proxyType, string proxyHost, int proxyPort, params PrivateKeyFile[] keyFiles)
  55. : this(host, port, username, proxyType, proxyHost, proxyPort, string.Empty, string.Empty, keyFiles)
  56. {
  57. }
  58. /// <summary>
  59. /// Initializes a new instance of the <see cref="PasswordConnectionInfo"/> class.
  60. /// </summary>
  61. /// <param name="host">Connection host.</param>
  62. /// <param name="port">The port.</param>
  63. /// <param name="username">Connection username.</param>
  64. /// <param name="proxyType">Type of the proxy.</param>
  65. /// <param name="proxyHost">The proxy host.</param>
  66. /// <param name="proxyPort">The proxy port.</param>
  67. /// <param name="proxyUsername">The proxy username.</param>
  68. /// <param name="keyFiles">The key files.</param>
  69. public PrivateKeyConnectionInfo(string host, int port, string username, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername, params PrivateKeyFile[] keyFiles)
  70. : this(host, port, username, proxyType, proxyHost, proxyPort, proxyUsername, string.Empty, keyFiles)
  71. {
  72. }
  73. /// <summary>
  74. /// Initializes a new instance of the <see cref="PasswordConnectionInfo"/> class.
  75. /// </summary>
  76. /// <param name="host">Connection host.</param>
  77. /// <param name="username">Connection username.</param>
  78. /// <param name="proxyType">Type of the proxy.</param>
  79. /// <param name="proxyHost">The proxy host.</param>
  80. /// <param name="proxyPort">The proxy port.</param>
  81. /// <param name="keyFiles">The key files.</param>
  82. public PrivateKeyConnectionInfo(string host, string username, ProxyTypes proxyType, string proxyHost, int proxyPort, params PrivateKeyFile[] keyFiles)
  83. : this(host, ConnectionInfo.DefaultPort, username, proxyType, proxyHost, proxyPort, string.Empty, string.Empty, keyFiles)
  84. {
  85. }
  86. /// <summary>
  87. /// Initializes a new instance of the <see cref="PasswordConnectionInfo"/> class.
  88. /// </summary>
  89. /// <param name="host">Connection host.</param>
  90. /// <param name="username">Connection username.</param>
  91. /// <param name="proxyType">Type of the proxy.</param>
  92. /// <param name="proxyHost">The proxy host.</param>
  93. /// <param name="proxyPort">The proxy port.</param>
  94. /// <param name="proxyUsername">The proxy username.</param>
  95. /// <param name="keyFiles">The key files.</param>
  96. public PrivateKeyConnectionInfo(string host, string username, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername, params PrivateKeyFile[] keyFiles)
  97. : this(host, ConnectionInfo.DefaultPort, username, proxyType, proxyHost, proxyPort, proxyUsername, string.Empty, keyFiles)
  98. {
  99. }
  100. /// <summary>
  101. /// Initializes a new instance of the <see cref="PasswordConnectionInfo"/> class.
  102. /// </summary>
  103. /// <param name="host">Connection host.</param>
  104. /// <param name="username">Connection username.</param>
  105. /// <param name="proxyType">Type of the proxy.</param>
  106. /// <param name="proxyHost">The proxy host.</param>
  107. /// <param name="proxyPort">The proxy port.</param>
  108. /// <param name="proxyUsername">The proxy username.</param>
  109. /// <param name="proxyPassword">The proxy password.</param>
  110. /// <param name="keyFiles">The key files.</param>
  111. public PrivateKeyConnectionInfo(string host, string username, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername, string proxyPassword, params PrivateKeyFile[] keyFiles)
  112. : this(host, ConnectionInfo.DefaultPort, username, proxyType, proxyHost, proxyPort, proxyUsername, proxyPassword, keyFiles)
  113. {
  114. }
  115. /// <summary>
  116. /// Initializes a new instance of the <see cref="PasswordConnectionInfo"/> class.
  117. /// </summary>
  118. /// <param name="host">Connection host.</param>
  119. /// <param name="port">The port.</param>
  120. /// <param name="username">Connection username.</param>
  121. /// <param name="proxyType">Type of the proxy.</param>
  122. /// <param name="proxyHost">The proxy host.</param>
  123. /// <param name="proxyPort">The proxy port.</param>
  124. /// <param name="proxyUsername">The proxy username.</param>
  125. /// <param name="proxyPassword">The proxy password.</param>
  126. /// <param name="keyFiles">The key files.</param>
  127. public PrivateKeyConnectionInfo(string host, int port, string username, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername, string proxyPassword, params PrivateKeyFile[] keyFiles)
  128. : base(host, port, username, proxyType, proxyHost, proxyPort, proxyUsername, proxyPassword, new PrivateKeyAuthenticationMethod(username, keyFiles))
  129. {
  130. this.KeyFiles = new Collection<PrivateKeyFile>(keyFiles);
  131. }
  132. #region IDisposable Members
  133. private bool _isDisposed;
  134. /// <summary>
  135. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  136. /// </summary>
  137. public void Dispose()
  138. {
  139. Dispose(true);
  140. GC.SuppressFinalize(this);
  141. }
  142. /// <summary>
  143. /// Releases unmanaged and - optionally - managed resources
  144. /// </summary>
  145. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  146. protected virtual void Dispose(bool disposing)
  147. {
  148. // Check to see if Dispose has already been called.
  149. if (!this._isDisposed)
  150. {
  151. // If disposing equals true, dispose all managed
  152. // and unmanaged resources.
  153. if (disposing)
  154. {
  155. // Dispose managed resources.
  156. if (this.AuthenticationMethods != null)
  157. {
  158. foreach (var authenticationMethods in this.AuthenticationMethods.OfType<IDisposable>())
  159. {
  160. authenticationMethods.Dispose();
  161. }
  162. }
  163. }
  164. // Note disposing has been done.
  165. _isDisposed = true;
  166. }
  167. }
  168. /// <summary>
  169. /// Releases unmanaged resources and performs other cleanup operations before the
  170. /// <see cref="PasswordConnectionInfo"/> is reclaimed by garbage collection.
  171. /// </summary>
  172. ~PrivateKeyConnectionInfo()
  173. {
  174. // Do not re-create Dispose clean-up code here.
  175. // Calling Dispose(false) is optimal in terms of
  176. // readability and maintainability.
  177. Dispose(false);
  178. }
  179. #endregion
  180. }
  181. }