1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
//
// 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/net/AuthKeyState.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<mtproto::PublicRsaKeyInterface> 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());
}
DcId dc_id() const final {
return dc_id_;
}
const std::shared_ptr<mtproto::PublicRsaKeyInterface> &public_rsa_key() final {
return public_rsa_key_;
}
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));
mtproto::AuthKey res;
if (!dc_key.empty()) {
unserialize(res, dc_key).ensure();
}
return res;
}
mtproto::AuthKey get_auth_key() final {
return get_auth_key_for_dc(dc_id_);
}
void set_auth_key(const mtproto::AuthKey &auth_key) final {
G()->td_db()->get_binlog_pmc()->set(get_auth_key_binlog_key(dc_id_), serialize(auth_key));
log_auth_key(auth_key);
notify();
}
// TODO: extract it from G()
void update_server_time_difference(double diff, bool force) final {
G()->update_server_time_difference(diff, force);
}
double get_server_time_difference() final {
return G()->get_server_time_difference();
}
void add_auth_key_listener(unique_ptr<Listener> listener) final {
CHECK(listener != nullptr);
if (listener->notify()) {
auto lock = rw_mutex_.lock_write();
auth_key_listeners_.push_back(std::move(listener));
}
}
void set_future_salts(const std::vector<mtproto::ServerSalt> &future_salts) final {
G()->td_db()->get_binlog_pmc()->set(get_future_salts_binlog_key(dc_id_), serialize(future_salts));
}
std::vector<mtproto::ServerSalt> get_future_salts() final {
string future_salts = G()->td_db()->get_binlog_pmc()->get(get_future_salts_binlog_key(dc_id_));
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<mtproto::PublicRsaKeyInterface> public_rsa_key_;
std::shared_ptr<Guard> guard_;
RwMutex rw_mutex_;
static string get_auth_key_binlog_key(DcId dc_id) {
return PSTRING() << "auth" << dc_id.get_raw_id();
}
static string get_future_salts_binlog_key(DcId dc_id) {
return PSTRING() << "salt" << dc_id.get_raw_id();
}
void notify() {
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) {
auto salts = get_future_salts();
int64 last_used = 0;
if (!salts.empty()) {
last_used = static_cast<int64>(salts[0].valid_until);
}
LOG(WARNING) << dc_id_ << " " << tag("auth_key_id", auth_key.id()) << tag("state", get_auth_key_state(auth_key))
<< tag("created_at", static_cast<int64>(auth_key.created_at())) << tag("last_used", last_used);
}
};
mtproto::AuthKey AuthDataShared::get_auth_key_for_dc(DcId dc_id) {
return AuthDataSharedImpl::get_auth_key_for_dc(dc_id);
}
std::shared_ptr<AuthDataShared> AuthDataShared::create(DcId dc_id,
std::shared_ptr<mtproto::PublicRsaKeyInterface> 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
|