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)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
2019-01-31 03:05:40 +01:00
|
|
|
#include "td/telegram/net/AuthDataShared.h"
|
|
|
|
#include "td/telegram/net/NetQuery.h"
|
|
|
|
#include "td/telegram/net/TempAuthKeyWatchdog.h"
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/mtproto/AuthData.h"
|
2019-01-31 03:05:40 +01:00
|
|
|
#include "td/mtproto/AuthKey.h"
|
2021-09-16 18:09:39 +02:00
|
|
|
#include "td/mtproto/ConnectionManager.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/mtproto/Handshake.h"
|
2023-09-21 16:52:33 +02:00
|
|
|
#include "td/mtproto/MessageId.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/mtproto/SessionConnection.h"
|
|
|
|
|
|
|
|
#include "td/actor/actor.h"
|
|
|
|
|
|
|
|
#include "td/utils/buffer.h"
|
2019-05-22 20:17:24 +02:00
|
|
|
#include "td/utils/CancellationToken.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/common.h"
|
2022-02-07 22:04:34 +01:00
|
|
|
#include "td/utils/FlatHashMap.h"
|
2022-03-11 19:38:48 +01:00
|
|
|
#include "td/utils/FlatHashSet.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/List.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/Status.h"
|
|
|
|
#include "td/utils/StringBuilder.h"
|
2020-08-17 15:13:18 +02:00
|
|
|
#include "td/utils/VectorQueue.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
#include <array>
|
|
|
|
#include <deque>
|
2020-08-18 17:37:10 +02:00
|
|
|
#include <functional>
|
2018-12-31 20:04:05 +01:00
|
|
|
#include <map>
|
|
|
|
#include <memory>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
namespace mtproto {
|
|
|
|
class RawConnection;
|
|
|
|
} // namespace mtproto
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
class GenAuthKeyActor;
|
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
class Session final
|
|
|
|
: public NetQueryCallback
|
|
|
|
, private mtproto::SessionConnection::Callback {
|
|
|
|
public:
|
|
|
|
class Callback {
|
|
|
|
public:
|
|
|
|
Callback() = default;
|
|
|
|
Callback(const Callback &) = delete;
|
|
|
|
Callback &operator=(const Callback &) = delete;
|
|
|
|
virtual ~Callback() = default;
|
|
|
|
virtual void on_failed() = 0;
|
|
|
|
virtual void on_closed() = 0;
|
2019-05-06 20:53:39 +02:00
|
|
|
virtual void request_raw_connection(unique_ptr<mtproto::AuthData> auth_data,
|
|
|
|
Promise<unique_ptr<mtproto::RawConnection>>) = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
virtual void on_tmp_auth_key_updated(mtproto::AuthKey auth_key) = 0;
|
2021-10-19 17:11:16 +02:00
|
|
|
virtual void on_server_salt_updated(vector<mtproto::ServerSalt> server_salts) = 0;
|
2023-01-25 15:48:04 +01:00
|
|
|
virtual void on_update(BufferSlice &&update, uint64 auth_key_id) = 0;
|
2019-03-15 10:00:18 +01:00
|
|
|
virtual void on_result(NetQueryPtr net_query) = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
2019-12-18 01:47:51 +01:00
|
|
|
Session(unique_ptr<Callback> callback, std::shared_ptr<AuthDataShared> shared_auth_data, int32 raw_dc_id, int32 dc_id,
|
2023-10-23 13:10:39 +02:00
|
|
|
bool is_primary, bool is_main, bool use_pfs, bool persist_tmp_auth_key, bool is_cdn,
|
|
|
|
bool need_destroy_auth_key, const mtproto::AuthKey &tmp_auth_key,
|
|
|
|
const vector<mtproto::ServerSalt> &server_salts);
|
2021-12-02 13:10:51 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void send(NetQueryPtr &&query);
|
2021-12-02 13:10:51 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void close();
|
|
|
|
|
2022-08-10 16:03:38 +02:00
|
|
|
static bool is_high_loaded();
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
private:
|
2021-07-04 04:58:54 +02:00
|
|
|
struct Query final : private ListNode {
|
2023-09-21 16:52:33 +02:00
|
|
|
mtproto::MessageId container_message_id_;
|
2023-08-30 23:47:39 +02:00
|
|
|
NetQueryPtr net_query_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2023-08-30 23:47:39 +02:00
|
|
|
bool is_acknowledged_ = false;
|
|
|
|
bool is_unknown_ = false;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2023-08-30 23:47:39 +02:00
|
|
|
const int8 connection_id_;
|
2023-09-01 15:13:00 +02:00
|
|
|
const double sent_at_;
|
2023-08-30 23:47:39 +02:00
|
|
|
|
2023-09-21 16:52:33 +02:00
|
|
|
Query(mtproto::MessageId message_id, NetQueryPtr &&net_query, int8 connection_id, double sent_at)
|
2023-08-30 23:47:39 +02:00
|
|
|
: container_message_id_(message_id)
|
|
|
|
, net_query_(std::move(net_query))
|
|
|
|
, connection_id_(connection_id)
|
|
|
|
, sent_at_(sent_at) {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ListNode *get_list_node() {
|
|
|
|
return static_cast<ListNode *>(this);
|
|
|
|
}
|
|
|
|
static Query *from_list_node(ListNode *list_node) {
|
|
|
|
return static_cast<Query *>(list_node);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-12-13 23:48:36 +01:00
|
|
|
// When connection is closed, mark all queries without ack as unknown.
|
2018-12-31 20:04:05 +01:00
|
|
|
// Ask state of all unknown queries when new connection is created.
|
|
|
|
//
|
2023-01-15 21:27:58 +01:00
|
|
|
// Just re-ask answer_message_id each time we get information about it.
|
2018-12-13 23:48:36 +01:00
|
|
|
// Though mtproto::Connection must ensure delivery of such query.
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2023-01-14 21:08:50 +01:00
|
|
|
const int32 raw_dc_id_; // numerical datacenter ID, i.e. 2
|
|
|
|
const int32 dc_id_; // unique datacenter ID, i.e. -10002
|
2023-01-14 22:25:22 +01:00
|
|
|
const bool is_primary_; // true for primary Sessions to all DCs
|
2023-01-14 21:08:50 +01:00
|
|
|
const bool is_main_; // true only for the primary Session(s) to the main DC
|
2023-07-17 18:21:18 +02:00
|
|
|
const bool persist_tmp_auth_key_;
|
2023-01-14 21:08:50 +01:00
|
|
|
const bool is_cdn_;
|
2023-10-23 13:10:39 +02:00
|
|
|
const bool need_destroy_auth_key_;
|
2018-12-31 20:04:05 +01:00
|
|
|
bool was_on_network_ = false;
|
|
|
|
bool network_flag_ = false;
|
|
|
|
bool online_flag_ = false;
|
2021-12-02 13:10:51 +01:00
|
|
|
bool logging_out_flag_ = false;
|
2018-12-31 20:04:05 +01:00
|
|
|
bool connection_online_flag_ = false;
|
2023-01-14 21:08:50 +01:00
|
|
|
enum class Mode : int8 { Tcp, Http } mode_ = Mode::Tcp;
|
2021-11-22 09:52:09 +01:00
|
|
|
uint32 network_generation_ = 0;
|
2020-01-07 23:27:12 +01:00
|
|
|
uint64 being_binded_tmp_auth_key_id_ = 0;
|
|
|
|
uint64 being_checked_main_auth_key_id_ = 0;
|
|
|
|
uint64 last_bind_query_id_ = 0;
|
|
|
|
uint64 last_check_query_id_ = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
double last_activity_timestamp_ = 0;
|
2022-05-03 14:33:09 +02:00
|
|
|
double last_success_timestamp_ = 0; // time when auth_key and Session definitely was valid
|
|
|
|
double last_bind_success_timestamp_ = 0; // time when auth_key and Session definitely was valid and authorized
|
2018-12-31 20:04:05 +01:00
|
|
|
size_t dropped_size_ = 0;
|
|
|
|
|
2023-09-21 16:52:33 +02:00
|
|
|
FlatHashSet<mtproto::MessageId, mtproto::MessageIdHash> unknown_queries_;
|
|
|
|
vector<mtproto::MessageId> to_cancel_message_ids_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-12-26 17:11:15 +01:00
|
|
|
// Do not invalidate iterators of these two containers!
|
2018-12-31 20:04:05 +01:00
|
|
|
// TODO: better data structures
|
2020-08-17 15:13:18 +02:00
|
|
|
struct PriorityQueue {
|
|
|
|
void push(NetQueryPtr query);
|
|
|
|
NetQueryPtr pop();
|
|
|
|
bool empty() const;
|
|
|
|
|
|
|
|
private:
|
2020-08-18 17:37:10 +02:00
|
|
|
std::map<int8, VectorQueue<NetQueryPtr>, std::greater<>> queries_;
|
2020-08-17 15:13:18 +02:00
|
|
|
};
|
|
|
|
PriorityQueue pending_queries_;
|
2023-09-21 16:52:33 +02:00
|
|
|
std::map<mtproto::MessageId, Query> sent_queries_;
|
2018-12-31 20:04:05 +01:00
|
|
|
std::deque<NetQueryPtr> pending_invoke_after_queries_;
|
|
|
|
ListNode sent_queries_list_;
|
|
|
|
|
|
|
|
struct ConnectionInfo {
|
2021-11-22 12:36:59 +01:00
|
|
|
int8 connection_id_ = 0;
|
|
|
|
Mode mode_ = Mode::Tcp;
|
|
|
|
enum class State : int8 { Empty, Connecting, Ready } state_ = State::Empty;
|
2019-05-01 15:13:48 +02:00
|
|
|
CancellationTokenSource cancellation_token_source_;
|
2021-11-22 12:36:59 +01:00
|
|
|
unique_ptr<mtproto::SessionConnection> connection_;
|
|
|
|
bool ask_info_ = false;
|
|
|
|
double wakeup_at_ = 0;
|
|
|
|
double created_at_ = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
ConnectionInfo *current_info_;
|
|
|
|
ConnectionInfo main_connection_;
|
|
|
|
ConnectionInfo long_poll_connection_;
|
2021-09-16 18:09:39 +02:00
|
|
|
mtproto::ConnectionManager::ConnectionToken connection_token_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
double cached_connection_timestamp_ = 0;
|
2018-09-27 03:19:03 +02:00
|
|
|
unique_ptr<mtproto::RawConnection> cached_connection_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
std::shared_ptr<Callback> callback_;
|
|
|
|
mtproto::AuthData auth_data_;
|
2020-01-07 13:42:04 +01:00
|
|
|
bool use_pfs_{false};
|
|
|
|
bool need_check_main_key_{false};
|
2018-12-31 20:04:05 +01:00
|
|
|
TempAuthKeyWatchdog::RegisteredAuthKey registered_temp_auth_key_;
|
|
|
|
std::shared_ptr<AuthDataShared> shared_auth_data_;
|
|
|
|
bool close_flag_ = false;
|
|
|
|
|
|
|
|
static constexpr double ACTIVITY_TIMEOUT = 60 * 5;
|
2020-07-31 19:19:18 +02:00
|
|
|
static constexpr size_t MAX_INFLIGHT_QUERIES = 1024;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
struct ContainerInfo {
|
|
|
|
size_t ref_cnt;
|
2023-09-21 16:52:33 +02:00
|
|
|
vector<mtproto::MessageId> message_ids;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
2023-09-21 16:52:33 +02:00
|
|
|
FlatHashMap<mtproto::MessageId, ContainerInfo, mtproto::MessageIdHash> sent_containers_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
friend class GenAuthKeyActor;
|
|
|
|
struct HandshakeInfo {
|
|
|
|
bool flag_ = false;
|
|
|
|
ActorOwn<detail::GenAuthKeyActor> actor_;
|
2018-09-27 03:19:03 +02:00
|
|
|
unique_ptr<mtproto::AuthKeyHandshake> handshake_;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
enum HandshakeId : int32 { MainAuthKeyHandshake = 0, TmpAuthKeyHandshake = 1 };
|
|
|
|
std::array<HandshakeInfo, 2> handshake_info_;
|
|
|
|
|
|
|
|
double wakeup_at_;
|
2018-09-27 03:19:03 +02:00
|
|
|
void on_handshake_ready(Result<unique_ptr<mtproto::AuthKeyHandshake>> r_handshake);
|
2018-12-31 20:04:05 +01:00
|
|
|
void create_gen_auth_key_actor(HandshakeId handshake_id);
|
2023-01-19 10:27:23 +01:00
|
|
|
void auth_loop(double now);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
// mtproto::Connection::Callback
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_connected() final;
|
|
|
|
void on_closed(Status status) final;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
Status on_pong() final;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-12-02 13:10:51 +01:00
|
|
|
void on_network(bool network_flag, uint32 network_generation);
|
|
|
|
void on_online(bool online_flag);
|
|
|
|
void on_logging_out(bool logging_out_flag);
|
|
|
|
|
2023-10-23 12:08:45 +02:00
|
|
|
void on_auth_key_updated();
|
|
|
|
void on_tmp_auth_key_updated();
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_server_salt_updated() final;
|
2023-05-14 20:58:54 +02:00
|
|
|
void on_server_time_difference_updated(bool force) final;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2023-09-21 16:52:33 +02:00
|
|
|
void on_new_session_created(uint64 unique_id, mtproto::MessageId first_message_id) final;
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_session_failed(Status status) final;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2023-09-21 16:52:33 +02:00
|
|
|
void on_container_sent(mtproto::MessageId container_message_id, vector<mtproto::MessageId> message_ids) final;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-09-10 15:31:04 +02:00
|
|
|
Status on_update(BufferSlice packet) final;
|
|
|
|
|
2023-09-21 16:52:33 +02:00
|
|
|
void on_message_ack(mtproto::MessageId message_id) final;
|
|
|
|
Status on_message_result_ok(mtproto::MessageId message_id, BufferSlice packet, size_t original_size) final;
|
|
|
|
void on_message_result_error(mtproto::MessageId message_id, int error_code, string message) final;
|
|
|
|
void on_message_failed(mtproto::MessageId message_id, Status status) final;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2023-09-21 16:52:33 +02:00
|
|
|
void on_message_info(mtproto::MessageId message_id, int32 state, mtproto::MessageId answer_message_id,
|
|
|
|
int32 answer_size, int32 source) final;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
Status on_destroy_auth_key() final;
|
2017-12-29 21:34:39 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void flush_pending_invoke_after_queries();
|
|
|
|
bool has_queries() const;
|
|
|
|
|
2023-09-21 16:52:33 +02:00
|
|
|
void dec_container(mtproto::MessageId container_message_id, Query *query);
|
|
|
|
void cleanup_container(mtproto::MessageId container_message_id, Query *query);
|
|
|
|
void mark_as_known(mtproto::MessageId message_id, Query *query);
|
|
|
|
void mark_as_unknown(mtproto::MessageId message_id, Query *query);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2023-09-21 16:52:33 +02:00
|
|
|
void on_message_ack_impl(mtproto::MessageId container_message_id, int32 type);
|
|
|
|
void on_message_ack_impl_inner(mtproto::MessageId message_id, int32 type, bool in_container);
|
|
|
|
void on_message_failed_inner(mtproto::MessageId message_id, bool in_container);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
// send NetQueryPtr to parent
|
|
|
|
void return_query(NetQueryPtr &&query);
|
|
|
|
void add_query(NetQueryPtr &&net_query);
|
|
|
|
void resend_query(NetQueryPtr query);
|
|
|
|
|
2023-01-19 10:27:23 +01:00
|
|
|
void connection_open(ConnectionInfo *info, double now, bool ask_info = false);
|
2018-09-27 03:19:03 +02:00
|
|
|
void connection_add(unique_ptr<mtproto::RawConnection> raw_connection);
|
2018-12-31 20:04:05 +01:00
|
|
|
void connection_check_mode(ConnectionInfo *info);
|
2018-09-27 03:19:03 +02:00
|
|
|
void connection_open_finish(ConnectionInfo *info, Result<unique_ptr<mtproto::RawConnection>> r_raw_connection);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2023-01-19 10:27:23 +01:00
|
|
|
void connection_online_update(double now, bool force);
|
2018-12-31 20:04:05 +01:00
|
|
|
void connection_close(ConnectionInfo *info);
|
|
|
|
void connection_flush(ConnectionInfo *info);
|
2023-09-21 16:52:33 +02:00
|
|
|
void connection_send_query(ConnectionInfo *info, NetQueryPtr &&net_query, mtproto::MessageId message_id = {});
|
2018-11-01 18:08:20 +01:00
|
|
|
bool need_send_bind_key() const;
|
|
|
|
bool need_send_query() const;
|
|
|
|
bool can_destroy_auth_key() const;
|
2018-12-31 20:04:05 +01:00
|
|
|
bool connection_send_bind_key(ConnectionInfo *info);
|
2020-01-07 13:42:04 +01:00
|
|
|
bool need_send_check_main_key() const;
|
|
|
|
bool connection_send_check_main_key(ConnectionInfo *info);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_result(NetQueryPtr query) final;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2020-01-07 13:42:04 +01:00
|
|
|
void on_bind_result(NetQueryPtr query);
|
|
|
|
void on_check_key_result(NetQueryPtr query);
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final;
|
2023-07-20 18:12:59 +02:00
|
|
|
void timeout_expired() final;
|
2021-07-03 22:51:36 +02:00
|
|
|
void loop() final;
|
|
|
|
void hangup() final;
|
|
|
|
void raw_event(const Event::Raw &event) final;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
friend StringBuilder &operator<<(StringBuilder &sb, Mode mode) {
|
2023-01-15 09:25:54 +01:00
|
|
|
return sb << (mode == Mode::Http ? "HTTP" : "TCP");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace td
|