From a8a82f39a501d9c1d9f2d52b92f5e67242f87101 Mon Sep 17 00:00:00 2001 From: Paulo Rogerio Panhoto Date: Tue, 26 Dec 2017 15:27:05 -0200 Subject: [PATCH 1/2] Modifications to Session and TelegramClient to allow a customised Session object to be passed on to Telegram Client and modify, in example, IP address and port. --- TLSharp.Core/Session.cs | 19 +++++++++---------- TLSharp.Core/TelegramClient.cs | 7 ++----- 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/TLSharp.Core/Session.cs b/TLSharp.Core/Session.cs index 987c5d2..f4bca81 100644 --- a/TLSharp.Core/Session.cs +++ b/TLSharp.Core/Session.cs @@ -69,13 +69,18 @@ namespace TLSharp.Core public long LastMessageId { get; set; } public int SessionExpires { get; set; } public TLUser TLUser { get; set; } + public ISessionStore Store { get { return _store; }} private Random random; private ISessionStore _store; - public Session(ISessionStore store) + public Session(ISessionStore store, string sessionUserId) { random = new Random(); + Id = GenerateRandomUlong (); + SessionUserId = sessionUserId; + ServerAddress = defaultConnectionAddress; + Port = defaultConnectionPort; _store = store; } @@ -133,7 +138,7 @@ namespace TLSharp.Core var authData = Serializers.Bytes.read(reader); - return new Session(store) + return new Session(store, sessionUserId) { AuthKey = new AuthKey(authData), Id = id, @@ -155,15 +160,9 @@ namespace TLSharp.Core _store.Save(this); } - public static Session TryLoadOrCreateNew(ISessionStore store, string sessionUserId) + public static Session GetSession(ISessionStore store, string sessionUserId, Session provided) { - return store.Load(sessionUserId) ?? new Session(store) - { - Id = GenerateRandomUlong(), - SessionUserId = sessionUserId, - ServerAddress = defaultConnectionAddress, - Port = defaultConnectionPort - }; + return store.Load (sessionUserId) ?? provided ?? new Session (store, sessionUserId); } private static ulong GenerateRandomUlong() diff --git a/TLSharp.Core/TelegramClient.cs b/TLSharp.Core/TelegramClient.cs index 0692486..ff9519e 100644 --- a/TLSharp.Core/TelegramClient.cs +++ b/TLSharp.Core/TelegramClient.cs @@ -31,22 +31,19 @@ namespace TLSharp.Core private TcpClientConnectionHandler _handler; public TelegramClient(int apiId, string apiHash, - ISessionStore store = null, string sessionUserId = "session", TcpClientConnectionHandler handler = null) + Session session = null, string sessionUserId = "session", TcpClientConnectionHandler handler = null) { if (apiId == default(int)) throw new MissingApiConfigurationException("API_ID"); if (string.IsNullOrEmpty(apiHash)) throw new MissingApiConfigurationException("API_HASH"); - if (store == null) - store = new FileSessionStore(); - TLContext.Init(); _apiHash = apiHash; _apiId = apiId; _handler = handler; - _session = Session.TryLoadOrCreateNew(store, sessionUserId); + _session = Session.GetSession(session?.Store ?? new FileSessionStore(), session?.SessionUserId ?? sessionUserId, session); _transport = new TcpTransport(_session.ServerAddress, _session.Port, _handler); } From a69db6ba27d66af25da2f977c9f7c2b3e876980e Mon Sep 17 00:00:00 2001 From: Paulo Rogerio Panhoto Date: Wed, 27 Dec 2017 17:32:27 -0200 Subject: [PATCH 2/2] TelegramClient exposes Session as a property so that TLUser can be retrieved by application in case the sign in process is bypassed. Example code: if (client.IsUserAuthorized()) user = client.Session.TLUser; else { /* sign in or sign up */ } --- TLSharp.Core/TelegramClient.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/TLSharp.Core/TelegramClient.cs b/TLSharp.Core/TelegramClient.cs index ff9519e..eae8b59 100644 --- a/TLSharp.Core/TelegramClient.cs +++ b/TLSharp.Core/TelegramClient.cs @@ -30,6 +30,8 @@ namespace TLSharp.Core private List dcOptions; private TcpClientConnectionHandler _handler; + public Session Session { get { return _session; } } + public TelegramClient(int apiId, string apiHash, Session session = null, string sessionUserId = "session", TcpClientConnectionHandler handler = null) {