首页 > 解决方案 > Renci SSH.net 更改远程 SFTP 端口请求 C#

问题描述

我正在用 C#(VS 2017)编写一小段代码,通过 SOCKS 代理使用 SFTP 将文件上传到远程服务器。

我之前曾将 Renci SSH.net 用于与此类似的其他应用程序,完全没有问题。

然而,在这个应用程序中,我遇到了一个问题,即 Renci SSH.net 对 Socks 代理的调用是针对与我请求的端口不同的端口。

这是我的代码

private static void testSFTPConnection()
 {
        //get the SFTP Connection Info
        ConnectionInfo connInfo = testRemoteSFTP();           

        try
        {
            //Start the SFTP Client
            using (SftpClient remoteSFTPClient = new SftpClient(connInfo))
            {


                //Connect the remote session
                remoteSFTPClient.Connect();

                //log a successful connection
               Console.WriteLine("SFTP File Upload - connection to the file destination server successful");

                //Disconnect the Destination SFTP connection
                remoteSFTPClient.Disconnect();
            }
        }
        catch(Exception ex)
        {
            Console.WriteLine("SFTP File Upload Exception: " + ex.Message);
        }
    }
public static ConnectionInfo testRemoteSFTP()
    {
        return new ConnectionInfo(remoteHost,50101, remoteUser,ProxyTypes.Socks5, proxyURL,proxyPort, proxyUser, proxyPwd, new PasswordAuthenticationMethod(remoteUser,remotePwd));

    }

如您所见,我正在设置连接信息以在端口 50101 上调用远程主机(根据他们的请求)但是当我运行代码时,SOCKS 代理告诉我我正在请求连接到端口 50297 上的远程服务器。

当我尝试在 VS 2017 中调试连接信息时,连接对象向我显示端口请求为 50101,但它仍然命中代理请求端口 50297

我使用 WinSCP 测试了通过端口 50101 上的 SOCKS 代理与远程服务器的连接,并且连接正常。

我使用 Wire Shark 尝试捕获我的传出连接请求,我可以看到端口请求已更改为 50297

我现在不知道这可能是什么原因或应用什么修复,所以在这方面的任何帮助将不胜感激

提前致谢

标签: c#

解决方案


推荐阅读