mirror of
https://github.com/sochix/TLSharp.git
synced 2026-03-20 20:25:32 +01:00
not async
This commit is contained in:
parent
f0c2f43aef
commit
cc5a3d6bb9
|
|
@ -5,31 +5,31 @@ namespace TLSharp.Core.Auth
|
||||||
{
|
{
|
||||||
public static class Authenticator
|
public static class Authenticator
|
||||||
{
|
{
|
||||||
public static async Task<Step3_Response> DoAuthentication(TcpTransport transport)
|
public static Step3_Response DoAuthentication(TcpTransport transport)
|
||||||
{
|
{
|
||||||
var sender = new MtProtoPlainSender(transport);
|
var sender = new MtProtoPlainSender(transport);
|
||||||
var step1 = new Step1_PQRequest();
|
var step1 = new Step1_PQRequest();
|
||||||
|
|
||||||
await sender.Send(step1.ToBytes());
|
sender.Send(step1.ToBytes());
|
||||||
var step1Response = step1.FromBytes(await sender.Receive());
|
var step1Response = step1.FromBytes(sender.Receive());
|
||||||
|
|
||||||
var step2 = new Step2_DHExchange();
|
var step2 = new Step2_DHExchange();
|
||||||
await sender.Send(step2.ToBytes(
|
sender.Send(step2.ToBytes(
|
||||||
step1Response.Nonce,
|
step1Response.Nonce,
|
||||||
step1Response.ServerNonce,
|
step1Response.ServerNonce,
|
||||||
step1Response.Fingerprints,
|
step1Response.Fingerprints,
|
||||||
step1Response.Pq));
|
step1Response.Pq));
|
||||||
|
|
||||||
var step2Response = step2.FromBytes(await sender.Receive());
|
var step2Response = step2.FromBytes(sender.Receive());
|
||||||
|
|
||||||
var step3 = new Step3_CompleteDHExchange();
|
var step3 = new Step3_CompleteDHExchange();
|
||||||
await sender.Send(step3.ToBytes(
|
sender.Send(step3.ToBytes(
|
||||||
step2Response.Nonce,
|
step2Response.Nonce,
|
||||||
step2Response.ServerNonce,
|
step2Response.ServerNonce,
|
||||||
step2Response.NewNonce,
|
step2Response.NewNonce,
|
||||||
step2Response.EncryptedAnswer));
|
step2Response.EncryptedAnswer));
|
||||||
|
|
||||||
var step3Response = step3.FromBytes(await sender.Receive());
|
var step3Response = step3.FromBytes(sender.Receive());
|
||||||
|
|
||||||
return step3Response;
|
return step3Response;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ namespace TLSharp.Core.Network
|
||||||
random = new Random();
|
random = new Random();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task Send(byte[] data)
|
public void Send(byte[] data)
|
||||||
{
|
{
|
||||||
using (var memoryStream = new MemoryStream())
|
using (var memoryStream = new MemoryStream())
|
||||||
{
|
{
|
||||||
|
|
@ -31,14 +31,14 @@ namespace TLSharp.Core.Network
|
||||||
|
|
||||||
byte[] packet = memoryStream.ToArray();
|
byte[] packet = memoryStream.ToArray();
|
||||||
|
|
||||||
await _transport.Send(packet);
|
_transport.Send(packet);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<byte[]> Receive()
|
public byte[] Receive()
|
||||||
{
|
{
|
||||||
var result = await _transport.Receieve();
|
var result = _transport.Receieve();
|
||||||
|
|
||||||
using (var memoryStream = new MemoryStream(result.Body))
|
using (var memoryStream = new MemoryStream(result.Body))
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -34,7 +34,7 @@ namespace TLSharp.Core.Network
|
||||||
return confirmed ? _session.Sequence++ * 2 + 1 : _session.Sequence * 2;
|
return confirmed ? _session.Sequence++ * 2 + 1 : _session.Sequence * 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task Send(TeleSharp.TL.TLMethod request)
|
public void Send(TeleSharp.TL.TLMethod request)
|
||||||
{
|
{
|
||||||
// TODO: refactor
|
// TODO: refactor
|
||||||
if (needConfirmation.Any())
|
if (needConfirmation.Any())
|
||||||
|
|
@ -44,7 +44,7 @@ namespace TLSharp.Core.Network
|
||||||
using (var writer = new BinaryWriter(memory))
|
using (var writer = new BinaryWriter(memory))
|
||||||
{
|
{
|
||||||
ackRequest.SerializeBody(writer);
|
ackRequest.SerializeBody(writer);
|
||||||
await Send(memory.ToArray(), ackRequest);
|
Send(memory.ToArray(), ackRequest);
|
||||||
needConfirmation.Clear();
|
needConfirmation.Clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -54,13 +54,13 @@ namespace TLSharp.Core.Network
|
||||||
using (var writer = new BinaryWriter(memory))
|
using (var writer = new BinaryWriter(memory))
|
||||||
{
|
{
|
||||||
request.SerializeBody(writer);
|
request.SerializeBody(writer);
|
||||||
await Send(memory.ToArray(), request);
|
Send(memory.ToArray(), request);
|
||||||
}
|
}
|
||||||
|
|
||||||
_session.Save();
|
_session.Save();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task Send(byte[] packet, TeleSharp.TL.TLMethod request)
|
public void Send(byte[] packet, TeleSharp.TL.TLMethod request)
|
||||||
{
|
{
|
||||||
request.MessageId = _session.GetNewMessageId();
|
request.MessageId = _session.GetNewMessageId();
|
||||||
|
|
||||||
|
|
@ -90,7 +90,7 @@ namespace TLSharp.Core.Network
|
||||||
writer.Write(msgKey);
|
writer.Write(msgKey);
|
||||||
writer.Write(ciphertext);
|
writer.Write(ciphertext);
|
||||||
|
|
||||||
await _transport.Send(ciphertextPacket.GetBuffer());
|
_transport.Send(ciphertextPacket.GetBuffer());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -142,11 +142,11 @@ namespace TLSharp.Core.Network
|
||||||
return new Tuple<byte[], ulong, int>(message, remoteMessageId, remoteSequence);
|
return new Tuple<byte[], ulong, int>(message, remoteMessageId, remoteSequence);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<byte[]> Receive(TeleSharp.TL.TLMethod request)
|
public byte[] Receive(TeleSharp.TL.TLMethod request)
|
||||||
{
|
{
|
||||||
while (!request.ConfirmReceived)
|
while (!request.ConfirmReceived)
|
||||||
{
|
{
|
||||||
var result = DecodeMessage((await _transport.Receieve()).Body);
|
var result = DecodeMessage((_transport.Receieve()).Body);
|
||||||
|
|
||||||
using (var messageStream = new MemoryStream(result.Item1, false))
|
using (var messageStream = new MemoryStream(result.Item1, false))
|
||||||
using (var messageReader = new BinaryReader(messageStream))
|
using (var messageReader = new BinaryReader(messageStream))
|
||||||
|
|
@ -158,17 +158,17 @@ namespace TLSharp.Core.Network
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task SendPingAsync()
|
public void SendPingAsync()
|
||||||
{
|
{
|
||||||
var pingRequest = new PingRequest();
|
var pingRequest = new PingRequest();
|
||||||
using (var memory = new MemoryStream())
|
using (var memory = new MemoryStream())
|
||||||
using (var writer = new BinaryWriter(memory))
|
using (var writer = new BinaryWriter(memory))
|
||||||
{
|
{
|
||||||
pingRequest.SerializeBody(writer);
|
pingRequest.SerializeBody(writer);
|
||||||
await Send(memory.ToArray(), pingRequest);
|
Send(memory.ToArray(), pingRequest);
|
||||||
}
|
}
|
||||||
|
|
||||||
await Receive(pingRequest);
|
Receive(pingRequest);
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool ProcessMessage(ulong messageId, int sequence, BinaryReader messageReader, TeleSharp.TL.TLMethod request)
|
private bool ProcessMessage(ulong messageId, int sequence, BinaryReader messageReader, TeleSharp.TL.TLMethod request)
|
||||||
|
|
|
||||||
|
|
@ -25,28 +25,28 @@ namespace TLSharp.Core.Network
|
||||||
_tcpClient = handler(address, port);
|
_tcpClient = handler(address, port);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task Send(byte[] packet)
|
public void Send(byte[] packet)
|
||||||
{
|
{
|
||||||
if (!_tcpClient.Connected)
|
if (!_tcpClient.Connected)
|
||||||
throw new InvalidOperationException("Client not connected to server.");
|
throw new InvalidOperationException("Client not connected to server.");
|
||||||
|
|
||||||
var tcpMessage = new TcpMessage(sendCounter, packet);
|
var tcpMessage = new TcpMessage(sendCounter, packet);
|
||||||
|
|
||||||
await _tcpClient.GetStream().WriteAsync(tcpMessage.Encode(), 0, tcpMessage.Encode().Length);
|
_tcpClient.GetStream().Write(tcpMessage.Encode(), 0, tcpMessage.Encode().Length);
|
||||||
sendCounter++;
|
sendCounter++;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<TcpMessage> Receieve()
|
public TcpMessage Receieve()
|
||||||
{
|
{
|
||||||
var stream = _tcpClient.GetStream();
|
var stream = _tcpClient.GetStream();
|
||||||
|
|
||||||
var packetLengthBytes = new byte[4];
|
var packetLengthBytes = new byte[4];
|
||||||
if (await stream.ReadAsync(packetLengthBytes, 0, 4) != 4)
|
if (stream.Read(packetLengthBytes, 0, 4) != 4)
|
||||||
throw new InvalidOperationException("Couldn't read the packet length");
|
throw new InvalidOperationException("Couldn't read the packet length");
|
||||||
int packetLength = BitConverter.ToInt32(packetLengthBytes, 0);
|
int packetLength = BitConverter.ToInt32(packetLengthBytes, 0);
|
||||||
|
|
||||||
var seqBytes = new byte[4];
|
var seqBytes = new byte[4];
|
||||||
if (await stream.ReadAsync(seqBytes, 0, 4) != 4)
|
if (stream.Read(seqBytes, 0, 4) != 4)
|
||||||
throw new InvalidOperationException("Couldn't read the sequence");
|
throw new InvalidOperationException("Couldn't read the sequence");
|
||||||
int seq = BitConverter.ToInt32(seqBytes, 0);
|
int seq = BitConverter.ToInt32(seqBytes, 0);
|
||||||
|
|
||||||
|
|
@ -57,7 +57,7 @@ namespace TLSharp.Core.Network
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
var bodyByte = new byte[packetLength - 12];
|
var bodyByte = new byte[packetLength - 12];
|
||||||
var availableBytes = await stream.ReadAsync(bodyByte, 0, neededToRead);
|
var availableBytes = stream.Read(bodyByte, 0, neededToRead);
|
||||||
neededToRead -= availableBytes;
|
neededToRead -= availableBytes;
|
||||||
Buffer.BlockCopy(bodyByte, 0, body, readBytes, availableBytes);
|
Buffer.BlockCopy(bodyByte, 0, body, readBytes, availableBytes);
|
||||||
readBytes += availableBytes;
|
readBytes += availableBytes;
|
||||||
|
|
@ -65,7 +65,7 @@ namespace TLSharp.Core.Network
|
||||||
while (readBytes != packetLength - 12);
|
while (readBytes != packetLength - 12);
|
||||||
|
|
||||||
var crcBytes = new byte[4];
|
var crcBytes = new byte[4];
|
||||||
if (await stream.ReadAsync(crcBytes, 0, 4) != 4)
|
if ( stream.Read(crcBytes, 0, 4) != 4)
|
||||||
throw new InvalidOperationException("Couldn't read the crc");
|
throw new InvalidOperationException("Couldn't read the crc");
|
||||||
int checksum = BitConverter.ToInt32(crcBytes, 0);
|
int checksum = BitConverter.ToInt32(crcBytes, 0);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -49,11 +49,11 @@ namespace TLSharp.Core
|
||||||
_transport = new TcpTransport(_session.ServerAddress, _session.Port, _handler);
|
_transport = new TcpTransport(_session.ServerAddress, _session.Port, _handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task ConnectAsync(bool reconnect = false)
|
public void ConnectAsync(bool reconnect = false)
|
||||||
{
|
{
|
||||||
if (_session.AuthKey == null || reconnect)
|
if (_session.AuthKey == null || reconnect)
|
||||||
{
|
{
|
||||||
var result = await Authenticator.DoAuthentication(_transport);
|
var result = Authenticator.DoAuthentication(_transport);
|
||||||
_session.AuthKey = result.AuthKey;
|
_session.AuthKey = result.AuthKey;
|
||||||
_session.TimeOffset = result.TimeOffset;
|
_session.TimeOffset = result.TimeOffset;
|
||||||
}
|
}
|
||||||
|
|
@ -72,13 +72,13 @@ namespace TLSharp.Core
|
||||||
SystemVersion = "Win 10.0"
|
SystemVersion = "Win 10.0"
|
||||||
};
|
};
|
||||||
var invokewithLayer = new TLRequestInvokeWithLayer() { Layer = 66, Query = request };
|
var invokewithLayer = new TLRequestInvokeWithLayer() { Layer = 66, Query = request };
|
||||||
await _sender.Send(invokewithLayer);
|
_sender.Send(invokewithLayer);
|
||||||
await _sender.Receive(invokewithLayer);
|
_sender.Receive(invokewithLayer);
|
||||||
|
|
||||||
dcOptions = ((TLConfig)invokewithLayer.Response).DcOptions.ToList();
|
dcOptions = ((TLConfig)invokewithLayer.Response).DcOptions.ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task ReconnectToDcAsync(int dcId, int times)
|
private void ReconnectToDcAsync(int dcId, int times)
|
||||||
{
|
{
|
||||||
if (dcOptions == null || !dcOptions.Any())
|
if (dcOptions == null || !dcOptions.Any())
|
||||||
throw new InvalidOperationException($"Can't reconnect. Establish initial connection first.");
|
throw new InvalidOperationException($"Can't reconnect. Establish initial connection first.");
|
||||||
|
|
@ -98,7 +98,7 @@ namespace TLSharp.Core
|
||||||
if (_session.TLUser != null)
|
if (_session.TLUser != null)
|
||||||
{
|
{
|
||||||
TLRequestExportAuthorization exportAuthorization = new TLRequestExportAuthorization() { DcId = dcId };
|
TLRequestExportAuthorization exportAuthorization = new TLRequestExportAuthorization() { DcId = dcId };
|
||||||
exported = await SendRequestAsync<TLExportedAuthorization>(exportAuthorization, times);
|
exported = SendRequestAsync<TLExportedAuthorization>(exportAuthorization, times);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -106,20 +106,20 @@ namespace TLSharp.Core
|
||||||
_session.ServerAddress = dc.IpAddress;
|
_session.ServerAddress = dc.IpAddress;
|
||||||
_session.Port = dc.Port;
|
_session.Port = dc.Port;
|
||||||
|
|
||||||
await ConnectAsync(true);
|
ConnectAsync(true);
|
||||||
|
|
||||||
|
|
||||||
if (_session.TLUser != null)
|
if (_session.TLUser != null)
|
||||||
{
|
{
|
||||||
TLRequestImportAuthorization importAuthorization = new TLRequestImportAuthorization() { Id = exported.Id, Bytes = exported.Bytes };
|
TLRequestImportAuthorization importAuthorization = new TLRequestImportAuthorization() { Id = exported.Id, Bytes = exported.Bytes };
|
||||||
var imported = await SendRequestAsync<TLAuthorization>(importAuthorization,times);
|
var imported = SendRequestAsync<TLAuthorization>(importAuthorization,times);
|
||||||
OnUserAuthenticated(((TLUser)imported.User));
|
OnUserAuthenticated(((TLUser)imported.User));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task RequestWithDcMigration(TLMethod request, int times)
|
private void RequestWithDcMigration(TLMethod request, int times)
|
||||||
{
|
{
|
||||||
if (_sender == null)
|
if (_sender == null)
|
||||||
throw new InvalidOperationException("Not connected!");
|
throw new InvalidOperationException("Not connected!");
|
||||||
|
|
@ -129,15 +129,15 @@ namespace TLSharp.Core
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await _sender.Send(request);
|
_sender.Send(request);
|
||||||
await _sender.Receive(request);
|
_sender.Receive(request);
|
||||||
completed = true;
|
completed = true;
|
||||||
}
|
}
|
||||||
catch(DataCenterMigrationException e)
|
catch(DataCenterMigrationException e)
|
||||||
{
|
{
|
||||||
if (times <= 150)
|
if (times <= 150)
|
||||||
{
|
{
|
||||||
await ReconnectToDcAsync(e.DC, times + 1);
|
ReconnectToDcAsync(e.DC, times + 1);
|
||||||
// prepare the request for another try
|
// prepare the request for another try
|
||||||
request.ConfirmReceived = false;
|
request.ConfirmReceived = false;
|
||||||
}
|
}
|
||||||
|
|
@ -162,31 +162,31 @@ namespace TLSharp.Core
|
||||||
return _session.TLUser != null;
|
return _session.TLUser != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<bool> IsPhoneRegisteredAsync(string phoneNumber)
|
public bool IsPhoneRegisteredAsync(string phoneNumber)
|
||||||
{
|
{
|
||||||
if (String.IsNullOrWhiteSpace(phoneNumber))
|
if (String.IsNullOrWhiteSpace(phoneNumber))
|
||||||
throw new ArgumentNullException(nameof(phoneNumber));
|
throw new ArgumentNullException(nameof(phoneNumber));
|
||||||
|
|
||||||
var authCheckPhoneRequest = new TLRequestCheckPhone() { PhoneNumber = phoneNumber };
|
var authCheckPhoneRequest = new TLRequestCheckPhone() { PhoneNumber = phoneNumber };
|
||||||
|
|
||||||
await RequestWithDcMigration(authCheckPhoneRequest,0);
|
RequestWithDcMigration(authCheckPhoneRequest,0);
|
||||||
|
|
||||||
return authCheckPhoneRequest.Response.PhoneRegistered;
|
return authCheckPhoneRequest.Response.PhoneRegistered;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<string> SendCodeRequestAsync(string phoneNumber)
|
public string SendCodeRequestAsync(string phoneNumber)
|
||||||
{
|
{
|
||||||
if (String.IsNullOrWhiteSpace(phoneNumber))
|
if (String.IsNullOrWhiteSpace(phoneNumber))
|
||||||
throw new ArgumentNullException(nameof(phoneNumber));
|
throw new ArgumentNullException(nameof(phoneNumber));
|
||||||
|
|
||||||
var request = new TLRequestSendCode() { PhoneNumber = phoneNumber, ApiId = _apiId, ApiHash = _apiHash };
|
var request = new TLRequestSendCode() { PhoneNumber = phoneNumber, ApiId = _apiId, ApiHash = _apiHash };
|
||||||
|
|
||||||
await RequestWithDcMigration(request,0);
|
RequestWithDcMigration(request,0);
|
||||||
|
|
||||||
return request.Response.PhoneCodeHash;
|
return request.Response.PhoneCodeHash;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<TLUser> MakeAuthAsync(string phoneNumber, string phoneCodeHash, string code)
|
public TLUser MakeAuthAsync(string phoneNumber, string phoneCodeHash, string code)
|
||||||
{
|
{
|
||||||
if (String.IsNullOrWhiteSpace(phoneNumber))
|
if (String.IsNullOrWhiteSpace(phoneNumber))
|
||||||
throw new ArgumentNullException(nameof(phoneNumber));
|
throw new ArgumentNullException(nameof(phoneNumber));
|
||||||
|
|
@ -199,23 +199,23 @@ namespace TLSharp.Core
|
||||||
|
|
||||||
var request = new TLRequestSignIn() { PhoneNumber = phoneNumber, PhoneCodeHash = phoneCodeHash, PhoneCode = code };
|
var request = new TLRequestSignIn() { PhoneNumber = phoneNumber, PhoneCodeHash = phoneCodeHash, PhoneCode = code };
|
||||||
|
|
||||||
await RequestWithDcMigration(request,0);
|
RequestWithDcMigration(request,0);
|
||||||
|
|
||||||
OnUserAuthenticated(((TLUser)request.Response.User));
|
OnUserAuthenticated(((TLUser)request.Response.User));
|
||||||
|
|
||||||
return ((TLUser)request.Response.User);
|
return ((TLUser)request.Response.User);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<TLPassword> GetPasswordSetting()
|
public TLPassword GetPasswordSetting()
|
||||||
{
|
{
|
||||||
var request = new TLRequestGetPassword();
|
var request = new TLRequestGetPassword();
|
||||||
|
|
||||||
await RequestWithDcMigration(request,0);
|
RequestWithDcMigration(request,0);
|
||||||
|
|
||||||
return ((TLPassword)request.Response);
|
return ((TLPassword)request.Response);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<TLUser> MakeAuthWithPasswordAsync(TLPassword password, string password_str)
|
public TLUser MakeAuthWithPasswordAsync(TLPassword password, string password_str)
|
||||||
{
|
{
|
||||||
|
|
||||||
byte[] password_Bytes = Encoding.UTF8.GetBytes(password_str);
|
byte[] password_Bytes = Encoding.UTF8.GetBytes(password_str);
|
||||||
|
|
@ -226,43 +226,43 @@ namespace TLSharp.Core
|
||||||
|
|
||||||
var request = new TLRequestCheckPassword() { PasswordHash = password_hash };
|
var request = new TLRequestCheckPassword() { PasswordHash = password_hash };
|
||||||
|
|
||||||
await RequestWithDcMigration(request,0);
|
RequestWithDcMigration(request,0);
|
||||||
|
|
||||||
OnUserAuthenticated(((TLUser)request.Response.User));
|
OnUserAuthenticated(((TLUser)request.Response.User));
|
||||||
|
|
||||||
return ((TLUser)request.Response.User);
|
return ((TLUser)request.Response.User);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<TLUser> SignUpAsync(string phoneNumber, string phoneCodeHash, string code, string firstName, string lastName)
|
public TLUser SignUpAsync(string phoneNumber, string phoneCodeHash, string code, string firstName, string lastName)
|
||||||
{
|
{
|
||||||
var request = new TLRequestSignUp() { PhoneNumber = phoneNumber, PhoneCode = code, PhoneCodeHash = phoneCodeHash, FirstName = firstName, LastName = lastName };
|
var request = new TLRequestSignUp() { PhoneNumber = phoneNumber, PhoneCode = code, PhoneCodeHash = phoneCodeHash, FirstName = firstName, LastName = lastName };
|
||||||
|
|
||||||
await RequestWithDcMigration(request,0);
|
RequestWithDcMigration(request,0);
|
||||||
|
|
||||||
OnUserAuthenticated(((TLUser)request.Response.User));
|
OnUserAuthenticated(((TLUser)request.Response.User));
|
||||||
|
|
||||||
return ((TLUser)request.Response.User);
|
return ((TLUser)request.Response.User);
|
||||||
}
|
}
|
||||||
public async Task<T> SendRequestAsync<T>(TLMethod methodToExecute, int times)
|
public T SendRequestAsync<T>(TLMethod methodToExecute, int times)
|
||||||
{
|
{
|
||||||
await RequestWithDcMigration(methodToExecute, times);
|
RequestWithDcMigration(methodToExecute, times);
|
||||||
|
|
||||||
var result = methodToExecute.GetType().GetProperty("Response").GetValue(methodToExecute);
|
var result = methodToExecute.GetType().GetProperty("Response").GetValue(methodToExecute);
|
||||||
|
|
||||||
return (T)result;
|
return (T)result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<TLContacts> GetContactsAsync()
|
public TLContacts GetContactsAsync()
|
||||||
{
|
{
|
||||||
if (!IsUserAuthorized())
|
if (!IsUserAuthorized())
|
||||||
throw new InvalidOperationException("Authorize user first!");
|
throw new InvalidOperationException("Authorize user first!");
|
||||||
|
|
||||||
var req = new TLRequestGetContacts() { Hash = "" };
|
var req = new TLRequestGetContacts() { Hash = "" };
|
||||||
|
|
||||||
return await SendRequestAsync<TLContacts>(req,0);
|
return SendRequestAsync<TLContacts>(req,0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<TLAbsUpdates> SendMessageAsync(TLAbsInputPeer peer, string message)
|
public TLAbsUpdates SendMessageAsync(TLAbsInputPeer peer, string message)
|
||||||
{
|
{
|
||||||
if (!IsUserAuthorized())
|
if (!IsUserAuthorized())
|
||||||
throw new InvalidOperationException("Authorize user first!");
|
throw new InvalidOperationException("Authorize user first!");
|
||||||
|
|
@ -273,20 +273,20 @@ namespace TLSharp.Core
|
||||||
Message = message,
|
Message = message,
|
||||||
RandomId = Helpers.GenerateRandomLong()
|
RandomId = Helpers.GenerateRandomLong()
|
||||||
};
|
};
|
||||||
return await SendRequestAsync<TLAbsUpdates>(x, 0);
|
return SendRequestAsync<TLAbsUpdates>(x, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<Boolean> SendTypingAsync(TLAbsInputPeer peer)
|
public Boolean SendTypingAsync(TLAbsInputPeer peer)
|
||||||
{
|
{
|
||||||
var req = new TLRequestSetTyping()
|
var req = new TLRequestSetTyping()
|
||||||
{
|
{
|
||||||
Action = new TLSendMessageTypingAction(),
|
Action = new TLSendMessageTypingAction(),
|
||||||
Peer = peer
|
Peer = peer
|
||||||
};
|
};
|
||||||
return await SendRequestAsync<Boolean>(req,0);
|
return SendRequestAsync<Boolean>(req,0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<TLAbsDialogs> GetUserDialogsAsync(int offsetDate = 0, int offsetId = 0, TLAbsInputPeer offsetPeer = null, int limit = 100)
|
public TLAbsDialogs GetUserDialogsAsync(int offsetDate = 0, int offsetId = 0, TLAbsInputPeer offsetPeer = null, int limit = 100)
|
||||||
{
|
{
|
||||||
if (!IsUserAuthorized())
|
if (!IsUserAuthorized())
|
||||||
throw new InvalidOperationException("Authorize user first!");
|
throw new InvalidOperationException("Authorize user first!");
|
||||||
|
|
@ -301,12 +301,12 @@ namespace TLSharp.Core
|
||||||
OffsetPeer = offsetPeer,
|
OffsetPeer = offsetPeer,
|
||||||
Limit = limit
|
Limit = limit
|
||||||
};
|
};
|
||||||
return await SendRequestAsync<TLAbsDialogs>(req,0);
|
return SendRequestAsync<TLAbsDialogs>(req,0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<TLAbsUpdates> SendUploadedPhoto(TLAbsInputPeer peer, TLAbsInputFile file, string caption)
|
public TLAbsUpdates SendUploadedPhoto(TLAbsInputPeer peer, TLAbsInputFile file, string caption)
|
||||||
{
|
{
|
||||||
return await SendRequestAsync<TLAbsUpdates>(new TLRequestSendMedia()
|
return SendRequestAsync<TLAbsUpdates>(new TLRequestSendMedia()
|
||||||
{
|
{
|
||||||
RandomId = Helpers.GenerateRandomLong(),
|
RandomId = Helpers.GenerateRandomLong(),
|
||||||
Background = false,
|
Background = false,
|
||||||
|
|
@ -316,10 +316,10 @@ namespace TLSharp.Core
|
||||||
},0);
|
},0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<TLAbsUpdates> SendUploadedDocument(
|
public TLAbsUpdates SendUploadedDocument(
|
||||||
TLAbsInputPeer peer, TLAbsInputFile file, string caption, string mimeType, TLVector<TLAbsDocumentAttribute> attributes)
|
TLAbsInputPeer peer, TLAbsInputFile file, string caption, string mimeType, TLVector<TLAbsDocumentAttribute> attributes)
|
||||||
{
|
{
|
||||||
return await SendRequestAsync<TLAbsUpdates>(new TLRequestSendMedia()
|
return SendRequestAsync<TLAbsUpdates>(new TLRequestSendMedia()
|
||||||
{
|
{
|
||||||
RandomId = Helpers.GenerateRandomLong(),
|
RandomId = Helpers.GenerateRandomLong(),
|
||||||
Background = false,
|
Background = false,
|
||||||
|
|
@ -335,10 +335,10 @@ namespace TLSharp.Core
|
||||||
},0);
|
},0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<TLFile> GetFile(TLAbsInputFileLocation location, int filePartSize, int offset = 0)
|
public TLFile GetFile(TLAbsInputFileLocation location, int filePartSize, int offset = 0)
|
||||||
{
|
{
|
||||||
TLFile result = null;
|
TLFile result = null;
|
||||||
result = await SendRequestAsync<TLFile>(new TLRequestGetFile()
|
result = SendRequestAsync<TLFile>(new TLRequestGetFile()
|
||||||
{
|
{
|
||||||
Location = location,
|
Location = location,
|
||||||
Limit = filePartSize,
|
Limit = filePartSize,
|
||||||
|
|
@ -347,12 +347,12 @@ namespace TLSharp.Core
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task SendPingAsync()
|
public void SendPingAsync()
|
||||||
{
|
{
|
||||||
await _sender.SendPingAsync();
|
_sender.SendPingAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<TLAbsMessages> GetHistoryAsync(TLAbsInputPeer peer, int offsetId = 0, int offsetDate = 0, int addOffset = 0, int limit = 100, int maxId = 0, int minId = 0)
|
public TLAbsMessages GetHistoryAsync(TLAbsInputPeer peer, int offsetId = 0, int offsetDate = 0, int addOffset = 0, int limit = 100, int maxId = 0, int minId = 0)
|
||||||
{
|
{
|
||||||
if (!IsUserAuthorized())
|
if (!IsUserAuthorized())
|
||||||
throw new InvalidOperationException("Authorize user first!");
|
throw new InvalidOperationException("Authorize user first!");
|
||||||
|
|
@ -367,7 +367,7 @@ namespace TLSharp.Core
|
||||||
MaxId = maxId,
|
MaxId = maxId,
|
||||||
MinId = minId
|
MinId = minId
|
||||||
};
|
};
|
||||||
return await SendRequestAsync<TLAbsMessages>(req,0);
|
return SendRequestAsync<TLAbsMessages>(req,0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -376,7 +376,7 @@ namespace TLSharp.Core
|
||||||
/// <param name="q">User or chat name</param>
|
/// <param name="q">User or chat name</param>
|
||||||
/// <param name="limit">Max result count</param>
|
/// <param name="limit">Max result count</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task<TLFound> SearchUserAsync(string q, int limit = 10)
|
public TLFound SearchUserAsync(string q, int limit = 10)
|
||||||
{
|
{
|
||||||
var r = new TeleSharp.TL.Contacts.TLRequestSearch
|
var r = new TeleSharp.TL.Contacts.TLRequestSearch
|
||||||
{
|
{
|
||||||
|
|
@ -384,7 +384,7 @@ namespace TLSharp.Core
|
||||||
Limit = limit
|
Limit = limit
|
||||||
};
|
};
|
||||||
|
|
||||||
return await SendRequestAsync<TLFound>(r,0);
|
return SendRequestAsync<TLFound>(r,0);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnUserAuthenticated(TLUser TLUser)
|
private void OnUserAuthenticated(TLUser TLUser)
|
||||||
|
|
|
||||||
|
|
@ -29,10 +29,10 @@ namespace TLSharp.Core.Utils
|
||||||
return md5_checksum;
|
return md5_checksum;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async Task<TLAbsInputFile> UploadFile(this TelegramClient client, string name, StreamReader reader)
|
public static TLAbsInputFile UploadFile(this TelegramClient client, string name, StreamReader reader)
|
||||||
{
|
{
|
||||||
const long tenMb = 10 * 1024 * 1024;
|
const long tenMb = 10 * 1024 * 1024;
|
||||||
return await UploadFile(name, reader, client, reader.BaseStream.Length >= tenMb);
|
return UploadFile(name, reader, client, reader.BaseStream.Length >= tenMb);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static byte[] GetFile(StreamReader reader)
|
private static byte[] GetFile(StreamReader reader)
|
||||||
|
|
@ -76,7 +76,7 @@ namespace TLSharp.Core.Utils
|
||||||
return fileParts;
|
return fileParts;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static async Task<TLAbsInputFile> UploadFile(string name, StreamReader reader,
|
private static TLAbsInputFile UploadFile(string name, StreamReader reader,
|
||||||
TelegramClient client, bool isBigFileUpload)
|
TelegramClient client, bool isBigFileUpload)
|
||||||
{
|
{
|
||||||
var file = GetFile(reader);
|
var file = GetFile(reader);
|
||||||
|
|
@ -91,7 +91,7 @@ namespace TLSharp.Core.Utils
|
||||||
|
|
||||||
if (isBigFileUpload)
|
if (isBigFileUpload)
|
||||||
{
|
{
|
||||||
await client.SendRequestAsync<bool>(new TLRequestSaveBigFilePart
|
client.SendRequestAsync<bool>(new TLRequestSaveBigFilePart
|
||||||
{
|
{
|
||||||
FileId = file_id,
|
FileId = file_id,
|
||||||
FilePart = partNumber,
|
FilePart = partNumber,
|
||||||
|
|
@ -101,7 +101,7 @@ namespace TLSharp.Core.Utils
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
await client.SendRequestAsync<bool>(new TLRequestSaveFilePart
|
client.SendRequestAsync<bool>(new TLRequestSaveFilePart
|
||||||
{
|
{
|
||||||
FileId = file_id,
|
FileId = file_id,
|
||||||
FilePart = partNumber,
|
FilePart = partNumber,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue