From 48077961ae730f84f1624f131dd42eb70db18cb6 Mon Sep 17 00:00:00 2001 From: Starli0n Date: Fri, 27 Jan 2017 10:58:47 +0100 Subject: [PATCH] Custom TCP hanlder to allow HTTP proxy auth --- TLSharp.Core/Network/TcpTransport.cs | 18 ++++++++++++------ TLSharp.Core/TelegramClient.cs | 9 ++++++--- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/TLSharp.Core/Network/TcpTransport.cs b/TLSharp.Core/Network/TcpTransport.cs index 9092828..254153e 100644 --- a/TLSharp.Core/Network/TcpTransport.cs +++ b/TLSharp.Core/Network/TcpTransport.cs @@ -1,22 +1,28 @@ using System; -using System.Linq; using System.Net; using System.Net.Sockets; using System.Threading.Tasks; namespace TLSharp.Core.Network { + public delegate TcpClient TcpClientConnectionHandler(string address, int port); + public class TcpTransport : IDisposable { private readonly TcpClient _tcpClient; private int sendCounter = 0; - public TcpTransport(string address, int port) + public TcpTransport(string address, int port, TcpClientConnectionHandler handler = null) { - _tcpClient = new TcpClient(); + if (handler == null) + { + _tcpClient = new TcpClient(); - var ipAddress = IPAddress.Parse(address); - _tcpClient.Connect(ipAddress, port); + var ipAddress = IPAddress.Parse(address); + _tcpClient.Connect(ipAddress, port); + } + else + _tcpClient = handler(address, port); } public async Task Send(byte[] packet) @@ -84,6 +90,6 @@ namespace TLSharp.Core.Network { if (_tcpClient.Connected) _tcpClient.Close(); - } + } } } diff --git a/TLSharp.Core/TelegramClient.cs b/TLSharp.Core/TelegramClient.cs index 9674ee8..d4efdb1 100644 --- a/TLSharp.Core/TelegramClient.cs +++ b/TLSharp.Core/TelegramClient.cs @@ -28,8 +28,10 @@ namespace TLSharp.Core private int _apiId = 0; private Session _session; private List dcOptions; + private TcpClientConnectionHandler _handler; - public TelegramClient(int apiId, string apiHash, ISessionStore store = null, string sessionUserId = "session") + public TelegramClient(int apiId, string apiHash, + ISessionStore store = null, string sessionUserId = "session", TcpClientConnectionHandler handler = null) { if (apiId == default(int)) throw new MissingApiConfigurationException("API_ID"); @@ -42,9 +44,10 @@ namespace TLSharp.Core TLContext.Init(); _apiHash = apiHash; _apiId = apiId; + _handler = handler; _session = Session.TryLoadOrCreateNew(store, sessionUserId); - _transport = new TcpTransport(_session.ServerAddress, _session.Port); + _transport = new TcpTransport(_session.ServerAddress, _session.Port, _handler); } public async Task ConnectAsync(bool reconnect = false) @@ -85,7 +88,7 @@ namespace TLSharp.Core var dc = dcOptions.First(d => d.id == dcId); - _transport = new TcpTransport(dc.ip_address, dc.port); + _transport = new TcpTransport(dc.ip_address, dc.port, _handler); _session.ServerAddress = dc.ip_address; _session.Port = dc.port;