tdlight/td/telegram/net/AuthDataShared.cpp

127 lines
3.8 KiB
C++
Raw Normal View History

//
2022-12-31 22:28:08 +01:00
// 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)
//
#include "td/telegram/net/AuthDataShared.h"
#include "td/telegram/Global.h"
#include "td/telegram/TdDb.h"
#include "td/utils/algorithm.h"
#include "td/utils/format.h"
#include "td/utils/logging.h"
#include "td/utils/port/RwMutex.h"
#include "td/utils/SliceBuilder.h"
#include "td/utils/tl_helpers.h"
namespace td {
class AuthDataSharedImpl final : public AuthDataShared {
public:
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)) {
log_auth_key(get_auth_key());
}
2021-07-03 22:51:36 +02:00
DcId dc_id() const final {
return dc_id_;
}
2021-07-03 22:51:36 +02:00
const std::shared_ptr<PublicRsaKeyShared> &public_rsa_key() final {
return public_rsa_key_;
}
2021-07-03 22:51:36 +02:00
mtproto::AuthKey get_auth_key() final {
string dc_key = G()->td_db()->get_binlog_pmc()->get(auth_key_key());
mtproto::AuthKey res;
if (!dc_key.empty()) {
unserialize(res, dc_key).ensure();
}
return res;
}
2021-02-01 13:07:10 +01:00
2021-07-03 22:51:36 +02:00
AuthKeyState get_auth_key_state() final {
2021-02-01 13:07:10 +01:00
return AuthDataShared::get_auth_key_state(get_auth_key());
}
2021-07-03 22:51:36 +02:00
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);
notify();
}
// TODO: extract it from G()
2021-07-03 22:51:36 +02:00
void update_server_time_difference(double diff) final {
G()->update_server_time_difference(diff);
}
2021-07-03 22:51:36 +02:00
double get_server_time_difference() final {
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);
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 {
G()->td_db()->get_binlog_pmc()->set(future_salts_key(), serialize(future_salts));
}
2021-07-03 22:51:36 +02:00
std::vector<mtproto::ServerSalt> get_future_salts() final {
string future_salts = G()->td_db()->get_binlog_pmc()->get(future_salts_key());
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_;
std::shared_ptr<Guard> guard_;
RwMutex rw_mutex_;
2021-07-09 01:17:26 +02:00
string auth_key_key() const {
return PSTRING() << "auth" << dc_id_.get_raw_id();
}
2021-07-09 01:17:26 +02:00
string future_salts_key() const {
return PSTRING() << "salt" << dc_id_.get_raw_id();
}
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();
});
}
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);
}
2021-02-01 13:07:10 +01:00
LOG(WARNING) << dc_id_ << " " << tag("auth_key_id", auth_key.id())
<< tag("state", AuthDataShared::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);
}
};
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));
}
} // namespace td