This project is read-only.

Proxy issue with Sftp class (client) with Renci.SshNet library

Feb 23, 2012 at 12:34 PM

Hi,

I am using Renci.SshNet library and from it I am using SftpClient class to connect with Secure FTP site.

I can successfully connected with secure ftp site while I use a network where proxy settings are required. But if I move to a network where proxy settings are mandatory then I am unable to connect it.

Is there any way to connect with Secure FTP site with such proxy settings?

Feb 23, 2012 at 1:11 PM

Maybe it will help if you remove the proxy settings?

Feb 23, 2012 at 1:24 PM

Are you connecting using Private Key Authentication?

Feb 23, 2012 at 1:51 PM

Did you downloaded the latest version from the source code tab?

Since proxy support was introduced couple month ago and I didnt have time yet to release a new version.

 

Thanks,

Oleg

Feb 23, 2012 at 2:32 PM

@roker002: I can't remove proxy settings.

@lg11105: No, I am not connected via Private Key Authentication.

@olegkap: I downloaded the source code a couple of days ago. I am not sure it has proxy support as I am unable to find something like that in entire code.

Feb 23, 2012 at 2:49 PM

Here some usage example that I posted here:

Usage example:
var connectionInfo = new PasswordConnectionInfo("1.2.3.4", "username", "password", ProxyTypes.Socks4, "127.0.0.1", 8088);
using (var ssh = new SshClient(connectionInfo))
{
ssh.Connect();
var rrr = ssh.RunCommand("ls -l");
}

 

See if it works for you.

 

It only supports SOCKS4 and SOCKS5 protocols for now.

 

Thanks,

Oleg

Feb 24, 2012 at 4:52 AM
Edited Feb 24, 2012 at 6:01 AM

Hi olegkap,

Thanks for your quick reply but the class "PasswordConnectionInfo", I have has 2 overloads only and none of them comply with what you have pasted above.

Overload 1 allows me to specify "Host, Username, Password".

While overload 2 takes 4 arguments "Host, Port, Username, Password".

 

No idea how to proceed further.

Feb 24, 2012 at 1:30 PM

You need to download latest version from the source code tab since this option has not been released yet.

 

Thanks,

Oleg

Feb 24, 2012 at 2:46 PM

Any idea when this version is going to release? I am looking for it eagerly.

Feb 24, 2012 at 3:00 PM

You can use it now if you like by downloading latest source code.

 

Thanks,

Oleg

Feb 24, 2012 at 3:16 PM

