Create an SSH tunnel

Dec 11, 2014 at 11:16 AM
Edited Dec 11, 2014 at 11:20 AM
I want to connect to an OpenVPN server through an SSH tunnel.

Until now I open the tunnel manually with the following command :
ssh -NfD 1080 username@remote.com
I want to do the exact same command but with SSH.net library in a C# application, and then use the SSH connection as a SOCKS proxy in OpenVPN client.

How can I do that ?
Dec 11, 2014 at 12:43 PM
Use a SshClient and a ForwardedPortDynamic.
Dec 11, 2014 at 12:49 PM
Edited Dec 11, 2014 at 12:51 PM
I did this :
using (var client = new SshClient("remote.com", "username", "password"))
{
         client.Connect();
         var port = new ForwardedPortLocal("localhost", 1080, "remote.com", 1080);
         client.AddForwardedPort(port);
         port.Exception += delegate(object sender, ExceptionEventArgs e)
         {
                 Console.WriteLine(e.Exception.ToString());
         };
         port.Start();
}
Console.ReadKey();
I can send commands like if I am connected to a server in ssh (commands like ls, pwd, mkdir etc) but I can't use the tunnel as a SOCKS proxy in openvpn.
The command
ssh -NfD 1080 username@remote.com
works, but not the C# application, and I don't understand why
Dec 11, 2014 at 1:10 PM
Edited Dec 11, 2014 at 1:21 PM
ForwardedPortLocal != ForwardedPortDynamic

From ssh man-page:
     -D [bind_address:]port
             Specifies a local ``dynamic'' application-level port forwarding.
Else the code looks fine.

Edit: On a second glance... Your Readkey() should be inside the using-block, or?
Else client would be disposed and disconnected after the forwarding started.
Dec 11, 2014 at 5:13 PM
Thanks, it worked. Here's my code :
public void Start()
{
      using (var client = new SshClient("vps97919.ovh.net", "tunnel", "123456"))
      {
           client.KeepAliveInterval = new TimeSpan(0, 0, 30);
           client.ConnectionInfo.Timeout = new TimeSpan(0, 0, 20);
           client.Connect();
           ForwardedPortDynamic port = new ForwardedPortDynamic("127.0.0.1", 1080);
           client.AddForwardedPort(port);
           port.Exception += delegate(object sender, ExceptionEventArgs e)
           {
                Console.WriteLine(e.Exception.ToString());
           };
           port.Start();
           System.Threading.Thread.Sleep(1000 * 60 * 8);
           port.Stop();
           client.Disconnect();
     }
this.Start();
}
So I now have two problems :
  • I have to change the remote address in my VPN config to its IP address otherwise it won't find the remote server
  • As you can see to keep the ssh connexion I use a Thread sleep with a recursive call to itself because my program is running in the background without a console so I can't use readkey. Have any idea of a better practice than that ?
Dec 11, 2014 at 5:44 PM
What are you using instead of console?
I usually create a simple WinForms App which I can minimze to the system tray.
Dec 11, 2014 at 8:02 PM
I use a console App but in the project settings I select the output as Windows application instead of Console Application
I tried a WinForms App but the SSH tunel closes when the Start function is finished.

I have my button connect that calls the function Start of my Tunnel object, which starts the tunnel. But if I don't put a Sleep it stops
Dec 12, 2014 at 5:35 AM
If you use the same code as some posts above, of course the tunnel is closed.
You are disposing the client object after port.Start(), thats what a using is block is for.

Just don't use using.

Maybe something like this:
SshClient client;

public void Start()
{
      client = new SshClient("vps97919.ovh.net", "tunnel", "123456")
      client.KeepAliveInterval = new TimeSpan(0, 0, 30);
      client.ConnectionInfo.Timeout = new TimeSpan(0, 0, 20);
      client.Connect();
      ForwardedPortDynamic port = new ForwardedPortDynamic("127.0.0.1", 1080);
      client.AddForwardedPort(port);
      port.Start();
}

public void Stop()
{
      client.Disconnect(); 
}
Just an example, to show the problem with your code.
Marked as answer by drieseng on 12/16/2014 at 4:13 AM
Dec 13, 2014 at 2:03 AM
Oh I feel like an idiot now.
Thank you for your help !