2018-12-31 20:04:05 +01:00
|
|
|
//
|
2021-01-01 13:57:46 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
|
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)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "td/telegram/Global.h"
|
|
|
|
#include "td/telegram/net/NetQueryCreator.h"
|
|
|
|
#include "td/telegram/net/NetQueryDispatcher.h"
|
|
|
|
#include "td/telegram/telegram_api.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/common.h"
|
|
|
|
#include "td/utils/format.h"
|
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/Time.h"
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
namespace td {
|
2018-04-24 18:21:47 +02:00
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class TempAuthKeyWatchdog final : public NetQueryCallback {
|
2018-12-31 20:04:05 +01:00
|
|
|
class RegisteredAuthKeyImpl {
|
|
|
|
public:
|
|
|
|
explicit RegisteredAuthKeyImpl(int64 auth_key_id)
|
|
|
|
: watchdog_(G()->temp_auth_key_watchdog()), auth_key_id_(auth_key_id) {
|
|
|
|
}
|
|
|
|
RegisteredAuthKeyImpl(const RegisteredAuthKeyImpl &other) = delete;
|
|
|
|
RegisteredAuthKeyImpl &operator=(const RegisteredAuthKeyImpl &other) = delete;
|
|
|
|
RegisteredAuthKeyImpl(RegisteredAuthKeyImpl &&other) = delete;
|
|
|
|
RegisteredAuthKeyImpl &operator=(RegisteredAuthKeyImpl &&other) = delete;
|
|
|
|
~RegisteredAuthKeyImpl() {
|
|
|
|
send_closure(watchdog_, &TempAuthKeyWatchdog::unregister_auth_key_id_impl, auth_key_id_);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ActorId<TempAuthKeyWatchdog> watchdog_;
|
|
|
|
int64 auth_key_id_;
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
2020-07-26 13:24:30 +02:00
|
|
|
explicit TempAuthKeyWatchdog(ActorShared<> parent) : parent_(std::move(parent)) {
|
2020-07-19 17:18:54 +02:00
|
|
|
}
|
|
|
|
|
2018-09-27 03:19:03 +02:00
|
|
|
using RegisteredAuthKey = unique_ptr<RegisteredAuthKeyImpl>;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
static RegisteredAuthKey register_auth_key_id(int64 id) {
|
|
|
|
send_closure(G()->temp_auth_key_watchdog(), &TempAuthKeyWatchdog::register_auth_key_id_impl, id);
|
2018-09-27 03:19:03 +02:00
|
|
|
return make_unique<RegisteredAuthKeyImpl>(id);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static constexpr double SYNC_WAIT = 0.1;
|
|
|
|
static constexpr double SYNC_WAIT_MAX = 1.0;
|
|
|
|
|
2020-07-19 17:18:54 +02:00
|
|
|
ActorShared<> parent_;
|
2018-12-31 20:04:05 +01:00
|
|
|
std::map<uint64, uint32> id_count_;
|
|
|
|
double sync_at_ = 0;
|
|
|
|
bool need_sync_ = false;
|
|
|
|
bool run_sync_ = false;
|
|
|
|
|
|
|
|
void register_auth_key_id_impl(int64 id) {
|
|
|
|
if (!++id_count_[id]) {
|
|
|
|
id_count_.erase(id);
|
|
|
|
}
|
|
|
|
need_sync();
|
|
|
|
}
|
|
|
|
|
|
|
|
void unregister_auth_key_id_impl(int64 id) {
|
|
|
|
if (!--id_count_[id]) {
|
|
|
|
id_count_.erase(id);
|
|
|
|
}
|
|
|
|
need_sync();
|
|
|
|
}
|
|
|
|
|
|
|
|
void need_sync() {
|
|
|
|
need_sync_ = true;
|
|
|
|
try_sync();
|
2019-02-21 16:58:20 +01:00
|
|
|
LOG(DEBUG) << "Need sync temp auth keys";
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void try_sync() {
|
|
|
|
if (run_sync_ || !need_sync_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto now = Time::now();
|
|
|
|
if (sync_at_ == 0) {
|
|
|
|
sync_at_ = now + SYNC_WAIT_MAX;
|
|
|
|
}
|
2019-02-21 16:58:20 +01:00
|
|
|
LOG(DEBUG) << "Set sync timeout";
|
2018-02-12 11:37:54 +01:00
|
|
|
set_timeout_at(min(sync_at_, now + SYNC_WAIT));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void timeout_expired() final {
|
2019-02-21 16:58:20 +01:00
|
|
|
LOG(DEBUG) << "Sync timeout expired";
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(!run_sync_);
|
|
|
|
if (!need_sync_) {
|
|
|
|
LOG(ERROR) << "Do not need sync..";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
need_sync_ = false;
|
|
|
|
run_sync_ = true;
|
|
|
|
sync_at_ = 0;
|
|
|
|
vector<int64> ids;
|
|
|
|
for (auto &id_count : id_count_) {
|
|
|
|
ids.push_back(id_count.first);
|
|
|
|
}
|
|
|
|
if (G()->close_flag()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
LOG(WARNING) << "Start auth_dropTempAuthKeys except keys " << format::as_array(ids);
|
2020-03-15 23:01:14 +01:00
|
|
|
auto query = G()->net_query_creator().create_unauth(telegram_api::auth_dropTempAuthKeys(std::move(ids)));
|
2018-12-31 20:04:05 +01:00
|
|
|
G()->net_query_dispatcher().dispatch_with_callback(std::move(query), actor_shared(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_result(NetQueryPtr query) final {
|
|
|
|
run_sync_ = false;
|
|
|
|
if (query->is_error()) {
|
2018-09-15 14:24:25 +02:00
|
|
|
if (G()->close_flag()) {
|
|
|
|
return;
|
|
|
|
}
|
2019-02-21 16:58:20 +01:00
|
|
|
LOG(ERROR) << "Receive error for auth_dropTempAuthKeys: " << query->error();
|
2018-12-31 20:04:05 +01:00
|
|
|
need_sync_ = true;
|
|
|
|
} else {
|
2019-02-21 16:58:20 +01:00
|
|
|
LOG(INFO) << "Receive OK for auth_dropTempAuthKeys";
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
try_sync();
|
|
|
|
}
|
|
|
|
};
|
2018-04-24 18:21:47 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|