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/SessionMultiProxy.h"
|
|
|
|
|
|
|
|
#include "td/telegram/net/SessionProxy.h"
|
|
|
|
|
2019-02-12 22:26:36 +01:00
|
|
|
#include "td/utils/common.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/format.h"
|
|
|
|
#include "td/utils/logging.h"
|
2023-11-23 06:31:56 +01:00
|
|
|
#include "td/utils/misc.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Slice.h"
|
2021-05-17 14:21:11 +02:00
|
|
|
#include "td/utils/SliceBuilder.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-03-17 21:43:58 +01:00
|
|
|
#include <algorithm>
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
namespace td {
|
|
|
|
|
|
|
|
SessionMultiProxy::~SessionMultiProxy() = default;
|
|
|
|
|
|
|
|
SessionMultiProxy::SessionMultiProxy(int32 session_count, std::shared_ptr<AuthDataShared> shared_auth_data,
|
2023-01-14 22:25:22 +01:00
|
|
|
bool is_primary, bool is_main, bool use_pfs, bool allow_media_only, bool is_media,
|
2023-10-23 13:05:50 +02:00
|
|
|
bool is_cdn)
|
2018-12-31 20:04:05 +01:00
|
|
|
: session_count_(session_count)
|
|
|
|
, auth_data_(std::move(shared_auth_data))
|
2023-01-14 22:25:22 +01:00
|
|
|
, is_primary_(is_primary)
|
2018-12-31 20:04:05 +01:00
|
|
|
, is_main_(is_main)
|
|
|
|
, use_pfs_(use_pfs)
|
|
|
|
, allow_media_only_(allow_media_only)
|
|
|
|
, is_media_(is_media)
|
2023-10-23 13:05:50 +02:00
|
|
|
, is_cdn_(is_cdn) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (allow_media_only_) {
|
|
|
|
CHECK(is_media_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SessionMultiProxy::send(NetQueryPtr query) {
|
|
|
|
size_t pos = 0;
|
2022-01-03 06:57:11 +01:00
|
|
|
if (query->auth_flag() == NetQuery::AuthFlag::On) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (query->session_rand()) {
|
|
|
|
pos = query->session_rand() % sessions_.size();
|
|
|
|
} else {
|
2019-03-15 11:37:01 +01:00
|
|
|
pos = std::min_element(sessions_.begin(), sessions_.end(),
|
2023-03-13 16:48:35 +01:00
|
|
|
[](const auto &a, const auto &b) { return a.query_count < b.query_count; }) -
|
2019-03-15 11:37:01 +01:00
|
|
|
sessions_.begin();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
2020-05-04 20:52:15 +02:00
|
|
|
// query->debug(PSTRING() << get_name() << ": send to proxy #" << pos);
|
2023-03-13 16:48:35 +01:00
|
|
|
sessions_[pos].query_count++;
|
2019-03-15 10:00:18 +01:00
|
|
|
send_closure(sessions_[pos].proxy, &SessionProxy::send, std::move(query));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SessionMultiProxy::update_main_flag(bool is_main) {
|
2023-10-23 14:57:36 +02:00
|
|
|
LOG(INFO) << "Update is_main to " << is_main;
|
2018-12-31 20:04:05 +01:00
|
|
|
is_main_ = is_main;
|
|
|
|
for (auto &session : sessions_) {
|
2019-03-15 10:00:18 +01:00
|
|
|
send_closure(session.proxy, &SessionProxy::update_main_flag, is_main);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
2017-12-29 21:34:39 +01:00
|
|
|
|
2023-10-23 13:00:22 +02:00
|
|
|
void SessionMultiProxy::destroy_auth_key() {
|
2023-10-23 13:58:12 +02:00
|
|
|
update_options(1, false, true);
|
2017-12-29 21:34:39 +01:00
|
|
|
}
|
2022-08-17 23:37:19 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void SessionMultiProxy::update_session_count(int32 session_count) {
|
2023-10-23 13:58:12 +02:00
|
|
|
update_options(session_count, use_pfs_, need_destroy_auth_key_);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2022-08-17 23:37:19 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void SessionMultiProxy::update_use_pfs(bool use_pfs) {
|
2023-10-23 13:58:12 +02:00
|
|
|
update_options(session_count_, use_pfs, need_destroy_auth_key_);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2023-10-23 13:58:12 +02:00
|
|
|
void SessionMultiProxy::update_options(int32 session_count, bool use_pfs, bool need_destroy_auth_key) {
|
|
|
|
if (need_destroy_auth_key_) {
|
|
|
|
LOG(INFO) << "Ignore session option changes while destroying auth key";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-11-08 09:26:37 +01:00
|
|
|
bool is_changed = false;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2023-11-08 09:26:37 +01:00
|
|
|
session_count = clamp(session_count, 1, 100);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (session_count != session_count_) {
|
|
|
|
session_count_ = session_count;
|
2023-10-23 14:57:36 +02:00
|
|
|
LOG(INFO) << "Update session_count to " << session_count_;
|
2023-11-08 09:26:37 +01:00
|
|
|
is_changed = true;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (use_pfs != use_pfs_) {
|
|
|
|
bool old_pfs_flag = get_pfs_flag();
|
|
|
|
use_pfs_ = use_pfs;
|
|
|
|
if (old_pfs_flag != get_pfs_flag()) {
|
2023-10-23 14:57:36 +02:00
|
|
|
LOG(INFO) << "Update use_pfs to " << use_pfs_;
|
2023-11-08 09:26:37 +01:00
|
|
|
is_changed = true;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
2023-10-23 13:58:12 +02:00
|
|
|
|
|
|
|
if (need_destroy_auth_key) {
|
|
|
|
need_destroy_auth_key_ = need_destroy_auth_key;
|
2023-11-08 09:26:37 +01:00
|
|
|
is_changed = true;
|
2023-10-23 14:57:36 +02:00
|
|
|
LOG(WARNING) << "Destroy auth key";
|
2023-10-23 13:58:12 +02:00
|
|
|
}
|
|
|
|
|
2023-11-08 09:26:37 +01:00
|
|
|
if (is_changed) {
|
2018-12-31 20:04:05 +01:00
|
|
|
init();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-10 20:02:24 +02:00
|
|
|
void SessionMultiProxy::update_mtproto_header() {
|
2018-06-13 17:17:27 +02:00
|
|
|
for (auto &session : sessions_) {
|
2019-03-15 10:00:18 +01:00
|
|
|
send_closure_later(session.proxy, &SessionProxy::update_mtproto_header);
|
2018-06-13 17:17:27 +02:00
|
|
|
}
|
2018-05-10 20:02:24 +02:00
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void SessionMultiProxy::start_up() {
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SessionMultiProxy::get_pfs_flag() const {
|
2019-12-28 12:25:17 +01:00
|
|
|
return use_pfs_ && !is_cdn_;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SessionMultiProxy::init() {
|
2019-03-15 10:00:18 +01:00
|
|
|
sessions_generation_++;
|
2018-12-31 20:04:05 +01:00
|
|
|
sessions_.clear();
|
2020-05-05 12:54:32 +02:00
|
|
|
if (is_main_ && session_count_ > 1) {
|
2018-12-31 20:04:05 +01:00
|
|
|
LOG(WARNING) << tag("session_count", session_count_);
|
|
|
|
}
|
|
|
|
for (int32 i = 0; i < session_count_; i++) {
|
|
|
|
string name = PSTRING() << "Session" << get_name().substr(Slice("SessionMulti").size())
|
|
|
|
<< format::cond(session_count_ > 1, format::concat("#", i));
|
2019-03-15 10:00:18 +01:00
|
|
|
|
|
|
|
SessionInfo info;
|
2021-07-04 04:58:54 +02:00
|
|
|
class Callback final : public SessionProxy::Callback {
|
2019-03-15 10:00:18 +01:00
|
|
|
public:
|
|
|
|
Callback(ActorId<SessionMultiProxy> parent, uint32 generation, int32 session_id)
|
|
|
|
: parent_(parent), generation_(generation), session_id_(session_id) {
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_query_finished() final {
|
2019-03-15 10:00:18 +01:00
|
|
|
send_closure(parent_, &SessionMultiProxy::on_query_finished, generation_, session_id_);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ActorId<SessionMultiProxy> parent_;
|
|
|
|
uint32 generation_;
|
|
|
|
int32 session_id_;
|
|
|
|
};
|
2023-05-04 14:54:21 +02:00
|
|
|
info.proxy =
|
|
|
|
create_actor<SessionProxy>(name, make_unique<Callback>(actor_id(this), sessions_generation_, i), auth_data_,
|
|
|
|
is_primary_, is_main_, allow_media_only_, is_media_, get_pfs_flag(),
|
|
|
|
session_count_ > 1 && is_primary_, is_cdn_, need_destroy_auth_key_ && i == 0);
|
2019-03-15 10:00:18 +01:00
|
|
|
sessions_.push_back(std::move(info));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SessionMultiProxy::on_query_finished(uint32 generation, int session_id) {
|
|
|
|
if (generation != sessions_generation_) {
|
|
|
|
return;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2023-03-13 16:48:35 +01:00
|
|
|
CHECK(static_cast<size_t>(session_id) < sessions_.size());
|
|
|
|
auto &query_count = sessions_[session_id].query_count;
|
|
|
|
CHECK(query_count > 0);
|
|
|
|
query_count--;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|