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/AuthDataShared.h"
|
|
|
|
|
|
|
|
#include "td/telegram/Global.h"
|
2023-05-18 13:33:00 +02:00
|
|
|
#include "td/telegram/net/AuthKeyState.h"
|
2019-01-06 20:59:17 +01:00
|
|
|
#include "td/telegram/TdDb.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-01-01 13:59:53 +01:00
|
|
|
#include "td/utils/algorithm.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/format.h"
|
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/port/RwMutex.h"
|
2021-05-17 14:21:11 +02:00
|
|
|
#include "td/utils/SliceBuilder.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/tl_helpers.h"
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class AuthDataSharedImpl final : public AuthDataShared {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2018-03-13 14:40:02 +01:00
|
|
|
AuthDataSharedImpl(DcId dc_id, std::shared_ptr<PublicRsaKeyShared> public_rsa_key, std::shared_ptr<Guard> guard)
|
|
|
|
: dc_id_(dc_id), public_rsa_key_(std::move(public_rsa_key)), guard_(std::move(guard)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
log_auth_key(get_auth_key());
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
DcId dc_id() const final {
|
2018-12-31 20:04:05 +01:00
|
|
|
return dc_id_;
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
const std::shared_ptr<PublicRsaKeyShared> &public_rsa_key() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
return public_rsa_key_;
|
|
|
|
}
|
|
|
|
|
2023-10-23 14:26:52 +02:00
|
|
|
static mtproto::AuthKey get_auth_key_for_dc(DcId dc_id) {
|
|
|
|
string dc_key = G()->td_db()->get_binlog_pmc()->get(get_auth_key_binlog_key(dc_id));
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
mtproto::AuthKey res;
|
|
|
|
if (!dc_key.empty()) {
|
|
|
|
unserialize(res, dc_key).ensure();
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
2021-02-01 13:07:10 +01:00
|
|
|
|
2023-10-23 14:26:52 +02:00
|
|
|
mtproto::AuthKey get_auth_key() final {
|
|
|
|
return get_auth_key_for_dc(dc_id_);
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void set_auth_key(const mtproto::AuthKey &auth_key) final {
|
2023-10-23 14:26:52 +02:00
|
|
|
G()->td_db()->get_binlog_pmc()->set(get_auth_key_binlog_key(dc_id_), serialize(auth_key));
|
2018-12-31 20:04:05 +01:00
|
|
|
log_auth_key(auth_key);
|
|
|
|
|
|
|
|
notify();
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: extract it from G()
|
2023-05-14 20:58:54 +02:00
|
|
|
void update_server_time_difference(double diff, bool force) final {
|
|
|
|
G()->update_server_time_difference(diff, force);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
double get_server_time_difference() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
return G()->get_server_time_difference();
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void add_auth_key_listener(unique_ptr<Listener> listener) final {
|
2023-01-30 16:21:57 +01:00
|
|
|
CHECK(listener != nullptr);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (listener->notify()) {
|
|
|
|
auto lock = rw_mutex_.lock_write();
|
|
|
|
auth_key_listeners_.push_back(std::move(listener));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void set_future_salts(const std::vector<mtproto::ServerSalt> &future_salts) final {
|
2023-10-23 14:26:52 +02:00
|
|
|
G()->td_db()->get_binlog_pmc()->set(get_future_salts_binlog_key(dc_id_), serialize(future_salts));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
std::vector<mtproto::ServerSalt> get_future_salts() final {
|
2023-10-23 14:26:52 +02:00
|
|
|
string future_salts = G()->td_db()->get_binlog_pmc()->get(get_future_salts_binlog_key(dc_id_));
|
2018-12-31 20:04:05 +01:00
|
|
|
std::vector<mtproto::ServerSalt> res;
|
|
|
|
if (!future_salts.empty()) {
|
|
|
|
unserialize(res, future_salts).ensure();
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
DcId dc_id_;
|
|
|
|
std::vector<unique_ptr<Listener>> auth_key_listeners_;
|
|
|
|
std::shared_ptr<PublicRsaKeyShared> public_rsa_key_;
|
2018-03-13 14:40:02 +01:00
|
|
|
std::shared_ptr<Guard> guard_;
|
2018-12-31 20:04:05 +01:00
|
|
|
RwMutex rw_mutex_;
|
|
|
|
|
2023-10-23 14:26:52 +02:00
|
|
|
static string get_auth_key_binlog_key(DcId dc_id) {
|
|
|
|
return PSTRING() << "auth" << dc_id.get_raw_id();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2023-10-23 14:26:52 +02:00
|
|
|
|
|
|
|
static string get_future_salts_binlog_key(DcId dc_id) {
|
|
|
|
return PSTRING() << "salt" << dc_id.get_raw_id();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void notify() {
|
2023-01-30 16:21:57 +01:00
|
|
|
auto lock = rw_mutex_.lock_write();
|
|
|
|
td::remove_if(auth_key_listeners_, [&](auto &listener) {
|
|
|
|
CHECK(listener != nullptr);
|
|
|
|
return !listener->notify();
|
|
|
|
});
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void log_auth_key(const mtproto::AuthKey &auth_key) {
|
2023-01-03 10:58:19 +01:00
|
|
|
auto salts = get_future_salts();
|
|
|
|
int64 last_used = 0;
|
|
|
|
if (!salts.empty()) {
|
|
|
|
last_used = static_cast<int64>(salts[0].valid_until);
|
|
|
|
}
|
2023-05-04 13:53:07 +02:00
|
|
|
LOG(WARNING) << dc_id_ << " " << tag("auth_key_id", auth_key.id()) << tag("state", get_auth_key_state(auth_key))
|
2023-01-03 10:58:19 +01:00
|
|
|
<< tag("created_at", static_cast<int64>(auth_key.created_at())) << tag("last_used", last_used);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-10-23 14:26:52 +02:00
|
|
|
mtproto::AuthKey AuthDataShared::get_auth_key_for_dc(DcId dc_id) {
|
|
|
|
return AuthDataSharedImpl::get_auth_key_for_dc(dc_id);
|
|
|
|
}
|
|
|
|
|
2018-03-13 14:40:02 +01:00
|
|
|
std::shared_ptr<AuthDataShared> AuthDataShared::create(DcId dc_id, std::shared_ptr<PublicRsaKeyShared> public_rsa_key,
|
|
|
|
std::shared_ptr<Guard> guard) {
|
|
|
|
return std::make_shared<AuthDataSharedImpl>(dc_id, std::move(public_rsa_key), std::move(guard));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2019-07-09 05:56:06 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|