From 3c1698dddf972372020452118fd8798e1d589613 Mon Sep 17 00:00:00 2001 From: Arseny Smirnov Date: Wed, 13 Jun 2018 18:17:27 +0300 Subject: [PATCH] Session: cache salt for temp keys, better on_mtproto_header GitOrigin-RevId: 02ba02aaf86e36402536d98d7bb80fd198c68378 --- td/telegram/ConfigManager.cpp | 3 ++- td/telegram/net/Session.cpp | 5 ++++- td/telegram/net/Session.h | 5 ++++- td/telegram/net/SessionMultiProxy.cpp | 4 +++- td/telegram/net/SessionProxy.cpp | 14 +++++++++++++- td/telegram/net/SessionProxy.h | 3 +++ 6 files changed, 29 insertions(+), 5 deletions(-) diff --git a/td/telegram/ConfigManager.cpp b/td/telegram/ConfigManager.cpp index a74536d1f..63543fc85 100644 --- a/td/telegram/ConfigManager.cpp +++ b/td/telegram/ConfigManager.cpp @@ -295,7 +295,8 @@ ActorOwn<> get_full_config(DcId dc_id, IPAddress ip_address, Promise int_dc_id += 10000; } session_ = create_actor("ConfigSession", std::move(session_callback), std::move(auth_data), int_dc_id, - false /*is_main*/, true /*use_pfs*/, false /*is_cdn*/, mtproto::AuthKey()); + false /*is_main*/, true /*use_pfs*/, false /*is_cdn*/, mtproto::AuthKey(), + std::vector()); auto query = G()->net_query_creator().create(create_storer(telegram_api::help_getConfig()), DcId::empty(), NetQuery::Type::Common, NetQuery::AuthFlag::Off, NetQuery::GzipFlag::On, 60 * 60 * 24); diff --git a/td/telegram/net/Session.cpp b/td/telegram/net/Session.cpp index 8cd0dbf32..1d425748c 100644 --- a/td/telegram/net/Session.cpp +++ b/td/telegram/net/Session.cpp @@ -107,7 +107,8 @@ class GenAuthKeyActor : public Actor { } // namespace detail Session::Session(unique_ptr callback, std::shared_ptr shared_auth_data, int32 dc_id, - bool is_main, bool use_pfs, bool is_cdn, const mtproto::AuthKey &tmp_auth_key) + bool is_main, bool use_pfs, bool is_cdn, const mtproto::AuthKey &tmp_auth_key, + std::vector server_salts) : dc_id_(dc_id), is_main_(is_main), is_cdn_(is_cdn) { VLOG(dc) << "Start connection"; @@ -118,6 +119,7 @@ Session::Session(unique_ptr callback, std::shared_ptr auth_data_.set_future_salts(shared_auth_data_->get_future_salts(), Time::now()); if (use_pfs && !tmp_auth_key.empty()) { auth_data_.set_tmp_auth_key(tmp_auth_key); + auth_data_.set_future_salts(std::move(server_salts), Time::now()); } uint64 session_id = 0; Random::secure_bytes(reinterpret_cast(&session_id), sizeof(session_id)); @@ -376,6 +378,7 @@ void Session::on_tmp_auth_key_updated() { void Session::on_server_salt_updated() { if (auth_data_.use_pfs()) { + callback_->on_server_salt_updated(auth_data_.get_future_salts()); return; } shared_auth_data_->set_future_salts(auth_data_.get_future_salts()); diff --git a/td/telegram/net/Session.h b/td/telegram/net/Session.h index 31ecf987e..28eab863f 100644 --- a/td/telegram/net/Session.h +++ b/td/telegram/net/Session.h @@ -56,11 +56,14 @@ class Session final virtual void on_closed() = 0; virtual void request_raw_connection(Promise>) = 0; virtual void on_tmp_auth_key_updated(mtproto::AuthKey auth_key) = 0; + virtual void on_server_salt_updated(std::vector server_salts) { + } // one still have to call close after on_closed }; Session(unique_ptr callback, std::shared_ptr shared_auth_data, int32 dc_id, bool is_main, - bool use_pfs, bool is_cdn, const mtproto::AuthKey &tmp_auth_key); + bool use_pfs, bool is_cdn, const mtproto::AuthKey &tmp_auth_key, + std::vector server_salts); void send(NetQueryPtr &&query); void on_network(bool network_flag, uint32 network_generation); void on_online(bool online_flag); diff --git a/td/telegram/net/SessionMultiProxy.cpp b/td/telegram/net/SessionMultiProxy.cpp index 352050409..e0a557b46 100644 --- a/td/telegram/net/SessionMultiProxy.cpp +++ b/td/telegram/net/SessionMultiProxy.cpp @@ -88,7 +88,9 @@ void SessionMultiProxy::update_options(int32 session_count, bool use_pfs) { } void SessionMultiProxy::update_mtproto_header() { - init(); + for (auto &session : sessions_) { + send_closure_later(session, &SessionProxy::update_mtproto_header); + } } void SessionMultiProxy::start_up() { diff --git a/td/telegram/net/SessionProxy.cpp b/td/telegram/net/SessionProxy.cpp index 288ff08f6..360adb7a1 100644 --- a/td/telegram/net/SessionProxy.cpp +++ b/td/telegram/net/SessionProxy.cpp @@ -47,6 +47,10 @@ class SessionCallback : public Session::Callback { send_closure(parent_, &SessionProxy::on_tmp_auth_key_updated, std::move(auth_key)); } + void on_server_salt_updated(std::vector server_salts) override { + send_closure(parent_, &SessionProxy::on_server_salt_updated, std::move(server_salts)); + } + private: ActorShared parent_; DcId dc_id_; @@ -128,6 +132,11 @@ void SessionProxy::on_failed() { open_session(); } +void SessionProxy::update_mtproto_header() { + close_session(); + open_session(); +} + void SessionProxy::on_closed() { } @@ -154,7 +163,7 @@ void SessionProxy::open_session(bool force) { session_ = create_actor( name, make_unique(actor_shared(this, session_generation_), dc_id, allow_media_only_, is_media_, hash), - auth_data_, int_dc_id, is_main_, use_pfs_, is_cdn_, tmp_auth_key_); + auth_data_, int_dc_id, is_main_, use_pfs_, is_cdn_, tmp_auth_key_, server_salts_); } void SessionProxy::update_auth_state() { @@ -187,5 +196,8 @@ void SessionProxy::on_tmp_auth_key_updated(mtproto::AuthKey auth_key) { LOG(WARNING) << "tmp_auth_key " << auth_key.id() << ": " << state; tmp_auth_key_ = std::move(auth_key); } +void SessionProxy::on_server_salt_updated(std::vector server_salts) { + server_salts_ = std::move(server_salts); +} } // namespace td diff --git a/td/telegram/net/SessionProxy.h b/td/telegram/net/SessionProxy.h index b83a95f36..af51c71e7 100644 --- a/td/telegram/net/SessionProxy.h +++ b/td/telegram/net/SessionProxy.h @@ -25,6 +25,7 @@ class SessionProxy : public Actor { void send(NetQueryPtr query); void update_main_flag(bool is_main); + void update_mtproto_header(); private: std::shared_ptr auth_data_; @@ -34,6 +35,7 @@ class SessionProxy : public Actor { bool is_media_; bool use_pfs_; mtproto::AuthKey tmp_auth_key_; + std::vector server_salts_; bool need_wait_for_key_; bool is_cdn_; ActorOwn session_; @@ -47,6 +49,7 @@ class SessionProxy : public Actor { void update_auth_state(); void on_tmp_auth_key_updated(mtproto::AuthKey auth_key); + void on_server_salt_updated(std::vector server_salts); void start_up() override; void tear_down() override;