.NET 端口转发问题
.NET port forwarding issue
我尝试在我的应用程序中创建 ssh tunel 并从 CLI 访问远程 mongo。
侦听端口 9901 的远程主机上的 ssh 服务器
远程 Mongo 端口:27017
本地主机有:Windows 10,VS 2019
程序集 Renci.SshNet,版本=2016.1.0.0
远程主机是 Debian 9.9
如果我从 Cygwin 进行端口转发,我会成功访问,但如果我从 C# 进行端口转发,我会失败。
场景一
开启端口转发:
ssh -p 9901 -L 50001:localhost:27017 user@host.net
从本地主机连接 Mongo:
c:\Program Files\MongoDB\Server.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
>
场景二
端口转发代码:
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");
}
}
}
}
控制台输出:
backup
mk.sh
temp
file1
file2
与 Mongo 的连接:
c:\Program Files\MongoDB\Server.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
您的代码应该类似于
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();
我尝试在我的应用程序中创建 ssh tunel 并从 CLI 访问远程 mongo。
侦听端口 9901 的远程主机上的 ssh 服务器
远程 Mongo 端口:27017
本地主机有:Windows 10,VS 2019
程序集 Renci.SshNet,版本=2016.1.0.0
远程主机是 Debian 9.9
如果我从 Cygwin 进行端口转发,我会成功访问,但如果我从 C# 进行端口转发,我会失败。
场景一
开启端口转发:
ssh -p 9901 -L 50001:localhost:27017 user@host.net
从本地主机连接 Mongo:
c:\Program Files\MongoDB\Server.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
>
场景二
端口转发代码:
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");
}
}
}
}
控制台输出:
backup
mk.sh
temp
file1
file2
与 Mongo 的连接:
c:\Program Files\MongoDB\Server.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
您的代码应该类似于
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();