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/PublicRsaKeyWatchdog.h"
|
|
|
|
|
|
|
|
#include "td/telegram/Global.h"
|
2020-03-16 20:00:44 +01:00
|
|
|
#include "td/telegram/net/NetQueryCreator.h"
|
2019-01-06 20:59:17 +01:00
|
|
|
#include "td/telegram/TdDb.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/telegram_api.h"
|
2021-07-09 01:31:42 +02:00
|
|
|
#include "td/telegram/Version.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2020-05-17 16:07:16 +02:00
|
|
|
#include "td/mtproto/RSA.h"
|
2019-01-31 03:05:40 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/Time.h"
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
PublicRsaKeyWatchdog::PublicRsaKeyWatchdog(ActorShared<> parent) : parent_(std::move(parent)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void PublicRsaKeyWatchdog::add_public_rsa_key(std::shared_ptr<PublicRsaKeyShared> key) {
|
2021-07-04 04:58:54 +02:00
|
|
|
class Listener final : public PublicRsaKeyShared::Listener {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
|
|
|
explicit Listener(ActorId<PublicRsaKeyWatchdog> parent) : parent_(std::move(parent)) {
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
bool notify() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
send_event(parent_, Event::yield());
|
|
|
|
return parent_.is_alive();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ActorId<PublicRsaKeyWatchdog> parent_;
|
|
|
|
};
|
|
|
|
|
2018-09-27 03:19:03 +02:00
|
|
|
key->add_listener(make_unique<Listener>(actor_id(this)));
|
2018-12-31 20:04:05 +01:00
|
|
|
sync_key(key);
|
|
|
|
keys_.push_back(std::move(key));
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PublicRsaKeyWatchdog::start_up() {
|
|
|
|
flood_control_.add_limit(1, 1);
|
|
|
|
flood_control_.add_limit(2, 60);
|
|
|
|
flood_control_.add_limit(3, 2 * 60);
|
|
|
|
|
2021-07-09 01:31:42 +02:00
|
|
|
string version = G()->td_db()->get_binlog_pmc()->get("cdn_config_version");
|
|
|
|
current_version_ = to_string(MTPROTO_LAYER);
|
|
|
|
if (version != current_version_) {
|
|
|
|
G()->td_db()->get_binlog_pmc()->erase("cdn_config" + version);
|
|
|
|
} else {
|
|
|
|
sync(BufferSlice(G()->td_db()->get_binlog_pmc()->get("cdn_config" + version)));
|
|
|
|
}
|
|
|
|
CHECK(keys_.empty());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PublicRsaKeyWatchdog::loop() {
|
|
|
|
if (has_query_) {
|
|
|
|
return;
|
|
|
|
}
|
2022-11-21 19:07:56 +01:00
|
|
|
auto now = Time::now();
|
|
|
|
if (now < flood_control_.get_wakeup_at()) {
|
|
|
|
set_timeout_at(flood_control_.get_wakeup_at() + 0.01);
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
bool ok = true;
|
|
|
|
for (auto &key : keys_) {
|
|
|
|
if (!key->has_keys()) {
|
|
|
|
ok = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ok) {
|
|
|
|
return;
|
|
|
|
}
|
2022-11-21 19:07:56 +01:00
|
|
|
flood_control_.add_event(now);
|
2018-12-31 20:04:05 +01:00
|
|
|
has_query_ = true;
|
2020-03-15 22:17:11 +01:00
|
|
|
auto query = G()->net_query_creator().create(telegram_api::help_getCdnConfig());
|
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));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PublicRsaKeyWatchdog::on_result(NetQueryPtr net_query) {
|
|
|
|
has_query_ = false;
|
|
|
|
yield();
|
|
|
|
if (net_query->is_error()) {
|
2021-02-08 13:56:08 +01:00
|
|
|
LOG(ERROR) << "Receive error for GetCdnConfig: " << net_query->move_as_error();
|
2021-07-09 01:17:26 +02:00
|
|
|
loop();
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto buf = net_query->move_as_ok();
|
2021-07-09 01:31:42 +02:00
|
|
|
G()->td_db()->get_binlog_pmc()->set("cdn_config_version", current_version_);
|
|
|
|
G()->td_db()->get_binlog_pmc()->set("cdn_config" + current_version_, buf.as_slice().str());
|
2018-12-31 20:04:05 +01:00
|
|
|
sync(std::move(buf));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PublicRsaKeyWatchdog::sync(BufferSlice cdn_config_serialized) {
|
|
|
|
if (cdn_config_serialized.empty()) {
|
2021-07-09 01:17:26 +02:00
|
|
|
loop();
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto r_keys = fetch_result<telegram_api::help_getCdnConfig>(cdn_config_serialized);
|
|
|
|
if (r_keys.is_error()) {
|
|
|
|
LOG(WARNING) << "Failed to deserialize help_getCdnConfig (probably not a problem) " << r_keys.error();
|
2021-07-09 01:17:26 +02:00
|
|
|
loop();
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
cdn_config_ = r_keys.move_as_ok();
|
2021-07-09 01:31:42 +02:00
|
|
|
if (keys_.empty()) {
|
|
|
|
LOG(INFO) << "Load " << to_string(cdn_config_);
|
|
|
|
} else {
|
|
|
|
LOG(INFO) << "Receive " << to_string(cdn_config_);
|
|
|
|
for (auto &key : keys_) {
|
|
|
|
sync_key(key);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PublicRsaKeyWatchdog::sync_key(std::shared_ptr<PublicRsaKeyShared> &key) {
|
|
|
|
if (!cdn_config_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (auto &config_key : cdn_config_->public_keys_) {
|
|
|
|
if (key->dc_id().get_raw_id() == config_key->dc_id_) {
|
2021-07-05 20:04:23 +02:00
|
|
|
auto r_rsa = mtproto::RSA::from_pem_public_key(config_key->public_key_);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (r_rsa.is_error()) {
|
|
|
|
LOG(ERROR) << r_rsa.error();
|
|
|
|
continue;
|
|
|
|
}
|
2019-12-28 13:04:32 +01:00
|
|
|
LOG(INFO) << "Add CDN " << key->dc_id() << " key with fingerprint " << r_rsa.ok().get_fingerprint();
|
2018-12-31 20:04:05 +01:00
|
|
|
key->add_rsa(r_rsa.move_as_ok());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-07-09 05:56:06 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|