ForwardedPort.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using System;
  2. using Renci.SshNet.Common;
  3. namespace Renci.SshNet
  4. {
  5. /// <summary>
  6. /// Base class for port forwarding functionality.
  7. /// </summary>
  8. public abstract class ForwardedPort : IForwardedPort
  9. {
  10. /// <summary>
  11. /// Gets or sets the session.
  12. /// </summary>
  13. /// <value>
  14. /// The session.
  15. /// </value>
  16. internal ISession Session { get; set; }
  17. /// <summary>
  18. /// Gets a value indicating whether port forwarding is started.
  19. /// </summary>
  20. /// <value>
  21. /// <see langword="true"/> if port forwarding is started; otherwise, <see langword="false"/>.
  22. /// </value>
  23. public abstract bool IsStarted { get; }
  24. /// <summary>
  25. /// The <see cref="Closing"/> event occurs as the forwarded port is being stopped.
  26. /// </summary>
  27. public event EventHandler Closing;
  28. /// <summary>
  29. /// Occurs when an exception is thrown.
  30. /// </summary>
  31. public event EventHandler<ExceptionEventArgs> Exception;
  32. /// <summary>
  33. /// Occurs when a port forwarding request is received.
  34. /// </summary>
  35. public event EventHandler<PortForwardEventArgs> RequestReceived;
  36. /// <summary>
  37. /// Starts port forwarding.
  38. /// </summary>
  39. /// <exception cref="InvalidOperationException">The current <see cref="ForwardedPort"/> is already started -or- is not linked to a SSH session.</exception>
  40. /// <exception cref="SshConnectionException">The client is not connected.</exception>
  41. public virtual void Start()
  42. {
  43. CheckDisposed();
  44. if (IsStarted)
  45. {
  46. throw new InvalidOperationException("Forwarded port is already started.");
  47. }
  48. if (Session is null)
  49. {
  50. throw new InvalidOperationException("Forwarded port is not added to a client.");
  51. }
  52. if (!Session.IsConnected)
  53. {
  54. throw new SshConnectionException("Client not connected.");
  55. }
  56. Session.ErrorOccured += Session_ErrorOccured;
  57. StartPort();
  58. }
  59. /// <summary>
  60. /// Stops port forwarding.
  61. /// </summary>
  62. #pragma warning disable CA1716 // Identifiers should not match keywords
  63. public virtual void Stop()
  64. #pragma warning restore CA1716 // Identifiers should not match keywords
  65. {
  66. if (IsStarted)
  67. {
  68. StopPort(Session.ConnectionInfo.Timeout);
  69. }
  70. }
  71. /// <summary>
  72. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  73. /// </summary>
  74. public void Dispose()
  75. {
  76. Dispose(disposing: true);
  77. GC.SuppressFinalize(this);
  78. }
  79. /// <summary>
  80. /// Starts port forwarding.
  81. /// </summary>
  82. protected abstract void StartPort();
  83. /// <summary>
  84. /// Stops port forwarding, and waits for the specified timeout until all pending
  85. /// requests are processed.
  86. /// </summary>
  87. /// <param name="timeout">The maximum amount of time to wait for pending requests to finish processing.</param>
  88. protected virtual void StopPort(TimeSpan timeout)
  89. {
  90. timeout.EnsureValidTimeout(nameof(timeout));
  91. RaiseClosing();
  92. var session = Session;
  93. if (session is not null)
  94. {
  95. session.ErrorOccured -= Session_ErrorOccured;
  96. }
  97. }
  98. /// <summary>
  99. /// Releases unmanaged and - optionally - managed resources.
  100. /// </summary>
  101. /// <param name="disposing"><see langowrd="true"/> to release both managed and unmanaged resources; <see langowrd="false"/> to release only unmanaged resources.</param>
  102. protected virtual void Dispose(bool disposing)
  103. {
  104. if (disposing)
  105. {
  106. var session = Session;
  107. if (session is not null)
  108. {
  109. StopPort(session.ConnectionInfo.Timeout);
  110. Session = null;
  111. }
  112. }
  113. }
  114. /// <summary>
  115. /// Ensures the current instance is not disposed.
  116. /// </summary>
  117. /// <exception cref="ObjectDisposedException">The current instance is disposed.</exception>
  118. protected abstract void CheckDisposed();
  119. /// <summary>
  120. /// Raises <see cref="Exception"/> event.
  121. /// </summary>
  122. /// <param name="exception">The exception.</param>
  123. protected void RaiseExceptionEvent(Exception exception)
  124. {
  125. Exception?.Invoke(this, new ExceptionEventArgs(exception));
  126. }
  127. /// <summary>
  128. /// Raises <see cref="RequestReceived"/> event.
  129. /// </summary>
  130. /// <param name="host">Request originator host.</param>
  131. /// <param name="port">Request originator port.</param>
  132. protected void RaiseRequestReceived(string host, uint port)
  133. {
  134. RequestReceived?.Invoke(this, new PortForwardEventArgs(host, port));
  135. }
  136. /// <summary>
  137. /// Raises the <see cref="IForwardedPort.Closing"/> event.
  138. /// </summary>
  139. private void RaiseClosing()
  140. {
  141. Closing?.Invoke(this, EventArgs.Empty);
  142. }
  143. /// <summary>
  144. /// Handles session ErrorOccured event.
  145. /// </summary>
  146. /// <param name="sender">The source of the event.</param>
  147. /// <param name="e">The <see cref="ExceptionEventArgs"/> instance containing the event data.</param>
  148. private void Session_ErrorOccured(object sender, ExceptionEventArgs e)
  149. {
  150. RaiseExceptionEvent(e.Exception);
  151. }
  152. }
  153. }