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/SessionProxy.h"
|
|
|
|
|
|
|
|
#include "td/telegram/Global.h"
|
2023-05-04 13:53:07 +02:00
|
|
|
#include "td/telegram/net/AuthKeyState.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/net/ConnectionCreator.h"
|
2018-06-26 01:43:11 +02:00
|
|
|
#include "td/telegram/net/DcId.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/net/NetQueryDispatcher.h"
|
|
|
|
#include "td/telegram/net/Session.h"
|
2021-09-10 16:32:39 +02:00
|
|
|
#include "td/telegram/Td.h"
|
2023-05-18 13:33:00 +02:00
|
|
|
#include "td/telegram/TdDb.h"
|
2023-07-27 11:52:29 +02:00
|
|
|
#include "td/telegram/telegram_api.h"
|
2019-03-17 21:43:58 +01:00
|
|
|
#include "td/telegram/UniqueId.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-09-18 23:47:05 +02:00
|
|
|
#include "td/utils/buffer.h"
|
2019-02-12 22:26:36 +01:00
|
|
|
#include "td/utils/common.h"
|
2023-07-27 11:52:29 +02:00
|
|
|
#include "td/utils/format.h"
|
2022-11-23 17:37:32 +01:00
|
|
|
#include "td/utils/HashTableUtils.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/logging.h"
|
2022-06-27 12:30:18 +02:00
|
|
|
#include "td/utils/Promise.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Slice.h"
|
2021-05-17 14:21:11 +02:00
|
|
|
#include "td/utils/SliceBuilder.h"
|
2023-05-18 13:33:00 +02:00
|
|
|
#include "td/utils/Time.h"
|
|
|
|
#include "td/utils/tl_helpers.h"
|
2023-07-27 11:52:29 +02:00
|
|
|
#include "td/utils/tl_parsers.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
namespace td {
|
2018-04-24 18:21:47 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
namespace mtproto {
|
|
|
|
class RawConnection;
|
|
|
|
} // namespace mtproto
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class SessionCallback final : public Session::Callback {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2022-11-23 17:37:32 +01:00
|
|
|
SessionCallback(ActorShared<SessionProxy> parent, DcId dc_id, bool allow_media_only, bool is_media, uint32 hash)
|
2018-12-31 20:04:05 +01:00
|
|
|
: parent_(std::move(parent))
|
|
|
|
, dc_id_(dc_id)
|
|
|
|
, allow_media_only_(allow_media_only)
|
|
|
|
, is_media_(is_media)
|
|
|
|
, hash_(hash) {
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_failed() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
send_closure(parent_, &SessionProxy::on_failed);
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_closed() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
send_closure(parent_, &SessionProxy::on_closed);
|
|
|
|
}
|
2019-05-06 20:53:39 +02:00
|
|
|
void request_raw_connection(unique_ptr<mtproto::AuthData> auth_data,
|
2021-07-03 22:51:36 +02:00
|
|
|
Promise<unique_ptr<mtproto::RawConnection>> promise) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
send_closure(G()->connection_creator(), &ConnectionCreator::request_raw_connection, dc_id_, allow_media_only_,
|
2019-05-06 20:53:39 +02:00
|
|
|
is_media_, std::move(promise), hash_, std::move(auth_data));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_tmp_auth_key_updated(mtproto::AuthKey auth_key) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
send_closure(parent_, &SessionProxy::on_tmp_auth_key_updated, std::move(auth_key));
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_server_salt_updated(std::vector<mtproto::ServerSalt> server_salts) final {
|
2018-06-13 17:17:27 +02:00
|
|
|
send_closure(parent_, &SessionProxy::on_server_salt_updated, std::move(server_salts));
|
|
|
|
}
|
|
|
|
|
2023-01-25 15:48:04 +01:00
|
|
|
void on_update(BufferSlice &&update, uint64 auth_key_id) final {
|
2023-07-27 11:52:29 +02:00
|
|
|
TlBufferParser parser(&update);
|
|
|
|
auto updates = telegram_api::Updates::fetch(parser);
|
|
|
|
parser.fetch_end();
|
|
|
|
if (parser.get_error()) {
|
|
|
|
LOG(ERROR) << "Failed to fetch update: " << parser.get_error() << format::as_hex_dump<4>(update.as_slice());
|
|
|
|
updates = nullptr;
|
|
|
|
}
|
|
|
|
send_closure_later(G()->td(), &Td::on_update, std::move(updates), auth_key_id);
|
2021-09-10 16:32:39 +02:00
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_result(NetQueryPtr query) final {
|
2021-09-10 16:32:39 +02:00
|
|
|
if (UniqueId::extract_type(query->id()) != UniqueId::BindKey) {
|
2019-03-15 11:37:01 +01:00
|
|
|
send_closure(parent_, &SessionProxy::on_query_finished);
|
|
|
|
}
|
2019-03-15 10:00:18 +01:00
|
|
|
G()->net_query_dispatcher().dispatch(std::move(query));
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
private:
|
|
|
|
ActorShared<SessionProxy> parent_;
|
|
|
|
DcId dc_id_;
|
|
|
|
bool allow_media_only_ = false;
|
|
|
|
bool is_media_ = false;
|
2022-11-23 17:37:32 +01:00
|
|
|
uint32 hash_ = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
2019-03-15 10:00:18 +01:00
|
|
|
SessionProxy::SessionProxy(unique_ptr<Callback> callback, std::shared_ptr<AuthDataShared> shared_auth_data,
|
2023-01-14 22:25:22 +01:00
|
|
|
bool is_primary, bool is_main, bool allow_media_only, bool is_media, bool use_pfs,
|
2023-10-23 13:10:39 +02:00
|
|
|
bool persist_tmp_auth_key, bool is_cdn, bool need_destroy_auth_key)
|
2019-03-15 10:00:18 +01:00
|
|
|
: callback_(std::move(callback))
|
|
|
|
, auth_data_(std::move(shared_auth_data))
|
2023-01-14 22:25:22 +01:00
|
|
|
, is_primary_(is_primary)
|
2018-12-31 20:04:05 +01:00
|
|
|
, is_main_(is_main)
|
|
|
|
, allow_media_only_(allow_media_only)
|
|
|
|
, is_media_(is_media)
|
|
|
|
, use_pfs_(use_pfs)
|
2023-05-04 14:54:21 +02:00
|
|
|
, persist_tmp_auth_key_(use_pfs && persist_tmp_auth_key)
|
2017-12-29 21:34:39 +01:00
|
|
|
, is_cdn_(is_cdn)
|
2023-10-23 13:10:39 +02:00
|
|
|
, need_destroy_auth_key_(need_destroy_auth_key) {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SessionProxy::start_up() {
|
2021-07-04 04:58:54 +02:00
|
|
|
class Listener final : public AuthDataShared::Listener {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
|
|
|
explicit Listener(ActorShared<SessionProxy> session_proxy) : session_proxy_(std::move(session_proxy)) {
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
bool notify() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!session_proxy_.is_alive()) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-07-23 01:14:34 +02:00
|
|
|
send_closure(session_proxy_, &SessionProxy::update_auth_key_state);
|
2018-12-31 20:04:05 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ActorShared<SessionProxy> session_proxy_;
|
|
|
|
};
|
2023-05-04 13:53:07 +02:00
|
|
|
auth_key_state_ = get_auth_key_state(auth_data_->get_auth_key());
|
2018-09-27 03:19:03 +02:00
|
|
|
auth_data_->add_auth_key_listener(make_unique<Listener>(actor_shared(this)));
|
2023-05-04 14:54:21 +02:00
|
|
|
|
|
|
|
string saved_auth_key = G()->td_db()->get_binlog_pmc()->get(tmp_auth_key_key());
|
|
|
|
if (!saved_auth_key.empty()) {
|
|
|
|
if (persist_tmp_auth_key_) {
|
|
|
|
unserialize(tmp_auth_key_, saved_auth_key).ensure();
|
|
|
|
if (tmp_auth_key_.expires_at() < Time::now()) {
|
|
|
|
tmp_auth_key_ = {};
|
|
|
|
} else {
|
|
|
|
LOG(WARNING) << "Loaded tmp_auth_key " << tmp_auth_key_.id() << ": " << get_auth_key_state(tmp_auth_key_);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
LOG(WARNING) << "Drop saved tmp_auth_key";
|
|
|
|
G()->td_db()->get_binlog_pmc()->erase(tmp_auth_key_key());
|
|
|
|
}
|
|
|
|
}
|
2017-12-29 21:34:39 +01:00
|
|
|
open_session();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SessionProxy::tear_down() {
|
|
|
|
for (auto &query : pending_queries_) {
|
|
|
|
query->resend();
|
2019-03-15 11:37:01 +01:00
|
|
|
callback_->on_query_finished();
|
2018-12-31 20:04:05 +01:00
|
|
|
G()->net_query_dispatcher().dispatch(std::move(query));
|
|
|
|
}
|
|
|
|
pending_queries_.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SessionProxy::send(NetQueryPtr query) {
|
2019-07-23 01:14:34 +02:00
|
|
|
if (query->auth_flag() == NetQuery::AuthFlag::On && auth_key_state_ != AuthKeyState::OK) {
|
2018-12-31 20:04:05 +01:00
|
|
|
query->debug(PSTRING() << get_name() << ": wait for auth");
|
|
|
|
pending_queries_.emplace_back(std::move(query));
|
|
|
|
return;
|
|
|
|
}
|
2017-12-29 21:34:39 +01:00
|
|
|
open_session(true);
|
2018-12-31 20:04:05 +01:00
|
|
|
query->debug(PSTRING() << get_name() << ": sent to session");
|
|
|
|
send_closure(session_, &Session::send, std::move(query));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SessionProxy::update_main_flag(bool is_main) {
|
|
|
|
if (is_main_ == is_main) {
|
|
|
|
return;
|
|
|
|
}
|
2023-10-23 14:57:36 +02:00
|
|
|
LOG(INFO) << "Update is_main to " << is_main;
|
2018-12-31 20:04:05 +01:00
|
|
|
is_main_ = is_main;
|
|
|
|
close_session();
|
|
|
|
open_session();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SessionProxy::on_failed() {
|
|
|
|
if (session_generation_ != get_link_token()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
close_session();
|
|
|
|
open_session();
|
|
|
|
}
|
|
|
|
|
2018-06-13 17:17:27 +02:00
|
|
|
void SessionProxy::update_mtproto_header() {
|
|
|
|
close_session();
|
|
|
|
open_session();
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void SessionProxy::on_closed() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void SessionProxy::close_session() {
|
|
|
|
send_closure(std::move(session_), &Session::close);
|
|
|
|
session_generation_++;
|
|
|
|
}
|
2022-06-06 19:36:15 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void SessionProxy::open_session(bool force) {
|
2017-12-29 21:34:39 +01:00
|
|
|
if (!session_.empty()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-11-03 07:33:22 +01:00
|
|
|
// There are several assumption that make this code OK
|
|
|
|
// 1. All unauthorized query will be sent into the same SessionProxy
|
|
|
|
// 2. All authorized query are delayed before we have authorization
|
|
|
|
// So only one SessionProxy will be active before we have authorization key
|
2020-02-26 16:15:19 +01:00
|
|
|
auto should_open = [&] {
|
2018-11-03 07:33:22 +01:00
|
|
|
if (force) {
|
|
|
|
return true;
|
2018-11-01 17:34:18 +01:00
|
|
|
}
|
2023-10-23 13:10:39 +02:00
|
|
|
if (need_destroy_auth_key_) {
|
2019-07-23 01:14:34 +02:00
|
|
|
return auth_key_state_ != AuthKeyState::Empty;
|
2018-11-01 17:34:18 +01:00
|
|
|
}
|
2023-01-16 14:43:07 +01:00
|
|
|
if (is_main_) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-07-23 01:14:34 +02:00
|
|
|
if (auth_key_state_ != AuthKeyState::OK) {
|
2018-11-06 12:06:08 +01:00
|
|
|
return false;
|
2018-11-03 07:33:22 +01:00
|
|
|
}
|
2023-01-16 14:43:07 +01:00
|
|
|
return !pending_queries_.empty();
|
2018-11-03 07:33:22 +01:00
|
|
|
}();
|
|
|
|
if (!should_open) {
|
|
|
|
return;
|
2017-12-29 21:34:39 +01:00
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(session_.empty());
|
2018-05-23 18:49:05 +02:00
|
|
|
auto dc_id = auth_data_->dc_id();
|
2018-12-31 20:04:05 +01:00
|
|
|
string name = PSTRING() << "Session" << get_name().substr(Slice("SessionProxy").size());
|
2018-05-23 18:49:05 +02:00
|
|
|
string hash_string = PSTRING() << name << " " << dc_id.get_raw_id() << " " << allow_media_only_;
|
2022-11-23 17:37:32 +01:00
|
|
|
auto hash = Hash<string>()(hash_string);
|
2019-12-18 01:47:51 +01:00
|
|
|
int32 raw_dc_id = dc_id.get_raw_id();
|
|
|
|
int32 int_dc_id = raw_dc_id;
|
2018-05-23 18:49:05 +02:00
|
|
|
if (G()->is_test_dc()) {
|
|
|
|
int_dc_id += 10000;
|
|
|
|
}
|
2018-06-04 00:17:59 +02:00
|
|
|
if (allow_media_only_ && !is_cdn_) {
|
2018-05-23 18:49:05 +02:00
|
|
|
int_dc_id = -int_dc_id;
|
|
|
|
}
|
|
|
|
session_ = create_actor<Session>(
|
|
|
|
name,
|
|
|
|
make_unique<SessionCallback>(actor_shared(this, session_generation_), dc_id, allow_media_only_, is_media_, hash),
|
2023-10-23 13:10:39 +02:00
|
|
|
auth_data_, raw_dc_id, int_dc_id, is_primary_, is_main_, use_pfs_, persist_tmp_auth_key_, is_cdn_,
|
|
|
|
need_destroy_auth_key_, tmp_auth_key_, server_salts_);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-07-23 01:14:34 +02:00
|
|
|
void SessionProxy::update_auth_key_state() {
|
|
|
|
auto old_auth_key_state = auth_key_state_;
|
2023-05-04 13:53:07 +02:00
|
|
|
auth_key_state_ = get_auth_key_state(auth_data_->get_auth_key());
|
2019-07-23 01:14:34 +02:00
|
|
|
if (auth_key_state_ != old_auth_key_state && old_auth_key_state == AuthKeyState::OK) {
|
2018-11-06 12:06:08 +01:00
|
|
|
close_session();
|
|
|
|
}
|
2018-11-02 16:55:55 +01:00
|
|
|
open_session();
|
2019-07-23 01:14:34 +02:00
|
|
|
if (session_.empty() || auth_key_state_ != AuthKeyState::OK) {
|
2018-11-03 07:33:22 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
for (auto &query : pending_queries_) {
|
|
|
|
query->debug(PSTRING() << get_name() << ": sent to session");
|
|
|
|
send_closure(session_, &Session::send, std::move(query));
|
|
|
|
}
|
|
|
|
pending_queries_.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SessionProxy::on_tmp_auth_key_updated(mtproto::AuthKey auth_key) {
|
2023-05-04 13:59:09 +02:00
|
|
|
LOG(WARNING) << "Have tmp_auth_key " << auth_key.id() << ": " << get_auth_key_state(auth_key);
|
2018-12-31 20:04:05 +01:00
|
|
|
tmp_auth_key_ = std::move(auth_key);
|
2023-05-04 14:54:21 +02:00
|
|
|
if (persist_tmp_auth_key_) {
|
|
|
|
G()->td_db()->get_binlog_pmc()->set(tmp_auth_key_key(), serialize(tmp_auth_key_));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
string SessionProxy::tmp_auth_key_key() const {
|
|
|
|
return PSTRING() << "tmp_auth" << get_name();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2021-09-10 15:45:39 +02:00
|
|
|
|
2018-06-13 17:17:27 +02:00
|
|
|
void SessionProxy::on_server_salt_updated(std::vector<mtproto::ServerSalt> server_salts) {
|
|
|
|
server_salts_ = std::move(server_salts);
|
|
|
|
}
|
2018-04-24 18:21:47 +02:00
|
|
|
|
2019-03-15 10:00:18 +01:00
|
|
|
void SessionProxy::on_query_finished() {
|
|
|
|
callback_->on_query_finished();
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|