Custom TCP hanlder to allow HTTP proxy auth

This commit is contained in:
Starli0n 2017-01-27 10:58:47 +01:00
parent 85c206cc24
commit 48077961ae
2 changed files with 18 additions and 9 deletions

View file

@ -1,23 +1,29 @@
using System; using System;
using System.Linq;
using System.Net; using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace TLSharp.Core.Network namespace TLSharp.Core.Network
{ {
public delegate TcpClient TcpClientConnectionHandler(string address, int port);
public class TcpTransport : IDisposable public class TcpTransport : IDisposable
{ {
private readonly TcpClient _tcpClient; private readonly TcpClient _tcpClient;
private int sendCounter = 0; private int sendCounter = 0;
public TcpTransport(string address, int port) public TcpTransport(string address, int port, TcpClientConnectionHandler handler = null)
{
if (handler == null)
{ {
_tcpClient = new TcpClient(); _tcpClient = new TcpClient();
var ipAddress = IPAddress.Parse(address); var ipAddress = IPAddress.Parse(address);
_tcpClient.Connect(ipAddress, port); _tcpClient.Connect(ipAddress, port);
} }
else
_tcpClient = handler(address, port);
}
public async Task Send(byte[] packet) public async Task Send(byte[] packet)
{ {

View file

@ -28,8 +28,10 @@ namespace TLSharp.Core
private int _apiId = 0; private int _apiId = 0;
private Session _session; private Session _session;
private List<TLDcOption> dcOptions; private List<TLDcOption> 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)) if (apiId == default(int))
throw new MissingApiConfigurationException("API_ID"); throw new MissingApiConfigurationException("API_ID");
@ -42,9 +44,10 @@ namespace TLSharp.Core
TLContext.Init(); TLContext.Init();
_apiHash = apiHash; _apiHash = apiHash;
_apiId = apiId; _apiId = apiId;
_handler = handler;
_session = Session.TryLoadOrCreateNew(store, sessionUserId); _session = Session.TryLoadOrCreateNew(store, sessionUserId);
_transport = new TcpTransport(_session.ServerAddress, _session.Port); _transport = new TcpTransport(_session.ServerAddress, _session.Port, _handler);
} }
public async Task<bool> ConnectAsync(bool reconnect = false) public async Task<bool> ConnectAsync(bool reconnect = false)
@ -85,7 +88,7 @@ namespace TLSharp.Core
var dc = dcOptions.First(d => d.id == dcId); 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.ServerAddress = dc.ip_address;
_session.Port = dc.port; _session.Port = dc.port;