have you released the latest version? If yes then please pass me the link because I can see the same version what I downloaded last time (on http://sshnet.codeplex.com/releases/view/77867).

Feb 24, 2012 at 3:48 PM

no,

What I mean is that you can go here:

http://sshnet.codeplex.com/SourceControl/list/changesets

and download latest available source code from there.

 

When new release will come out it will have this included already.

 

Thanks,

Oleg

Feb 24, 2012 at 4:43 PM

Thanks Oleg,

I have downloaded it. I will get back to you after its use.

Feb 27, 2012 at 5:33 AM

Hi Oleg,

As I mentioned above I have downloaded the latest code but I found that it works for SOCKET4/SOCKET5 proxies.

But my proxy is HTTP. My proxy server doesn't works on SOCKET4/5, I need to create HTTP connection with it.

Any idea how to implement it.

Feb 27, 2012 at 2:19 PM

Hi,

Its a protocol, so I guess to find protocol description somewhere on the internet and follow it.

 

I personally dont have a HTTP proxy server here where I could test it against so I didn't implement it.

If you have HTTP proxy server that I can use to test this feature I could take a look at it later this week, please open a feature request for it so I dont forget.

 

Or, also, if you can implement it yourself and want me to incorporate your code into the library, it will work to.

 

Thanks,

Oleg

Feb 28, 2012 at 11:04 AM

Hi Oleg,

Thanks for all support and really it is too helpful.

Yes, you can use my proxy server (HTTP) to test HTTP proxy implementation.

I was trying to implement HTTP by myself today but honestly I found some piece of code where I got stuck.

So, it is will be very helpful if it can be implemented by you later this week (as you said above).

 

And thanks again for you support, I really appreciate it.

Feb 28, 2012 at 5:54 PM

Ok,

 

I will try to implement it later then.

 

But what is the HTTP Proxy server that I can use? You can send it to me privately if you wish.

 

I tried to use few public ones, but with no luck :(

 

Also, if it requires username/password then I will need them too.

 

Thanks,

Oleg

Feb 29, 2012 at 10:16 AM

Hi Oleg,

 

Please provide me you email id.

Feb 29, 2012 at 1:42 PM

You can send it to olegkap@gmail.com.

 

Thanks,

Oleg

Mar 7, 2012 at 10:51 AM

Hi Oleg, 

It (newly added code) works and solved my problem too. 

Thanks a ton for your help and support.

I really appreciate it.

Jun 18, 2012 at 3:42 PM
Edited Jun 18, 2012 at 3:49 PM

Guys,

Is HTTP proxy supported in the newest version? If so, could you please provide a code example how to use it with SFTP?

Thanks,

Greg

Jun 18, 2012 at 3:52 PM

Hi,

 

Yes,

it should work. Here is an example:

 

            var connectionInfo = new PasswordConnectionInfo("host", 22, "username", "password", ProxyTypes.Http, "proxyhost", 3128);
            using (var shell = new SshClient(connectionInfo))
            {
                shell.Connect();
                var aa = shell.RunCommand("ls -l");
                shell.Disconnect();
            }

Hope it helps.

 

Thanks,

Oleg

Jun 19, 2012 at 8:51 AM
Edited Jun 19, 2012 at 10:18 AM

Thanks for fast reply but the issue is not resolved. Below are the code and the exception I get. Can you please take a look if the params are ok? Can I use IP as a hostname and vice versa? I tried also the version with SshClient instead of SftpClient but the same result.

Thanks,

Greg

 

var connectionInfo = new PasswordConnectionInfo("123.123.123.123", 21, "FtpUser", "FtpPasswd",ProxyTypes.Http, 
"proxy.somewhere.com",8080);
SftpClient sftpClient = new SftpClient(connectionInfo);
sftpClient.Connect();

 

 

The exception:
System.NotSupportedException was unhandled
  Message=Result cannot be called on a failed Match.
  Source=System
  StackTrace:
       at System.Text.RegularExpressions.Match.Result(String replacement)
       at Renci.SshNet.Session.ConnectHttp(Socket socket)
       at Renci.SshNet.Session.Connect()
       at Renci.SshNet.BaseClient.Connect()
Jun 20, 2012 at 9:00 AM

I took a look into wireshark logs and I noticed that the request sent to proxy didn't contain user name and password for the FTP server. The answer from proxy is 'Forbidden' and then I see the exception from the post above. Should the username and password be passed as a plaintext in the http request to the proxy?

Thanks,

Greg

Jun 20, 2012 at 9:28 AM

yes you should pass UserName and Password along with your Domain name (if you have any) because to process your request using proxy server your proxy server will authenticate your request based on provided credentails. If no parameters are passed for authentication it will throw this error.

Jun 20, 2012 at 9:45 AM

I passed the username and password(see code above)but I don't see them in the network trace. I'm trying to figure out if this is ok.

Jun 20, 2012 at 10:01 AM
Edited Jun 20, 2012 at 10:03 AM

Please refer the below code:

 

ConnectionInfo connectionInfo = null

;  

if (this .IsProxyConnectionRequired)  else

{

connectionInfo =

 

new PasswordConnectionInfo(this.FTPServer,this.FTPServerPort, this.FTPUserID, this.FTPPassword, ProxyTypes.Http, this.NetworkProxyServer, Convert.ToInt32(this.networkProxyServerPort), this.NetworkProxyUserDomain + @"\" + this.networkProxyUserId, this.NetworkProxyUserPassword);

 

}

 

connectionInfo =

new PasswordConnectionInfo(this.FTPServer, this.FTPServerPort, this.FTPUserID, this.FTPPassword); 

}

 

 

 

 

 

{

Jun 20, 2012 at 11:06 AM

Guys, your library is ok, the reason why it doesn't work is my proxy configuration. Anyway it would be good to fix the exception that is returned in that case: 'Result cannot be called on a failed Match.' doesn't make sense.

Thanks,

Greg

Oct 26, 2014 at 2:37 PM
hi friend

how to connect to sftp server with (http proxy) AND (private key) ????

thanks :X
Nov 4, 2014 at 7:33 PM
This should do it:
using Renci.SshNet;

namespace SshNetTests
{
    class Program
    {
        public static void Main()
        {
            var privateKeyAuthenticationMethod = new PrivateKeyAuthenticationMethod("<user>",
                new PrivateKeyFile(@"<path to private key>"));
            var connectionInfo = new ConnectionInfo("<SSH server hostname or IP address>",
                                                    22,
                                                    "<user>",
                                                    ProxyTypes.Http,
                                                    "<proxy server hostname or IP address>",
                                                    8080,
                                                    "<proxy user>",
                                                    "<proxy password>",
                                                    privateKeyAuthenticationMethod);

            using (var sftpClient = new SftpClient(connectionInfo))
            {
                sftpClient.Connect();
            }
        }

    }
}
Marked as answer by drieseng on 11/4/2014 at 11:33 AM