|
| 1 | +using System; |
| 2 | +using System.Net; |
| 3 | +using System.Net.Sockets; |
| 4 | +using System.Threading; |
| 5 | +using Renci.SshNet.Abstractions; |
| 6 | +using Renci.SshNet.Common; |
| 7 | +using Renci.SshNet.Messages.Connection; |
| 8 | + |
| 9 | +namespace Renci.SshNet.Channels |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// Implements "direct-tcpip" SSH channel. |
| 13 | + /// </summary> |
| 14 | + internal class JumpChannel |
| 15 | + { |
| 16 | + private Socket listener; |
| 17 | + private ISession _session; |
| 18 | + private EventWaitHandle _channelOpen = new AutoResetEvent(false); |
| 19 | + |
| 20 | + /// <summary> |
| 21 | + /// Gets the bound host. |
| 22 | + /// </summary> |
| 23 | + public string BoundHost { get; private set; } |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// Gets the bound port. |
| 27 | + /// </summary> |
| 28 | + public uint BoundPort { get; private set; } |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Gets the forwarded host. |
| 32 | + /// </summary> |
| 33 | + public string Host { get; private set; } |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Gets the forwarded port. |
| 37 | + /// </summary> |
| 38 | + public uint Port { get; private set; } |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// Gets a value indicating whether port forwarding is started. |
| 42 | + /// </summary> |
| 43 | + /// <value> |
| 44 | + /// <c>true</c> if port forwarding is started; otherwise, <c>false</c>. |
| 45 | + /// </value> |
| 46 | + public bool IsStarted |
| 47 | + { get; private set; } |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// Initializes a new instance of the <see cref="ForwardedPortLocal"/> class. |
| 51 | + /// </summary> |
| 52 | + /// <param name="session"></param> |
| 53 | + /// <param name="host">The host.</param> |
| 54 | + /// <param name="port">The port.</param> |
| 55 | + /// <exception cref="ArgumentNullException"><paramref name="host"/> is <c>null</c>.</exception> |
| 56 | + /// <exception cref="ArgumentOutOfRangeException"><paramref name="port" /> is greater than <see cref="F:System.Net.IPEndPoint.MaxPort" />.</exception> |
| 57 | + /// <example> |
| 58 | + /// <code source="..\..\src\Renci.SshNet.Tests\Classes\ForwardedPortLocalTest.cs" region="Example SshClient AddForwardedPort Start Stop ForwardedPortLocal" language="C#" title="Local port forwarding" /> |
| 59 | + /// </example> |
| 60 | + public JumpChannel(ISession session, string host, uint port) |
| 61 | + { |
| 62 | + if (host == null) |
| 63 | + throw new ArgumentNullException("host"); |
| 64 | + |
| 65 | + port.ValidatePort("port"); |
| 66 | + |
| 67 | + Host = host; |
| 68 | + Port = port; |
| 69 | + |
| 70 | + _session = session; |
| 71 | + } |
| 72 | + |
| 73 | + public Socket Connect() |
| 74 | + { |
| 75 | + var ep = new IPEndPoint(IPAddress.Loopback, 0); |
| 76 | + listener = new Socket(ep.AddressFamily, SocketType.Stream, ProtocolType.Tcp) { NoDelay = true }; |
| 77 | + listener.Bind(ep); |
| 78 | + listener.Listen(1); |
| 79 | + |
| 80 | + IsStarted = true; |
| 81 | + |
| 82 | + // update bound port (in case original was passed as zero) |
| 83 | + ep.Port = ((IPEndPoint)listener.LocalEndPoint).Port; |
| 84 | + |
| 85 | + var e = new SocketAsyncEventArgs(); |
| 86 | + e.Completed += AcceptCompleted; |
| 87 | + |
| 88 | + // only accept new connections while we are started |
| 89 | + if (!listener.AcceptAsync(e)) |
| 90 | + { |
| 91 | + AcceptCompleted(null, e); |
| 92 | + } |
| 93 | + |
| 94 | + Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); |
| 95 | + socket.Connect(ep); |
| 96 | + |
| 97 | + // Wait for channel to open |
| 98 | + _session.WaitOnHandle(_channelOpen); |
| 99 | + listener.Dispose(); |
| 100 | + listener = null; |
| 101 | + |
| 102 | + return socket; |
| 103 | + } |
| 104 | + |
| 105 | + #region IDisposable Members |
| 106 | + |
| 107 | + private bool _isDisposed; |
| 108 | + |
| 109 | + /// <summary> |
| 110 | + /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. |
| 111 | + /// </summary> |
| 112 | + public void Dispose() |
| 113 | + { |
| 114 | + Dispose(true); |
| 115 | + GC.SuppressFinalize(this); |
| 116 | + } |
| 117 | + |
| 118 | + /// <summary> |
| 119 | + /// Releases unmanaged and - optionally - managed resources |
| 120 | + /// </summary> |
| 121 | + /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param> |
| 122 | + protected void Dispose(bool disposing) |
| 123 | + { |
| 124 | + if (_isDisposed) |
| 125 | + return; |
| 126 | + |
| 127 | + _isDisposed = true; |
| 128 | + } |
| 129 | + |
| 130 | + /// <summary> |
| 131 | + /// Releases unmanaged resources and performs other cleanup operations before the |
| 132 | + /// <see cref="ForwardedPortLocal"/> is reclaimed by garbage collection. |
| 133 | + /// </summary> |
| 134 | + ~JumpChannel() |
| 135 | + { |
| 136 | + Dispose(false); |
| 137 | + } |
| 138 | + |
| 139 | + #endregion |
| 140 | + |
| 141 | + |
| 142 | + private void AcceptCompleted(object sender, SocketAsyncEventArgs e) |
| 143 | + { |
| 144 | + if (e.SocketError == SocketError.OperationAborted || e.SocketError == SocketError.NotSocket) |
| 145 | + { |
| 146 | + // server was stopped |
| 147 | + return; |
| 148 | + } |
| 149 | + |
| 150 | + // capture client socket |
| 151 | + var clientSocket = e.AcceptSocket; |
| 152 | + |
| 153 | + if (e.SocketError != SocketError.Success) |
| 154 | + { |
| 155 | + // dispose broken client socket |
| 156 | + CloseClientSocket(clientSocket); |
| 157 | + return; |
| 158 | + } |
| 159 | + |
| 160 | + _channelOpen.Set(); |
| 161 | + |
| 162 | + // process connection |
| 163 | + ProcessAccept(clientSocket); |
| 164 | + } |
| 165 | + |
| 166 | + private void ProcessAccept(Socket clientSocket) |
| 167 | + { |
| 168 | + // close the client socket if we're no longer accepting new connections |
| 169 | + if (!IsStarted) |
| 170 | + { |
| 171 | + CloseClientSocket(clientSocket); |
| 172 | + return; |
| 173 | + } |
| 174 | + |
| 175 | + try |
| 176 | + { |
| 177 | + var originatorEndPoint = (IPEndPoint)clientSocket.RemoteEndPoint; |
| 178 | + |
| 179 | + using (var channel = _session.CreateChannelDirectTcpip()) |
| 180 | + { |
| 181 | + channel.Open(Host, Port, null, clientSocket); |
| 182 | + channel.Bind(); |
| 183 | + } |
| 184 | + } |
| 185 | + catch |
| 186 | + { |
| 187 | + CloseClientSocket(clientSocket); |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + private static void CloseClientSocket(Socket clientSocket) |
| 192 | + { |
| 193 | + if (clientSocket.Connected) |
| 194 | + { |
| 195 | + try |
| 196 | + { |
| 197 | + clientSocket.Shutdown(SocketShutdown.Send); |
| 198 | + } |
| 199 | + catch (Exception) |
| 200 | + { |
| 201 | + // ignore exception when client socket was already closed |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + clientSocket.Dispose(); |
| 206 | + } |
| 207 | + } |
| 208 | +} |
0 commit comments