.NET 端口转发问题

Posted

技术标签:

【中文标题】.NET 端口转发问题【英文标题】:.NET port forwarding issue 【发布时间】:2020-03-16 04:00:36 【问题描述】:

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

远程主机上的 ssh 服务器监听 9901 端口 远程 Mongo 端口:27017 本地主机有:Windows 10、VS 2019 汇编 Renci.SshNet,版本=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 文件2

与 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

网络统计:

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

【问题讨论】:

我认为你不应该在 ForwardPortLocal 方法中使用 'localhost',尝试使用 '127.0.0.1' 代替。 【参考方案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();

【讨论】:

谢谢!!!看来,该主机需要是 ip - 127.0.0.1,而不是 localhost。我还使用了 Convert.ToUInt32() 和 4 位端口号。

以上是关于.NET 端口转发问题的主要内容,如果未能解决你的问题,请参考以下文章

.Net 6 Web Api docker 端口转发问题

iptables端口复用和端口转发

Linux下本地端口转发

SSH.NET > 用于端口转发的 SSH 客户端

使用 iptables 进行端口转发

如何使用 Ruby 进行动态端口转发