2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
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/Session.h"
|
|
|
|
|
|
|
|
#include "td/telegram/DhCache.h"
|
|
|
|
#include "td/telegram/Global.h"
|
2020-10-05 17:07:23 +02:00
|
|
|
#include "td/telegram/net/DcAuthManager.h"
|
2020-03-16 20:00:44 +01:00
|
|
|
#include "td/telegram/net/DcId.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/net/MtprotoHeader.h"
|
|
|
|
#include "td/telegram/net/NetQuery.h"
|
|
|
|
#include "td/telegram/net/NetQueryDispatcher.h"
|
2018-06-26 01:43:11 +02:00
|
|
|
#include "td/telegram/net/NetType.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/StateManager.h"
|
2021-10-27 16:32:09 +02:00
|
|
|
#include "td/telegram/telegram_api.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/UniqueId.h"
|
|
|
|
|
2021-07-05 20:20:07 +02:00
|
|
|
#include "td/mtproto/DhCallback.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/mtproto/Handshake.h"
|
|
|
|
#include "td/mtproto/HandshakeActor.h"
|
|
|
|
#include "td/mtproto/RawConnection.h"
|
2020-05-17 16:07:16 +02:00
|
|
|
#include "td/mtproto/RSA.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/mtproto/SessionConnection.h"
|
2019-01-31 03:13:59 +01:00
|
|
|
#include "td/mtproto/TransportType.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-06-28 13:02:14 +02:00
|
|
|
#include "td/actor/PromiseFuture.h"
|
|
|
|
|
2021-01-01 13:59:53 +01:00
|
|
|
#include "td/utils/algorithm.h"
|
2020-03-15 23:01:14 +01:00
|
|
|
#include "td/utils/as.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/format.h"
|
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/misc.h"
|
2022-06-14 15:11:38 +02:00
|
|
|
#include "td/utils/port/thread_local.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Random.h"
|
|
|
|
#include "td/utils/Slice.h"
|
2021-05-17 14:21:11 +02:00
|
|
|
#include "td/utils/SliceBuilder.h"
|
2022-01-31 13:56:44 +01:00
|
|
|
#include "td/utils/Span.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Time.h"
|
|
|
|
#include "td/utils/Timer.h"
|
|
|
|
#include "td/utils/tl_parsers.h"
|
2021-07-26 19:04:43 +02:00
|
|
|
#include "td/utils/utf8.h"
|
2022-06-11 00:47:00 +02:00
|
|
|
#include "td/utils/VectorQueue.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-08-10 16:03:38 +02:00
|
|
|
#include <atomic>
|
2022-06-14 15:30:09 +02:00
|
|
|
#include <memory>
|
2018-12-31 20:04:05 +01:00
|
|
|
#include <tuple>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
namespace detail {
|
2018-05-14 21:00:38 +02:00
|
|
|
|
2022-06-10 15:17:18 +02:00
|
|
|
class SemaphoreActor final : public Actor {
|
|
|
|
public:
|
|
|
|
explicit SemaphoreActor(size_t capacity) : capacity_(capacity) {
|
|
|
|
}
|
|
|
|
|
2022-06-11 00:47:00 +02:00
|
|
|
void execute(Promise<Promise<Unit>> promise) {
|
2022-06-10 15:17:18 +02:00
|
|
|
if (capacity_ == 0) {
|
|
|
|
pending_.push(std::move(promise));
|
|
|
|
} else {
|
|
|
|
start(std::move(promise));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
size_t capacity_;
|
2022-06-11 00:47:00 +02:00
|
|
|
VectorQueue<Promise<Promise<Unit>>> pending_;
|
|
|
|
|
|
|
|
void finish(Result<Unit>) {
|
2022-06-10 15:17:18 +02:00
|
|
|
capacity_++;
|
2022-06-11 00:47:00 +02:00
|
|
|
if (!pending_.empty()) {
|
2022-06-10 15:17:18 +02:00
|
|
|
start(pending_.pop());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-11 00:47:00 +02:00
|
|
|
void start(Promise<Promise<Unit>> promise) {
|
2022-06-10 15:17:18 +02:00
|
|
|
CHECK(capacity_ > 0);
|
|
|
|
capacity_--;
|
|
|
|
promise.set_value(promise_send_closure(actor_id(this), &SemaphoreActor::finish));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Semaphore {
|
|
|
|
explicit Semaphore(size_t capacity) {
|
2022-06-14 15:11:38 +02:00
|
|
|
semaphore_ = create_actor<SemaphoreActor>("Semaphore", capacity).release();
|
2022-06-10 15:17:18 +02:00
|
|
|
}
|
|
|
|
|
2022-06-11 00:47:00 +02:00
|
|
|
void execute(Promise<Promise<Unit>> promise) {
|
2022-06-10 15:17:18 +02:00
|
|
|
send_closure(semaphore_, &SemaphoreActor::execute, std::move(promise));
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2022-06-11 00:47:00 +02:00
|
|
|
ActorId<SemaphoreActor> semaphore_;
|
2022-06-10 15:17:18 +02:00
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class GenAuthKeyActor final : public Actor {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2018-11-18 13:33:06 +01:00
|
|
|
GenAuthKeyActor(Slice name, unique_ptr<mtproto::AuthKeyHandshake> handshake,
|
|
|
|
unique_ptr<mtproto::AuthKeyHandshakeContext> context,
|
2018-09-27 03:19:03 +02:00
|
|
|
Promise<unique_ptr<mtproto::RawConnection>> connection_promise,
|
|
|
|
Promise<unique_ptr<mtproto::AuthKeyHandshake>> handshake_promise,
|
2018-12-31 20:04:05 +01:00
|
|
|
std::shared_ptr<Session::Callback> callback)
|
2018-11-18 13:33:06 +01:00
|
|
|
: name_(name.str())
|
|
|
|
, handshake_(std::move(handshake))
|
2018-12-31 20:04:05 +01:00
|
|
|
, context_(std::move(context))
|
|
|
|
, connection_promise_(std::move(connection_promise))
|
|
|
|
, handshake_promise_(std::move(handshake_promise))
|
|
|
|
, callback_(std::move(callback)) {
|
2022-08-10 16:03:38 +02:00
|
|
|
if (actor_count_.fetch_add(1, std::memory_order_relaxed) == MIN_HIGH_LOAD_ACTOR_COUNT - 1) {
|
|
|
|
LOG(WARNING) << "Number of GenAuthKeyActor exceeded high-load threshold";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
GenAuthKeyActor(const GenAuthKeyActor &) = delete;
|
|
|
|
GenAuthKeyActor &operator=(const GenAuthKeyActor &) = delete;
|
|
|
|
GenAuthKeyActor(GenAuthKeyActor &&) = delete;
|
|
|
|
GenAuthKeyActor &operator=(GenAuthKeyActor &&) = delete;
|
|
|
|
~GenAuthKeyActor() final {
|
|
|
|
if (actor_count_.fetch_sub(1, std::memory_order_relaxed) == MIN_HIGH_LOAD_ACTOR_COUNT) {
|
|
|
|
LOG(WARNING) << "Number of GenAuthKeyActor became lower than high-load threshold";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool is_high_loaded() {
|
|
|
|
return actor_count_.load(std::memory_order_relaxed) >= MIN_HIGH_LOAD_ACTOR_COUNT;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void on_network(uint32 network_generation) {
|
|
|
|
if (network_generation_ != network_generation) {
|
|
|
|
send_closure(std::move(child_), &mtproto::HandshakeActor::close);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-11-18 13:33:06 +01:00
|
|
|
string name_;
|
2018-12-31 20:04:05 +01:00
|
|
|
uint32 network_generation_ = 0;
|
2018-09-27 03:19:03 +02:00
|
|
|
unique_ptr<mtproto::AuthKeyHandshake> handshake_;
|
|
|
|
unique_ptr<mtproto::AuthKeyHandshakeContext> context_;
|
|
|
|
Promise<unique_ptr<mtproto::RawConnection>> connection_promise_;
|
|
|
|
Promise<unique_ptr<mtproto::AuthKeyHandshake>> handshake_promise_;
|
2018-12-31 20:04:05 +01:00
|
|
|
std::shared_ptr<Session::Callback> callback_;
|
2019-05-01 15:13:48 +02:00
|
|
|
CancellationTokenSource cancellation_token_source_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
ActorOwn<mtproto::HandshakeActor> child_;
|
2022-06-10 15:17:18 +02:00
|
|
|
Promise<Unit> finish_promise_;
|
2022-06-09 16:12:59 +02:00
|
|
|
|
2022-08-10 16:03:38 +02:00
|
|
|
static constexpr size_t MIN_HIGH_LOAD_ACTOR_COUNT = 100;
|
|
|
|
static std::atomic<size_t> actor_count_;
|
|
|
|
|
2022-06-10 15:17:18 +02:00
|
|
|
static TD_THREAD_LOCAL Semaphore *semaphore_;
|
|
|
|
Semaphore &get_handshake_semaphore() {
|
2022-07-20 09:56:48 +02:00
|
|
|
auto old_context = set_context(std::make_shared<ActorContext>());
|
|
|
|
auto old_tag = set_tag(string());
|
2022-06-10 15:17:18 +02:00
|
|
|
init_thread_local<Semaphore>(semaphore_, 50);
|
2022-07-20 09:56:48 +02:00
|
|
|
set_context(std::move(old_context));
|
|
|
|
set_tag(std::move(old_tag));
|
2022-06-10 15:17:18 +02:00
|
|
|
return *semaphore_;
|
2022-06-09 16:12:59 +02:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final {
|
2018-02-20 01:14:47 +01:00
|
|
|
// Bug in Android clang and MSVC
|
2018-12-31 20:04:05 +01:00
|
|
|
// std::tuple<Result<int>> b(std::forward_as_tuple(Result<int>()));
|
2022-06-10 15:17:18 +02:00
|
|
|
get_handshake_semaphore().execute(promise_send_closure(actor_id(this), &GenAuthKeyActor::do_start_up));
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-06-11 00:47:00 +02:00
|
|
|
void do_start_up(Result<Promise<Unit>> r_finish_promise) {
|
2022-06-10 15:17:18 +02:00
|
|
|
if (r_finish_promise.is_error()) {
|
|
|
|
LOG(ERROR) << "Unexpected error: " << r_finish_promise.error();
|
|
|
|
} else {
|
|
|
|
finish_promise_ = r_finish_promise.move_as_ok();
|
2022-06-09 11:10:38 +02:00
|
|
|
}
|
2019-05-06 20:53:39 +02:00
|
|
|
callback_->request_raw_connection(
|
|
|
|
nullptr, PromiseCreator::cancellable_lambda(
|
|
|
|
cancellation_token_source_.get_cancellation_token(),
|
|
|
|
[actor_id = actor_id(this)](Result<unique_ptr<mtproto::RawConnection>> r_raw_connection) {
|
|
|
|
send_closure(actor_id, &GenAuthKeyActor::on_connection, std::move(r_raw_connection), false);
|
|
|
|
}));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void hangup() final {
|
2018-05-30 18:38:17 +02:00
|
|
|
if (connection_promise_) {
|
2021-05-31 20:51:48 +02:00
|
|
|
connection_promise_.set_error(Status::Error(1, "Canceled"));
|
2018-05-30 18:38:17 +02:00
|
|
|
}
|
|
|
|
if (handshake_promise_) {
|
2021-05-31 20:51:48 +02:00
|
|
|
handshake_promise_.set_error(Status::Error(1, "Canceled"));
|
2018-05-30 18:38:17 +02:00
|
|
|
}
|
2018-05-31 09:04:05 +02:00
|
|
|
stop();
|
2018-05-30 18:38:17 +02:00
|
|
|
}
|
|
|
|
|
2018-09-27 03:19:03 +02:00
|
|
|
void on_connection(Result<unique_ptr<mtproto::RawConnection>> r_raw_connection, bool dummy) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (r_raw_connection.is_error()) {
|
|
|
|
connection_promise_.set_error(r_raw_connection.move_as_error());
|
|
|
|
handshake_promise_.set_value(std::move(handshake_));
|
|
|
|
return;
|
|
|
|
}
|
2018-05-14 21:00:38 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
auto raw_connection = r_raw_connection.move_as_ok();
|
2018-05-14 21:00:38 +02:00
|
|
|
VLOG(dc) << "Receive raw connection " << raw_connection.get();
|
2021-03-26 16:04:01 +01:00
|
|
|
network_generation_ = raw_connection->extra().extra;
|
2018-12-31 20:04:05 +01:00
|
|
|
child_ = create_actor_on_scheduler<mtproto::HandshakeActor>(
|
2018-11-18 13:33:06 +01:00
|
|
|
PSLICE() << name_ + "::HandshakeActor", G()->get_slow_net_scheduler_id(), std::move(handshake_),
|
|
|
|
std::move(raw_connection), std::move(context_), 10, std::move(connection_promise_),
|
|
|
|
std::move(handshake_promise_));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
};
|
2018-05-14 21:00:38 +02:00
|
|
|
|
2022-08-10 16:03:38 +02:00
|
|
|
std::atomic<size_t> GenAuthKeyActor::actor_count_;
|
2022-06-10 15:17:18 +02:00
|
|
|
TD_THREAD_LOCAL Semaphore *GenAuthKeyActor::semaphore_{};
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace detail
|
|
|
|
|
2020-08-17 15:13:18 +02:00
|
|
|
void Session::PriorityQueue::push(NetQueryPtr query) {
|
2020-08-17 16:35:34 +02:00
|
|
|
auto priority = query->priority();
|
|
|
|
queries_[priority].push(std::move(query));
|
2020-08-17 15:13:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
NetQueryPtr Session::PriorityQueue::pop() {
|
2020-08-18 17:37:10 +02:00
|
|
|
CHECK(!empty());
|
|
|
|
auto it = queries_.begin();
|
2020-08-17 15:13:18 +02:00
|
|
|
auto res = it->second.pop();
|
|
|
|
if (it->second.empty()) {
|
2020-08-18 17:37:10 +02:00
|
|
|
queries_.erase(it);
|
2020-08-17 15:13:18 +02:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Session::PriorityQueue::empty() const {
|
|
|
|
return queries_.empty();
|
|
|
|
}
|
|
|
|
|
2019-12-18 01:47:51 +01:00
|
|
|
Session::Session(unique_ptr<Callback> callback, std::shared_ptr<AuthDataShared> shared_auth_data, int32 raw_dc_id,
|
2023-07-17 18:21:18 +02:00
|
|
|
int32 dc_id, bool is_primary, bool is_main, bool use_pfs, bool persist_tmp_auth_key, bool is_cdn,
|
|
|
|
bool need_destroy, const mtproto::AuthKey &tmp_auth_key,
|
|
|
|
const vector<mtproto::ServerSalt> &server_salts)
|
2023-01-14 22:25:22 +01:00
|
|
|
: raw_dc_id_(raw_dc_id)
|
|
|
|
, dc_id_(dc_id)
|
|
|
|
, is_primary_(is_primary)
|
|
|
|
, is_main_(is_main)
|
2023-07-17 18:21:18 +02:00
|
|
|
, persist_tmp_auth_key_(use_pfs && persist_tmp_auth_key)
|
2023-01-14 22:25:22 +01:00
|
|
|
, is_cdn_(is_cdn)
|
|
|
|
, need_destroy_(need_destroy) {
|
2021-11-22 09:52:09 +01:00
|
|
|
VLOG(dc) << "Start connection " << tag("need_destroy", need_destroy_);
|
|
|
|
if (need_destroy_) {
|
2017-12-29 21:34:39 +01:00
|
|
|
use_pfs = false;
|
|
|
|
CHECK(!is_cdn);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
shared_auth_data_ = std::move(shared_auth_data);
|
|
|
|
auth_data_.set_use_pfs(use_pfs);
|
|
|
|
auth_data_.set_main_auth_key(shared_auth_data_->get_auth_key());
|
2020-01-08 18:05:08 +01:00
|
|
|
// auth_data_.break_main_auth_key();
|
2023-05-14 20:58:54 +02:00
|
|
|
auth_data_.reset_server_time_difference(shared_auth_data_->get_server_time_difference());
|
2023-01-19 10:27:23 +01:00
|
|
|
auto now = Time::now();
|
|
|
|
auth_data_.set_future_salts(shared_auth_data_->get_future_salts(), now);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (use_pfs && !tmp_auth_key.empty()) {
|
|
|
|
auth_data_.set_tmp_auth_key(tmp_auth_key);
|
2022-06-20 13:18:25 +02:00
|
|
|
if (is_main_) {
|
|
|
|
registered_temp_auth_key_ = TempAuthKeyWatchdog::register_auth_key_id(auth_data_.get_tmp_auth_key().id());
|
|
|
|
}
|
2023-01-19 10:27:23 +01:00
|
|
|
auth_data_.set_future_salts(server_salts, now);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
uint64 session_id = 0;
|
2019-02-04 17:39:09 +01:00
|
|
|
do {
|
|
|
|
Random::secure_bytes(reinterpret_cast<uint8 *>(&session_id), sizeof(session_id));
|
|
|
|
} while (session_id == 0);
|
2019-02-04 16:44:29 +01:00
|
|
|
auth_data_.set_session_id(session_id);
|
2020-01-07 13:42:04 +01:00
|
|
|
use_pfs_ = use_pfs;
|
2018-05-24 18:54:57 +02:00
|
|
|
LOG(WARNING) << "Generate new session_id " << session_id << " for " << (use_pfs ? "temp " : "")
|
2019-12-18 01:47:51 +01:00
|
|
|
<< (is_cdn ? "CDN " : "") << "auth key " << auth_data_.get_auth_key().id() << " for "
|
|
|
|
<< (is_main_ ? "main " : "") << "DC" << dc_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
callback_ = std::shared_ptr<Callback>(callback.release());
|
|
|
|
|
2021-11-22 12:36:59 +01:00
|
|
|
main_connection_.connection_id_ = 0;
|
|
|
|
long_poll_connection_.connection_id_ = 1;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
if (is_cdn) {
|
2023-07-26 11:48:59 +02:00
|
|
|
auth_data_.set_header(G()->mtproto_header().get_anonymous_header());
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
2023-07-26 11:48:59 +02:00
|
|
|
auth_data_.set_header(G()->mtproto_header().get_default_header());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2023-01-19 10:27:23 +01:00
|
|
|
last_activity_timestamp_ = now;
|
|
|
|
last_success_timestamp_ = now - 366 * 86400;
|
|
|
|
last_bind_success_timestamp_ = now - 366 * 86400;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2022-08-10 16:03:38 +02:00
|
|
|
bool Session::is_high_loaded() {
|
|
|
|
return detail::GenAuthKeyActor::is_high_loaded();
|
|
|
|
}
|
|
|
|
|
2018-11-01 18:08:20 +01:00
|
|
|
bool Session::can_destroy_auth_key() const {
|
2017-12-29 21:34:39 +01:00
|
|
|
return need_destroy_;
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void Session::start_up() {
|
2021-07-04 04:58:54 +02:00
|
|
|
class StateCallback final : public StateManager::Callback {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
|
|
|
explicit StateCallback(ActorId<Session> session) : session_(std::move(session)) {
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
bool on_network(NetType network_type, uint32 network_generation) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
send_closure(session_, &Session::on_network, network_type != NetType::None, network_generation);
|
|
|
|
return session_.is_alive();
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
bool on_online(bool online_flag) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
send_closure(session_, &Session::on_online, online_flag);
|
|
|
|
return session_.is_alive();
|
|
|
|
}
|
2021-12-02 13:10:51 +01:00
|
|
|
bool on_logging_out(bool logging_out_flag) final {
|
|
|
|
send_closure(session_, &Session::on_logging_out, logging_out_flag);
|
|
|
|
return session_.is_alive();
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
ActorId<Session> session_;
|
|
|
|
};
|
|
|
|
send_closure(G()->state_manager(), &StateManager::add_callback, make_unique<StateCallback>(actor_id(this)));
|
|
|
|
|
|
|
|
yield();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Session::on_network(bool network_flag, uint32 network_generation) {
|
|
|
|
was_on_network_ = true;
|
|
|
|
network_flag_ = network_flag;
|
|
|
|
if (network_generation_ != network_generation) {
|
|
|
|
network_generation_ = network_generation;
|
|
|
|
connection_close(&main_connection_);
|
|
|
|
connection_close(&long_poll_connection_);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto &handshake_info : handshake_info_) {
|
|
|
|
if (handshake_info.actor_.empty()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
send_closure(handshake_info.actor_, &detail::GenAuthKeyActor::on_network, network_generation);
|
|
|
|
}
|
|
|
|
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Session::on_online(bool online_flag) {
|
2023-01-14 21:08:50 +01:00
|
|
|
LOG(DEBUG) << "Set online flag to " << online_flag;
|
2018-12-31 20:04:05 +01:00
|
|
|
online_flag_ = online_flag;
|
2023-01-19 10:27:23 +01:00
|
|
|
connection_online_update(Time::now(), true);
|
2018-12-31 20:04:05 +01:00
|
|
|
loop();
|
|
|
|
}
|
|
|
|
|
2021-12-02 13:10:51 +01:00
|
|
|
void Session::on_logging_out(bool logging_out_flag) {
|
2023-01-14 21:08:50 +01:00
|
|
|
LOG(DEBUG) << "Set logging out flag to " << logging_out_flag;
|
2021-12-02 13:10:51 +01:00
|
|
|
logging_out_flag_ = logging_out_flag;
|
2023-01-19 10:27:23 +01:00
|
|
|
connection_online_update(Time::now(), true);
|
2021-12-02 13:10:51 +01:00
|
|
|
loop();
|
|
|
|
}
|
|
|
|
|
2023-01-19 10:27:23 +01:00
|
|
|
void Session::connection_online_update(double now, bool force) {
|
2023-01-14 22:25:22 +01:00
|
|
|
bool new_connection_online_flag =
|
2023-01-19 10:27:23 +01:00
|
|
|
(online_flag_ || logging_out_flag_) && (has_queries() || last_activity_timestamp_ + 10 > now || is_primary_);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (connection_online_flag_ == new_connection_online_flag && !force) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
connection_online_flag_ = new_connection_online_flag;
|
2018-05-14 21:00:38 +02:00
|
|
|
VLOG(dc) << "Set connection_online " << connection_online_flag_;
|
2021-11-22 12:36:59 +01:00
|
|
|
if (main_connection_.connection_) {
|
2023-01-14 22:25:22 +01:00
|
|
|
main_connection_.connection_->set_online(connection_online_flag_, is_primary_);
|
2018-11-20 13:07:27 +01:00
|
|
|
}
|
2021-11-22 12:36:59 +01:00
|
|
|
if (long_poll_connection_.connection_) {
|
2023-01-14 22:25:22 +01:00
|
|
|
long_poll_connection_.connection_->set_online(connection_online_flag_, is_primary_);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Session::send(NetQueryPtr &&query) {
|
|
|
|
last_activity_timestamp_ = Time::now();
|
|
|
|
|
2023-02-20 11:11:39 +01:00
|
|
|
// query->debug(PSTRING() << get_name() << ": received from SessionProxy");
|
2019-02-04 16:44:29 +01:00
|
|
|
query->set_session_id(auth_data_.get_session_id());
|
2023-02-26 12:22:31 +01:00
|
|
|
VLOG(net_query) << "Receive query " << query;
|
2018-12-31 20:04:05 +01:00
|
|
|
if (query->update_is_ready()) {
|
|
|
|
return_query(std::move(query));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
add_query(std::move(query));
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
|
2020-01-07 13:42:04 +01:00
|
|
|
void Session::on_bind_result(NetQueryPtr query) {
|
2020-01-07 23:27:12 +01:00
|
|
|
LOG(INFO) << "Receive answer to BindKey: " << query;
|
|
|
|
being_binded_tmp_auth_key_id_ = 0;
|
|
|
|
last_bind_query_id_ = 0;
|
2020-01-08 00:09:22 +01:00
|
|
|
|
|
|
|
Status status;
|
2018-12-31 20:04:05 +01:00
|
|
|
if (query->is_error()) {
|
2023-08-30 17:25:26 +02:00
|
|
|
status = query->move_as_error();
|
2020-01-07 13:42:04 +01:00
|
|
|
if (status.code() == 400 && status.message() == "ENCRYPTED_MESSAGE_INVALID") {
|
2022-09-29 13:50:25 +02:00
|
|
|
auto server_time = G()->server_time();
|
|
|
|
auto auth_key_creation_date = auth_data_.get_main_auth_key().created_at();
|
|
|
|
auto auth_key_age = server_time - auth_key_creation_date;
|
|
|
|
auto is_server_time_reliable = G()->is_server_time_reliable();
|
|
|
|
auto last_success_time = use_pfs_ ? last_bind_success_timestamp_ : last_success_timestamp_;
|
|
|
|
auto now = Time::now();
|
|
|
|
bool has_immunity =
|
|
|
|
!is_server_time_reliable || auth_key_age < 60 || (auth_key_age > 86400 && last_success_time > now - 86400);
|
|
|
|
auto debug = PSTRING() << ". Server time is " << server_time << ", auth key created at " << auth_key_creation_date
|
2023-01-03 13:06:12 +01:00
|
|
|
<< ", is_server_time_reliable = " << is_server_time_reliable << ", use_pfs = " << use_pfs_
|
2022-09-29 13:50:25 +02:00
|
|
|
<< ", last_success_time = " << last_success_time << ", now = " << now;
|
2020-01-07 13:42:04 +01:00
|
|
|
if (!use_pfs_) {
|
|
|
|
if (has_immunity) {
|
2022-09-29 13:50:25 +02:00
|
|
|
LOG(WARNING) << "Do not drop main key, because it was created too recently" << debug;
|
2020-01-07 13:42:04 +01:00
|
|
|
} else {
|
2022-09-29 13:50:25 +02:00
|
|
|
LOG(WARNING) << "Drop main key because check with temporary key failed" << debug;
|
2020-01-07 13:42:04 +01:00
|
|
|
auth_data_.drop_main_auth_key();
|
|
|
|
on_auth_key_updated();
|
2022-05-02 20:17:06 +02:00
|
|
|
G()->log_out("Main authorization key is invalid");
|
2020-01-07 13:42:04 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (has_immunity) {
|
2022-09-29 13:50:25 +02:00
|
|
|
LOG(WARNING) << "Do not validate main key, because it was created too recently" << debug;
|
2020-01-07 13:42:04 +01:00
|
|
|
} else {
|
|
|
|
need_check_main_key_ = true;
|
|
|
|
auth_data_.set_use_pfs(false);
|
2023-02-26 12:22:31 +01:00
|
|
|
LOG(WARNING) << "Receive ENCRYPTED_MESSAGE_INVALID error, validate main key" << debug;
|
2020-01-07 13:42:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
2023-08-30 17:25:26 +02:00
|
|
|
auto answer = query->move_as_ok();
|
|
|
|
auto r_flag = fetch_result<telegram_api::auth_bindTempAuthKey>(answer);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (r_flag.is_error()) {
|
|
|
|
status = r_flag.move_as_error();
|
2020-01-07 16:16:57 +01:00
|
|
|
} else if (!r_flag.ok()) {
|
|
|
|
status = Status::Error("Returned false");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (status.is_ok()) {
|
2020-01-07 16:16:57 +01:00
|
|
|
LOG(INFO) << "Bound temp auth key " << auth_data_.get_tmp_auth_key().id();
|
2018-12-31 20:04:05 +01:00
|
|
|
auth_data_.on_bind();
|
2022-05-03 15:06:14 +02:00
|
|
|
last_bind_success_timestamp_ = Time::now();
|
2018-12-31 20:04:05 +01:00
|
|
|
on_tmp_auth_key_updated();
|
2022-10-09 13:15:54 +02:00
|
|
|
} else if (status.message() == "DispatchTtlError") {
|
2020-01-08 00:09:22 +01:00
|
|
|
LOG(INFO) << "Resend bind auth key " << auth_data_.get_tmp_auth_key().id() << " request after DispatchTtlError";
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
|
|
|
LOG(ERROR) << "BindKey failed: " << status;
|
2020-01-07 13:42:04 +01:00
|
|
|
connection_close(&main_connection_);
|
|
|
|
connection_close(&long_poll_connection_);
|
|
|
|
}
|
|
|
|
|
|
|
|
yield();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Session::on_check_key_result(NetQueryPtr query) {
|
2020-01-07 23:27:12 +01:00
|
|
|
LOG(INFO) << "Receive answer to GetNearestDc: " << query;
|
|
|
|
being_checked_main_auth_key_id_ = 0;
|
|
|
|
last_check_query_id_ = 0;
|
2020-01-08 00:09:22 +01:00
|
|
|
|
|
|
|
Status status;
|
2020-01-07 13:42:04 +01:00
|
|
|
if (query->is_error()) {
|
2023-08-30 17:25:26 +02:00
|
|
|
status = query->move_as_error();
|
2020-01-07 13:42:04 +01:00
|
|
|
} else {
|
2023-08-30 17:25:26 +02:00
|
|
|
auto answer = query->move_as_ok();
|
|
|
|
auto r_flag = fetch_result<telegram_api::help_getNearestDc>(answer);
|
2020-01-07 13:42:04 +01:00
|
|
|
if (r_flag.is_error()) {
|
|
|
|
status = r_flag.move_as_error();
|
|
|
|
}
|
|
|
|
}
|
2022-10-09 13:15:54 +02:00
|
|
|
if (status.is_ok() || status.code() != -404) {
|
2020-01-07 13:42:04 +01:00
|
|
|
LOG(INFO) << "Check main key ok";
|
|
|
|
need_check_main_key_ = false;
|
|
|
|
auth_data_.set_use_pfs(true);
|
|
|
|
} else {
|
|
|
|
LOG(ERROR) << "Check main key failed: " << status;
|
|
|
|
connection_close(&main_connection_);
|
|
|
|
connection_close(&long_poll_connection_);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2020-01-07 13:42:04 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
yield();
|
|
|
|
}
|
|
|
|
|
2020-01-07 13:42:04 +01:00
|
|
|
void Session::on_result(NetQueryPtr query) {
|
|
|
|
CHECK(UniqueId::extract_type(query->id()) == UniqueId::BindKey);
|
2020-01-07 23:27:12 +01:00
|
|
|
if (last_bind_query_id_ == query->id()) {
|
2020-01-07 13:42:04 +01:00
|
|
|
return on_bind_result(std::move(query));
|
|
|
|
}
|
2020-01-07 23:27:12 +01:00
|
|
|
if (last_check_query_id_ == query->id()) {
|
2020-01-07 13:42:04 +01:00
|
|
|
return on_check_key_result(std::move(query));
|
|
|
|
}
|
|
|
|
query->clear();
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void Session::return_query(NetQueryPtr &&query) {
|
|
|
|
last_activity_timestamp_ = Time::now();
|
|
|
|
|
|
|
|
query->set_session_id(0);
|
2019-03-15 10:00:18 +01:00
|
|
|
callback_->on_result(std::move(query));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Session::flush_pending_invoke_after_queries() {
|
|
|
|
while (!pending_invoke_after_queries_.empty()) {
|
|
|
|
auto &query = pending_invoke_after_queries_.front();
|
2020-08-17 15:13:18 +02:00
|
|
|
pending_queries_.push(std::move(query));
|
2018-12-31 20:04:05 +01:00
|
|
|
pending_invoke_after_queries_.pop_front();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Session::close() {
|
2018-11-06 12:06:08 +01:00
|
|
|
LOG(INFO) << "Close session (external)";
|
2018-12-31 20:04:05 +01:00
|
|
|
close_flag_ = true;
|
|
|
|
connection_close(&main_connection_);
|
|
|
|
connection_close(&long_poll_connection_);
|
|
|
|
|
|
|
|
for (auto &it : sent_queries_) {
|
2023-08-30 23:47:39 +02:00
|
|
|
auto &query = it.second.net_query_;
|
2018-12-31 20:04:05 +01:00
|
|
|
query->set_message_id(0);
|
2020-08-17 15:13:18 +02:00
|
|
|
pending_queries_.push(std::move(query));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
sent_queries_.clear();
|
|
|
|
sent_containers_.clear();
|
|
|
|
|
|
|
|
flush_pending_invoke_after_queries();
|
|
|
|
CHECK(sent_queries_.empty());
|
|
|
|
while (!pending_queries_.empty()) {
|
2020-08-17 15:13:18 +02:00
|
|
|
auto query = pending_queries_.pop();
|
2018-12-31 20:04:05 +01:00
|
|
|
query->set_error_resend();
|
|
|
|
return_query(std::move(query));
|
|
|
|
}
|
|
|
|
|
|
|
|
callback_->on_closed();
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Session::hangup() {
|
|
|
|
LOG(DEBUG) << "HANGUP";
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Session::raw_event(const Event::Raw &event) {
|
|
|
|
auto message_id = event.u64;
|
|
|
|
auto it = sent_queries_.find(message_id);
|
|
|
|
if (it == sent_queries_.end()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dec_container(it->first, &it->second);
|
|
|
|
mark_as_known(it->first, &it->second);
|
|
|
|
|
2023-08-30 23:47:39 +02:00
|
|
|
auto query = std::move(it->second.net_query_);
|
2023-08-30 16:58:37 +02:00
|
|
|
LOG(DEBUG) << "Drop answer for " << query;
|
2018-12-31 20:04:05 +01:00
|
|
|
query->set_message_id(0);
|
|
|
|
sent_queries_.erase(it);
|
|
|
|
return_query(std::move(query));
|
|
|
|
|
2021-11-22 12:36:59 +01:00
|
|
|
if (main_connection_.state_ == ConnectionInfo::State::Ready) {
|
|
|
|
main_connection_.connection_->cancel_answer(message_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
|
|
|
to_cancel_.push_back(message_id);
|
|
|
|
}
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Connection::Callback **/
|
|
|
|
void Session::on_connected() {
|
|
|
|
if (is_main_) {
|
2021-09-16 18:09:39 +02:00
|
|
|
connection_token_ =
|
|
|
|
mtproto::ConnectionManager::connection(static_cast<ActorId<mtproto::ConnectionManager>>(G()->state_manager()));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
2021-09-16 18:09:39 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
Status Session::on_pong() {
|
|
|
|
constexpr int MAX_QUERY_TIMEOUT = 60;
|
|
|
|
constexpr int MIN_CONNECTION_ACTIVE = 60;
|
|
|
|
if (current_info_ == &main_connection_ &&
|
2021-11-22 12:36:59 +01:00
|
|
|
Timestamp::at(current_info_->created_at_ + MIN_CONNECTION_ACTIVE).is_in_past()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
Status status;
|
|
|
|
if (!unknown_queries_.empty()) {
|
2023-08-20 20:00:42 +02:00
|
|
|
status = Status::Error(PSLICE() << "No state info for " << unknown_queries_.size() << " queries from auth key "
|
|
|
|
<< auth_data_.get_auth_key().id() << " for "
|
2023-01-19 10:27:23 +01:00
|
|
|
<< format::as_time(Time::now() - current_info_->created_at_));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (!sent_queries_list_.empty()) {
|
|
|
|
for (auto it = sent_queries_list_.prev; it != &sent_queries_list_; it = it->prev) {
|
|
|
|
auto query = Query::from_list_node(it);
|
|
|
|
if (Timestamp::at(query->sent_at_ + MAX_QUERY_TIMEOUT).is_in_past()) {
|
|
|
|
if (status.is_ok()) {
|
2023-08-30 23:47:39 +02:00
|
|
|
status = Status::Error(PSLICE()
|
|
|
|
<< "No answer from auth key " << auth_data_.get_auth_key().id() << " for "
|
|
|
|
<< query->net_query_ << " for " << format::as_time(Time::now() - query->sent_at_));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2023-08-30 23:47:39 +02:00
|
|
|
query->is_acknowledged_ = false;
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (status.is_error()) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
2021-09-16 18:09:39 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void Session::on_auth_key_updated() {
|
|
|
|
shared_auth_data_->set_auth_key(auth_data_.get_main_auth_key());
|
|
|
|
}
|
2021-09-16 18:09:39 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void Session::on_tmp_auth_key_updated() {
|
|
|
|
callback_->on_tmp_auth_key_updated(auth_data_.get_tmp_auth_key());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Session::on_server_salt_updated() {
|
|
|
|
if (auth_data_.use_pfs()) {
|
2018-06-13 17:17:27 +02:00
|
|
|
callback_->on_server_salt_updated(auth_data_.get_future_salts());
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
shared_auth_data_->set_future_salts(auth_data_.get_future_salts());
|
|
|
|
}
|
|
|
|
|
2023-05-14 20:58:54 +02:00
|
|
|
void Session::on_server_time_difference_updated(bool force) {
|
|
|
|
shared_auth_data_->update_server_time_difference(auth_data_.get_server_time_difference(), force);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Session::on_closed(Status status) {
|
|
|
|
if (!close_flag_ && is_main_) {
|
|
|
|
connection_token_.reset();
|
|
|
|
}
|
2021-11-22 12:36:59 +01:00
|
|
|
auto raw_connection = current_info_->connection_->move_as_raw_connection();
|
2019-05-09 21:57:35 +02:00
|
|
|
Scheduler::unsubscribe_before_close(raw_connection->get_poll_info().get_pollable_fd_ref());
|
|
|
|
raw_connection->close();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
if (status.is_error() && status.code() == -404) {
|
|
|
|
if (auth_data_.use_pfs()) {
|
|
|
|
LOG(WARNING) << "Invalidate tmp_key";
|
|
|
|
auth_data_.drop_tmp_auth_key();
|
|
|
|
on_tmp_auth_key_updated();
|
|
|
|
yield();
|
|
|
|
} else if (is_cdn_) {
|
2020-01-08 00:09:22 +01:00
|
|
|
LOG(WARNING) << "Invalidate CDN tmp_key";
|
2018-12-31 20:04:05 +01:00
|
|
|
auth_data_.drop_main_auth_key();
|
|
|
|
on_auth_key_updated();
|
2021-10-17 12:07:20 +02:00
|
|
|
on_session_failed(status.clone());
|
2017-12-29 21:34:39 +01:00
|
|
|
} else if (need_destroy_) {
|
2023-07-11 12:42:47 +02:00
|
|
|
LOG(WARNING) << "Session connection was closed, because main auth_key has been successfully destroyed";
|
2017-12-29 21:34:39 +01:00
|
|
|
auth_data_.drop_main_auth_key();
|
|
|
|
on_auth_key_updated();
|
2020-01-07 13:42:04 +01:00
|
|
|
} else {
|
2020-01-07 16:16:57 +01:00
|
|
|
// log out if has error and or 1 minute is passed from start, or 1 minute has passed since auth_key creation
|
2020-01-07 13:42:04 +01:00
|
|
|
if (!use_pfs_) {
|
2020-01-08 18:05:08 +01:00
|
|
|
LOG(WARNING) << "Use PFS to check main key";
|
2020-01-07 13:42:04 +01:00
|
|
|
auth_data_.set_use_pfs(true);
|
|
|
|
} else if (need_check_main_key_) {
|
|
|
|
LOG(WARNING) << "Invalidate main key";
|
|
|
|
auth_data_.drop_main_auth_key();
|
|
|
|
on_auth_key_updated();
|
2022-05-02 20:17:06 +02:00
|
|
|
G()->log_out("Main PFS authorization key is invalid");
|
2023-07-11 12:42:47 +02:00
|
|
|
} else {
|
|
|
|
LOG(WARNING) << "Session connection was closed: " << status << ' ' << current_info_->connection_->get_name();
|
2020-01-07 13:42:04 +01:00
|
|
|
}
|
2020-01-08 18:05:08 +01:00
|
|
|
yield();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2023-07-11 12:42:47 +02:00
|
|
|
} else {
|
|
|
|
if (status.is_error()) {
|
|
|
|
LOG(WARNING) << "Session connection with " << sent_queries_.size() << " pending requests was closed: " << status
|
|
|
|
<< ' ' << current_info_->connection_->get_name();
|
|
|
|
} else {
|
|
|
|
LOG(INFO) << "Session connection with " << sent_queries_.size() << " pending requests was closed: " << status
|
|
|
|
<< ' ' << current_info_->connection_->get_name();
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2020-01-07 16:16:57 +01:00
|
|
|
// resend all queries without ack
|
2018-12-31 20:04:05 +01:00
|
|
|
for (auto it = sent_queries_.begin(); it != sent_queries_.end();) {
|
2023-08-30 23:47:39 +02:00
|
|
|
if (!it->second.is_acknowledged_ && it->second.connection_id_ == current_info_->connection_id_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
// container vector leak otherwise
|
|
|
|
cleanup_container(it->first, &it->second);
|
|
|
|
|
|
|
|
// mark query as unknown
|
|
|
|
if (status.is_error() && status.code() == 500) {
|
|
|
|
cleanup_container(it->first, &it->second);
|
|
|
|
mark_as_known(it->first, &it->second);
|
|
|
|
|
2023-08-30 23:47:39 +02:00
|
|
|
auto &query = it->second.net_query_;
|
2019-02-21 16:58:20 +01:00
|
|
|
VLOG(net_query) << "Resend query (on_disconnected, no ack) " << query;
|
2018-12-31 20:04:05 +01:00
|
|
|
query->set_message_id(0);
|
2018-07-08 01:47:46 +02:00
|
|
|
query->set_error(Status::Error(500, PSLICE() << "Session failed: " << status.message()),
|
2021-11-22 12:36:59 +01:00
|
|
|
current_info_->connection_->get_name().str());
|
2018-12-31 20:04:05 +01:00
|
|
|
return_query(std::move(query));
|
|
|
|
it = sent_queries_.erase(it);
|
|
|
|
} else {
|
|
|
|
mark_as_unknown(it->first, &it->second);
|
2018-04-23 00:14:49 +02:00
|
|
|
++it;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-22 12:36:59 +01:00
|
|
|
current_info_->connection_.reset();
|
|
|
|
current_info_->state_ = ConnectionInfo::State::Empty;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2023-09-01 15:40:19 +02:00
|
|
|
void Session::on_new_session_created(uint64 unique_id, uint64 first_message_id) {
|
2023-09-03 01:07:12 +02:00
|
|
|
LOG(INFO) << "New session " << unique_id << " created with first message_id " << format::as_hex(first_message_id);
|
2023-01-03 13:06:12 +01:00
|
|
|
if (!use_pfs_ && !auth_data_.use_pfs()) {
|
2022-05-03 14:33:09 +02:00
|
|
|
last_success_timestamp_ = Time::now();
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
if (is_main_) {
|
2018-06-04 23:12:34 +02:00
|
|
|
LOG(DEBUG) << "Sending updatesTooLong to force getDifference";
|
2020-03-15 23:01:14 +01:00
|
|
|
BufferSlice packet(4);
|
2023-02-02 15:40:47 +01:00
|
|
|
as<int32>(packet.as_mutable_slice().begin()) = telegram_api::updatesTooLong::ID;
|
2021-09-10 16:32:39 +02:00
|
|
|
last_activity_timestamp_ = Time::now();
|
2023-01-25 15:48:04 +01:00
|
|
|
callback_->on_update(std::move(packet), auth_data_.get_auth_key().id());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2023-09-01 15:40:19 +02:00
|
|
|
auto first_query_it = sent_queries_.find(first_message_id);
|
|
|
|
if (first_query_it != sent_queries_.end()) {
|
|
|
|
first_message_id = first_query_it->second.container_message_id_;
|
2023-09-03 01:07:12 +02:00
|
|
|
LOG(INFO) << "Update first_message_id to container's " << format::as_hex(first_message_id);
|
2023-09-01 15:40:19 +02:00
|
|
|
} else {
|
2023-09-03 01:07:12 +02:00
|
|
|
LOG(INFO) << "Failed to find query " << format::as_hex(first_message_id) << " from the new session";
|
2023-09-01 15:40:19 +02:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
for (auto it = sent_queries_.begin(); it != sent_queries_.end();) {
|
|
|
|
Query *query_ptr = &it->second;
|
2023-08-30 23:47:39 +02:00
|
|
|
if (query_ptr->container_message_id_ < first_message_id) {
|
2018-12-31 20:04:05 +01:00
|
|
|
// container vector leak otherwise
|
2023-09-01 15:40:19 +02:00
|
|
|
cleanup_container(it->first, query_ptr);
|
|
|
|
mark_as_known(it->first, query_ptr);
|
2023-09-01 15:48:25 +02:00
|
|
|
resend_query(std::move(query_ptr->net_query_));
|
2018-12-31 20:04:05 +01:00
|
|
|
it = sent_queries_.erase(it);
|
|
|
|
} else {
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Session::on_session_failed(Status status) {
|
|
|
|
if (status.is_error()) {
|
|
|
|
LOG(WARNING) << "Session failed: " << status;
|
|
|
|
} else {
|
|
|
|
LOG(INFO) << "Session will be closed soon";
|
|
|
|
}
|
|
|
|
// this connection will be closed soon
|
|
|
|
close_flag_ = true;
|
|
|
|
callback_->on_failed();
|
|
|
|
}
|
|
|
|
|
2023-01-15 21:27:58 +01:00
|
|
|
void Session::on_container_sent(uint64 container_message_id, vector<uint64> message_ids) {
|
|
|
|
CHECK(container_message_id != 0);
|
2022-02-09 22:59:52 +01:00
|
|
|
|
2023-01-15 21:27:58 +01:00
|
|
|
td::remove_if(message_ids, [&](uint64 message_id) {
|
|
|
|
auto it = sent_queries_.find(message_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (it == sent_queries_.end()) {
|
|
|
|
return true; // remove
|
|
|
|
}
|
2023-08-30 23:47:39 +02:00
|
|
|
it->second.container_message_id_ = container_message_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
return false;
|
|
|
|
});
|
2023-01-15 21:27:58 +01:00
|
|
|
if (message_ids.empty()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
2023-01-15 21:27:58 +01:00
|
|
|
auto size = message_ids.size();
|
|
|
|
sent_containers_.emplace(container_message_id, ContainerInfo{size, std::move(message_ids)});
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2023-01-15 21:27:58 +01:00
|
|
|
void Session::on_message_ack(uint64 message_id) {
|
|
|
|
on_message_ack_impl(message_id, 1);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2020-08-13 15:57:28 +02:00
|
|
|
|
2023-01-15 21:27:58 +01:00
|
|
|
void Session::on_message_ack_impl(uint64 container_message_id, int32 type) {
|
|
|
|
auto cit = sent_containers_.find(container_message_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (cit != sent_containers_.end()) {
|
|
|
|
auto container_info = std::move(cit->second);
|
2022-02-09 22:59:52 +01:00
|
|
|
sent_containers_.erase(cit);
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
for (auto message_id : container_info.message_ids) {
|
|
|
|
on_message_ack_impl_inner(message_id, type, true);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-15 21:27:58 +01:00
|
|
|
on_message_ack_impl_inner(container_message_id, type, false);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2023-01-15 21:27:58 +01:00
|
|
|
void Session::on_message_ack_impl_inner(uint64 message_id, int32 type, bool in_container) {
|
|
|
|
auto it = sent_queries_.find(message_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (it == sent_queries_.end()) {
|
|
|
|
return;
|
|
|
|
}
|
2023-08-30 23:47:39 +02:00
|
|
|
VLOG(net_query) << "Ack " << it->second.net_query_;
|
|
|
|
it->second.is_acknowledged_ = true;
|
2019-08-16 13:57:41 +02:00
|
|
|
{
|
2023-08-30 23:47:39 +02:00
|
|
|
auto lock = it->second.net_query_->lock();
|
|
|
|
it->second.net_query_->get_data_unsafe().ack_state_ |= type;
|
2019-08-16 13:57:41 +02:00
|
|
|
}
|
2023-08-30 23:47:39 +02:00
|
|
|
it->second.net_query_->quick_ack_promise_.set_value(Unit());
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!in_container) {
|
2023-01-15 21:27:58 +01:00
|
|
|
cleanup_container(message_id, &it->second);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
mark_as_known(it->first, &it->second);
|
|
|
|
}
|
|
|
|
|
2023-01-15 21:27:58 +01:00
|
|
|
void Session::dec_container(uint64 container_message_id, Query *query) {
|
2023-08-30 23:47:39 +02:00
|
|
|
if (query->container_message_id_ == container_message_id) {
|
2018-12-31 20:04:05 +01:00
|
|
|
// message was sent without any container
|
|
|
|
return;
|
|
|
|
}
|
2023-08-30 23:47:39 +02:00
|
|
|
auto it = sent_containers_.find(query->container_message_id_);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (it == sent_containers_.end()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
CHECK(it->second.ref_cnt > 0);
|
|
|
|
it->second.ref_cnt--;
|
|
|
|
if (it->second.ref_cnt == 0) {
|
|
|
|
sent_containers_.erase(it);
|
|
|
|
}
|
|
|
|
}
|
2020-08-13 15:57:28 +02:00
|
|
|
|
2023-01-15 21:27:58 +01:00
|
|
|
void Session::cleanup_container(uint64 container_message_id, Query *query) {
|
2023-08-30 23:47:39 +02:00
|
|
|
if (query->container_message_id_ == container_message_id) {
|
2018-12-31 20:04:05 +01:00
|
|
|
// message was sent without any container
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we can forget container now, since we have an answer for its part.
|
|
|
|
// TODO: we can do it only for one element per container
|
2023-08-30 23:47:39 +02:00
|
|
|
sent_containers_.erase(query->container_message_id_);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2023-01-15 21:27:58 +01:00
|
|
|
void Session::mark_as_known(uint64 message_id, Query *query) {
|
2019-08-16 13:57:41 +02:00
|
|
|
{
|
2023-08-30 23:47:39 +02:00
|
|
|
auto lock = query->net_query_->lock();
|
|
|
|
query->net_query_->get_data_unsafe().unknown_state_ = false;
|
2019-08-16 13:57:41 +02:00
|
|
|
}
|
2023-08-30 23:47:39 +02:00
|
|
|
if (!query->is_unknown_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
2023-08-30 23:47:39 +02:00
|
|
|
VLOG(net_query) << "Mark as known " << query->net_query_;
|
|
|
|
query->is_unknown_ = false;
|
2023-01-15 21:27:58 +01:00
|
|
|
unknown_queries_.erase(message_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (unknown_queries_.empty()) {
|
|
|
|
flush_pending_invoke_after_queries();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-15 21:27:58 +01:00
|
|
|
void Session::mark_as_unknown(uint64 message_id, Query *query) {
|
2019-08-16 13:57:41 +02:00
|
|
|
{
|
2023-08-30 23:47:39 +02:00
|
|
|
auto lock = query->net_query_->lock();
|
|
|
|
query->net_query_->get_data_unsafe().unknown_state_ = true;
|
2019-08-16 13:57:41 +02:00
|
|
|
}
|
2023-08-30 23:47:39 +02:00
|
|
|
if (query->is_unknown_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
2023-08-30 23:47:39 +02:00
|
|
|
VLOG(net_query) << "Mark as unknown " << query->net_query_;
|
|
|
|
query->is_unknown_ = true;
|
2023-01-15 21:27:58 +01:00
|
|
|
CHECK(message_id != 0);
|
|
|
|
unknown_queries_.insert(message_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-09-10 15:31:04 +02:00
|
|
|
Status Session::on_update(BufferSlice packet) {
|
|
|
|
if (is_cdn_) {
|
2023-05-04 14:13:31 +02:00
|
|
|
return Status::Error("Receive an update from a CDN connection");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2021-09-10 15:31:04 +02:00
|
|
|
|
2023-01-03 13:06:12 +01:00
|
|
|
if (!use_pfs_ && !auth_data_.use_pfs()) {
|
2022-05-03 15:06:14 +02:00
|
|
|
last_success_timestamp_ = Time::now();
|
|
|
|
}
|
2021-09-10 16:32:39 +02:00
|
|
|
last_activity_timestamp_ = Time::now();
|
2023-01-25 15:48:04 +01:00
|
|
|
callback_->on_update(std::move(packet), auth_data_.get_auth_key().id());
|
2021-09-10 15:31:04 +02:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2023-01-15 21:27:58 +01:00
|
|
|
Status Session::on_message_result_ok(uint64 message_id, BufferSlice packet, size_t original_size) {
|
2021-04-13 15:52:41 +02:00
|
|
|
last_success_timestamp_ = Time::now();
|
2019-12-18 01:47:51 +01:00
|
|
|
|
|
|
|
TlParser parser(packet.as_slice());
|
2023-03-14 19:45:23 +01:00
|
|
|
int32 response_id = parser.fetch_int();
|
2019-12-18 01:47:51 +01:00
|
|
|
|
2023-01-15 21:27:58 +01:00
|
|
|
auto it = sent_queries_.find(message_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (it == sent_queries_.end()) {
|
2023-01-15 21:27:58 +01:00
|
|
|
LOG(DEBUG) << "Drop result to " << tag("message_id", format::as_hex(message_id))
|
2023-03-14 19:45:23 +01:00
|
|
|
<< tag("original_size", original_size) << tag("response_id", format::as_hex(response_id));
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2020-11-18 15:49:29 +01:00
|
|
|
if (original_size > 16 * 1024) {
|
|
|
|
dropped_size_ += original_size;
|
2018-12-31 20:04:05 +01:00
|
|
|
if (dropped_size_ > (256 * 1024)) {
|
|
|
|
auto dropped_size = dropped_size_;
|
|
|
|
dropped_size_ = 0;
|
|
|
|
return Status::Error(
|
2022-01-24 13:01:35 +01:00
|
|
|
2, PSLICE() << "Too many dropped packets " << tag("total_size", format::as_size(dropped_size)));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
2019-12-18 01:47:51 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
auth_data_.on_api_response();
|
|
|
|
Query *query_ptr = &it->second;
|
2023-08-30 23:47:39 +02:00
|
|
|
VLOG(net_query) << "Return query result " << query_ptr->net_query_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-12-18 01:47:51 +01:00
|
|
|
if (!parser.get_error()) {
|
2020-08-11 15:31:50 +02:00
|
|
|
// Steal authorization information.
|
|
|
|
// It is a dirty hack, yep.
|
2023-03-14 19:45:23 +01:00
|
|
|
if (response_id == telegram_api::auth_authorization::ID ||
|
|
|
|
response_id == telegram_api::auth_loginTokenSuccess::ID ||
|
|
|
|
response_id == telegram_api::auth_sentCodeSuccess::ID) {
|
2023-08-30 23:47:39 +02:00
|
|
|
if (query_ptr->net_query_->tl_constructor() != telegram_api::auth_importAuthorization::ID) {
|
2019-12-18 01:47:51 +01:00
|
|
|
G()->net_query_dispatcher().set_main_dc_id(raw_dc_id_);
|
|
|
|
}
|
|
|
|
auth_data_.set_auth_flag(true);
|
|
|
|
shared_auth_data_->set_auth_key(auth_data_.get_main_auth_key());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-15 21:27:58 +01:00
|
|
|
cleanup_container(message_id, query_ptr);
|
|
|
|
mark_as_known(message_id, query_ptr);
|
2023-08-30 23:47:39 +02:00
|
|
|
query_ptr->net_query_->on_net_read(original_size);
|
|
|
|
query_ptr->net_query_->set_ok(std::move(packet));
|
|
|
|
query_ptr->net_query_->set_message_id(0);
|
|
|
|
return_query(std::move(query_ptr->net_query_));
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
sent_queries_.erase(it);
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2023-01-15 21:27:58 +01:00
|
|
|
void Session::on_message_result_error(uint64 message_id, int error_code, string message) {
|
2021-07-26 19:04:43 +02:00
|
|
|
if (!check_utf8(message)) {
|
|
|
|
LOG(ERROR) << "Receive invalid error message \"" << message << '"';
|
|
|
|
message = "INVALID_UTF8_ERROR_MESSAGE";
|
|
|
|
}
|
|
|
|
if (error_code <= -10000 || error_code >= 10000 || error_code == 0) {
|
|
|
|
LOG(ERROR) << "Receive invalid error code " << error_code << " with message \"" << message << '"';
|
|
|
|
error_code = 500;
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
// UNAUTHORIZED
|
2021-07-26 19:04:43 +02:00
|
|
|
if (error_code == 401 && message != "SESSION_PASSWORD_NEEDED") {
|
2022-01-26 19:50:42 +01:00
|
|
|
if (auth_data_.use_pfs() && message == CSlice("AUTH_KEY_PERM_EMPTY")) {
|
|
|
|
LOG(INFO) << "Receive AUTH_KEY_PERM_EMPTY in session " << auth_data_.get_session_id() << " for auth key "
|
2019-09-02 18:24:50 +02:00
|
|
|
<< auth_data_.get_tmp_auth_key().id();
|
2021-11-22 09:52:09 +01:00
|
|
|
// temporary key can be dropped any time
|
2018-12-31 20:04:05 +01:00
|
|
|
auth_data_.drop_tmp_auth_key();
|
|
|
|
on_tmp_auth_key_updated();
|
|
|
|
error_code = 500;
|
|
|
|
} else {
|
2022-01-26 19:50:42 +01:00
|
|
|
if (auth_data_.use_pfs() && !is_main_) {
|
|
|
|
// temporary key can be dropped any time
|
|
|
|
auth_data_.drop_tmp_auth_key();
|
|
|
|
on_tmp_auth_key_updated();
|
|
|
|
error_code = 500;
|
|
|
|
}
|
|
|
|
|
2021-11-22 09:52:09 +01:00
|
|
|
bool can_drop_main_auth_key_without_logging_out = is_cdn_;
|
2022-01-26 19:50:42 +01:00
|
|
|
if (!is_main_ && G()->net_query_dispatcher().get_main_dc_id().get_raw_id() != raw_dc_id_) {
|
|
|
|
can_drop_main_auth_key_without_logging_out = true;
|
2021-11-22 09:52:09 +01:00
|
|
|
}
|
2022-01-26 19:50:42 +01:00
|
|
|
LOG(INFO) << "Receive 401, " << message << " in session " << auth_data_.get_session_id() << " for auth key "
|
|
|
|
<< auth_data_.get_auth_key().id() << ", PFS = " << auth_data_.use_pfs() << ", is_main = " << is_main_
|
|
|
|
<< ", can_drop_main_auth_key_without_logging_out = " << can_drop_main_auth_key_without_logging_out;
|
2021-11-22 09:52:09 +01:00
|
|
|
if (can_drop_main_auth_key_without_logging_out) {
|
|
|
|
auth_data_.drop_main_auth_key();
|
|
|
|
on_auth_key_updated();
|
|
|
|
error_code = 500;
|
|
|
|
} else {
|
|
|
|
if (message == "USER_DEACTIVATED_BAN") {
|
2023-07-05 15:18:45 +02:00
|
|
|
LOG(ERROR) << "Your phone number was banned for suspicious activity. If you think that this is a mistake, "
|
|
|
|
"please try to log in from an official mobile app and send a email to recover the account by "
|
|
|
|
"following instructions provided by the app.";
|
2021-11-22 09:52:09 +01:00
|
|
|
}
|
|
|
|
auth_data_.set_auth_flag(false);
|
2022-05-02 19:59:07 +02:00
|
|
|
G()->log_out(message);
|
2021-11-22 09:52:09 +01:00
|
|
|
shared_auth_data_->set_auth_key(auth_data_.get_main_auth_key());
|
|
|
|
on_session_failed(Status::OK());
|
2019-09-02 18:24:50 +02:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
2022-02-07 20:05:01 +01:00
|
|
|
if (error_code == 400 && (message == "CONNECTION_NOT_INITED" || message == "CONNECTION_LAYER_INVALID")) {
|
|
|
|
LOG(WARNING) << "Receive " << message;
|
|
|
|
auth_data_.on_connection_not_inited();
|
|
|
|
error_code = 500;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2023-01-15 21:27:58 +01:00
|
|
|
if (message_id == 0) {
|
2023-02-26 11:08:03 +01:00
|
|
|
LOG(ERROR) << "Receive an error without message_id";
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-12 15:56:06 +02:00
|
|
|
if (error_code < 0) {
|
2022-03-14 12:08:04 +01:00
|
|
|
LOG(WARNING) << "Receive MTProto error " << error_code << " : " << message << " in session "
|
|
|
|
<< auth_data_.get_session_id() << " for auth key " << auth_data_.get_auth_key().id() << " with "
|
|
|
|
<< sent_queries_.size() << " pending requests";
|
2020-08-12 15:56:06 +02:00
|
|
|
}
|
2023-01-15 21:27:58 +01:00
|
|
|
auto it = sent_queries_.find(message_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (it == sent_queries_.end()) {
|
2022-05-03 15:17:14 +02:00
|
|
|
current_info_->connection_->force_ack();
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Query *query_ptr = &it->second;
|
2023-08-30 23:47:39 +02:00
|
|
|
VLOG(net_query) << "Return query error " << query_ptr->net_query_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2023-01-15 21:27:58 +01:00
|
|
|
cleanup_container(message_id, query_ptr);
|
|
|
|
mark_as_known(message_id, query_ptr);
|
2023-08-30 23:47:39 +02:00
|
|
|
query_ptr->net_query_->set_error(Status::Error(error_code, message), current_info_->connection_->get_name().str());
|
|
|
|
query_ptr->net_query_->set_message_id(0);
|
|
|
|
return_query(std::move(query_ptr->net_query_));
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
sent_queries_.erase(it);
|
|
|
|
}
|
|
|
|
|
2023-01-15 21:27:58 +01:00
|
|
|
void Session::on_message_failed_inner(uint64 message_id, bool in_container) {
|
|
|
|
LOG(INFO) << "Message inner failed " << message_id;
|
|
|
|
auto it = sent_queries_.find(message_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (it == sent_queries_.end()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Query *query_ptr = &it->second;
|
|
|
|
if (!in_container) {
|
2023-01-15 21:27:58 +01:00
|
|
|
cleanup_container(message_id, query_ptr);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2023-01-15 21:27:58 +01:00
|
|
|
mark_as_known(message_id, query_ptr);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2023-08-30 23:47:39 +02:00
|
|
|
query_ptr->net_query_->debug_send_failed();
|
|
|
|
resend_query(std::move(query_ptr->net_query_));
|
2018-12-31 20:04:05 +01:00
|
|
|
sent_queries_.erase(it);
|
|
|
|
}
|
|
|
|
|
2023-01-15 21:27:58 +01:00
|
|
|
void Session::on_message_failed(uint64 message_id, Status status) {
|
|
|
|
LOG(INFO) << "Message failed: " << tag("message_id", message_id) << tag("status", status);
|
2018-12-31 20:04:05 +01:00
|
|
|
status.ignore();
|
|
|
|
|
2023-01-15 21:27:58 +01:00
|
|
|
auto cit = sent_containers_.find(message_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (cit != sent_containers_.end()) {
|
|
|
|
auto container_info = std::move(cit->second);
|
2022-02-09 22:59:52 +01:00
|
|
|
sent_containers_.erase(cit);
|
|
|
|
|
2023-01-15 21:27:58 +01:00
|
|
|
for (auto contained_message_id : container_info.message_ids) {
|
|
|
|
on_message_failed_inner(contained_message_id, true);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-15 21:27:58 +01:00
|
|
|
on_message_failed_inner(message_id, false);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2023-09-01 15:11:21 +02:00
|
|
|
void Session::on_message_info(uint64 message_id, int32 state, uint64 answer_message_id, int32 answer_size,
|
|
|
|
int32 source) {
|
2023-01-15 21:27:58 +01:00
|
|
|
auto it = sent_queries_.find(message_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (it != sent_queries_.end()) {
|
2023-08-30 23:47:39 +02:00
|
|
|
if (it->second.net_query_->update_is_ready()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
dec_container(it->first, &it->second);
|
|
|
|
mark_as_known(it->first, &it->second);
|
|
|
|
|
2023-08-30 23:47:39 +02:00
|
|
|
auto query = std::move(it->second.net_query_);
|
2018-12-31 20:04:05 +01:00
|
|
|
query->set_message_id(0);
|
|
|
|
sent_queries_.erase(it);
|
|
|
|
return_query(std::move(query));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2023-01-15 21:27:58 +01:00
|
|
|
if (message_id != 0) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (it == sent_queries_.end()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
switch (state & 7) {
|
|
|
|
case 1:
|
|
|
|
case 2:
|
|
|
|
case 3:
|
2023-03-05 13:51:00 +01:00
|
|
|
return on_message_failed(message_id,
|
|
|
|
Status::Error("Message wasn't received by the server and must be re-sent"));
|
2018-12-31 20:04:05 +01:00
|
|
|
case 0:
|
2023-01-15 21:27:58 +01:00
|
|
|
if (answer_message_id == 0) {
|
|
|
|
LOG(ERROR) << "Unexpected message_info.state == 0 " << tag("message_id", message_id) << tag("state", state)
|
|
|
|
<< tag("answer_message_id", answer_message_id);
|
|
|
|
return on_message_failed(message_id, Status::Error("Unexpected message_info.state == 0"));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
// fallthrough
|
|
|
|
case 4:
|
2023-09-01 15:11:21 +02:00
|
|
|
CHECK(0 <= source && source <= 3);
|
|
|
|
on_message_ack_impl(message_id, (answer_message_id ? 2 : 0) | (((state | source) & ((1 << 28) - 1)) << 2));
|
2018-12-31 20:04:05 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LOG(ERROR) << "Invalid message info " << tag("state", state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-15 21:27:58 +01:00
|
|
|
// ok, we are waiting for result of message_id. let's ask to resend it
|
|
|
|
if (answer_message_id != 0) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (it != sent_queries_.end()) {
|
2023-08-30 16:58:37 +02:00
|
|
|
VLOG_IF(net_query, message_id != 0) << "Resend answer " << tag("answer_message_id", answer_message_id)
|
2023-08-30 23:47:39 +02:00
|
|
|
<< tag("answer_size", answer_size) << it->second.net_query_;
|
|
|
|
it->second.net_query_->debug(PSTRING() << get_name() << ": resend answer");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2023-01-15 21:27:58 +01:00
|
|
|
current_info_->connection_->resend_answer(answer_message_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
2020-08-13 15:57:28 +02:00
|
|
|
|
2017-12-29 21:34:39 +01:00
|
|
|
Status Session::on_destroy_auth_key() {
|
|
|
|
auth_data_.drop_main_auth_key();
|
|
|
|
on_auth_key_updated();
|
|
|
|
return Status::Error("Close because of on_destroy_auth_key");
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
bool Session::has_queries() const {
|
|
|
|
return !pending_invoke_after_queries_.empty() || !pending_queries_.empty() || !sent_queries_.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Session::resend_query(NetQueryPtr query) {
|
2023-09-01 15:48:25 +02:00
|
|
|
VLOG(net_query) << "Resend " << query;
|
|
|
|
query->set_message_id(0);
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
if (UniqueId::extract_type(query->id()) == UniqueId::BindKey) {
|
|
|
|
query->set_error_resend();
|
|
|
|
return_query(std::move(query));
|
|
|
|
} else {
|
|
|
|
add_query(std::move(query));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Session::add_query(NetQueryPtr &&net_query) {
|
2023-09-01 15:48:25 +02:00
|
|
|
CHECK(UniqueId::extract_type(net_query->id()) != UniqueId::BindKey);
|
2023-02-20 11:11:39 +01:00
|
|
|
net_query->debug(PSTRING() << get_name() << ": pending");
|
2020-08-17 15:13:18 +02:00
|
|
|
pending_queries_.push(std::move(net_query));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Session::connection_send_query(ConnectionInfo *info, NetQueryPtr &&net_query, uint64 message_id) {
|
2021-11-22 12:36:59 +01:00
|
|
|
CHECK(info->state_ == ConnectionInfo::State::Ready);
|
2018-12-31 20:04:05 +01:00
|
|
|
current_info_ = info;
|
|
|
|
|
|
|
|
if (net_query->update_is_ready()) {
|
|
|
|
return return_query(std::move(net_query));
|
|
|
|
}
|
|
|
|
|
2022-01-28 15:06:54 +01:00
|
|
|
Span<NetQueryRef> invoke_after = net_query->invoke_after();
|
2023-09-01 15:48:25 +02:00
|
|
|
vector<uint64> invoke_after_ids;
|
2022-01-28 15:06:54 +01:00
|
|
|
for (auto &ref : invoke_after) {
|
|
|
|
auto invoke_after_id = ref->message_id();
|
|
|
|
if (ref->session_id() != auth_data_.get_session_id() || invoke_after_id == 0) {
|
2018-12-31 20:04:05 +01:00
|
|
|
net_query->set_error_resend_invoke_after();
|
|
|
|
return return_query(std::move(net_query));
|
|
|
|
}
|
2022-01-28 15:06:54 +01:00
|
|
|
invoke_after_ids.push_back(invoke_after_id);
|
|
|
|
}
|
|
|
|
if (!invoke_after.empty()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!unknown_queries_.empty()) {
|
2023-02-20 11:11:39 +01:00
|
|
|
net_query->debug(PSTRING() << get_name() << ": wait unknown query to invoke after it");
|
2018-12-31 20:04:05 +01:00
|
|
|
pending_invoke_after_queries_.push_back(std::move(net_query));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-19 10:27:23 +01:00
|
|
|
auto now = Time::now();
|
2021-11-22 09:52:09 +01:00
|
|
|
bool immediately_fail_query = false;
|
|
|
|
if (!immediately_fail_query) {
|
2023-02-20 11:11:39 +01:00
|
|
|
net_query->debug(PSTRING() << get_name() << ": send to an MTProto connection");
|
2021-11-22 09:52:09 +01:00
|
|
|
auto r_message_id =
|
2021-11-22 12:36:59 +01:00
|
|
|
info->connection_->send_query(net_query->query().clone(), net_query->gzip_flag() == NetQuery::GzipFlag::On,
|
2022-01-28 15:06:54 +01:00
|
|
|
message_id, invoke_after_ids, static_cast<bool>(net_query->quick_ack_promise_));
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-11-22 09:52:09 +01:00
|
|
|
net_query->on_net_write(net_query->query().size());
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-11-22 09:52:09 +01:00
|
|
|
if (r_message_id.is_error()) {
|
|
|
|
LOG(FATAL) << "Failed to send query: " << r_message_id.error();
|
|
|
|
}
|
|
|
|
message_id = r_message_id.ok();
|
|
|
|
} else {
|
|
|
|
if (message_id == 0) {
|
2023-01-19 10:27:23 +01:00
|
|
|
message_id = auth_data_.next_message_id(now);
|
2021-11-22 09:52:09 +01:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2023-08-30 16:58:37 +02:00
|
|
|
net_query->set_message_id(message_id);
|
|
|
|
VLOG(net_query) << "Send query to connection " << net_query
|
2023-01-15 21:27:58 +01:00
|
|
|
<< tag("invoke_after", transform(invoke_after_ids, [](auto message_id) {
|
|
|
|
return PSTRING() << format::as_hex(message_id);
|
|
|
|
}));
|
2019-08-16 13:57:41 +02:00
|
|
|
{
|
|
|
|
auto lock = net_query->lock();
|
2020-06-10 01:03:09 +02:00
|
|
|
net_query->get_data_unsafe().unknown_state_ = false;
|
|
|
|
net_query->get_data_unsafe().ack_state_ = 0;
|
2019-08-16 13:57:41 +02:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!net_query->cancel_slot_.empty()) {
|
2019-02-21 16:58:20 +01:00
|
|
|
LOG(DEBUG) << "Set event for net_query cancellation " << tag("message_id", format::as_hex(message_id));
|
2018-12-31 20:04:05 +01:00
|
|
|
net_query->cancel_slot_.set_event(EventCreator::raw(actor_id(), message_id));
|
|
|
|
}
|
2023-01-19 10:27:23 +01:00
|
|
|
auto status =
|
|
|
|
sent_queries_.emplace(message_id, Query{message_id, std::move(net_query), main_connection_.connection_id_, now});
|
2022-02-03 12:34:10 +01:00
|
|
|
LOG_CHECK(status.second) << message_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
sent_queries_list_.put(status.first->second.get_list_node());
|
|
|
|
if (!status.second) {
|
2019-05-13 18:34:23 +02:00
|
|
|
LOG(FATAL) << "Duplicate message_id [message_id = " << message_id << "]";
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2021-11-22 09:52:09 +01:00
|
|
|
if (immediately_fail_query) {
|
|
|
|
on_message_result_error(message_id, 401, "TEST_ERROR");
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2023-01-19 10:27:23 +01:00
|
|
|
void Session::connection_open(ConnectionInfo *info, double now, bool ask_info) {
|
2021-11-22 12:36:59 +01:00
|
|
|
CHECK(info->state_ == ConnectionInfo::State::Empty);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!network_flag_) {
|
|
|
|
return;
|
|
|
|
}
|
2023-01-19 10:27:23 +01:00
|
|
|
if (!auth_data_.has_auth_key(now)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
2021-11-22 12:36:59 +01:00
|
|
|
info->ask_info_ = ask_info;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-11-22 12:36:59 +01:00
|
|
|
info->state_ = ConnectionInfo::State::Connecting;
|
2019-05-01 15:13:48 +02:00
|
|
|
info->cancellation_token_source_ = CancellationTokenSource{};
|
2018-12-31 20:04:05 +01:00
|
|
|
// NB: rely on constant location of info
|
2018-06-13 18:20:42 +02:00
|
|
|
auto promise = PromiseCreator::cancellable_lambda(
|
2019-05-01 15:13:48 +02:00
|
|
|
info->cancellation_token_source_.get_cancellation_token(),
|
2023-07-19 21:41:27 +02:00
|
|
|
[actor_id = actor_id(this), info](Result<unique_ptr<mtproto::RawConnection>> res) {
|
2018-12-31 20:04:05 +01:00
|
|
|
send_closure(actor_id, &Session::connection_open_finish, info, std::move(res));
|
|
|
|
});
|
|
|
|
|
|
|
|
if (cached_connection_) {
|
2018-05-14 21:00:38 +02:00
|
|
|
VLOG(dc) << "Reuse cached connection";
|
2018-12-31 20:04:05 +01:00
|
|
|
promise.set_value(std::move(cached_connection_));
|
|
|
|
} else {
|
2018-05-14 21:00:38 +02:00
|
|
|
VLOG(dc) << "Request new connection";
|
2019-05-06 20:53:39 +02:00
|
|
|
unique_ptr<mtproto::AuthData> auth_data;
|
2023-01-19 10:27:23 +01:00
|
|
|
if (auth_data_.use_pfs() && auth_data_.has_auth_key(now)) {
|
2019-06-27 17:46:25 +02:00
|
|
|
// auth_data = make_unique<mtproto::AuthData>(auth_data_);
|
2019-05-06 20:53:39 +02:00
|
|
|
}
|
|
|
|
callback_->request_raw_connection(std::move(auth_data), std::move(promise));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2023-01-19 10:27:23 +01:00
|
|
|
info->wakeup_at_ = now + 1000;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2018-09-27 03:19:03 +02:00
|
|
|
void Session::connection_add(unique_ptr<mtproto::RawConnection> raw_connection) {
|
2018-05-14 21:00:38 +02:00
|
|
|
VLOG(dc) << "Cache connection " << raw_connection.get();
|
2018-12-31 20:04:05 +01:00
|
|
|
cached_connection_ = std::move(raw_connection);
|
|
|
|
cached_connection_timestamp_ = Time::now();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Session::connection_check_mode(ConnectionInfo *info) {
|
2021-11-22 12:36:59 +01:00
|
|
|
if (close_flag_ || info->state_ != ConnectionInfo::State::Ready) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
2021-11-22 12:36:59 +01:00
|
|
|
if (info->mode_ != mode_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
LOG(WARNING) << "Close connection because of outdated mode_";
|
|
|
|
connection_close(info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Session::connection_open_finish(ConnectionInfo *info,
|
2018-09-27 03:19:03 +02:00
|
|
|
Result<unique_ptr<mtproto::RawConnection>> r_raw_connection) {
|
2021-11-22 12:36:59 +01:00
|
|
|
if (close_flag_ || info->state_ != ConnectionInfo::State::Connecting) {
|
2018-05-14 21:00:38 +02:00
|
|
|
VLOG(dc) << "Ignore raw connection while closing";
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
current_info_ = info;
|
|
|
|
if (r_raw_connection.is_error()) {
|
|
|
|
LOG(WARNING) << "Failed to open socket: " << r_raw_connection.error();
|
2021-11-22 12:36:59 +01:00
|
|
|
info->state_ = ConnectionInfo::State::Empty;
|
2018-12-31 20:04:05 +01:00
|
|
|
yield();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto raw_connection = r_raw_connection.move_as_ok();
|
2018-05-14 21:00:38 +02:00
|
|
|
VLOG(dc) << "Receive raw connection " << raw_connection.get();
|
2021-03-26 16:04:01 +01:00
|
|
|
if (raw_connection->extra().extra != network_generation_) {
|
2023-02-26 12:22:31 +01:00
|
|
|
LOG(WARNING) << "Receive RawConnection with old network_generation";
|
2021-11-22 12:36:59 +01:00
|
|
|
info->state_ = ConnectionInfo::State::Empty;
|
2018-12-31 20:04:05 +01:00
|
|
|
yield();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-30 19:01:18 +02:00
|
|
|
Mode expected_mode =
|
|
|
|
raw_connection->get_transport_type().type == mtproto::TransportType::Http ? Mode::Http : Mode::Tcp;
|
2018-12-31 20:04:05 +01:00
|
|
|
if (mode_ != expected_mode) {
|
2018-05-14 21:00:38 +02:00
|
|
|
VLOG(dc) << "Change mode " << mode_ << "--->" << expected_mode;
|
2018-12-31 20:04:05 +01:00
|
|
|
mode_ = expected_mode;
|
2021-11-22 12:36:59 +01:00
|
|
|
if (info->connection_id_ == 1 && mode_ != Mode::Http) {
|
2023-01-15 10:00:26 +01:00
|
|
|
LOG(WARNING) << "Receive TCP connection for long poll connection";
|
2018-12-31 20:04:05 +01:00
|
|
|
connection_add(std::move(raw_connection));
|
2021-11-22 12:36:59 +01:00
|
|
|
info->state_ = ConnectionInfo::State::Empty;
|
2018-12-31 20:04:05 +01:00
|
|
|
yield();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mtproto::SessionConnection::Mode mode;
|
|
|
|
Slice mode_name;
|
|
|
|
if (mode_ == Mode::Tcp) {
|
|
|
|
mode = mtproto::SessionConnection::Mode::Tcp;
|
2023-01-15 09:25:54 +01:00
|
|
|
mode_name = Slice("TCP");
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
2021-11-22 12:36:59 +01:00
|
|
|
if (info->connection_id_ == 0) {
|
2018-12-31 20:04:05 +01:00
|
|
|
mode = mtproto::SessionConnection::Mode::Http;
|
2023-01-15 09:25:54 +01:00
|
|
|
mode_name = Slice("HTTP");
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
|
|
|
mode = mtproto::SessionConnection::Mode::HttpLongPoll;
|
2023-01-15 09:25:54 +01:00
|
|
|
mode_name = Slice("LongPoll");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
2021-03-26 16:04:01 +01:00
|
|
|
auto name = PSTRING() << get_name() << "::Connect::" << mode_name << "::" << raw_connection->extra().debug_str;
|
2019-02-21 16:58:20 +01:00
|
|
|
LOG(INFO) << "Finished to open connection " << name;
|
2021-11-22 12:36:59 +01:00
|
|
|
info->connection_ = make_unique<mtproto::SessionConnection>(mode, std::move(raw_connection), &auth_data_);
|
2019-08-01 15:03:18 +02:00
|
|
|
if (can_destroy_auth_key()) {
|
2021-11-22 12:36:59 +01:00
|
|
|
info->connection_->destroy_key();
|
|
|
|
}
|
2023-01-14 22:25:22 +01:00
|
|
|
info->connection_->set_online(connection_online_flag_, is_primary_);
|
2021-11-22 12:36:59 +01:00
|
|
|
info->connection_->set_name(name);
|
|
|
|
Scheduler::subscribe(info->connection_->get_poll_info().extract_pollable_fd(this));
|
|
|
|
info->mode_ = mode_;
|
|
|
|
info->state_ = ConnectionInfo::State::Ready;
|
2023-01-19 10:27:23 +01:00
|
|
|
info->created_at_ = Time::now();
|
|
|
|
info->wakeup_at_ = info->created_at_ + 10;
|
2020-07-31 19:19:18 +02:00
|
|
|
if (unknown_queries_.size() > MAX_INFLIGHT_QUERIES) {
|
2022-01-24 13:01:35 +01:00
|
|
|
LOG(ERROR) << "With current limits `Too many queries with unknown state` error must be impossible";
|
|
|
|
on_session_failed(Status::Error("Too many queries with unknown state"));
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
2021-11-22 12:36:59 +01:00
|
|
|
if (info->ask_info_) {
|
2023-01-15 21:27:58 +01:00
|
|
|
for (auto &message_id : unknown_queries_) {
|
|
|
|
info->connection_->get_state_info(message_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2023-01-15 21:27:58 +01:00
|
|
|
for (auto &message_id : to_cancel_) {
|
|
|
|
info->connection_->cancel_answer(message_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
to_cancel_.clear();
|
|
|
|
}
|
|
|
|
yield();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Session::connection_flush(ConnectionInfo *info) {
|
2021-11-22 12:36:59 +01:00
|
|
|
CHECK(info->state_ == ConnectionInfo::State::Ready);
|
2018-12-31 20:04:05 +01:00
|
|
|
current_info_ = info;
|
2021-11-22 12:36:59 +01:00
|
|
|
info->wakeup_at_ = info->connection_->flush(static_cast<mtproto::SessionConnection::Callback *>(this));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Session::connection_close(ConnectionInfo *info) {
|
|
|
|
current_info_ = info;
|
2021-11-22 12:36:59 +01:00
|
|
|
if (info->state_ != ConnectionInfo::State::Ready) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
2021-11-22 12:36:59 +01:00
|
|
|
info->connection_->force_close(static_cast<mtproto::SessionConnection::Callback *>(this));
|
|
|
|
CHECK(info->state_ == ConnectionInfo::State::Empty);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2020-08-13 15:57:28 +02:00
|
|
|
|
2020-01-07 13:42:04 +01:00
|
|
|
bool Session::need_send_check_main_key() const {
|
2020-01-07 23:27:12 +01:00
|
|
|
return need_check_main_key_ && auth_data_.get_main_auth_key().id() != being_checked_main_auth_key_id_;
|
2020-01-07 13:42:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Session::connection_send_check_main_key(ConnectionInfo *info) {
|
|
|
|
if (!need_check_main_key_) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
uint64 key_id = auth_data_.get_main_auth_key().id();
|
2020-01-07 23:27:12 +01:00
|
|
|
if (key_id == being_checked_main_auth_key_id_) {
|
2020-01-07 13:42:04 +01:00
|
|
|
return false;
|
|
|
|
}
|
2021-11-22 12:36:59 +01:00
|
|
|
CHECK(info->state_ != ConnectionInfo::State::Empty);
|
2020-01-07 13:42:04 +01:00
|
|
|
LOG(INFO) << "Check main key";
|
2020-01-07 23:27:12 +01:00
|
|
|
being_checked_main_auth_key_id_ = key_id;
|
|
|
|
last_check_query_id_ = UniqueId::next(UniqueId::BindKey);
|
2022-02-01 17:34:14 +01:00
|
|
|
NetQueryPtr query = G()->net_query_creator().create(last_check_query_id_, telegram_api::help_getNearestDc(), {},
|
|
|
|
DcId::main(), NetQuery::Type::Common, NetQuery::AuthFlag::On);
|
2020-06-10 01:44:54 +02:00
|
|
|
query->dispatch_ttl_ = 0;
|
2020-01-07 13:42:04 +01:00
|
|
|
query->set_callback(actor_shared(this));
|
|
|
|
connection_send_query(info, std::move(query));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-11-01 18:08:20 +01:00
|
|
|
bool Session::need_send_bind_key() const {
|
2020-01-07 23:27:12 +01:00
|
|
|
return auth_data_.use_pfs() && !auth_data_.get_bind_flag() &&
|
|
|
|
auth_data_.get_tmp_auth_key().id() != being_binded_tmp_auth_key_id_;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2020-08-13 15:57:28 +02:00
|
|
|
|
2018-11-01 18:08:20 +01:00
|
|
|
bool Session::need_send_query() const {
|
2020-01-07 13:42:04 +01:00
|
|
|
return !close_flag_ && !need_check_main_key_ && (!auth_data_.use_pfs() || auth_data_.get_bind_flag()) &&
|
|
|
|
!pending_queries_.empty() && !can_destroy_auth_key();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2020-01-07 13:42:04 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
bool Session::connection_send_bind_key(ConnectionInfo *info) {
|
2021-11-22 12:36:59 +01:00
|
|
|
CHECK(info->state_ != ConnectionInfo::State::Empty);
|
2018-12-31 20:04:05 +01:00
|
|
|
uint64 key_id = auth_data_.get_tmp_auth_key().id();
|
2020-01-07 23:27:12 +01:00
|
|
|
if (key_id == being_binded_tmp_auth_key_id_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return false;
|
|
|
|
}
|
2020-01-07 23:27:12 +01:00
|
|
|
being_binded_tmp_auth_key_id_ = key_id;
|
|
|
|
last_bind_query_id_ = UniqueId::next(UniqueId::BindKey);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
int64 perm_auth_key_id = auth_data_.get_main_auth_key().id();
|
|
|
|
int64 nonce = Random::secure_int64();
|
2021-10-19 17:11:16 +02:00
|
|
|
auto expires_at = static_cast<int32>(auth_data_.get_server_time(auth_data_.get_tmp_auth_key().expires_at()));
|
2023-05-14 21:42:58 +02:00
|
|
|
uint64 message_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
BufferSlice encrypted;
|
2021-11-22 12:36:59 +01:00
|
|
|
std::tie(message_id, encrypted) = info->connection_->encrypted_bind(perm_auth_key_id, nonce, expires_at);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
LOG(INFO) << "Bind key: " << tag("tmp", key_id) << tag("perm", static_cast<uint64>(perm_auth_key_id));
|
|
|
|
NetQueryPtr query = G()->net_query_creator().create(
|
2020-01-07 23:27:12 +01:00
|
|
|
last_bind_query_id_,
|
2022-02-01 16:51:20 +01:00
|
|
|
telegram_api::auth_bindTempAuthKey(perm_auth_key_id, nonce, expires_at, std::move(encrypted)), {}, DcId::main(),
|
2020-03-15 23:01:14 +01:00
|
|
|
NetQuery::Type::Common, NetQuery::AuthFlag::On);
|
2020-06-10 01:44:54 +02:00
|
|
|
query->dispatch_ttl_ = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
query->set_callback(actor_shared(this));
|
|
|
|
connection_send_query(info, std::move(query), message_id);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-09-27 03:19:03 +02:00
|
|
|
void Session::on_handshake_ready(Result<unique_ptr<mtproto::AuthKeyHandshake>> r_handshake) {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto handshake_id = narrow_cast<HandshakeId>(get_link_token() - 1);
|
|
|
|
bool is_main = handshake_id == MainAuthKeyHandshake;
|
|
|
|
auto &info = handshake_info_[handshake_id];
|
|
|
|
info.flag_ = false;
|
|
|
|
info.actor_.reset();
|
|
|
|
|
|
|
|
if (r_handshake.is_error()) {
|
|
|
|
LOG(ERROR) << "Handshake failed: " << r_handshake.move_as_error();
|
|
|
|
} else {
|
|
|
|
auto handshake = r_handshake.move_as_ok();
|
|
|
|
if (!handshake->is_ready_for_finish()) {
|
2022-06-09 16:12:59 +02:00
|
|
|
LOG(INFO) << "Handshake is not yet ready";
|
2018-12-31 20:04:05 +01:00
|
|
|
info.handshake_ = std::move(handshake);
|
|
|
|
} else {
|
|
|
|
if (is_main) {
|
2020-01-08 18:30:07 +01:00
|
|
|
auth_data_.set_main_auth_key(handshake->release_auth_key());
|
2018-12-31 20:04:05 +01:00
|
|
|
on_auth_key_updated();
|
|
|
|
} else {
|
2020-01-08 18:30:07 +01:00
|
|
|
auth_data_.set_tmp_auth_key(handshake->release_auth_key());
|
2018-12-31 20:04:05 +01:00
|
|
|
if (is_main_) {
|
2020-01-08 18:30:07 +01:00
|
|
|
registered_temp_auth_key_ = TempAuthKeyWatchdog::register_auth_key_id(auth_data_.get_tmp_auth_key().id());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
on_tmp_auth_key_updated();
|
|
|
|
}
|
2019-02-04 16:44:29 +01:00
|
|
|
LOG(WARNING) << "Update auth key in session_id " << auth_data_.get_session_id() << " to "
|
2018-12-31 20:04:05 +01:00
|
|
|
<< auth_data_.get_auth_key().id();
|
|
|
|
connection_close(&main_connection_);
|
|
|
|
connection_close(&long_poll_connection_);
|
|
|
|
|
|
|
|
// Salt of temporary key is different salt. Do not rewrite it
|
|
|
|
if (auth_data_.use_pfs() ^ is_main) {
|
2023-01-19 10:27:23 +01:00
|
|
|
auth_data_.set_server_salt(handshake->get_server_salt(), Time::now());
|
2018-12-31 20:04:05 +01:00
|
|
|
on_server_salt_updated();
|
|
|
|
}
|
2020-01-08 18:30:07 +01:00
|
|
|
if (auth_data_.update_server_time_difference(handshake->get_server_time_diff())) {
|
2023-05-14 20:58:54 +02:00
|
|
|
on_server_time_difference_updated(true);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Session::create_gen_auth_key_actor(HandshakeId handshake_id) {
|
|
|
|
auto &info = handshake_info_[handshake_id];
|
|
|
|
if (info.flag_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
LOG(INFO) << "Create GenAuthKeyActor " << handshake_id;
|
|
|
|
info.flag_ = true;
|
|
|
|
bool is_main = handshake_id == MainAuthKeyHandshake;
|
|
|
|
if (!info.handshake_) {
|
2023-05-04 15:00:14 +02:00
|
|
|
auto key_validity_time = is_main && !is_cdn_ ? 0 : Random::fast(23 * 60 * 60, 24 * 60 * 60);
|
|
|
|
info.handshake_ = make_unique<mtproto::AuthKeyHandshake>(dc_id_, key_validity_time);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2021-07-04 04:58:54 +02:00
|
|
|
class AuthKeyHandshakeContext final : public mtproto::AuthKeyHandshakeContext {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2021-07-05 20:04:23 +02:00
|
|
|
AuthKeyHandshakeContext(mtproto::DhCallback *dh_callback,
|
|
|
|
std::shared_ptr<mtproto::PublicRsaKeyInterface> public_rsa_key)
|
2018-12-31 20:04:05 +01:00
|
|
|
: dh_callback_(dh_callback), public_rsa_key_(std::move(public_rsa_key)) {
|
|
|
|
}
|
2021-07-05 20:04:23 +02:00
|
|
|
mtproto::DhCallback *get_dh_callback() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
return dh_callback_;
|
|
|
|
}
|
2021-07-05 20:04:23 +02:00
|
|
|
mtproto::PublicRsaKeyInterface *get_public_rsa_key_interface() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
return public_rsa_key_.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-07-05 20:04:23 +02:00
|
|
|
mtproto::DhCallback *dh_callback_;
|
|
|
|
std::shared_ptr<mtproto::PublicRsaKeyInterface> public_rsa_key_;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
2022-06-09 11:10:38 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
info.actor_ = create_actor<detail::GenAuthKeyActor>(
|
2018-11-18 13:33:06 +01:00
|
|
|
PSLICE() << get_name() << "::GenAuthKey", get_name(), std::move(info.handshake_),
|
2018-09-27 03:19:03 +02:00
|
|
|
td::make_unique<AuthKeyHandshakeContext>(DhCache::instance(), shared_auth_data_->public_rsa_key()),
|
2018-12-31 20:04:05 +01:00
|
|
|
PromiseCreator::lambda(
|
2022-03-05 12:35:19 +01:00
|
|
|
[actor_id = actor_id(this), guard = callback_](Result<unique_ptr<mtproto::RawConnection>> r_connection) {
|
2019-06-19 15:56:55 +02:00
|
|
|
if (r_connection.is_error()) {
|
|
|
|
if (r_connection.error().code() != 1) {
|
|
|
|
LOG(WARNING) << "Failed to open connection: " << r_connection.error();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2022-03-05 12:35:19 +01:00
|
|
|
send_closure(actor_id, &Session::connection_add, r_connection.move_as_ok());
|
2018-12-31 20:04:05 +01:00
|
|
|
}),
|
2019-06-19 15:56:55 +02:00
|
|
|
PromiseCreator::lambda([self = actor_shared(this, handshake_id + 1),
|
|
|
|
handshake_perf = PerfWarningTimer("handshake", 1000.1),
|
|
|
|
guard = callback_](Result<unique_ptr<mtproto::AuthKeyHandshake>> handshake) mutable {
|
|
|
|
// later is just to avoid lost hangup
|
|
|
|
send_closure_later(std::move(self), &Session::on_handshake_ready, std::move(handshake));
|
|
|
|
}),
|
2018-12-31 20:04:05 +01:00
|
|
|
callback_);
|
|
|
|
}
|
|
|
|
|
2023-01-19 10:27:23 +01:00
|
|
|
void Session::auth_loop(double now) {
|
2017-12-29 21:34:39 +01:00
|
|
|
if (can_destroy_auth_key()) {
|
|
|
|
return;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
if (auth_data_.need_main_auth_key()) {
|
|
|
|
create_gen_auth_key_actor(MainAuthKeyHandshake);
|
|
|
|
}
|
2023-07-17 18:21:18 +02:00
|
|
|
if (auth_data_.need_tmp_auth_key(now, persist_tmp_auth_key_ ? 2 * 60 : 60 * 60)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
create_gen_auth_key_actor(TmpAuthKeyHandshake);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-20 18:12:59 +02:00
|
|
|
void Session::timeout_expired() {
|
|
|
|
send_closure_later(actor_id(this), &Session::loop);
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void Session::loop() {
|
|
|
|
if (!was_on_network_) {
|
|
|
|
return;
|
|
|
|
}
|
2023-01-19 10:27:23 +01:00
|
|
|
auto now = Time::now();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2023-01-19 10:27:23 +01:00
|
|
|
if (cached_connection_timestamp_ < now - 10) {
|
2018-12-31 20:04:05 +01:00
|
|
|
cached_connection_.reset();
|
|
|
|
}
|
2023-01-19 10:27:23 +01:00
|
|
|
if (!is_main_ && !has_queries() && !need_destroy_ && last_activity_timestamp_ < now - ACTIVITY_TIMEOUT) {
|
2018-12-31 20:04:05 +01:00
|
|
|
on_session_failed(Status::OK());
|
|
|
|
}
|
|
|
|
|
2023-01-19 10:27:23 +01:00
|
|
|
auth_loop(now);
|
|
|
|
connection_online_update(now, false);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
double wakeup_at = 0;
|
2021-11-22 12:36:59 +01:00
|
|
|
main_connection_.wakeup_at_ = 0;
|
|
|
|
long_poll_connection_.wakeup_at_ = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
// NB: order is crucial. First long_poll_connection, then main_connection
|
2023-01-02 13:38:04 +01:00
|
|
|
// Otherwise, queries could be sent with big delay
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
connection_check_mode(&main_connection_);
|
|
|
|
connection_check_mode(&long_poll_connection_);
|
|
|
|
if (mode_ == Mode::Http) {
|
2021-11-22 12:36:59 +01:00
|
|
|
if (long_poll_connection_.state_ == ConnectionInfo::State::Ready) {
|
2018-12-31 20:04:05 +01:00
|
|
|
connection_flush(&long_poll_connection_);
|
|
|
|
}
|
2021-11-22 12:36:59 +01:00
|
|
|
if (!close_flag_ && long_poll_connection_.state_ == ConnectionInfo::State::Empty) {
|
2023-01-19 10:27:23 +01:00
|
|
|
connection_open(&long_poll_connection_, now);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2021-11-22 12:36:59 +01:00
|
|
|
relax_timeout_at(&wakeup_at, long_poll_connection_.wakeup_at_);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-11-22 12:36:59 +01:00
|
|
|
if (main_connection_.state_ == ConnectionInfo::State::Ready) {
|
2018-12-31 20:04:05 +01:00
|
|
|
// do not send queries before we have key and e.t.c
|
|
|
|
// do not send queries before tmp_key is bound
|
|
|
|
bool need_flush = true;
|
2021-11-22 12:36:59 +01:00
|
|
|
while (main_connection_.state_ == ConnectionInfo::State::Ready) {
|
2023-01-19 10:27:23 +01:00
|
|
|
if (auth_data_.is_ready(now)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (need_send_query()) {
|
2020-07-31 19:19:18 +02:00
|
|
|
while (!pending_queries_.empty() && sent_queries_.size() < MAX_INFLIGHT_QUERIES) {
|
2020-08-17 15:13:18 +02:00
|
|
|
auto query = pending_queries_.pop();
|
2018-12-31 20:04:05 +01:00
|
|
|
connection_send_query(&main_connection_, std::move(query));
|
2020-07-31 19:19:18 +02:00
|
|
|
need_flush = true;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (need_send_bind_key()) {
|
|
|
|
// send auth.bindTempAuthKey
|
|
|
|
connection_send_bind_key(&main_connection_);
|
|
|
|
need_flush = true;
|
|
|
|
}
|
2020-01-07 13:42:04 +01:00
|
|
|
if (need_send_check_main_key()) {
|
|
|
|
connection_send_check_main_key(&main_connection_);
|
|
|
|
need_flush = true;
|
|
|
|
}
|
2019-03-17 11:29:32 +01:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
if (need_flush) {
|
|
|
|
connection_flush(&main_connection_);
|
|
|
|
need_flush = false;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-22 12:36:59 +01:00
|
|
|
if (!close_flag_ && main_connection_.state_ == ConnectionInfo::State::Empty) {
|
2023-01-19 10:27:23 +01:00
|
|
|
connection_open(&main_connection_, now, true /*send ask_info*/);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2023-03-08 16:27:58 +01:00
|
|
|
connection_online_update(now, false); // has_queries() could have been changed
|
|
|
|
|
2021-11-22 12:36:59 +01:00
|
|
|
relax_timeout_at(&wakeup_at, main_connection_.wakeup_at_);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
if (wakeup_at != 0) {
|
|
|
|
set_timeout_at(wakeup_at);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|