TLSharp/TLSharp.Core/TelegramClient.cs

435 lines
15 KiB
C#
Raw Normal View History

2015-09-28 04:01:17 +02:00
using System;
using System.Collections.Generic;
2015-09-28 04:01:17 +02:00
using System.Linq;
2016-11-16 14:31:00 +01:00
using System.Security.Cryptography;
using System.Text;
2015-09-28 04:01:17 +02:00
using System.Threading.Tasks;
2016-09-24 15:38:26 +02:00
using TeleSharp.TL;
2016-10-29 10:47:18 +02:00
using TeleSharp.TL.Account;
2016-09-24 15:38:26 +02:00
using TeleSharp.TL.Auth;
2016-10-11 15:28:57 +02:00
using TeleSharp.TL.Contacts;
2016-10-11 15:32:38 +02:00
using TeleSharp.TL.Help;
2016-10-11 15:28:57 +02:00
using TeleSharp.TL.Messages;
2016-10-23 12:29:18 +02:00
using TeleSharp.TL.Upload;
2016-10-11 15:32:38 +02:00
using TLSharp.Core.Auth;
using TLSharp.Core.MTProto.Crypto;
using TLSharp.Core.Network;
using TLSharp.Core.Utils;
2016-10-29 10:47:18 +02:00
using TLAuthorization = TeleSharp.TL.Auth.TLAuthorization;
2015-09-28 04:01:17 +02:00
namespace TLSharp.Core
{
2016-12-15 21:02:23 +01:00
public class TelegramClient : IDisposable
2016-04-18 12:50:57 +02:00
{
private MtProtoSender _sender;
private AuthKey _key;
private TcpTransport _transport;
private string _apiHash = "";
private int _apiId = 0;
2016-04-18 12:50:57 +02:00
private Session _session;
2016-09-24 15:38:26 +02:00
private List<TLDcOption> dcOptions;
private TcpClientConnectionHandler _handler;
2016-07-20 08:26:55 +02:00
public TelegramClient(int apiId, string apiHash,
ISessionStore store = null, string sessionUserId = "session", TcpClientConnectionHandler handler = null)
2016-04-18 12:50:57 +02:00
{
if (apiId == default(int))
throw new MissingApiConfigurationException("API_ID");
if (string.IsNullOrEmpty(apiHash))
throw new MissingApiConfigurationException("API_HASH");
if (store == null)
store = new FileSessionStore();
2016-10-11 16:31:30 +02:00
2016-09-24 15:38:26 +02:00
TLContext.Init();
_apiHash = apiHash;
_apiId = apiId;
_handler = handler;
2016-04-18 12:50:57 +02:00
_session = Session.TryLoadOrCreateNew(store, sessionUserId);
_transport = new TcpTransport(_session.ServerAddress, _session.Port, _handler);
2016-04-18 12:50:57 +02:00
}
2016-10-11 15:32:38 +02:00
public async Task<bool> ConnectAsync(bool reconnect = false)
2016-04-18 12:50:57 +02:00
{
if (_session.AuthKey == null || reconnect)
{
var result = await Authenticator.DoAuthentication(_transport);
_session.AuthKey = result.AuthKey;
_session.TimeOffset = result.TimeOffset;
}
_sender = new MtProtoSender(_transport, _session);
//set-up layer
var config = new TLRequestGetConfig();
var request = new TLRequestInitConnection()
{
api_id = _apiId,
app_version = "1.0.0",
device_model = "PC",
lang_code = "en",
query = config,
system_version = "Win 10.0"
};
var invokewithLayer = new TLRequestInvokeWithLayer() { layer = 66, query = request };
await _sender.Send(invokewithLayer);
await _sender.Receive(invokewithLayer);
2016-04-18 12:50:57 +02:00
dcOptions = ((TLConfig)invokewithLayer.Response).dc_options.lists;
2016-04-18 12:50:57 +02:00
return true;
}
2016-10-11 15:32:38 +02:00
private async Task ReconnectToDcAsync(int dcId)
2016-04-18 12:50:57 +02:00
{
if (dcOptions == null || !dcOptions.Any())
throw new InvalidOperationException($"Can't reconnect. Establish initial connection first.");
2016-09-24 15:38:26 +02:00
var dc = dcOptions.First(d => d.id == dcId);
2016-04-18 12:50:57 +02:00
_transport = new TcpTransport(dc.ip_address, dc.port, _handler);
2016-04-18 12:50:57 +02:00
_session.ServerAddress = dc.ip_address;
2016-09-24 15:38:26 +02:00
_session.Port = dc.port;
2016-04-18 12:50:57 +02:00
2016-10-11 15:32:38 +02:00
await ConnectAsync(true);
2016-04-18 12:50:57 +02:00
}
public bool IsUserAuthorized()
{
2016-09-24 15:38:26 +02:00
return _session.TLUser != null;
}
2016-10-11 15:32:38 +02:00
public async Task<bool> IsPhoneRegisteredAsync(string phoneNumber)
2016-09-24 15:38:26 +02:00
{
if (String.IsNullOrWhiteSpace(phoneNumber))
throw new ArgumentNullException(nameof(phoneNumber));
2016-09-24 15:38:26 +02:00
if (_sender == null)
throw new InvalidOperationException("Not connected!");
var authCheckPhoneRequest = new TLRequestCheckPhone() { phone_number = phoneNumber };
var completed = false;
while(!completed)
{
try
{
await _sender.Send(authCheckPhoneRequest);
await _sender.Receive(authCheckPhoneRequest);
completed = true;
}
catch(PhoneMigrationException e)
{
await ReconnectToDcAsync(e.DC);
}
}
2016-09-24 15:38:26 +02:00
return authCheckPhoneRequest.Response.phone_registered;
2016-04-18 12:50:57 +02:00
}
2016-10-11 15:32:38 +02:00
public async Task<string> SendCodeRequestAsync(string phoneNumber)
2016-04-18 12:50:57 +02:00
{
if (String.IsNullOrWhiteSpace(phoneNumber))
throw new ArgumentNullException(nameof(phoneNumber));
2016-04-18 12:50:57 +02:00
var completed = false;
2016-09-24 15:38:26 +02:00
TLRequestSendCode request = null;
2016-04-18 12:50:57 +02:00
while (!completed)
{
2016-09-24 15:38:26 +02:00
request = new TLRequestSendCode() { phone_number = phoneNumber, api_id = _apiId, api_hash = _apiHash };
2016-04-18 12:50:57 +02:00
try
{
await _sender.Send(request);
2016-07-20 09:09:27 +02:00
await _sender.Receive(request);
2016-04-18 12:50:57 +02:00
completed = true;
}
catch (DataCenterMigrationException ex)
2016-04-18 12:50:57 +02:00
{
await ReconnectToDcAsync(ex.DC);
2016-04-18 12:50:57 +02:00
}
}
2016-09-24 15:38:26 +02:00
return request.Response.phone_code_hash;
2016-04-18 12:50:57 +02:00
}
2016-10-11 15:32:38 +02:00
public async Task<TLUser> MakeAuthAsync(string phoneNumber, string phoneCodeHash, string code)
2016-04-18 12:50:57 +02:00
{
if (String.IsNullOrWhiteSpace(phoneNumber))
throw new ArgumentNullException(nameof(phoneNumber));
if (String.IsNullOrWhiteSpace(phoneCodeHash))
throw new ArgumentNullException(nameof(phoneCodeHash));
if (String.IsNullOrWhiteSpace(code))
throw new ArgumentNullException(nameof(code));
var request = new TLRequestSignIn() { phone_number = phoneNumber, phone_code_hash = phoneCodeHash, phone_code = code };
var completed = false;
while (!completed)
{
try
{
await _sender.Send(request);
await _sender.Receive(request);
completed = true;
}
catch (PhoneMigrationException e)
{
await ReconnectToDcAsync(e.DC);
}
}
2016-01-17 10:16:44 +01:00
2016-09-24 15:38:26 +02:00
OnUserAuthenticated(((TLUser)request.Response.user));
2016-04-18 12:50:57 +02:00
2016-09-24 15:38:26 +02:00
return ((TLUser)request.Response.user);
2016-09-06 17:37:05 +02:00
}
2016-11-16 14:31:00 +01:00
public async Task<TLPassword> GetPasswordSetting()
{
var request = new TLRequestGetPassword();
await _sender.Send(request);
await _sender.Receive(request);
return ((TLPassword)request.Response);
}
public async Task<TLUser> MakeAuthWithPasswordAsync(TLPassword password, string password_str)
{
byte[] password_bytes = Encoding.UTF8.GetBytes(password_str);
IEnumerable<byte> rv = password.current_salt.Concat(password_bytes).Concat(password.current_salt);
SHA256Managed hashstring = new SHA256Managed();
var password_hash = hashstring.ComputeHash(rv.ToArray());
var request = new TLRequestCheckPassword() { password_hash = password_hash };
await _sender.Send(request);
await _sender.Receive(request);
OnUserAuthenticated(((TLUser)request.Response.user));
return ((TLUser)request.Response.user);
}
2016-09-06 17:37:05 +02:00
2016-10-11 15:32:38 +02:00
public async Task<TLUser> SignUpAsync(string phoneNumber, string phoneCodeHash, string code, string firstName, string lastName)
2016-09-24 15:38:26 +02:00
{
var request = new TLRequestSignUp() { phone_number = phoneNumber, phone_code = code, phone_code_hash = phoneCodeHash, first_name = firstName, last_name = lastName };
await _sender.Send(request);
await _sender.Receive(request);
2016-04-18 12:50:57 +02:00
2016-09-24 15:38:26 +02:00
OnUserAuthenticated(((TLUser)request.Response.user));
2016-04-18 12:50:57 +02:00
2016-09-24 15:38:26 +02:00
return ((TLUser)request.Response.user);
}
2016-10-29 10:47:18 +02:00
public async Task<T> SendRequestAsync<T>(TLMethod methodToExecute)
2016-09-24 15:38:26 +02:00
{
2016-10-29 10:47:18 +02:00
await _sender.Send(methodToExecute);
await _sender.Receive(methodToExecute);
2016-10-29 10:47:18 +02:00
var result = methodToExecute.GetType().GetProperty("Response").GetValue(methodToExecute);
return (T)result;
}
public async Task<TLContacts> GetContactsAsync()
{
if (!IsUserAuthorized())
throw new InvalidOperationException("Authorize user first!");
var req = new TLRequestGetContacts() { hash = "" };
return await SendRequestAsync<TLContacts>(req);
}
public async Task<TLAbsUpdates> SendMessageAsync(TLAbsInputPeer peer, string message)
{
if (!IsUserAuthorized())
throw new InvalidOperationException("Authorize user first!");
return await SendRequestAsync<TLAbsUpdates>(
new TLRequestSendMessage()
{
peer = peer,
message = message,
random_id = Helpers.GenerateRandomLong()
});
2016-09-24 15:38:26 +02:00
}
2016-10-11 15:28:57 +02:00
public async Task<Boolean> SendTypingAsync(TLAbsInputPeer peer)
{
var req = new TLRequestSetTyping()
{
action = new TLSendMessageTypingAction(),
peer = peer
};
return await SendRequestAsync<Boolean>(req);
}
2016-10-11 15:28:57 +02:00
2016-10-26 18:43:19 +02:00
public async Task<TLAbsDialogs> GetUserDialogsAsync()
{
var peer = new TLInputPeerSelf();
2016-10-26 18:43:19 +02:00
return await SendRequestAsync<TLAbsDialogs>(
new TLRequestGetDialogs() { offset_date = 0, offset_peer = peer, limit = 100 });
}
public async Task<TLAbsUpdates> SendUploadedPhoto(TLAbsInputPeer peer, TLAbsInputFile file, string caption)
2016-10-29 10:47:18 +02:00
{
return await SendRequestAsync<TLAbsUpdates>(new TLRequestSendMedia()
{
random_id = Helpers.GenerateRandomLong(),
background = false,
clear_draft = false,
media = new TLInputMediaUploadedPhoto() { file = file, caption = caption },
peer = peer
});
}
public async Task<TLAbsUpdates> SendUploadedDocument(
TLAbsInputPeer peer, TLAbsInputFile file, string caption, string mimeType, TLVector<TLAbsDocumentAttribute> attributes)
{
2016-10-29 10:47:18 +02:00
return await SendRequestAsync<TLAbsUpdates>(new TLRequestSendMedia()
{
random_id = Helpers.GenerateRandomLong(),
background = false,
clear_draft = false,
media = new TLInputMediaUploadedDocument()
{
file = file,
caption = caption,
mime_type = mimeType,
attributes = attributes
},
peer = peer
});
}
public async Task<TLFile> GetFile(TLAbsInputFileLocation location, int filePartSize, int offset = 0)
2016-10-23 12:29:18 +02:00
{
2016-10-29 10:47:18 +02:00
TLFile result = null;
try
2016-10-23 12:29:18 +02:00
{
2016-10-29 10:47:18 +02:00
result = await SendRequestAsync<TLFile>(new TLRequestGetFile()
{
location = location,
limit = filePartSize,
offset = offset
2016-10-29 10:47:18 +02:00
});
}
catch (FileMigrationException ex)
{
var exportedAuth = await SendRequestAsync<TLExportedAuthorization>(new TLRequestExportAuthorization() { dc_id = ex.DC });
var authKey = _session.AuthKey;
var timeOffset = _session.TimeOffset;
var serverAddress = _session.ServerAddress;
var serverPort = _session.Port;
await ReconnectToDcAsync(ex.DC);
var auth = await SendRequestAsync<TLAuthorization>(new TLRequestImportAuthorization
{
bytes = exportedAuth.bytes,
id = exportedAuth.id
});
result = await GetFile(location, filePartSize, offset);
2016-10-29 10:47:18 +02:00
_session.AuthKey = authKey;
_session.TimeOffset = timeOffset;
_transport = new TcpTransport(serverAddress, serverPort);
2016-11-16 14:31:00 +01:00
_session.ServerAddress = serverAddress;
2016-10-29 10:47:18 +02:00
_session.Port = serverPort;
await ConnectAsync();
}
return result;
}
2016-10-23 12:29:18 +02:00
public async Task SendPingAsync()
{
await _sender.SendPingAsync();
}
public async Task<TLAbsMessages> GetHistoryAsync(TLAbsInputPeer peer, int offset, int max_id, int limit)
{
if (!IsUserAuthorized())
throw new InvalidOperationException("Authorize user first!");
var req = new TLRequestGetHistory()
{
peer = peer,
add_offset = offset,
max_id = max_id,
limit = limit
};
return await SendRequestAsync<TLAbsMessages>(req);
}
/// <summary>
/// Serch user or chat. API: contacts.search#11f812d8 q:string limit:int = contacts.Found;
/// </summary>
/// <param name="q">User or chat name</param>
/// <param name="limit">Max result count</param>
/// <returns></returns>
public async Task<TLFound> SearchUserAsync(string q, int limit = 10)
{
var r = new TeleSharp.TL.Contacts.TLRequestSearch
{
q = q,
limit = limit
};
return await SendRequestAsync<TLFound>(r);
}
private void OnUserAuthenticated(TLUser TLUser)
2016-09-06 17:37:05 +02:00
{
2016-09-24 15:38:26 +02:00
_session.TLUser = TLUser;
_session.SessionExpires = int.MaxValue;
2016-09-06 17:37:05 +02:00
_session.Save();
2017-04-24 16:46:53 +02:00
}
public bool IsConnected
{
get
{
if (_transport == null)
return false;
return _transport.IsConnected;
}
}
2016-12-15 21:02:23 +01:00
public void Dispose()
{
if (_transport != null)
{
_transport.Dispose();
_transport = null;
}
}
}
public class MissingApiConfigurationException : Exception
{
public const string InfoUrl = "https://github.com/sochix/TLSharp#quick-configuration";
2016-10-29 10:47:18 +02:00
internal MissingApiConfigurationException(string invalidParamName) :
base($"Your {invalidParamName} setting is missing. Adjust the configuration first, see {InfoUrl}")
{
}
2016-04-18 12:50:57 +02:00
}
public class InvalidPhoneCodeException : Exception
{
internal InvalidPhoneCodeException(string msg) : base(msg) { }
}
2016-11-16 14:31:00 +01:00
public class CloudPasswordNeededException : Exception
{
internal CloudPasswordNeededException(string msg) : base(msg) { }
}
2015-09-28 04:01:17 +02:00
}