Reuse the same PublicRsaKeySharedMain object.

This commit is contained in:
levlam 2024-01-22 02:52:58 +03:00
parent 0dd1934090
commit 1f116cdd99
7 changed files with 54 additions and 63 deletions

View File

@ -442,7 +442,8 @@ static ActorOwn<> get_full_config(DcOption option, Promise<tl_object_ptr<telegra
class SimpleAuthData final : public AuthDataShared {
public:
explicit SimpleAuthData(DcId dc_id) : dc_id_(dc_id) {
explicit SimpleAuthData(DcId dc_id)
: dc_id_(dc_id), public_rsa_key_(PublicRsaKeySharedMain::create(G()->is_test_dc())) {
}
DcId dc_id() const final {
return dc_id_;
@ -492,10 +493,9 @@ static ActorOwn<> get_full_config(DcOption option, Promise<tl_object_ptr<telegra
private:
DcId dc_id_;
std::shared_ptr<mtproto::PublicRsaKeyInterface> public_rsa_key_ =
std::make_shared<PublicRsaKeySharedMain>(G()->is_test_dc());
std::shared_ptr<mtproto::PublicRsaKeyInterface> public_rsa_key_;
vector<unique_ptr<Listener>> auth_key_listeners_;
std::vector<unique_ptr<Listener>> auth_key_listeners_;
void notify() {
td::remove_if(auth_key_listeners_, [&](auto &listener) {
CHECK(listener != nullptr);
@ -506,6 +506,7 @@ static ActorOwn<> get_full_config(DcOption option, Promise<tl_object_ptr<telegra
string auth_key_key() const {
return PSTRING() << "config_recovery_auth" << dc_id().get_raw_id();
}
string future_salts_key() const {
return PSTRING() << "config_recovery_salt" << dc_id().get_raw_id();
}

View File

@ -529,11 +529,11 @@ class TestProxyRequest final : public RequestOnceActor {
return nullptr;
}
mtproto::PublicRsaKeyInterface *get_public_rsa_key_interface() final {
return &public_rsa_key_;
return public_rsa_key_.get();
}
private:
PublicRsaKeySharedMain public_rsa_key_{false};
std::shared_ptr<mtproto::PublicRsaKeyInterface> public_rsa_key_ = PublicRsaKeySharedMain::create(false);
};
auto handshake = make_unique<mtproto::AuthKeyHandshake>(dc_id_, 3600);
auto data = r_data.move_as_ok();

View File

@ -154,7 +154,7 @@ Status NetQueryDispatcher::wait_dc_init(DcId dc_id, bool force) {
std::shared_ptr<mtproto::PublicRsaKeyInterface> public_rsa_key;
bool is_cdn = false;
if (dc_id.is_internal()) {
public_rsa_key = common_public_rsa_key_;
public_rsa_key = PublicRsaKeySharedMain::create(G()->is_test_dc());
} else {
auto public_rsa_key_cdn = std::make_shared<PublicRsaKeySharedCdn>(dc_id);
send_closure_later(public_rsa_key_watchdog_, &PublicRsaKeyWatchdog::add_public_rsa_key, public_rsa_key_cdn);
@ -301,7 +301,6 @@ NetQueryDispatcher::NetQueryDispatcher(const std::function<ActorShared<>()> &cre
LOG(INFO) << tag("main_dc_id", main_dc_id_.load(std::memory_order_relaxed));
delayer_ = create_actor<NetQueryDelayer>("NetQueryDelayer", create_reference());
dc_auth_manager_ = create_actor<DcAuthManager>("DcAuthManager", create_reference());
common_public_rsa_key_ = std::make_shared<PublicRsaKeySharedMain>(G()->is_test_dc());
public_rsa_key_watchdog_ = create_actor<PublicRsaKeyWatchdog>("PublicRsaKeyWatchdog", create_reference());
sequence_dispatcher_ = MultiSequenceDispatcher::create("MultiSequenceDispatcher");

View File

@ -27,7 +27,6 @@ namespace td {
class DcAuthManager;
class MultiSequenceDispatcher;
class NetQueryDelayer;
class PublicRsaKeySharedMain;
class PublicRsaKeyWatchdog;
class SessionMultiProxy;
@ -79,7 +78,6 @@ class NetQueryDispatcher {
#else
std::atomic<int32> main_dc_id_{1};
#endif
std::shared_ptr<PublicRsaKeySharedMain> common_public_rsa_key_;
ActorOwn<PublicRsaKeyWatchdog> public_rsa_key_watchdog_;
std::mutex main_dc_id_mutex_;
std::shared_ptr<Guard> td_guard_;

View File

@ -7,58 +7,57 @@
#include "td/telegram/net/PublicRsaKeySharedMain.h"
#include "td/utils/format.h"
#include "td/utils/logging.h"
#include "td/utils/Slice.h"
#include "td/utils/SliceBuilder.h"
#include <algorithm>
namespace td {
PublicRsaKeySharedMain::PublicRsaKeySharedMain(bool is_test) {
auto add_pem = [this](CSlice pem) {
auto r_rsa = mtproto::RSA::from_pem_public_key(pem);
LOG_CHECK(r_rsa.is_ok()) << r_rsa.error() << " " << pem;
if (r_rsa.is_ok()) {
auto rsa = r_rsa.move_as_ok();
auto fingerprint = rsa.get_fingerprint();
if (get_rsa_key_unsafe(fingerprint) != nullptr) {
return;
}
keys_.push_back(RsaKey{std::move(rsa), fingerprint});
}
std::shared_ptr<PublicRsaKeySharedMain> PublicRsaKeySharedMain::create(bool is_test) {
auto add_pem = [](vector<RsaKey> &keys, CSlice pem) {
auto rsa = mtproto::RSA::from_pem_public_key(pem).move_as_ok();
auto fingerprint = rsa.get_fingerprint();
keys.push_back(RsaKey{std::move(rsa), fingerprint});
};
if (is_test) {
add_pem(
"-----BEGIN RSA PUBLIC KEY-----\n"
"MIIBCgKCAQEAyMEdY1aR+sCR3ZSJrtztKTKqigvO/vBfqACJLZtS7QMgCGXJ6XIR\n"
"yy7mx66W0/sOFa7/1mAZtEoIokDP3ShoqF4fVNb6XeqgQfaUHd8wJpDWHcR2OFwv\n"
"plUUI1PLTktZ9uW2WE23b+ixNwJjJGwBDJPQEQFBE+vfmH0JP503wr5INS1poWg/\n"
"j25sIWeYPHYeOrFp/eXaqhISP6G+q2IeTaWTXpwZj4LzXq5YOpk4bYEQ6mvRq7D1\n"
"aHWfYmlEGepfaYR8Q0YqvvhYtMte3ITnuSJs171+GDqpdKcSwHnd6FudwGO4pcCO\n"
"j4WcDuXc2CTHgH8gFTNhp/Y8/SpDOhvn9QIDAQAB\n"
"-----END RSA PUBLIC KEY-----");
return;
static auto test_public_rsa_key = [&] {
vector<RsaKey> keys;
add_pem(keys,
"-----BEGIN RSA PUBLIC KEY-----\n"
"MIIBCgKCAQEAyMEdY1aR+sCR3ZSJrtztKTKqigvO/vBfqACJLZtS7QMgCGXJ6XIR\n"
"yy7mx66W0/sOFa7/1mAZtEoIokDP3ShoqF4fVNb6XeqgQfaUHd8wJpDWHcR2OFwv\n"
"plUUI1PLTktZ9uW2WE23b+ixNwJjJGwBDJPQEQFBE+vfmH0JP503wr5INS1poWg/\n"
"j25sIWeYPHYeOrFp/eXaqhISP6G+q2IeTaWTXpwZj4LzXq5YOpk4bYEQ6mvRq7D1\n"
"aHWfYmlEGepfaYR8Q0YqvvhYtMte3ITnuSJs171+GDqpdKcSwHnd6FudwGO4pcCO\n"
"j4WcDuXc2CTHgH8gFTNhp/Y8/SpDOhvn9QIDAQAB\n"
"-----END RSA PUBLIC KEY-----");
return std::make_shared<PublicRsaKeySharedMain>(std::move(keys));
}();
return test_public_rsa_key;
} else {
static auto main_public_rsa_key = [&] {
vector<RsaKey> keys;
add_pem(keys,
"-----BEGIN RSA PUBLIC KEY-----\n"
"MIIBCgKCAQEA6LszBcC1LGzyr992NzE0ieY+BSaOW622Aa9Bd4ZHLl+TuFQ4lo4g\n"
"5nKaMBwK/BIb9xUfg0Q29/2mgIR6Zr9krM7HjuIcCzFvDtr+L0GQjae9H0pRB2OO\n"
"62cECs5HKhT5DZ98K33vmWiLowc621dQuwKWSQKjWf50XYFw42h21P2KXUGyp2y/\n"
"+aEyZ+uVgLLQbRA1dEjSDZ2iGRy12Mk5gpYc397aYp438fsJoHIgJ2lgMv5h7WY9\n"
"t6N/byY9Nw9p21Og3AoXSL2q/2IJ1WRUhebgAdGVMlV1fkuOQoEzR7EdpqtQD9Cs\n"
"5+bfo3Nhmcyvk5ftB0WkJ9z6bNZ7yxrP8wIDAQAB\n"
"-----END RSA PUBLIC KEY-----");
return std::make_shared<PublicRsaKeySharedMain>(std::move(keys));
}();
return main_public_rsa_key;
}
add_pem(
"-----BEGIN RSA PUBLIC KEY-----\n"
"MIIBCgKCAQEA6LszBcC1LGzyr992NzE0ieY+BSaOW622Aa9Bd4ZHLl+TuFQ4lo4g\n"
"5nKaMBwK/BIb9xUfg0Q29/2mgIR6Zr9krM7HjuIcCzFvDtr+L0GQjae9H0pRB2OO\n"
"62cECs5HKhT5DZ98K33vmWiLowc621dQuwKWSQKjWf50XYFw42h21P2KXUGyp2y/\n"
"+aEyZ+uVgLLQbRA1dEjSDZ2iGRy12Mk5gpYc397aYp438fsJoHIgJ2lgMv5h7WY9\n"
"t6N/byY9Nw9p21Og3AoXSL2q/2IJ1WRUhebgAdGVMlV1fkuOQoEzR7EdpqtQD9Cs\n"
"5+bfo3Nhmcyvk5ftB0WkJ9z6bNZ7yxrP8wIDAQAB\n"
"-----END RSA PUBLIC KEY-----");
}
Result<mtproto::PublicRsaKeyInterface::RsaKey> PublicRsaKeySharedMain::get_rsa_key(const vector<int64> &fingerprints) {
for (auto fingerprint : fingerprints) {
const auto *rsa_key = get_rsa_key_unsafe(fingerprint);
if (rsa_key != nullptr) {
return RsaKey{rsa_key->rsa.clone(), fingerprint};
for (const auto &key : keys_) {
if (key.fingerprint == fingerprint) {
return RsaKey{key.rsa.clone(), fingerprint};
}
}
}
return Status::Error(PSLICE() << "Unknown fingerprints " << format::as_array(fingerprints));
@ -68,13 +67,4 @@ void PublicRsaKeySharedMain::drop_keys() {
// nothing to do
}
const mtproto::PublicRsaKeyInterface::RsaKey *PublicRsaKeySharedMain::get_rsa_key_unsafe(int64 fingerprint) const {
auto it = std::find_if(keys_.begin(), keys_.end(),
[fingerprint](const auto &value) { return value.fingerprint == fingerprint; });
if (it == keys_.end()) {
return nullptr;
}
return &*it;
}
} // namespace td

View File

@ -15,7 +15,10 @@ namespace td {
class PublicRsaKeySharedMain final : public mtproto::PublicRsaKeyInterface {
public:
explicit PublicRsaKeySharedMain(bool is_test);
explicit PublicRsaKeySharedMain(vector<RsaKey> &&keys) : keys_(std::move(keys)) {
}
static std::shared_ptr<PublicRsaKeySharedMain> create(bool is_test);
Result<RsaKey> get_rsa_key(const vector<int64> &fingerprints) final;
@ -23,8 +26,6 @@ class PublicRsaKeySharedMain final : public mtproto::PublicRsaKeyInterface {
private:
vector<RsaKey> keys_;
const RsaKey *get_rsa_key_unsafe(int64 fingerprint) const;
};
} // namespace td

View File

@ -46,6 +46,8 @@
#include "td/utils/tests.h"
#include "td/utils/Time.h"
#include <memory>
TEST(Mtproto, GetHostByNameActor) {
int threads_n = 1;
td::ConcurrentScheduler sched(threads_n, 0);
@ -300,11 +302,11 @@ class HandshakeContext final : public td::mtproto::AuthKeyHandshakeContext {
return nullptr;
}
td::mtproto::PublicRsaKeyInterface *get_public_rsa_key_interface() final {
return &public_rsa_key;
return public_rsa_key_.get();
}
private:
td::PublicRsaKeySharedMain public_rsa_key{true};
std::shared_ptr<td::mtproto::PublicRsaKeyInterface> public_rsa_key_ = td::PublicRsaKeySharedMain::create(true);
};
class HandshakeTestActor final : public td::Actor {