ForwardedPort.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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
  9. {
  10. /// <summary>
  11. /// Gets or sets the session.
  12. /// </summary>
  13. /// <value>
  14. /// The session.
  15. /// </value>
  16. internal Session Session { get; set; }
  17. /// <summary>
  18. /// Gets the bound host.
  19. /// </summary>
  20. public string BoundHost { get; internal set; }
  21. /// <summary>
  22. /// Gets the bound port.
  23. /// </summary>
  24. public uint BoundPort { get; internal set; }
  25. /// <summary>
  26. /// Gets the forwarded host.
  27. /// </summary>
  28. public string Host { get; internal set; }
  29. /// <summary>
  30. /// Gets the forwarded port.
  31. /// </summary>
  32. public uint Port { get; internal set; }
  33. /// <summary>
  34. /// Gets or sets a value indicating whether port forwarding started.
  35. /// </summary>
  36. /// <value>
  37. /// <c>true</c> if port forwarding started; otherwise, <c>false</c>.
  38. /// </value>
  39. public bool IsStarted { get; protected set; }
  40. /// <summary>
  41. /// Occurs when exception is thrown.
  42. /// </summary>
  43. public event EventHandler<ExceptionEventArgs> Exception;
  44. /// <summary>
  45. /// Occurs when port forwarding request received.
  46. /// </summary>
  47. public event EventHandler<PortForwardEventArgs> RequestReceived;
  48. /// <summary>
  49. /// Initializes a new instance of the <see cref="Port"/> class.
  50. /// </summary>
  51. internal ForwardedPort()
  52. {
  53. }
  54. /// <summary>
  55. /// Starts port forwarding.
  56. /// </summary>
  57. public virtual void Start()
  58. {
  59. if (this.Session == null)
  60. {
  61. throw new InvalidOperationException("Session property is null.");
  62. }
  63. if (!this.Session.IsConnected)
  64. {
  65. throw new SshConnectionException("Not connected.");
  66. }
  67. this.Session.ErrorOccured += Session_ErrorOccured;
  68. }
  69. /// <summary>
  70. /// Stops port forwarding.
  71. /// </summary>
  72. public virtual void Stop()
  73. {
  74. this.Session.ErrorOccured -= Session_ErrorOccured;
  75. }
  76. /// <summary>
  77. /// Raises <see cref="Renci.SshNet.ForwardedPort.Exception"/> event.
  78. /// </summary>
  79. /// <param name="execption">The exception.</param>
  80. protected void RaiseExceptionEvent(Exception execption)
  81. {
  82. if (this.Exception != null)
  83. {
  84. this.Exception(this, new ExceptionEventArgs(execption));
  85. }
  86. }
  87. /// <summary>
  88. /// Raises <see cref="Renci.SshNet.ForwardedPort.RequestReceived"/> event.
  89. /// </summary>
  90. /// <param name="host">Request originator host.</param>
  91. /// <param name="port">Request originator port.</param>
  92. protected void RaiseRequestReceived(string host, uint port)
  93. {
  94. if (this.RequestReceived != null)
  95. {
  96. this.RequestReceived(this, new PortForwardEventArgs(host, port));
  97. }
  98. }
  99. /// <summary>
  100. /// Handles session ErrorOccured event.
  101. /// </summary>
  102. /// <param name="sender">The source of the event.</param>
  103. /// <param name="e">The <see cref="ExceptionEventArgs"/> instance containing the event data.</param>
  104. private void Session_ErrorOccured(object sender, ExceptionEventArgs e)
  105. {
  106. this.RaiseExceptionEvent(e.Exception);
  107. }
  108. }
  109. }