Weird problem on SSH Port Forwarding

Jan 8, 2013 at 1:35 AM
Edited Jan 8, 2013 at 1:52 AM

HI,

Does anyone had experience on port forwarding for more than 3 ports?

I tried to use the following code to do SSH Tunneling,

ForwardedPortLocal fowardPort = new ForwardedPortLocal("127.0.0.1", 22100, "10.112.125.42", 22000);
sshClient.AddForwardedPort(fowardPort);
ForwardedPortLocal fowardPort2 = new ForwardedPortLocal("127.0.0.1", 16100, "10.112.125.42", 16000);
sshClient.AddForwardedPort(fowardPort2);
ForwardedPort fowardPort3 = new ForwardedPort("127.0.0.1", 18500, "10.112.125.42", 18000);
shClient.AddForwardedPort("127.0.0.1", 18500, "10.112.125.42", 1800);

fowardPort.Start();
fowardPort2.Start();
fowardPort3.Start();

The weird thing is I can do any combination for 2 forward port, (start any 2 forwardPorts)

But if I enable all 3 forwardPorts, the port forwarding suddenly does work., the local ports are opened and listening, but data cannot send though.

I wonder is this cause by performance drop(if there any) when starting 3 port forwarding?

Coordinator
Jan 8, 2013 at 3:26 PM

Hi,

 

I think the example you provided will not compile.

 

I just tried this code:

            using (SshClient sshClient = new SshClient(connectionInfo))
            {
                sshClient.Connect();

                var fowardPort = new ForwardedPortLocal("127.0.0.1", 10000, "www.cnn.com", 80);
                sshClient.AddForwardedPort(fowardPort);
                var fowardPort2 = new ForwardedPortLocal("127.0.0.1", 10001, "www.cnn.com", 80);
                sshClient.AddForwardedPort(fowardPort2);
                var fowardPort3 = new ForwardedPortLocal("127.0.0.1", 10002, "www.cnn.com", 80);
                sshClient.AddForwardedPort(fowardPort3);

                fowardPort.Start();
                fowardPort2.Start();
                fowardPort3.Start();
                Thread.Sleep(1000 * 60 * 5);

                sshClient.Disconnect();
            }

And all worked as expected.

I managed to go to http://localhost:10000/, http://localhost:10001/ and http://localhost:10002/ on my local machine with no problem.

 

Can you please provide compilable example that you using where it does not work.

 

Thanks,

Oleg

Jan 9, 2013 at 2:11 AM
Edited Jan 9, 2013 at 2:18 AM

Thanks for the reply,

I made typo when I edit the format of the first post, as you point out, 

The code I am using is about the same as yours

  sshClient = new SshClient("10.112.125.42", myUsername, myPassword);
  sshClient.Connect();
  ForwardedPortLocal fowardPort = new ForwardedPortLocal("127.0.0.1", 22100, "10.112.125.42", 22000);
  sshClient.AddForwardedPort(fowardPort);
  fowardPort.Start();
  ForwardedPortLocal fowardPort2 = new ForwardedPortLocal("127.0.0.1", 16100, "10.112.125.42", 16000);
  sshClient.AddForwardedPort(fowardPort2);
  fowardPort2.Start();              
  ForwardedPort fowardPort3 = new ForwardedPortLocal("127.0.0.1", 18500, "10.112.125.42", 18000);
  sshClient.AddForwardedPort(fowardPort3);

I am trying to start a program to set up a ssh tunnel for other applications to use, like Putty port forwarding function.

After further testing I found that the problem is more relate to "connect" to three ports, instead of "starting" 3 forwarding port.

So it works fine if I only connect to 2 ports, but when I connect all 3 ports, 

the message isn't going through the tunnel to reach the server side.

Could it be resources issue?   as I am running on notebook only, not server machine

I am not sure what to test for it, sorry if I couldn't provide enough information 

Thanks

