2018-12-31 20:04:05 +01:00
|
|
|
//
|
2024-01-01 01:07:21 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// 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)
|
|
|
|
//
|
|
|
|
#include "td/telegram/net/DcAuthManager.h"
|
|
|
|
|
|
|
|
#include "td/telegram/Global.h"
|
|
|
|
#include "td/telegram/net/AuthDataShared.h"
|
2023-05-04 13:53:07 +02:00
|
|
|
#include "td/telegram/net/AuthKeyState.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/net/NetQuery.h"
|
|
|
|
#include "td/telegram/net/NetQueryDispatcher.h"
|
2019-01-06 20:59:17 +01:00
|
|
|
#include "td/telegram/TdDb.h"
|
2021-09-18 23:47:05 +02:00
|
|
|
#include "td/telegram/telegram_api.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/UniqueId.h"
|
|
|
|
|
2021-09-18 23:47:05 +02:00
|
|
|
#include "td/actor/actor.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/misc.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <limits>
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
2020-10-05 17:07:23 +02:00
|
|
|
int VERBOSITY_NAME(dc) = VERBOSITY_NAME(DEBUG) + 2;
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
DcAuthManager::DcAuthManager(ActorShared<> parent) {
|
|
|
|
parent_ = std::move(parent);
|
|
|
|
auto s_main_dc_id = G()->td_db()->get_binlog_pmc()->get("main_dc_id");
|
|
|
|
if (!s_main_dc_id.empty()) {
|
2019-04-09 00:37:20 +02:00
|
|
|
auto main_dc_id = to_integer<int32>(s_main_dc_id);
|
|
|
|
if (DcId::is_valid(main_dc_id)) {
|
|
|
|
main_dc_id_ = DcId::internal(main_dc_id);
|
2019-12-18 01:47:51 +01:00
|
|
|
VLOG(dc) << "Init main DcId to " << main_dc_id_;
|
2019-04-09 00:37:20 +02:00
|
|
|
} else {
|
2019-12-18 01:47:51 +01:00
|
|
|
LOG(ERROR) << "Receive invalid main DcId " << main_dc_id;
|
2019-04-09 00:37:20 +02:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DcAuthManager::add_dc(std::shared_ptr<AuthDataShared> auth_data) {
|
|
|
|
VLOG(dc) << "Register " << auth_data->dc_id();
|
2021-07-04 04:58:54 +02:00
|
|
|
class Listener final : public AuthDataShared::Listener {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
|
|
|
explicit Listener(ActorShared<DcAuthManager> dc_manager) : dc_manager_(std::move(dc_manager)) {
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
bool notify() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!dc_manager_.is_alive()) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-07-23 01:14:34 +02:00
|
|
|
send_closure(dc_manager_, &DcAuthManager::update_auth_key_state);
|
2018-12-31 20:04:05 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ActorShared<DcAuthManager> dc_manager_;
|
|
|
|
};
|
|
|
|
|
|
|
|
DcInfo info;
|
|
|
|
info.dc_id = auth_data->dc_id();
|
|
|
|
CHECK(info.dc_id.is_exact());
|
|
|
|
info.shared_auth_data = std::move(auth_data);
|
2023-05-04 13:53:07 +02:00
|
|
|
info.auth_key_state = get_auth_key_state(info.shared_auth_data->get_auth_key());
|
2021-02-01 12:51:38 +01:00
|
|
|
VLOG(dc) << "Add " << info.dc_id << " with auth key state " << info.auth_key_state;
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!main_dc_id_.is_exact()) {
|
|
|
|
main_dc_id_ = info.dc_id;
|
2019-12-18 01:47:51 +01:00
|
|
|
VLOG(dc) << "Set main DcId to " << main_dc_id_;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-09-27 03:19:03 +02:00
|
|
|
info.shared_auth_data->add_auth_key_listener(make_unique<Listener>(actor_shared(this, info.dc_id.get_raw_id())));
|
2018-12-31 20:04:05 +01:00
|
|
|
dcs_.emplace_back(std::move(info));
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DcAuthManager::update_main_dc(DcId new_main_dc_id) {
|
|
|
|
main_dc_id_ = new_main_dc_id;
|
2019-12-18 01:47:51 +01:00
|
|
|
VLOG(dc) << "Update main DcId to " << main_dc_id_;
|
2018-12-31 20:04:05 +01:00
|
|
|
loop();
|
|
|
|
}
|
|
|
|
|
|
|
|
DcAuthManager::DcInfo &DcAuthManager::get_dc(int32 dc_id) {
|
|
|
|
auto *dc = find_dc(dc_id);
|
|
|
|
CHECK(dc);
|
|
|
|
return *dc;
|
|
|
|
}
|
|
|
|
DcAuthManager::DcInfo *DcAuthManager::find_dc(int32 dc_id) {
|
|
|
|
auto it = std::find_if(dcs_.begin(), dcs_.end(), [&](auto &x) { return x.dc_id.get_raw_id() == dc_id; });
|
|
|
|
if (it == dcs_.end()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return &*it;
|
|
|
|
}
|
|
|
|
|
2019-07-23 01:14:34 +02:00
|
|
|
void DcAuthManager::update_auth_key_state() {
|
2021-10-19 17:11:16 +02:00
|
|
|
auto dc_id = narrow_cast<int32>(get_link_token());
|
2018-12-31 20:04:05 +01:00
|
|
|
auto &dc = get_dc(dc_id);
|
2023-05-04 13:53:07 +02:00
|
|
|
dc.auth_key_state = get_auth_key_state(dc.shared_auth_data->get_auth_key());
|
2021-02-01 13:07:10 +01:00
|
|
|
VLOG(dc) << "Update " << dc_id << " auth key state from " << dc.auth_key_state << " to " << dc.auth_key_state;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
|
2023-09-02 21:20:05 +02:00
|
|
|
void DcAuthManager::on_result(NetQueryPtr net_query) {
|
2021-10-19 17:11:16 +02:00
|
|
|
auto dc_id = narrow_cast<int32>(get_link_token());
|
2018-12-31 20:04:05 +01:00
|
|
|
auto &dc = get_dc(dc_id);
|
2023-09-02 21:20:05 +02:00
|
|
|
CHECK(dc.wait_id == net_query->id());
|
2018-12-31 20:04:05 +01:00
|
|
|
dc.wait_id = std::numeric_limits<decltype(dc.wait_id)>::max();
|
|
|
|
switch (dc.state) {
|
|
|
|
case DcInfo::State::Import: {
|
2023-09-02 21:39:26 +02:00
|
|
|
auto r_result_auth_exported = fetch_result<telegram_api::auth_exportAuthorization>(std::move(net_query));
|
2018-09-21 19:52:22 +02:00
|
|
|
if (r_result_auth_exported.is_error()) {
|
2023-09-02 21:39:26 +02:00
|
|
|
LOG(WARNING) << "Receive error for auth.exportAuthorization: " << r_result_auth_exported.error();
|
2018-12-31 20:04:05 +01:00
|
|
|
dc.state = DcInfo::State::Export;
|
|
|
|
break;
|
|
|
|
}
|
2018-09-21 19:52:22 +02:00
|
|
|
auto result_auth_exported = r_result_auth_exported.move_as_ok();
|
|
|
|
dc.export_id = result_auth_exported->id_;
|
|
|
|
dc.export_bytes = std::move(result_auth_exported->bytes_);
|
2018-12-31 20:04:05 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DcInfo::State::BeforeOk: {
|
2023-09-02 21:39:26 +02:00
|
|
|
auto result_auth = fetch_result<telegram_api::auth_importAuthorization>(std::move(net_query));
|
2018-12-31 20:04:05 +01:00
|
|
|
if (result_auth.is_error()) {
|
2023-09-02 21:39:26 +02:00
|
|
|
LOG(WARNING) << "Receive error for auth.importAuthorization: " << result_auth.error();
|
2018-12-31 20:04:05 +01:00
|
|
|
dc.state = DcInfo::State::Export;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
dc.state = DcInfo::State::Ok;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DcAuthManager::dc_loop(DcInfo &dc) {
|
2019-07-23 01:14:34 +02:00
|
|
|
VLOG(dc) << "In dc_loop: " << dc.dc_id << " " << dc.auth_key_state;
|
|
|
|
if (dc.auth_key_state == AuthKeyState::OK) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
2020-01-08 16:54:42 +01:00
|
|
|
if (dc.state == DcInfo::State::Ok) {
|
|
|
|
LOG(WARNING) << "Lost key in " << dc.dc_id << ", restart dc_loop";
|
|
|
|
dc.state = DcInfo::State::Waiting;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(dc.shared_auth_data);
|
|
|
|
switch (dc.state) {
|
|
|
|
case DcInfo::State::Waiting: {
|
|
|
|
// wait for timeout
|
2020-08-04 01:21:20 +02:00
|
|
|
// break;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
case DcInfo::State::Export: {
|
|
|
|
// send auth.exportAuthorization to auth_dc
|
|
|
|
VLOG(dc) << "Send exportAuthorization to " << dc.dc_id;
|
|
|
|
auto id = UniqueId::next();
|
2020-03-15 23:01:14 +01:00
|
|
|
auto query = G()->net_query_creator().create(id, telegram_api::auth_exportAuthorization(dc.dc_id.get_raw_id()),
|
2022-02-01 16:51:20 +01:00
|
|
|
{}, DcId::main(), NetQuery::Type::Common, NetQuery::AuthFlag::On);
|
2020-06-10 01:44:54 +02:00
|
|
|
query->total_timeout_limit_ = 60 * 60 * 24;
|
2020-03-14 23:44:21 +01:00
|
|
|
G()->net_query_dispatcher().dispatch_with_callback(std::move(query), actor_shared(this, dc.dc_id.get_raw_id()));
|
2018-12-31 20:04:05 +01:00
|
|
|
dc.wait_id = id;
|
|
|
|
dc.export_id = -1;
|
|
|
|
dc.state = DcInfo::State::Import;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DcInfo::State::Import: {
|
|
|
|
// send auth.importAuthorization to dc
|
|
|
|
if (dc.export_id == -1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
uint64 id = UniqueId::next();
|
|
|
|
VLOG(dc) << "Send importAuthorization to " << dc.dc_id;
|
2020-03-14 23:44:21 +01:00
|
|
|
auto query = G()->net_query_creator().create(
|
2022-02-01 16:51:20 +01:00
|
|
|
id, telegram_api::auth_importAuthorization(dc.export_id, std::move(dc.export_bytes)), {}, dc.dc_id,
|
2020-03-15 23:01:14 +01:00
|
|
|
NetQuery::Type::Common, NetQuery::AuthFlag::Off);
|
2020-06-10 01:44:54 +02:00
|
|
|
query->total_timeout_limit_ = 60 * 60 * 24;
|
2020-03-14 23:44:21 +01:00
|
|
|
G()->net_query_dispatcher().dispatch_with_callback(std::move(query), actor_shared(this, dc.dc_id.get_raw_id()));
|
2018-12-31 20:04:05 +01:00
|
|
|
dc.wait_id = id;
|
|
|
|
dc.state = DcInfo::State::BeforeOk;
|
|
|
|
break;
|
|
|
|
}
|
2020-03-14 23:44:21 +01:00
|
|
|
case DcInfo::State::BeforeOk:
|
2018-12-31 20:04:05 +01:00
|
|
|
break;
|
2020-03-14 23:44:21 +01:00
|
|
|
case DcInfo::State::Ok:
|
2018-12-31 20:04:05 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-23 13:12:25 +02:00
|
|
|
void DcAuthManager::destroy(Promise<Unit> promise) {
|
|
|
|
need_destroy_auth_key_ = true;
|
2017-12-29 21:34:39 +01:00
|
|
|
destroy_promise_ = std::move(promise);
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DcAuthManager::destroy_loop() {
|
2023-10-23 13:12:25 +02:00
|
|
|
if (!need_destroy_auth_key_) {
|
2017-12-29 21:34:39 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
bool is_ready{true};
|
|
|
|
for (auto &dc : dcs_) {
|
2019-07-23 01:14:34 +02:00
|
|
|
is_ready &= dc.auth_key_state == AuthKeyState::Empty;
|
2017-12-29 21:34:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_ready) {
|
2019-12-18 01:47:51 +01:00
|
|
|
VLOG(dc) << "Destroy auth keys loop is ready, all keys are destroyed";
|
2017-12-29 21:34:39 +01:00
|
|
|
destroy_promise_.set_value(Unit());
|
2023-10-23 13:12:25 +02:00
|
|
|
need_destroy_auth_key_ = false;
|
2017-12-29 21:34:39 +01:00
|
|
|
} else {
|
2019-12-18 01:47:51 +01:00
|
|
|
VLOG(dc) << "DC is not ready for destroying auth key";
|
2017-12-29 21:34:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void DcAuthManager::loop() {
|
|
|
|
if (close_flag_) {
|
|
|
|
VLOG(dc) << "Skip loop because close_flag";
|
|
|
|
return;
|
|
|
|
}
|
2017-12-29 21:34:39 +01:00
|
|
|
destroy_loop();
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!main_dc_id_.is_exact()) {
|
|
|
|
VLOG(dc) << "Skip loop because main_dc_id is unknown";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto main_dc = find_dc(main_dc_id_.get_raw_id());
|
2019-07-23 01:14:34 +02:00
|
|
|
if (!main_dc || main_dc->auth_key_state != AuthKeyState::OK) {
|
2022-05-02 20:41:04 +02:00
|
|
|
if (main_dc && need_check_authorization_is_ok_) {
|
2022-05-02 19:59:07 +02:00
|
|
|
G()->log_out("Authorization check failed in DcAuthManager");
|
2022-05-02 19:44:37 +02:00
|
|
|
}
|
2021-02-01 12:51:38 +01:00
|
|
|
VLOG(dc) << "Skip loop, because main DC is " << main_dc_id_ << ", main auth key state is "
|
|
|
|
<< (main_dc != nullptr ? main_dc->auth_key_state : AuthKeyState::Empty);
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
2022-05-02 19:44:37 +02:00
|
|
|
need_check_authorization_is_ok_ = false;
|
2018-12-31 20:04:05 +01:00
|
|
|
for (auto &dc : dcs_) {
|
|
|
|
dc_loop(dc);
|
|
|
|
}
|
|
|
|
}
|
2019-07-09 05:56:06 +02:00
|
|
|
|
2022-05-02 17:34:01 +02:00
|
|
|
void DcAuthManager::check_authorization_is_ok() {
|
2022-05-02 19:44:37 +02:00
|
|
|
need_check_authorization_is_ok_ = true;
|
2022-05-02 17:34:01 +02:00
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|