TLSharp
TLSharp copied to clipboard
Proxy example HTTP IPv4 IPv6
Hello, can I show an example of how to connect using a proxy?
I found something https://github.com/sochix/TLSharp/pull/373
I'm not sure, that you'll be able to use the http proxy, but using the socks proxy is definitely possible.
@dvygolov how i can do that? You can show example? I will be VERY grateful if you tell me
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 thx, i will try with HttpProxyClient.cs
@skvoshiz , ok, please write back, if it works.
@dvygolov so, its work with Socks5ProxyClient. But class HttpProxyClient dont have proxyLogin and proxyPassword=( And i cant use proxy without auth
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 i will try, because my goal - its connetc with http IPv6 proxy Thx for help
So, this lib work only on SOCKS IPv4
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 };
}
Its work only for IPv4, i need IPv6
@skvoshiz I have the same problem. Do you have the solution to solve this?