Unable to connect to PostgreSQL server via SSH: No connection could be made because the target machine actively refused it

c# entity-framework-core portforwarding postgresql ssh.net

Question

I am trying to automatically create an SSH connection for a program that uses an SSH tunnel to update a local database from a remote PostgreSQL server. Up to this time, I have been manually opening a tunnel with PuTTY (including local port forwarding instructions with the -L command). I want to use ssh.net to automatically open the port when the program is run. Once the connection is made, the program uses Entity Framework Core to access the remote database.

When I open the SSH connection with PuTTY, the program runs fine. This is the PuTTY command:

//plink.exe -i "C:\Users\user.name\Desktop\host_private_key.ppk" -L 6544:111.22.33.66:6543 -N user@address.io -pw *PASSWORD*"

(login details removed for privacy)

This is the ssh.net code that I have trying to open the same connection:

public void MakeSSHTunnel()
        {
            string password = "password";
            // path of RSA private key in openSSH format:
            string privateKeyPath = "C:/Users/user.name/.ssh/id_rsa";

            try
            {
                // creates variable to transmit RSA private key + passphrase to server via SSH.NET, openSSH compatible.
                var privateKeyFile = new PrivateKeyFile(privateKeyPath, password);
                string serverAddress = "address.io";
                string user = "user";

                // allows for the remote port forwarding options required by the server 
                using (var client = new SshClient(serverAddress, user, privateKeyFile))
                {
                    client.Connect();
                    var tunnel = new ForwardedPortLocal(6544, "111.22.33.66", 6543);
                    client.AddForwardedPort(tunnel);

                    // testing weather the connection has been successful:
                    if (client.IsConnected)
                    {
                        Console.WriteLine("OPENTUNNEL.CS: Connection to {0} successful.", serverAddress);
                        state = "Open";
                    }
                    else
                    {
                        Console.WriteLine("Connection to {0} failed.");
                        state = "Closed";
                    }

                    tunnel.Exception += delegate (object sender, ExceptionEventArgs e)
                    {
                        Console.WriteLine(e.Exception.ToString());
                    };
                    tunnel.Start();
                    Program.RunBackup();


                    // ... closes the port ... //
                    tunnel.Stop();
                    client.Disconnect();
                }
            }
            catch (UnauthorizedAccessException e)
            {
                Console.WriteLine(e);
            }
        }

I am confused since in the above, the if (client.IsConnected) returns true.

The error seems to be occurring when the Entity Framework Core OnConfiguring() method passes details of the connection with its optionsBuilder:

optionsBuilder.UseNpgsql($"Host=127.0.0.1;Port=6544;Database=user;Username=user;Password=databasePassworh;CommandTimeout=300;Timeout=300;SSL Mode=Require;Trust Server Certificate=true;Convert Infinity DateTime=true");

The errors that are arising are:

NpgsqlException: Exception while connecting

and:

ExtendedSocketException: No connection could be made because the target machine actively refused it. 127.0.0.1:6544

I have double checked all passwords, and read through all the SSH.NET documentation and code examples, and left all the previously working (via PuTTY) code untouched.

If anyone can see what I'm doing wrong, I would be grateful. C#, SSH.NET and port forwarding are new to me, please tell me where I'm being an idiot.

1
0
1/21/2020 1:40:23 AM

Popular Answer

This code is now working. I believe the problem was that in line:

var tunnel = new ForwardedPortLocal(6544, "111.22.33.66", 6543);

the 'bound port' did not include an address. I had seen examples where this was undefined, and had followed these. On stepping through the code, I noticed that the field was blank, and decided to try 127.0.0.1. This is now successfully connecting to the database. It works with:

var tunnel = new ForwardedPortLocal("127.0.0.1", 6544, "111.22.33.66", 6543); 

Thanks for looking into this and for your contributions.

0
1/23/2020 9:52:31 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow