ArxOne / FTP

Simple FTP client
MIT License
37 stars 15 forks source link

Could you make some examples with proxy server #23

Open rmaniac opened 8 years ago

rmaniac commented 8 years ago

Hi! Could you give me an example for using proxies in your framework. I found a Delegte ConectProxy wich can be set in FtpClientParameter Class. In a Class I would like to use the FtpClient, both proxy and ftp data are known. But i don't realy understand how can I produce a SocketObject from given EndPoint.

Thanks a lot!

picrap commented 8 years ago

Well... That's a good open question :smile: This FTP assembly does not handle proxy servers directly (SOCKS, HTTP connect...). Instead you need to plug you own library in order to create a socket from a given endpoint. Personnally, I use Starksoft Aspen library which provides some proxy servers. With a simple intermediate method, I am able to branch the FTP client to a proxy. If you can wait a bit, I'll provide a sample. However, you got the idea.

picrap commented 8 years ago

This is an untested code, however it is clear enough to help (I hope!)

// this comes from Starksoft Aspen library (available as NuGet package)
private readonly Starksoft.Net.Proxy.ProxyClientFactory _factory 
    = new Starksoft.Net.Proxy.ProxyClientFactory();

public string ProxyHost { get; set; } = "MyProxy";
public int ProxyPort { get; set; } = 1080;

// this is the method to use with FTP
public Socket ProxyConnect(EndPoint endPoint)
{
    // SOCK4 is an example, of course :)
    var proxy = _factory.CreateProxyClient(Starksoft.Net.Proxy.ProxyType.Socks4, 
        ProxyHost, ProxyPort);
    try
    {
        // now we have two options: the endpoint provides name or IP
        // test as name
        var dnsEndPoint = endPoint as DnsEndPoint;
        if(dnsEndPoint != null)
            return proxy.CreateConnection(dnsEndPoint.Host, dnsEndPoint.Port).Client;
        // test as IP
        var ipEndPoint = endPoint as IPEndPoint;
        if(ipEndPoint != null)
            return proxy.CreateConnection(ipEndPoint.Address.ToString(), ipEndPoint.Port).Client;
        throw new ArgumentException(nameof(endPoint));
    }
    catch (Starksoft.Net.Proxy.ProxyException pe)
    {
        throw new IOException("SOCKS4 proxy exception", pe);
    }
}

Let me know if this helps

zharris6 commented 7 years ago

I Was able to get a Socks5 proxy working with auth.

Using this package:

https://github.com/ThrDev/Socks5

here is a simple example:

private Socket proxy(EndPoint a) {

        DnsEndPoint Endpoint = ((DnsEndPoint)(a));

        Socks5Client client = new Socks5Client("ip", port, Endpoint.Host, Endpoint.Port, "proxyuser", "proxy pass");
        client.Connect();

        if (client.Connected) {
            return client.Client.Sock;
        }
         return null;

    }
picrap commented 7 years ago

Yes, that's how it works 😉