Reuse the same PublicRsaKeySharedMain object.
This commit is contained in:
parent
0dd1934090
commit
1f116cdd99
@ -442,7 +442,8 @@ static ActorOwn<> get_full_config(DcOption option, Promise<tl_object_ptr<telegra
|
|||||||
|
|
||||||
class SimpleAuthData final : public AuthDataShared {
|
class SimpleAuthData final : public AuthDataShared {
|
||||||
public:
|
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 {
|
DcId dc_id() const final {
|
||||||
return dc_id_;
|
return dc_id_;
|
||||||
@ -492,10 +493,9 @@ static ActorOwn<> get_full_config(DcOption option, Promise<tl_object_ptr<telegra
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
DcId dc_id_;
|
DcId dc_id_;
|
||||||
std::shared_ptr<mtproto::PublicRsaKeyInterface> public_rsa_key_ =
|
std::shared_ptr<mtproto::PublicRsaKeyInterface> public_rsa_key_;
|
||||||
std::make_shared<PublicRsaKeySharedMain>(G()->is_test_dc());
|
vector<unique_ptr<Listener>> auth_key_listeners_;
|
||||||
|
|
||||||
std::vector<unique_ptr<Listener>> auth_key_listeners_;
|
|
||||||
void notify() {
|
void notify() {
|
||||||
td::remove_if(auth_key_listeners_, [&](auto &listener) {
|
td::remove_if(auth_key_listeners_, [&](auto &listener) {
|
||||||
CHECK(listener != nullptr);
|
CHECK(listener != nullptr);
|
||||||
@ -506,6 +506,7 @@ static ActorOwn<> get_full_config(DcOption option, Promise<tl_object_ptr<telegra
|
|||||||
string auth_key_key() const {
|
string auth_key_key() const {
|
||||||
return PSTRING() << "config_recovery_auth" << dc_id().get_raw_id();
|
return PSTRING() << "config_recovery_auth" << dc_id().get_raw_id();
|
||||||
}
|
}
|
||||||
|
|
||||||
string future_salts_key() const {
|
string future_salts_key() const {
|
||||||
return PSTRING() << "config_recovery_salt" << dc_id().get_raw_id();
|
return PSTRING() << "config_recovery_salt" << dc_id().get_raw_id();
|
||||||
}
|
}
|
||||||
|
@ -529,11 +529,11 @@ class TestProxyRequest final : public RequestOnceActor {
|
|||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
mtproto::PublicRsaKeyInterface *get_public_rsa_key_interface() final {
|
mtproto::PublicRsaKeyInterface *get_public_rsa_key_interface() final {
|
||||||
return &public_rsa_key_;
|
return public_rsa_key_.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
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 handshake = make_unique<mtproto::AuthKeyHandshake>(dc_id_, 3600);
|
||||||
auto data = r_data.move_as_ok();
|
auto data = r_data.move_as_ok();
|
||||||
|
@ -154,7 +154,7 @@ Status NetQueryDispatcher::wait_dc_init(DcId dc_id, bool force) {
|
|||||||
std::shared_ptr<mtproto::PublicRsaKeyInterface> public_rsa_key;
|
std::shared_ptr<mtproto::PublicRsaKeyInterface> public_rsa_key;
|
||||||
bool is_cdn = false;
|
bool is_cdn = false;
|
||||||
if (dc_id.is_internal()) {
|
if (dc_id.is_internal()) {
|
||||||
public_rsa_key = common_public_rsa_key_;
|
public_rsa_key = PublicRsaKeySharedMain::create(G()->is_test_dc());
|
||||||
} else {
|
} else {
|
||||||
auto public_rsa_key_cdn = std::make_shared<PublicRsaKeySharedCdn>(dc_id);
|
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);
|
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));
|
LOG(INFO) << tag("main_dc_id", main_dc_id_.load(std::memory_order_relaxed));
|
||||||
delayer_ = create_actor<NetQueryDelayer>("NetQueryDelayer", create_reference());
|
delayer_ = create_actor<NetQueryDelayer>("NetQueryDelayer", create_reference());
|
||||||
dc_auth_manager_ = create_actor<DcAuthManager>("DcAuthManager", 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());
|
public_rsa_key_watchdog_ = create_actor<PublicRsaKeyWatchdog>("PublicRsaKeyWatchdog", create_reference());
|
||||||
sequence_dispatcher_ = MultiSequenceDispatcher::create("MultiSequenceDispatcher");
|
sequence_dispatcher_ = MultiSequenceDispatcher::create("MultiSequenceDispatcher");
|
||||||
|
|
||||||
|
@ -27,7 +27,6 @@ namespace td {
|
|||||||
class DcAuthManager;
|
class DcAuthManager;
|
||||||
class MultiSequenceDispatcher;
|
class MultiSequenceDispatcher;
|
||||||
class NetQueryDelayer;
|
class NetQueryDelayer;
|
||||||
class PublicRsaKeySharedMain;
|
|
||||||
class PublicRsaKeyWatchdog;
|
class PublicRsaKeyWatchdog;
|
||||||
class SessionMultiProxy;
|
class SessionMultiProxy;
|
||||||
|
|
||||||
@ -79,7 +78,6 @@ class NetQueryDispatcher {
|
|||||||
#else
|
#else
|
||||||
std::atomic<int32> main_dc_id_{1};
|
std::atomic<int32> main_dc_id_{1};
|
||||||
#endif
|
#endif
|
||||||
std::shared_ptr<PublicRsaKeySharedMain> common_public_rsa_key_;
|
|
||||||
ActorOwn<PublicRsaKeyWatchdog> public_rsa_key_watchdog_;
|
ActorOwn<PublicRsaKeyWatchdog> public_rsa_key_watchdog_;
|
||||||
std::mutex main_dc_id_mutex_;
|
std::mutex main_dc_id_mutex_;
|
||||||
std::shared_ptr<Guard> td_guard_;
|
std::shared_ptr<Guard> td_guard_;
|
||||||
|
@ -7,58 +7,57 @@
|
|||||||
#include "td/telegram/net/PublicRsaKeySharedMain.h"
|
#include "td/telegram/net/PublicRsaKeySharedMain.h"
|
||||||
|
|
||||||
#include "td/utils/format.h"
|
#include "td/utils/format.h"
|
||||||
#include "td/utils/logging.h"
|
|
||||||
#include "td/utils/Slice.h"
|
#include "td/utils/Slice.h"
|
||||||
#include "td/utils/SliceBuilder.h"
|
#include "td/utils/SliceBuilder.h"
|
||||||
|
|
||||||
#include <algorithm>
|
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
|
|
||||||
PublicRsaKeySharedMain::PublicRsaKeySharedMain(bool is_test) {
|
std::shared_ptr<PublicRsaKeySharedMain> PublicRsaKeySharedMain::create(bool is_test) {
|
||||||
auto add_pem = [this](CSlice pem) {
|
auto add_pem = [](vector<RsaKey> &keys, CSlice pem) {
|
||||||
auto r_rsa = mtproto::RSA::from_pem_public_key(pem);
|
auto rsa = mtproto::RSA::from_pem_public_key(pem).move_as_ok();
|
||||||
LOG_CHECK(r_rsa.is_ok()) << r_rsa.error() << " " << pem;
|
auto fingerprint = rsa.get_fingerprint();
|
||||||
|
keys.push_back(RsaKey{std::move(rsa), fingerprint});
|
||||||
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});
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if (is_test) {
|
if (is_test) {
|
||||||
add_pem(
|
static auto test_public_rsa_key = [&] {
|
||||||
"-----BEGIN RSA PUBLIC KEY-----\n"
|
vector<RsaKey> keys;
|
||||||
"MIIBCgKCAQEAyMEdY1aR+sCR3ZSJrtztKTKqigvO/vBfqACJLZtS7QMgCGXJ6XIR\n"
|
add_pem(keys,
|
||||||
"yy7mx66W0/sOFa7/1mAZtEoIokDP3ShoqF4fVNb6XeqgQfaUHd8wJpDWHcR2OFwv\n"
|
"-----BEGIN RSA PUBLIC KEY-----\n"
|
||||||
"plUUI1PLTktZ9uW2WE23b+ixNwJjJGwBDJPQEQFBE+vfmH0JP503wr5INS1poWg/\n"
|
"MIIBCgKCAQEAyMEdY1aR+sCR3ZSJrtztKTKqigvO/vBfqACJLZtS7QMgCGXJ6XIR\n"
|
||||||
"j25sIWeYPHYeOrFp/eXaqhISP6G+q2IeTaWTXpwZj4LzXq5YOpk4bYEQ6mvRq7D1\n"
|
"yy7mx66W0/sOFa7/1mAZtEoIokDP3ShoqF4fVNb6XeqgQfaUHd8wJpDWHcR2OFwv\n"
|
||||||
"aHWfYmlEGepfaYR8Q0YqvvhYtMte3ITnuSJs171+GDqpdKcSwHnd6FudwGO4pcCO\n"
|
"plUUI1PLTktZ9uW2WE23b+ixNwJjJGwBDJPQEQFBE+vfmH0JP503wr5INS1poWg/\n"
|
||||||
"j4WcDuXc2CTHgH8gFTNhp/Y8/SpDOhvn9QIDAQAB\n"
|
"j25sIWeYPHYeOrFp/eXaqhISP6G+q2IeTaWTXpwZj4LzXq5YOpk4bYEQ6mvRq7D1\n"
|
||||||
"-----END RSA PUBLIC KEY-----");
|
"aHWfYmlEGepfaYR8Q0YqvvhYtMte3ITnuSJs171+GDqpdKcSwHnd6FudwGO4pcCO\n"
|
||||||
return;
|
"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) {
|
Result<mtproto::PublicRsaKeyInterface::RsaKey> PublicRsaKeySharedMain::get_rsa_key(const vector<int64> &fingerprints) {
|
||||||
for (auto fingerprint : fingerprints) {
|
for (auto fingerprint : fingerprints) {
|
||||||
const auto *rsa_key = get_rsa_key_unsafe(fingerprint);
|
for (const auto &key : keys_) {
|
||||||
if (rsa_key != nullptr) {
|
if (key.fingerprint == fingerprint) {
|
||||||
return RsaKey{rsa_key->rsa.clone(), fingerprint};
|
return RsaKey{key.rsa.clone(), fingerprint};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Status::Error(PSLICE() << "Unknown fingerprints " << format::as_array(fingerprints));
|
return Status::Error(PSLICE() << "Unknown fingerprints " << format::as_array(fingerprints));
|
||||||
@ -68,13 +67,4 @@ void PublicRsaKeySharedMain::drop_keys() {
|
|||||||
// nothing to do
|
// 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
|
} // namespace td
|
||||||
|
@ -15,7 +15,10 @@ namespace td {
|
|||||||
|
|
||||||
class PublicRsaKeySharedMain final : public mtproto::PublicRsaKeyInterface {
|
class PublicRsaKeySharedMain final : public mtproto::PublicRsaKeyInterface {
|
||||||
public:
|
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;
|
Result<RsaKey> get_rsa_key(const vector<int64> &fingerprints) final;
|
||||||
|
|
||||||
@ -23,8 +26,6 @@ class PublicRsaKeySharedMain final : public mtproto::PublicRsaKeyInterface {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
vector<RsaKey> keys_;
|
vector<RsaKey> keys_;
|
||||||
|
|
||||||
const RsaKey *get_rsa_key_unsafe(int64 fingerprint) const;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace td
|
} // namespace td
|
||||||
|
@ -46,6 +46,8 @@
|
|||||||
#include "td/utils/tests.h"
|
#include "td/utils/tests.h"
|
||||||
#include "td/utils/Time.h"
|
#include "td/utils/Time.h"
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
TEST(Mtproto, GetHostByNameActor) {
|
TEST(Mtproto, GetHostByNameActor) {
|
||||||
int threads_n = 1;
|
int threads_n = 1;
|
||||||
td::ConcurrentScheduler sched(threads_n, 0);
|
td::ConcurrentScheduler sched(threads_n, 0);
|
||||||
@ -300,11 +302,11 @@ class HandshakeContext final : public td::mtproto::AuthKeyHandshakeContext {
|
|||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
td::mtproto::PublicRsaKeyInterface *get_public_rsa_key_interface() final {
|
td::mtproto::PublicRsaKeyInterface *get_public_rsa_key_interface() final {
|
||||||
return &public_rsa_key;
|
return public_rsa_key_.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
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 {
|
class HandshakeTestActor final : public td::Actor {
|
||||||
|
Loading…
Reference in New Issue
Block a user