PrivateKeyConnectionInfo.cs 9.3 KB

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