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/DcOptionsSet.h"
|
|
|
|
|
2020-07-16 20:37:13 +02:00
|
|
|
#include "td/telegram/ConfigManager.h"
|
2020-07-16 21:36:59 +02:00
|
|
|
#include "td/telegram/Global.h"
|
2020-07-16 20:37:13 +02:00
|
|
|
|
2020-07-26 13:24:30 +02:00
|
|
|
#include "td/actor/actor.h"
|
|
|
|
|
2021-01-01 13:59:53 +01:00
|
|
|
#include "td/utils/algorithm.h"
|
2018-02-28 11:25:07 +01:00
|
|
|
#include "td/utils/format.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/logging.h"
|
2021-05-17 14:21:11 +02:00
|
|
|
#include "td/utils/SliceBuilder.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <set>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
void DcOptionsSet::add_dc_options(DcOptions dc_options) {
|
|
|
|
std::vector<DcOptionId> new_ordered_options;
|
|
|
|
for (auto &option : dc_options.dc_options) {
|
|
|
|
auto *info = register_dc_option(std::move(option));
|
|
|
|
new_ordered_options.push_back(DcOptionId{info->pos});
|
|
|
|
}
|
|
|
|
|
|
|
|
std::set<DcOptionId> new_ordered_options_set(new_ordered_options.begin(), new_ordered_options.end());
|
|
|
|
for (auto option_id : ordered_options_) {
|
|
|
|
if (!new_ordered_options_set.count(option_id)) {
|
|
|
|
new_ordered_options.push_back(option_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ordered_options_ = std::move(new_ordered_options);
|
|
|
|
for (size_t i = 0; i < ordered_options_.size(); i++) {
|
|
|
|
options_[ordered_options_[i].pos]->order = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DcOptions DcOptionsSet::get_dc_options() const {
|
|
|
|
DcOptions result;
|
|
|
|
for (auto id : ordered_options_) {
|
|
|
|
result.dc_options.push_back(options_[id.pos]->option);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-06-04 19:50:34 +02:00
|
|
|
vector<DcOptionsSet::ConnectionInfo> DcOptionsSet::find_all_connections(DcId dc_id, bool allow_media_only,
|
2018-07-27 02:54:25 +02:00
|
|
|
bool use_static, bool prefer_ipv6,
|
|
|
|
bool only_http) {
|
2019-12-05 13:36:32 +01:00
|
|
|
LOG(DEBUG) << "Find all " << (allow_media_only ? "media " : "") << "connections in " << dc_id
|
|
|
|
<< ". use_static = " << use_static << ", prefer_ipv6 = " << prefer_ipv6 << ", only_http = " << only_http;
|
2018-12-31 20:04:05 +01:00
|
|
|
std::vector<ConnectionInfo> options;
|
|
|
|
std::vector<ConnectionInfo> static_options;
|
|
|
|
|
2018-07-01 16:19:59 +02:00
|
|
|
if (prefer_ipv6) {
|
|
|
|
use_static = false;
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
for (auto &option_info : options_) {
|
|
|
|
auto &option = option_info->option;
|
|
|
|
if (option.get_dc_id() != dc_id) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!option.is_valid()) {
|
|
|
|
LOG(INFO) << "Skip invalid DC option";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!allow_media_only && option.is_media_only()) {
|
|
|
|
LOG(DEBUG) << "Skip media only option";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ConnectionInfo info;
|
|
|
|
info.option = &option;
|
|
|
|
info.order = option_info->order;
|
|
|
|
|
|
|
|
OptionStat *option_stat = get_option_stat(option_info.get());
|
|
|
|
|
2018-07-27 02:54:25 +02:00
|
|
|
if (!only_http) {
|
|
|
|
info.use_http = false;
|
|
|
|
info.stat = &option_stat->tcp_stat;
|
|
|
|
if (option.is_static()) {
|
|
|
|
static_options.push_back(info);
|
|
|
|
} else {
|
|
|
|
options.push_back(info);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2018-07-27 02:54:25 +02:00
|
|
|
if (only_http) {
|
2018-08-17 18:03:07 +02:00
|
|
|
if (!option.is_obfuscated_tcp_only() && !option.is_static() && (prefer_ipv6 || !option.is_ipv6())) {
|
2018-07-27 02:54:25 +02:00
|
|
|
info.use_http = true;
|
|
|
|
info.stat = &option_stat->http_stat;
|
|
|
|
options.push_back(info);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (use_static) {
|
|
|
|
if (!static_options.empty()) {
|
|
|
|
options = std::move(static_options);
|
2018-06-04 19:50:34 +02:00
|
|
|
} else {
|
|
|
|
bool have_ipv4 = std::any_of(options.begin(), options.end(), [](auto &v) { return !v.option->is_ipv6(); });
|
|
|
|
if (have_ipv4) {
|
2019-10-21 15:25:56 +02:00
|
|
|
td::remove_if(options, [](auto &v) { return v.option->is_ipv6(); });
|
2018-06-04 19:50:34 +02:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (options.empty()) {
|
|
|
|
options = std::move(static_options);
|
|
|
|
}
|
|
|
|
}
|
2018-07-01 16:19:59 +02:00
|
|
|
|
|
|
|
if (prefer_ipv6) {
|
|
|
|
bool have_ipv6 = std::any_of(options.begin(), options.end(), [](auto &v) { return v.option->is_ipv6(); });
|
|
|
|
if (have_ipv6) {
|
2019-10-21 15:25:56 +02:00
|
|
|
td::remove_if(options, [](auto &v) { return !v.option->is_ipv6(); });
|
2018-07-01 16:19:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
bool have_media_only = std::any_of(options.begin(), options.end(), [](auto &v) { return v.option->is_media_only(); });
|
|
|
|
if (have_media_only) {
|
2019-10-21 15:25:56 +02:00
|
|
|
td::remove_if(options, [](auto &v) { return !v.option->is_media_only(); });
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2018-06-03 01:35:32 +02:00
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
2018-07-01 16:19:59 +02:00
|
|
|
Result<DcOptionsSet::ConnectionInfo> DcOptionsSet::find_connection(DcId dc_id, bool allow_media_only, bool use_static,
|
2018-07-27 02:54:25 +02:00
|
|
|
bool prefer_ipv6, bool only_http) {
|
|
|
|
auto options = find_all_connections(dc_id, allow_media_only, use_static, prefer_ipv6, only_http);
|
2018-06-03 01:35:32 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
if (options.empty()) {
|
2020-07-16 20:37:13 +02:00
|
|
|
send_closure(G()->config_manager(), &ConfigManager::lazy_request_config);
|
2018-02-27 10:38:49 +01:00
|
|
|
return Status::Error(PSLICE() << "No such connection: " << tag("dc_id", dc_id)
|
2018-07-01 16:19:59 +02:00
|
|
|
<< tag("allow_media_only", allow_media_only) << tag("use_static", use_static)
|
|
|
|
<< tag("prefer_ipv6", prefer_ipv6));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-09-28 04:14:21 +02:00
|
|
|
auto last_error_at = std::min_element(options.begin(), options.end(), [](const auto &a_option, const auto &b_option) {
|
|
|
|
return a_option.stat->error_at > b_option.stat->error_at;
|
|
|
|
})->stat->error_at;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
auto result = *std::min_element(options.begin(), options.end(), [](const auto &a_option, const auto &b_option) {
|
|
|
|
auto &a = *a_option.stat;
|
|
|
|
auto &b = *b_option.stat;
|
|
|
|
auto a_state = a.state();
|
|
|
|
auto b_state = b.state();
|
|
|
|
if (a_state != b_state) {
|
|
|
|
return a_state < b_state;
|
|
|
|
}
|
2018-06-03 01:35:32 +02:00
|
|
|
if (a_state == Stat::State::Ok) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (a_option.order == b_option.order) {
|
|
|
|
return a_option.use_http < b_option.use_http;
|
|
|
|
}
|
|
|
|
return a_option.order < b_option.order;
|
2018-06-03 01:35:32 +02:00
|
|
|
} else if (a_state == Stat::State::Error) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return a.error_at < b.error_at;
|
|
|
|
}
|
|
|
|
return a_option.order < b_option.order;
|
|
|
|
});
|
|
|
|
result.should_check = !result.stat->is_ok() || result.use_http || last_error_at > Time::now_cached() - 10;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DcOptionsSet::reset() {
|
2018-06-25 02:08:37 +02:00
|
|
|
options_.clear();
|
2018-12-31 20:04:05 +01:00
|
|
|
ordered_options_.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
DcOptionsSet::DcOptionInfo *DcOptionsSet::register_dc_option(DcOption &&option) {
|
2018-09-27 03:19:03 +02:00
|
|
|
auto info = make_unique<DcOptionInfo>(std::move(option), options_.size());
|
2018-12-31 20:04:05 +01:00
|
|
|
init_option_stat(info.get());
|
|
|
|
auto result = info.get();
|
|
|
|
options_.push_back(std::move(info));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DcOptionsSet::init_option_stat(DcOptionInfo *option_info) {
|
|
|
|
const auto &ip_address = option_info->option.get_ip_address();
|
2020-07-13 22:20:16 +02:00
|
|
|
auto it_ok = option_to_stat_id_.emplace(ip_address, 0);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (it_ok.second) {
|
2018-09-27 03:19:03 +02:00
|
|
|
it_ok.first->second = option_stats_.create(make_unique<OptionStat>());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
option_info->stat_id = it_ok.first->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
DcOptionsSet::OptionStat *DcOptionsSet::get_option_stat(const DcOptionInfo *option_info) {
|
|
|
|
return option_stats_.get(option_info->stat_id)->get();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|