diff --git a/td/telegram/ConfigManager.cpp b/td/telegram/ConfigManager.cpp index 327ac00d..56e3ff99 100644 --- a/td/telegram/ConfigManager.cpp +++ b/td/telegram/ConfigManager.cpp @@ -9,6 +9,7 @@ #include "td/telegram/ConfigShared.h" #include "td/telegram/Global.h" #include "td/telegram/logevent/LogEvent.h" +#include "td/telegram/net/AuthDataShared.h" #include "td/telegram/net/ConnectionCreator.h" #include "td/telegram/net/DcId.h" #include "td/telegram/net/DcOptions.h" @@ -344,9 +345,9 @@ ActorOwn<> get_full_config(DcOption option, Promise promise) { } return res; } - std::pair get_auth_state() override { + std::pair get_auth_key_state() override { auto auth_key = get_auth_key(); - AuthState state = AuthDataShared::get_auth_state(auth_key); + AuthKeyState state = AuthDataShared::get_auth_key_state(auth_key); return std::make_pair(state, auth_key.was_auth_flag()); } void set_auth_key(const mtproto::AuthKey &auth_key) override { diff --git a/td/telegram/net/AuthDataShared.cpp b/td/telegram/net/AuthDataShared.cpp index e00838f6..4e2eac56 100644 --- a/td/telegram/net/AuthDataShared.cpp +++ b/td/telegram/net/AuthDataShared.cpp @@ -42,11 +42,11 @@ class AuthDataSharedImpl : public AuthDataShared { } return res; } - using AuthDataShared::get_auth_state; - std::pair get_auth_state() override { + using AuthDataShared::get_auth_key_state; + std::pair get_auth_key_state() override { // TODO (perf): auto auth_key = get_auth_key(); - AuthState state = get_auth_state(auth_key); + AuthKeyState state = get_auth_key_state(auth_key); return std::make_pair(state, auth_key.was_auth_flag()); } @@ -109,7 +109,7 @@ class AuthDataSharedImpl : public AuthDataShared { } void log_auth_key(const mtproto::AuthKey &auth_key) { - LOG(WARNING) << dc_id_ << " " << tag("auth_key_id", auth_key.id()) << tag("state", get_auth_state(auth_key)); + LOG(WARNING) << dc_id_ << " " << tag("auth_key_id", auth_key.id()) << tag("state", get_auth_key_state(auth_key)); } }; diff --git a/td/telegram/net/AuthDataShared.h b/td/telegram/net/AuthDataShared.h index cfd55d33..de09006f 100644 --- a/td/telegram/net/AuthDataShared.h +++ b/td/telegram/net/AuthDataShared.h @@ -21,18 +21,18 @@ namespace td { -enum class AuthState : int32 { Empty, KeyNoAuth, OK }; +enum class AuthKeyState : int32 { Empty, NoAuth, OK }; -inline StringBuilder &operator<<(StringBuilder &sb, AuthState state) { +inline StringBuilder &operator<<(StringBuilder &sb, AuthKeyState state) { switch (state) { - case AuthState::Empty: + case AuthKeyState::Empty: return sb << "Empty"; - case AuthState::KeyNoAuth: - return sb << "KeyNoAuth"; - case AuthState::OK: + case AuthKeyState::NoAuth: + return sb << "NoAuth"; + case AuthKeyState::OK: return sb << "OK"; default: - return sb << "Unknown AuthState"; + return sb << "Unknown AuthKeyState"; } } @@ -51,7 +51,7 @@ class AuthDataShared { virtual DcId dc_id() const = 0; virtual const std::shared_ptr &public_rsa_key() = 0; virtual mtproto::AuthKey get_auth_key() = 0; - virtual std::pair get_auth_state() = 0; + virtual std::pair get_auth_key_state() = 0; virtual void set_auth_key(const mtproto::AuthKey &auth_key) = 0; virtual void update_server_time_difference(double diff) = 0; virtual double get_server_time_difference() = 0; @@ -60,16 +60,14 @@ class AuthDataShared { virtual void set_future_salts(const std::vector &future_salts) = 0; virtual std::vector get_future_salts() = 0; - static AuthState get_auth_state(const mtproto::AuthKey &auth_key) { - AuthState state; + static AuthKeyState get_auth_key_state(const mtproto::AuthKey &auth_key) { if (auth_key.empty()) { - state = AuthState::Empty; + return AuthKeyState::Empty; } else if (auth_key.auth_flag()) { - state = AuthState::OK; + return AuthKeyState::OK; } else { - state = AuthState::KeyNoAuth; + return AuthKeyState::NoAuth; } - return state; } static std::shared_ptr create(DcId dc_id, std::shared_ptr public_rsa_key, diff --git a/td/telegram/net/DcAuthManager.cpp b/td/telegram/net/DcAuthManager.cpp index 192ad366..c34f1919 100644 --- a/td/telegram/net/DcAuthManager.cpp +++ b/td/telegram/net/DcAuthManager.cpp @@ -50,7 +50,7 @@ void DcAuthManager::add_dc(std::shared_ptr auth_data) { if (!dc_manager_.is_alive()) { return false; } - send_closure(dc_manager_, &DcAuthManager::update_auth_state); + send_closure(dc_manager_, &DcAuthManager::update_auth_key_state); return true; } @@ -62,8 +62,8 @@ void DcAuthManager::add_dc(std::shared_ptr auth_data) { info.dc_id = auth_data->dc_id(); CHECK(info.dc_id.is_exact()); info.shared_auth_data = std::move(auth_data); - auto state_was_auth = info.shared_auth_data->get_auth_state(); - info.auth_state = state_was_auth.first; + auto state_was_auth = info.shared_auth_data->get_auth_key_state(); + info.auth_key_state = state_was_auth.first; was_auth_ |= state_was_auth.second; if (!main_dc_id_.is_exact()) { main_dc_id_ = info.dc_id; @@ -91,13 +91,13 @@ DcAuthManager::DcInfo *DcAuthManager::find_dc(int32 dc_id) { return &*it; } -void DcAuthManager::update_auth_state() { +void DcAuthManager::update_auth_key_state() { int32 dc_id = narrow_cast(get_link_token()); auto &dc = get_dc(dc_id); - auto state_was_auth = dc.shared_auth_data->get_auth_state(); - VLOG(dc) << "Update dc auth state " << tag("dc_id", dc_id) << tag("old_auth_state", dc.auth_state) - << tag("new_auth_state", state_was_auth.first); - dc.auth_state = state_was_auth.first; + auto state_was_auth = dc.shared_auth_data->get_auth_key_state(); + VLOG(dc) << "Update DC auth key state " << tag("dc_id", dc_id) << tag("old_auth_key_state", dc.auth_key_state) + << tag("new_auth_key_state", state_was_auth.first); + dc.auth_key_state = state_was_auth.first; was_auth_ |= state_was_auth.second; loop(); @@ -149,8 +149,8 @@ void DcAuthManager::on_result(NetQueryPtr result) { } void DcAuthManager::dc_loop(DcInfo &dc) { - VLOG(dc) << "In dc_loop: " << dc.dc_id << " " << dc.auth_state; - if (dc.auth_state == AuthState::OK) { + VLOG(dc) << "In dc_loop: " << dc.dc_id << " " << dc.auth_key_state; + if (dc.auth_key_state == AuthKeyState::OK) { return; } CHECK(dc.shared_auth_data); @@ -209,7 +209,7 @@ void DcAuthManager::destroy_loop() { } bool is_ready{true}; for (auto &dc : dcs_) { - is_ready &= dc.auth_state == AuthState::Empty; + is_ready &= dc.auth_key_state == AuthKeyState::Empty; } if (is_ready) { @@ -231,13 +231,13 @@ void DcAuthManager::loop() { return; } auto main_dc = find_dc(main_dc_id_.get_raw_id()); - if (!main_dc || main_dc->auth_state != AuthState::OK) { + if (!main_dc || main_dc->auth_key_state != AuthKeyState::OK) { if (was_auth_) { G()->shared_config().set_option_boolean("auth", false); destroy_loop(); } VLOG(dc) << "Skip loop because auth state of main dc " << main_dc_id_.get_raw_id() << " is " - << (main_dc != nullptr ? (PSTRING() << main_dc->auth_state) : "unknown"); + << (main_dc != nullptr ? (PSTRING() << main_dc->auth_key_state) : "unknown"); return; } diff --git a/td/telegram/net/DcAuthManager.h b/td/telegram/net/DcAuthManager.h index bf41fa64..1d0d2800 100644 --- a/td/telegram/net/DcAuthManager.h +++ b/td/telegram/net/DcAuthManager.h @@ -32,7 +32,7 @@ class DcAuthManager : public NetQueryCallback { struct DcInfo { DcId dc_id; std::shared_ptr shared_auth_data; - AuthState auth_state; + AuthKeyState auth_key_state; enum class State : int32 { Waiting, Export, Import, BeforeOk, Ok }; State state = State::Waiting; @@ -52,7 +52,7 @@ class DcAuthManager : public NetQueryCallback { DcInfo &get_dc(int32 dc_id); DcInfo *find_dc(int32 dc_id); - void update_auth_state(); + void update_auth_key_state(); void on_result(NetQueryPtr result) override; void dc_loop(DcInfo &dc); diff --git a/td/telegram/net/SessionMultiProxy.h b/td/telegram/net/SessionMultiProxy.h index 2883c8af..79b54c6a 100644 --- a/td/telegram/net/SessionMultiProxy.h +++ b/td/telegram/net/SessionMultiProxy.h @@ -58,8 +58,6 @@ class SessionMultiProxy : public Actor { bool get_pfs_flag() const; - void update_auth_state(); - void on_query_finished(uint32 generation, int session_id); }; diff --git a/td/telegram/net/SessionProxy.cpp b/td/telegram/net/SessionProxy.cpp index d69bc06c..3717cf8e 100644 --- a/td/telegram/net/SessionProxy.cpp +++ b/td/telegram/net/SessionProxy.cpp @@ -95,14 +95,14 @@ void SessionProxy::start_up() { if (!session_proxy_.is_alive()) { return false; } - send_closure(session_proxy_, &SessionProxy::update_auth_state); + send_closure(session_proxy_, &SessionProxy::update_auth_key_state); return true; } private: ActorShared session_proxy_; }; - auth_state_ = auth_data_->get_auth_state().first; + auth_key_state_ = auth_data_->get_auth_key_state().first; auth_data_->add_auth_key_listener(make_unique(actor_shared(this))); open_session(); } @@ -117,7 +117,7 @@ void SessionProxy::tear_down() { } void SessionProxy::send(NetQueryPtr query) { - if (query->auth_flag() == NetQuery::AuthFlag::On && auth_state_ != AuthState::OK) { + if (query->auth_flag() == NetQuery::AuthFlag::On && auth_key_state_ != AuthKeyState::OK) { query->debug(PSTRING() << get_name() << ": wait for auth"); pending_queries_.emplace_back(std::move(query)); return; @@ -176,9 +176,9 @@ void SessionProxy::open_session(bool force) { return true; } if (need_destroy_) { - return auth_state_ != AuthState::Empty; + return auth_key_state_ != AuthKeyState::Empty; } - if (auth_state_ != AuthState::OK) { + if (auth_key_state_ != AuthKeyState::OK) { return false; } return is_main_ || !pending_queries_.empty(); @@ -205,14 +205,14 @@ void SessionProxy::open_session(bool force) { auth_data_, int_dc_id, is_main_, use_pfs_, is_cdn_, need_destroy_, tmp_auth_key_, server_salts_); } -void SessionProxy::update_auth_state() { - auto old_auth_state = auth_state_; - auth_state_ = auth_data_->get_auth_state().first; - if (auth_state_ != old_auth_state && old_auth_state == AuthState::OK) { +void SessionProxy::update_auth_key_state() { + auto old_auth_key_state = auth_key_state_; + auth_key_state_ = auth_data_->get_auth_key_state().first; + if (auth_key_state_ != old_auth_key_state && old_auth_key_state == AuthKeyState::OK) { close_session(); } open_session(); - if (session_.empty() || auth_state_ != AuthState::OK) { + if (session_.empty() || auth_key_state_ != AuthKeyState::OK) { return; } for (auto &query : pending_queries_) { diff --git a/td/telegram/net/SessionProxy.h b/td/telegram/net/SessionProxy.h index e7348ff7..968359a0 100644 --- a/td/telegram/net/SessionProxy.h +++ b/td/telegram/net/SessionProxy.h @@ -40,7 +40,7 @@ class SessionProxy : public Actor { private: unique_ptr callback_; std::shared_ptr auth_data_; - AuthState auth_state_; + AuthKeyState auth_key_state_; bool is_main_; bool allow_media_only_; bool is_media_; @@ -58,7 +58,7 @@ class SessionProxy : public Actor { void close_session(); void open_session(bool force = false); - void update_auth_state(); + void update_auth_key_state(); void on_tmp_auth_key_updated(mtproto::AuthKey auth_key); void on_server_salt_updated(std::vector server_salts);