Coordinator
Jan 9, 2013 at 12:59 PM

Hi,

 

Can you try this?

                sshClient.Connect();

                var fowardPort = new ForwardedPortLocal("127.0.0.1", 10000, "www.cnn.com", 80);
                sshClient.AddForwardedPort(fowardPort);
                var fowardPort2 = new ForwardedPortLocal("127.0.0.1", 10001, "www.cnn.com", 80);
                sshClient.AddForwardedPort(fowardPort2);
                var fowardPort3 = new ForwardedPortLocal("127.0.0.1", 10002, "www.cnn.com", 80);
                sshClient.AddForwardedPort(fowardPort3);

                fowardPort.RequestReceived += delegate(object sender, PortForwardEventArgs e)
                {
                    Console.WriteLine("fowardPort:" + e.OriginatorHost + ":" + e.OriginatorPort);
                };

                fowardPort2.RequestReceived += delegate(object sender, PortForwardEventArgs e)
                {
                    Console.WriteLine("fowardPort2:" + e.OriginatorHost + ":" + e.OriginatorPort);
                };

                fowardPort3.RequestReceived += delegate(object sender, PortForwardEventArgs e)
                {
                    Console.WriteLine("fowardPort3:" + e.OriginatorHost + ":" + e.OriginatorPort);
                };


                fowardPort.Start();
                fowardPort2.Start();
                fowardPort3.Start();
                Thread.Sleep(1000 * 60 * 5);

                sshClient.Disconnect();

Which should indicate to you any connection made on this port.

This output in my case:

fowardPort:127.0.0.1:17323
fowardPort:127.0.0.1:17324
fowardPort:127.0.0.1:17366
fowardPort2:127.0.0.1:17368
fowardPort2:127.0.0.1:17371
fowardPort2:127.0.0.1:17378
fowardPort3:127.0.0.1:17379
fowardPort3:127.0.0.1:17383
fowardPort3:127.0.0.1:17384

So the above tells me that port forwarding works, receive requests and sends responses back.

Please let me know what you find.

Thanks,

Oleg

 

Apr 8, 2013 at 12:10 PM
    private SshClient SshC = new SshClient("Host", Port, "User", "Password");
    private ForwardedPortLocal LPF1, LPF2, LPF3, RgF1, RqF1, RqF2, RqF3, MDB;


                SshC.Connect();
        MDB = new ForwardedPortLocal("127.0.0.1", 3306, "0.0.0.0", 3306);
        SshC.AddForwardedPort(MDB);
        MDB.Start();

        LPF1 = new ForwardedPortLocal("127.0.0.1", 8100, "0.0.0.0", 8100);
        SshC.AddForwardedPort(LPF1);
        LPF1.Start();

        LPF2 = new ForwardedPortLocal("127.0.0.1", 8008, "0.0.0.0", 8008);
        SshC.AddForwardedPort(LPF2);
        LPF2.Start();

        LPF3 = new ForwardedPortLocal("127.0.0.1", 8010, "0.0.0.0", 8010);
        SshC.AddForwardedPort(LPF3);
        LPF3.Start();

        RqF1 = new ForwardedPortLocal("127.0.0.1", 8014, "0.0.0.0", 8014);
        SshC.AddForwardedPort(RqF1);
        RqF1.Start();

        RqF2 = new ForwardedPortLocal("127.0.0.1", 8040, "0.0.0.0", 8040);
        SshC.AddForwardedPort(RqF2);
        RqF2.Start();

When I switch from one port to another in my application I recieve the Following Error
Exception:Thrown: "An existing connection was forcibly closed by the remote host" (System.Net.Sockets.SocketException)
A System.Net.Sockets.SocketException was thrown: "An existing connection was forcibly closed by the remote host"
Apr 10, 2013 at 4:17 PM
Hello, just now i open new thread with an similar problem.

When i open 2 forwardedlocalport, the first port is opened, the socket connect, with any data is received.