From cfa1a7028b3abaebab5ca69c74a168632adcc65f Mon Sep 17 00:00:00 2001 From: olegkap_cp Date: Fri, 6 Jan 2012 18:57:48 +0000 Subject: [PATCH] Refactor PortForwarding Fix ChannelDirectTcpip clean up after socket is close by remote host Add ForwardedPortDynamic to support dynamic forwarding, SOCKS4 and SOCKS5 are supported. --- .../ForwardedPortDynamic.NET35.cs | 13 + .../ForwardedPortLocal.NET35.cs | 75 ----- .../Renci.SshNet.NET35.csproj | 12 +- .../Renci.SshNet.Silverlight.csproj | 5 +- .../TestPortForwarding.NET40.cs | 199 ++++++------- .../SshClientTests/TestPortForwarding.cs | 148 +++++----- .../Renci.SshNet.WindowsPhone.csproj | 5 +- .../Channels/ChannelDirectTcpip.cs | 48 ++- Renci.SshClient/Renci.SshNet/ForwardedPort.cs | 20 -- .../Renci.SshNet/ForwardedPortDynamic.NET.cs | 277 ++++++++++++++++++ .../ForwardedPortDynamic.NET40.cs | 13 + .../Renci.SshNet/ForwardedPortDynamic.cs | 121 ++++++++ .../Renci.SshNet/ForwardedPortLocal.NET.cs | 91 ++++++ .../Renci.SshNet/ForwardedPortLocal.NET40.cs | 83 +----- .../Renci.SshNet/ForwardedPortLocal.cs | 65 ++++ .../Renci.SshNet/ForwardedPortRemote.cs | 59 ++++ .../Renci.SshNet/Renci.SshNet.csproj | 8 + Renci.SshClient/Renci.SshNet/SshClient.cs | 65 +--- 18 files changed, 886 insertions(+), 421 deletions(-) create mode 100644 Renci.SshClient/Renci.SshNet.NET35/ForwardedPortDynamic.NET35.cs create mode 100644 Renci.SshClient/Renci.SshNet/ForwardedPortDynamic.NET.cs create mode 100644 Renci.SshClient/Renci.SshNet/ForwardedPortDynamic.NET40.cs create mode 100644 Renci.SshClient/Renci.SshNet/ForwardedPortDynamic.cs create mode 100644 Renci.SshClient/Renci.SshNet/ForwardedPortLocal.NET.cs diff --git a/Renci.SshClient/Renci.SshNet.NET35/ForwardedPortDynamic.NET35.cs b/Renci.SshClient/Renci.SshNet.NET35/ForwardedPortDynamic.NET35.cs new file mode 100644 index 00000000..ff20813b --- /dev/null +++ b/Renci.SshClient/Renci.SshNet.NET35/ForwardedPortDynamic.NET35.cs @@ -0,0 +1,13 @@ +using System; +using System.Threading; + +namespace Renci.SshNet +{ + public partial class ForwardedPortDynamic + { + partial void ExecuteThread(Action action) + { + ThreadPool.QueueUserWorkItem((o) => { action(); }); + } + } +} diff --git a/Renci.SshClient/Renci.SshNet.NET35/ForwardedPortLocal.NET35.cs b/Renci.SshClient/Renci.SshNet.NET35/ForwardedPortLocal.NET35.cs index 86a534fc..0bbfec6d 100644 --- a/Renci.SshClient/Renci.SshNet.NET35/ForwardedPortLocal.NET35.cs +++ b/Renci.SshClient/Renci.SshNet.NET35/ForwardedPortLocal.NET35.cs @@ -11,84 +11,9 @@ namespace Renci.SshNet /// public partial class ForwardedPortLocal { - private TcpListener _listener; - partial void ExecuteThread(Action action) { ThreadPool.QueueUserWorkItem((o) => { action(); }); } - - partial void InternalStart() - { - // If port already started don't start it again - if (this.IsStarted) - return; - - var ep = new IPEndPoint(Dns.GetHostAddresses(this.BoundHost)[0], (int)this.BoundPort); - - this._listener = new TcpListener(ep); - this._listener.Start(); - - this._listenerTaskCompleted = new ManualResetEvent(false); - this.ExecuteThread(() => - { - try - { - while (true) - { - var socket = this._listener.AcceptSocket(); - - this.ExecuteThread(() => - { - try - { - IPEndPoint originatorEndPoint = socket.RemoteEndPoint as IPEndPoint; - - this.RaiseRequestReceived(originatorEndPoint.Address.ToString(), (uint)originatorEndPoint.Port); - - var channel = this.Session.CreateChannel(); - - channel.Bind(this.Host, this.Port, socket); - } - catch (Exception exp) - { - this.RaiseExceptionEvent(exp); - } - }); - } - } - catch (SocketException exp) - { - if (!(exp.SocketErrorCode == SocketError.Interrupted)) - { - this.RaiseExceptionEvent(exp); - } - } - catch (Exception exp) - { - this.RaiseExceptionEvent(exp); - } - finally - { - this._listenerTaskCompleted.Set(); - } - }); - - this.IsStarted = true; - } - - partial void InternalStop() - { - // If port not started you cant stop it - if (!this.IsStarted) - return; - - this._listener.Stop(); - this._listenerTaskCompleted.WaitOne(this.Session.ConnectionInfo.Timeout); - this._listenerTaskCompleted.Dispose(); - this._listenerTaskCompleted = null; - - this.IsStarted = false; - } } } diff --git a/Renci.SshClient/Renci.SshNet.NET35/Renci.SshNet.NET35.csproj b/Renci.SshClient/Renci.SshNet.NET35/Renci.SshNet.NET35.csproj index fcbf3c44..bc428036 100644 --- a/Renci.SshClient/Renci.SshNet.NET35/Renci.SshNet.NET35.csproj +++ b/Renci.SshClient/Renci.SshNet.NET35/Renci.SshNet.NET35.csproj @@ -217,9 +217,18 @@ ForwardedPort.cs + + ForwardedPortDynamic.cs + + + ForwardedPortDynamic.NET.cs + ForwardedPortLocal.cs + + ForwardedPortLocal.NET.cs + ForwardedPortRemote.cs @@ -729,6 +738,7 @@ + @@ -742,7 +752,7 @@ - +