PrivateKeyConnectionInfo.cs 10 KB

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