From 14c570f3348a028949518b951704651bf1ded0ac Mon Sep 17 00:00:00 2001 From: levlam Date: Thu, 4 May 2023 14:53:07 +0300 Subject: [PATCH] Move AuthKeyState to a separate header. --- CMakeLists.txt | 1 + td/telegram/ConfigManager.cpp | 3 --- td/telegram/net/AuthDataShared.cpp | 7 +---- td/telegram/net/AuthDataShared.h | 28 +------------------- td/telegram/net/AuthKeyState.h | 41 ++++++++++++++++++++++++++++++ td/telegram/net/DcAuthManager.cpp | 5 ++-- td/telegram/net/SessionProxy.cpp | 5 ++-- 7 files changed, 50 insertions(+), 40 deletions(-) create mode 100644 td/telegram/net/AuthKeyState.h diff --git a/CMakeLists.txt b/CMakeLists.txt index b31df8905..5756b5687 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -669,6 +669,7 @@ set(TDLIB_SOURCE td/telegram/MinChannel.h td/telegram/misc.h td/telegram/net/AuthDataShared.h + td/telegram/net/AuthKeyState.h td/telegram/net/ConnectionCreator.h td/telegram/net/DcAuthManager.h td/telegram/net/DcId.h diff --git a/td/telegram/ConfigManager.cpp b/td/telegram/ConfigManager.cpp index f8386f5cf..0e9db8a78 100644 --- a/td/telegram/ConfigManager.cpp +++ b/td/telegram/ConfigManager.cpp @@ -453,9 +453,6 @@ static ActorOwn<> get_full_config(DcOption option, Promisetd_db()->get_binlog_pmc()->set(auth_key_key(), serialize(auth_key)); diff --git a/td/telegram/net/AuthDataShared.cpp b/td/telegram/net/AuthDataShared.cpp index cab5658b4..df7251d70 100644 --- a/td/telegram/net/AuthDataShared.cpp +++ b/td/telegram/net/AuthDataShared.cpp @@ -43,10 +43,6 @@ class AuthDataSharedImpl final : public AuthDataShared { return res; } - AuthKeyState get_auth_key_state() final { - return AuthDataShared::get_auth_key_state(get_auth_key()); - } - void set_auth_key(const mtproto::AuthKey &auth_key) final { G()->td_db()->get_binlog_pmc()->set(auth_key_key(), serialize(auth_key)); log_auth_key(auth_key); @@ -112,8 +108,7 @@ class AuthDataSharedImpl final : public AuthDataShared { if (!salts.empty()) { last_used = static_cast(salts[0].valid_until); } - LOG(WARNING) << dc_id_ << " " << tag("auth_key_id", auth_key.id()) - << tag("state", AuthDataShared::get_auth_key_state(auth_key)) + LOG(WARNING) << dc_id_ << " " << tag("auth_key_id", auth_key.id()) << tag("state", get_auth_key_state(auth_key)) << tag("created_at", static_cast(auth_key.created_at())) << tag("last_used", last_used); } }; diff --git a/td/telegram/net/AuthDataShared.h b/td/telegram/net/AuthDataShared.h index 319214ccf..c96d76f41 100644 --- a/td/telegram/net/AuthDataShared.h +++ b/td/telegram/net/AuthDataShared.h @@ -6,6 +6,7 @@ // #pragma once +#include "td/telegram/net/AuthKeyState.h" #include "td/telegram/net/DcId.h" #include "td/telegram/net/PublicRsaKeyShared.h" @@ -14,28 +15,12 @@ #include "td/utils/common.h" #include "td/utils/ScopeGuard.h" -#include "td/utils/StringBuilder.h" #include #include namespace td { -enum class AuthKeyState : int32 { Empty, NoAuth, OK }; - -inline StringBuilder &operator<<(StringBuilder &sb, AuthKeyState state) { - switch (state) { - case AuthKeyState::Empty: - return sb << "Empty"; - case AuthKeyState::NoAuth: - return sb << "NoAuth"; - case AuthKeyState::OK: - return sb << "OK"; - default: - return sb << "Unknown AuthKeyState"; - } -} - class AuthDataShared { public: virtual ~AuthDataShared() = default; @@ -51,7 +36,6 @@ 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 AuthKeyState 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 +44,6 @@ class AuthDataShared { virtual void set_future_salts(const std::vector &future_salts) = 0; virtual std::vector get_future_salts() = 0; - static AuthKeyState get_auth_key_state(const mtproto::AuthKey &auth_key) { - if (auth_key.empty()) { - return AuthKeyState::Empty; - } else if (auth_key.auth_flag()) { - return AuthKeyState::OK; - } else { - return AuthKeyState::NoAuth; - } - } - static std::shared_ptr create(DcId dc_id, std::shared_ptr public_rsa_key, std::shared_ptr guard); }; diff --git a/td/telegram/net/AuthKeyState.h b/td/telegram/net/AuthKeyState.h new file mode 100644 index 000000000..6c6cb12fe --- /dev/null +++ b/td/telegram/net/AuthKeyState.h @@ -0,0 +1,41 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023 +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +#pragma once + +#include "td/mtproto/AuthKey.h" + +#include "td/utils/common.h" +#include "td/utils/StringBuilder.h" + +namespace td { + +enum class AuthKeyState : int32 { Empty, NoAuth, OK }; + +inline AuthKeyState get_auth_key_state(const mtproto::AuthKey &auth_key) { + if (auth_key.empty()) { + return AuthKeyState::Empty; + } else if (auth_key.auth_flag()) { + return AuthKeyState::OK; + } else { + return AuthKeyState::NoAuth; + } +} + +inline StringBuilder &operator<<(StringBuilder &sb, AuthKeyState state) { + switch (state) { + case AuthKeyState::Empty: + return sb << "Empty"; + case AuthKeyState::NoAuth: + return sb << "NoAuth"; + case AuthKeyState::OK: + return sb << "OK"; + default: + return sb << "Unknown AuthKeyState"; + } +} + +} // namespace td diff --git a/td/telegram/net/DcAuthManager.cpp b/td/telegram/net/DcAuthManager.cpp index eec6ad7ac..2c5e116e2 100644 --- a/td/telegram/net/DcAuthManager.cpp +++ b/td/telegram/net/DcAuthManager.cpp @@ -8,6 +8,7 @@ #include "td/telegram/Global.h" #include "td/telegram/net/AuthDataShared.h" +#include "td/telegram/net/AuthKeyState.h" #include "td/telegram/net/NetQuery.h" #include "td/telegram/net/NetQueryDispatcher.h" #include "td/telegram/TdDb.h" @@ -62,7 +63,7 @@ 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); - info.auth_key_state = info.shared_auth_data->get_auth_key_state(); + info.auth_key_state = get_auth_key_state(info.shared_auth_data->get_auth_key()); VLOG(dc) << "Add " << info.dc_id << " with auth key state " << info.auth_key_state; if (!main_dc_id_.is_exact()) { main_dc_id_ = info.dc_id; @@ -95,7 +96,7 @@ DcAuthManager::DcInfo *DcAuthManager::find_dc(int32 dc_id) { void DcAuthManager::update_auth_key_state() { auto dc_id = narrow_cast(get_link_token()); auto &dc = get_dc(dc_id); - dc.auth_key_state = dc.shared_auth_data->get_auth_key_state(); + dc.auth_key_state = get_auth_key_state(dc.shared_auth_data->get_auth_key()); VLOG(dc) << "Update " << dc_id << " auth key state from " << dc.auth_key_state << " to " << dc.auth_key_state; loop(); diff --git a/td/telegram/net/SessionProxy.cpp b/td/telegram/net/SessionProxy.cpp index 3e0bde4d9..5dff1d4a0 100644 --- a/td/telegram/net/SessionProxy.cpp +++ b/td/telegram/net/SessionProxy.cpp @@ -7,6 +7,7 @@ #include "td/telegram/net/SessionProxy.h" #include "td/telegram/Global.h" +#include "td/telegram/net/AuthKeyState.h" #include "td/telegram/net/ConnectionCreator.h" #include "td/telegram/net/DcId.h" #include "td/telegram/net/NetQueryDispatcher.h" @@ -107,7 +108,7 @@ void SessionProxy::start_up() { private: ActorShared session_proxy_; }; - auth_key_state_ = auth_data_->get_auth_key_state(); + auth_key_state_ = get_auth_key_state(auth_data_->get_auth_key()); auth_data_->add_auth_key_listener(make_unique(actor_shared(this))); open_session(); } @@ -222,7 +223,7 @@ void SessionProxy::open_session(bool force) { void SessionProxy::update_auth_key_state() { auto old_auth_key_state = auth_key_state_; - auth_key_state_ = auth_data_->get_auth_key_state(); + auth_key_state_ = get_auth_key_state(auth_data_->get_auth_key()); if (auth_key_state_ != old_auth_key_state && old_auth_key_state == AuthKeyState::OK) { close_session(); }