TLSharp icon indicating copy to clipboard operation
TLSharp copied to clipboard

Proxy example HTTP IPv4 IPv6

Open skvoshiz opened this issue 8 years ago • 13 comments

Hello, can I show an example of how to connect using a proxy?

skvoshiz avatar Oct 04 '17 17:10 skvoshiz

I found something https://github.com/sochix/TLSharp/pull/373

skvoshiz avatar Oct 04 '17 19:10 skvoshiz

I'm not sure, that you'll be able to use the http proxy, but using the socks proxy is definitely possible.

dvygolov avatar Oct 04 '17 19:10 dvygolov

@dvygolov how i can do that? You can show example? I will be VERY grateful if you tell me

skvoshiz avatar Oct 04 '17 19:10 skvoshiz

Here is how i do it:

var proxy = await _proxyProviderFactory.GetProxyProvider().GetProxyAsync().ConfigureAwait(false);
if (!string.IsNullOrEmpty(proxy))
{
    TcpClient TcpHandler(string address, int port)
    {
        var split = proxy.Split(':');
        var socksProxy = new Socks5ProxyClient(split[0], int.Parse(split[1]), split[2], split[3]);
        var tcpClient = socksProxy.CreateConnection(address, port);
        return tcpClient;
    }

    _client = new TelegramClient(_telegramApiId, _telegramApiHash, _sessionStore,handler:TcpHandler);
}
else _client = new TelegramClient(_telegramApiId, _telegramApiHash, _sessionStore);

Socks5ProxyClient is a class that I took from this project on GitHub.

dvygolov avatar Oct 04 '17 20:10 dvygolov

@dvygolov thx, i will try with HttpProxyClient.cs

skvoshiz avatar Oct 04 '17 20:10 skvoshiz

@skvoshiz , ok, please write back, if it works.

dvygolov avatar Oct 04 '17 20:10 dvygolov

@dvygolov so, its work with Socks5ProxyClient. But class HttpProxyClient dont have proxyLogin and proxyPassword=( And i cant use proxy without auth

skvoshiz avatar Oct 04 '17 20:10 skvoshiz

So you have to find somewhere an implementation, that supports authorization, or create it yourself. As a matter of fact Windows Telegram client supports HTTP-proxies with login/password, so it should be possible.

dvygolov avatar Oct 04 '17 20:10 dvygolov

@dvygolov i will try, because my goal - its connetc with http IPv6 proxy Thx for help

skvoshiz avatar Oct 04 '17 20:10 skvoshiz

So, this lib work only on SOCKS IPv4

skvoshiz avatar Oct 04 '17 23:10 skvoshiz

I've found this code on Stackoverflow, modify it with proxy username and password and give it a try: No guarantees though.

private static TcpClient conectarProxy(string httpProxyHost, int httpProxyPort)
        {
            var url = "http://" + httpProxyHost + ":" + httpProxyPort;
            var proxyUrl = WebRequest.DefaultWebProxy.GetProxy(new Uri(url));
            WebResponse response = null;
            var tentativas = 10;

            while (tentativas >= 0)
            {
                var request = (HttpWebRequest)WebRequest.Create(url);
                request.KeepAlive = true;
                var webProxy = new WebProxy(proxyUrl);
                request.Proxy = webProxy;
                request.Method = "CONNECT";
                request.Timeout = 3000;

                tentativas--;
                try
                {
                    response = request.GetResponse();
                    break;
                }
                catch (Exception ex)
                {
                    if (tentativas >= 0 && ex.Message.Equals("The operation has timed out", StringComparison.InvariantCultureIgnoreCase))
                    {
                        Console.WriteLine("Ocorreu timeout ao tentar se conectar pelo proxy.");
                    }
                    else
                    {
                        throw new Exception("Algo deu errado", ex);
                    }
                }
            }
            var responseStream = response.GetResponseStream();
            Debug.Assert(responseStream != null);

            const BindingFlags Flags = BindingFlags.NonPublic | BindingFlags.Instance;

            var rsType = responseStream.GetType();
            var connectionProperty = rsType.GetProperty("Connection", Flags);

            var connection = connectionProperty.GetValue(responseStream, null);
            var connectionType = connection.GetType();
            var networkStreamProperty = connectionType.GetProperty("NetworkStream", Flags);

            var networkStream = networkStreamProperty.GetValue(connection, null);
            var nsType = networkStream.GetType();
            var socketProperty = nsType.GetProperty("Socket", Flags);
            var socket = (Socket)socketProperty.GetValue(networkStream, null);

            return new TcpClient { Client = socket };
        }

dvygolov avatar Oct 05 '17 11:10 dvygolov

Its work only for IPv4, i need IPv6

skvoshiz avatar Oct 20 '17 21:10 skvoshiz

@skvoshiz I have the same problem. Do you have the solution to solve this?

vuminhit avatar Jun 09 '20 03:06 vuminhit