.NET端口转发问题

时间:2019-11-20 12:22:43

标签: c# .net mongodb ssh ssh-tunnel

我尝试在应用程序中创建ssh tunel并从CLI访问远程mongo。

远程主机上的ssh服务器侦听端口9901
远程Mongo端口:27017
本地主机具有:Windows 10,VS 2019
程序集Renci.SshNet,Version = 2016.1.0.0
远程主机是Debian 9.9

如果我从Cygwin进行端口转发,则可以成功访问,但是如果从C#中进行访问,则失败。

场景1

开放端口转发:

ssh -p 9901 -L 50001:localhost:27017 user@host.net

从本地主机连接Mongo:

c:\Program Files\MongoDB\Server\3.6\bin>mongo --port 50001 --host localhost
MongoDB shell version v3.6.13
connecting to: mongodb://localhost:50001/?gssapiServiceName=mongodb
WARNING: No implicit session: Logical Sessions are only supported on server versions 3.6 and greater.
Implicit session: dummy session
MongoDB server version: 3.2.11
WARNING: shell and server versions do not match
>

场景2

端口转发代码:

namespace VT_lic
{
    public static class PortForwarding
    {        
        static SshClient client;
        static ForwardedPortLocal portFwld;

        public static void Connect ()
        {
            PasswordConnectionInfo conn = new PasswordConnectionInfo(
                "host.net",
                9901,
                "user",
                "password"
            );

            client = new SshClient(conn);
            portFwld = new ForwardedPortLocal("localhost", 50001, "host.net", 27017);

            try
            {
                Console.WriteLine("Trying SSH connection...");
                client.Connect();
                Console.WriteLine(client.CreateCommand("ls").Execute());

                if (!client.IsConnected)
                    throw new Exception("Kosha ssh connection failed");       

                client.AddForwardedPort(portFwld);
                portFwld.Start();

                if (!portFwld.IsStarted)
                    throw new Exception("Kosha port forward failed");

            }
            catch (System.Net.Sockets.SocketException ex1)
            {
                throw new Exception("Kosha port forward failed");
            }
        }
    }
}

控制台输出:

  

备份
  mk.sh
  临时
  文件1
  file2

与Mongo的连接:

c:\Program Files\MongoDB\Server\3.6\bin>mongo --port 50001 --host localhost
MongoDB shell version v3.6.13
connecting to: mongodb://localhost:50001/?gssapiServiceName=mongodb

Netstat:

C:\WINDOWS\system32>netstat -a -n | find "50001"
  TCP    127.0.0.1:58045        127.0.0.1:50001        SYN_SENT
  TCP    [::1]:50001            [::]:0                 LISTENING

我尝试了所有选项:

new ForwardedPortLocal("localhost", 50001, "host.net", 27017);
new ForwardedPortLocal("localhost", 50001, "localhost", 27017);
new ForwardedPortLocal(50001, "localhost", 27017);
new ForwardedPortLocal(27017, "localhost", 50001);
new ForwardedPortDynamic("localhost", 27017); // --port 27017 in CLI also

1 个答案:

答案 0 :(得分:1)

您的代码应类似于

string boundIP = "127.0.0.1";
string boundPort = "5477";

PasswordConnectionInfo connection = new PasswordConnectionInfo(server_config.Get("server_ip"),Convert.ToInt32(server_config.Get("ssh_port")),server_config.Get("ssh_user"), server_config.Get("ssh_password"));            
    SshClient client = new SshClient(connection);
    client.Connect();        
    //forwardIP = 127.0.0.1 
    // hostIP = 127.0.0.1 because I assume you want to access db which is running on 127.0.0.1
    ForwardedPortLocal forwardedPortLocal = new ForwardedPortLocal(forwardIP, Convert.ToUInt32(forwardPort), "127.0.0.1", Convert.ToUInt32(server_config.Get("database_port")));
    client.AddForwardedPort(forwardedPortLocal);
    forwardedPortLocal.Start();