0 votes
by (330 points)
edited

I'd like the SFTP client to automatically use the system wide proxy settings (under Windows XP+). Is this possible?

Cheers, Christian

Applies to: Rebex SFTP

1 Answer

+1 vote
by (58.9k points)
edited

Rebex SFTP supports these system proxy types (which can be set in Internet Options/Connections/Lan Settings/Advanced):

  • Secure (https) ~ corresponds to Rebex.Net.ProxyType.HttpConnect
  • Socks (version 4) ~ corresponds to Rebex.Net.ProxyType.Socks4a

The other two options which can be set using system settings (i.e. Ftp and Http are not currently supported by Rebex.SFTP component). If you only set a system proxy without specifying its type, it corresponds to https proxy.

You could use the class SystemProxyHelper to automatically retrieve the system proxy settings like this:

Sftp sftp = new Sftp();
SystemProxyHelper.UseSystemProxy(sftp);

string server = "server";
int port = Sftp.DefaultPort;
string username = "user";
string password = "password";

sftp.Connect(server, port);
sftp.Login(username, password);

//...

sftp.Disconnect();

public class SystemProxyHelper
{
    public static void UseSystemProxy(Sftp sftp)
    {
        Microsoft.Win32.RegistryKey registry = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings");

        if ((int)registry.GetValue("ProxyEnable") == 1)
        {
            string key = (string)registry.GetValue("ProxyServer");

            if (!string.IsNullOrEmpty(key))
            {
                foreach (string proxy in key.Split(new char[] { ';' }))
                {
                    SystemProxy systemProxy = new SystemProxy(proxy);
                    if (systemProxy.Type == "https" || systemProxy.Type == "socks")
                    {
                        sftp.Proxy = systemProxy.TranslateToRebexProxy();
                        break;
                    }
                }

                if (sftp.Proxy.ProxyType == ProxyType.None)
                    throw new ApplicationException("No proxy supported by Rebex SFTP found in system.");

            }
        }
    }

    public class SystemProxy
    {
        public string Server { get; set; }
        public int Port { get; set; }
        public string Type { get; set; }

        public SystemProxy(string proxy)
        {
            int index;
            if ((index = proxy.IndexOf('=')) == -1)
            {
                ParseServerPort(proxy);
                Type = "https";
            }
            else
            {
                ParseServerPort(proxy.Substring(index + 1));
                Type = proxy.Substring(0, index);
            }
        }

        private void ParseServerPort(string serverPort)
        {
            int index = serverPort.IndexOf(':');
            Server = serverPort.Substring(0, index);
            Port = Int32.Parse(serverPort.Substring(index + 1));
        }

        public Proxy TranslateToRebexProxy()
        {
            Proxy proxy = new Proxy();

            switch (Type)
            {
                case "https": proxy.ProxyType = ProxyType.HttpConnect; break;
                case "socks": proxy.ProxyType = ProxyType.Socks4a; break;
                default: throw new ApplicationException(string.Format("Proxy type {0} is not supported by Rebex SFTP.", Type));
            }

            proxy.Host = Server;
            proxy.Port = Port;
            return proxy;
        }
    }
}
by (330 points)
edited

Thanks for letting me know! I'll check this out.

...