2018-12-31 20:04:05 +01:00
|
|
|
//
|
2024-01-01 01:07:21 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#include "td/telegram/net/ConnectionCreator.h"
|
|
|
|
|
|
|
|
#include "td/telegram/ConfigManager.h"
|
|
|
|
#include "td/telegram/Global.h"
|
2023-03-01 21:49:22 +01:00
|
|
|
#include "td/telegram/LinkManager.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/logevent/LogEvent.h"
|
2018-05-08 22:02:15 +02:00
|
|
|
#include "td/telegram/MessagesManager.h"
|
|
|
|
#include "td/telegram/net/MtprotoHeader.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"
|
2020-04-30 20:00:58 +02:00
|
|
|
#include "td/telegram/Td.h"
|
2019-01-06 20:59:17 +01:00
|
|
|
#include "td/telegram/TdDb.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-05-06 18:59:49 +02:00
|
|
|
#include "td/mtproto/Ping.h"
|
2019-07-23 00:50:12 +02:00
|
|
|
#include "td/mtproto/ProxySecret.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/mtproto/RawConnection.h"
|
2019-06-26 17:27:19 +02:00
|
|
|
#include "td/mtproto/TlsInit.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
#include "td/net/GetHostByNameActor.h"
|
2018-07-26 15:49:18 +02:00
|
|
|
#include "td/net/HttpProxy.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/net/Socks5.h"
|
2018-07-26 02:28:02 +02:00
|
|
|
#include "td/net/TransparentProxy.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-01-01 13:59:53 +01:00
|
|
|
#include "td/utils/algorithm.h"
|
2018-07-27 02:54:25 +02:00
|
|
|
#include "td/utils/base64.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/format.h"
|
|
|
|
#include "td/utils/logging.h"
|
2018-05-08 17:08:26 +02:00
|
|
|
#include "td/utils/misc.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/port/IPAddress.h"
|
2019-05-10 18:27:18 +02:00
|
|
|
#include "td/utils/Random.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/ScopeGuard.h"
|
2021-05-17 14:21:11 +02:00
|
|
|
#include "td/utils/SliceBuilder.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Time.h"
|
|
|
|
#include "td/utils/tl_helpers.h"
|
|
|
|
|
2018-05-14 21:00:38 +02:00
|
|
|
#include <utility>
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
namespace td {
|
2018-02-07 21:48:40 +01:00
|
|
|
|
2018-10-31 11:54:06 +01:00
|
|
|
int VERBOSITY_NAME(connections) = VERBOSITY_NAME(INFO);
|
2018-08-17 16:02:28 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
namespace detail {
|
2018-02-07 21:48:40 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
class StatsCallback final : public mtproto::RawConnection::StatsCallback {
|
|
|
|
public:
|
|
|
|
StatsCallback(std::shared_ptr<NetStatsCallback> net_stats_callback, ActorId<ConnectionCreator> connection_creator,
|
2022-11-23 17:37:32 +01:00
|
|
|
uint32 hash, DcOptionsSet::Stat *option_stat)
|
2018-12-31 20:04:05 +01:00
|
|
|
: net_stats_callback_(std::move(net_stats_callback))
|
|
|
|
, connection_creator_(std::move(connection_creator))
|
|
|
|
, hash_(hash)
|
|
|
|
, option_stat_(option_stat) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_read(uint64 bytes) final {
|
2023-03-13 15:26:23 +01:00
|
|
|
if (net_stats_callback_ != nullptr) {
|
|
|
|
net_stats_callback_->on_read(bytes);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
void on_write(uint64 bytes) final {
|
2023-03-13 15:26:23 +01:00
|
|
|
if (net_stats_callback_ != nullptr) {
|
|
|
|
net_stats_callback_->on_write(bytes);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void on_pong() final {
|
2018-05-08 16:48:30 +02:00
|
|
|
if (option_stat_) {
|
|
|
|
send_lambda(connection_creator_, [stat = option_stat_] { stat->on_ok(); });
|
|
|
|
}
|
2018-05-15 14:21:09 +02:00
|
|
|
send_closure(connection_creator_, &ConnectionCreator::on_pong, hash_);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-05-15 14:21:09 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void on_error() final {
|
2018-05-08 16:48:30 +02:00
|
|
|
if (option_stat_) {
|
|
|
|
send_lambda(connection_creator_, [stat = option_stat_] { stat->on_error(); });
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void on_mtproto_error() final {
|
|
|
|
send_closure(connection_creator_, &ConnectionCreator::on_mtproto_error, hash_);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::shared_ptr<NetStatsCallback> net_stats_callback_;
|
|
|
|
ActorId<ConnectionCreator> connection_creator_;
|
2022-11-23 17:37:32 +01:00
|
|
|
uint32 hash_;
|
2018-12-31 20:04:05 +01:00
|
|
|
DcOptionsSet::Stat *option_stat_;
|
|
|
|
};
|
2018-02-07 21:48:40 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
ConnectionCreator::ClientInfo::ClientInfo() {
|
2020-08-05 19:07:28 +02:00
|
|
|
sanity_flood_control.add_limit(5, 10);
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
flood_control.add_limit(1, 1);
|
|
|
|
flood_control.add_limit(4, 2);
|
|
|
|
flood_control.add_limit(8, 3);
|
|
|
|
|
|
|
|
flood_control_online.add_limit(1, 4);
|
|
|
|
flood_control_online.add_limit(5, 5);
|
|
|
|
|
|
|
|
mtproto_error_flood_control.add_limit(1, 1);
|
|
|
|
mtproto_error_flood_control.add_limit(4, 2);
|
|
|
|
mtproto_error_flood_control.add_limit(8, 3);
|
|
|
|
}
|
|
|
|
|
2023-05-14 22:00:56 +02:00
|
|
|
uint64 ConnectionCreator::ClientInfo::extract_session_id() {
|
2019-05-10 18:27:18 +02:00
|
|
|
if (!session_ids_.empty()) {
|
|
|
|
auto res = *session_ids_.begin();
|
|
|
|
session_ids_.erase(session_ids_.begin());
|
|
|
|
return res;
|
|
|
|
}
|
2023-05-14 22:00:56 +02:00
|
|
|
uint64 res = 0;
|
2019-05-10 18:27:18 +02:00
|
|
|
while (res == 0) {
|
2023-05-14 22:00:56 +02:00
|
|
|
res = Random::secure_uint64();
|
2019-05-10 18:27:18 +02:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2023-05-14 22:00:56 +02:00
|
|
|
void ConnectionCreator::ClientInfo::add_session_id(uint64 session_id) {
|
2019-05-10 18:27:18 +02:00
|
|
|
if (session_id != 0) {
|
|
|
|
session_ids_.insert(session_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
ConnectionCreator::ConnectionCreator(ActorShared<> parent) : parent_(std::move(parent)) {
|
|
|
|
}
|
|
|
|
|
2023-05-05 12:51:19 +02:00
|
|
|
ConnectionCreator::ConnectionCreator(ConnectionCreator &&) = default;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2023-05-05 12:51:19 +02:00
|
|
|
ConnectionCreator &ConnectionCreator::operator=(ConnectionCreator &&) = default;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
ConnectionCreator::~ConnectionCreator() = default;
|
|
|
|
|
|
|
|
void ConnectionCreator::set_net_stats_callback(std::shared_ptr<NetStatsCallback> common_callback,
|
|
|
|
std::shared_ptr<NetStatsCallback> media_callback) {
|
|
|
|
common_net_stats_callback_ = std::move(common_callback);
|
|
|
|
media_net_stats_callback_ = std::move(media_callback);
|
|
|
|
}
|
|
|
|
|
2018-07-27 17:10:45 +02:00
|
|
|
void ConnectionCreator::add_proxy(int32 old_proxy_id, string server, int32 port, bool enable,
|
2018-05-14 21:00:38 +02:00
|
|
|
td_api::object_ptr<td_api::ProxyType> proxy_type,
|
|
|
|
Promise<td_api::object_ptr<td_api::proxy>> promise) {
|
2021-09-28 16:31:00 +02:00
|
|
|
TRY_RESULT_PROMISE(promise, new_proxy, Proxy::create_proxy(std::move(server), port, proxy_type.get()));
|
2018-07-27 17:10:45 +02:00
|
|
|
if (old_proxy_id >= 0) {
|
|
|
|
if (proxies_.count(old_proxy_id) == 0) {
|
|
|
|
return promise.set_error(Status::Error(400, "Proxy not found"));
|
|
|
|
}
|
|
|
|
auto &old_proxy = proxies_[old_proxy_id];
|
|
|
|
if (old_proxy == new_proxy) {
|
|
|
|
if (enable) {
|
|
|
|
enable_proxy_impl(old_proxy_id);
|
|
|
|
}
|
|
|
|
return promise.set_value(get_proxy_object(old_proxy_id));
|
|
|
|
}
|
|
|
|
if (old_proxy_id == active_proxy_id_) {
|
|
|
|
enable = true;
|
|
|
|
disable_proxy_impl();
|
|
|
|
}
|
|
|
|
|
|
|
|
proxies_.erase(old_proxy_id);
|
|
|
|
G()->td_db()->get_binlog_pmc()->erase(get_proxy_used_database_key(old_proxy_id));
|
|
|
|
proxy_last_used_date_.erase(old_proxy_id);
|
|
|
|
proxy_last_used_saved_date_.erase(old_proxy_id);
|
2023-11-23 22:09:36 +01:00
|
|
|
} else {
|
|
|
|
#if TD_EMSCRIPTEN || TD_DARWIN_WATCH_OS
|
|
|
|
return promise.set_error(Status::Error(400, "The method is unsupported for the platform"));
|
|
|
|
#endif
|
2018-07-27 17:10:45 +02:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-05-14 21:00:38 +02:00
|
|
|
auto proxy_id = [&] {
|
|
|
|
for (auto &proxy : proxies_) {
|
|
|
|
if (proxy.second == new_proxy) {
|
|
|
|
return proxy.first;
|
|
|
|
}
|
2018-05-08 22:02:15 +02:00
|
|
|
}
|
2018-05-14 21:00:38 +02:00
|
|
|
|
2018-07-27 17:10:45 +02:00
|
|
|
int32 proxy_id = old_proxy_id;
|
|
|
|
if (proxy_id < 0) {
|
|
|
|
CHECK(max_proxy_id_ >= 2);
|
|
|
|
proxy_id = max_proxy_id_++;
|
|
|
|
G()->td_db()->get_binlog_pmc()->set("proxy_max_id", to_string(max_proxy_id_));
|
|
|
|
}
|
2022-06-02 15:19:16 +02:00
|
|
|
bool is_inserted = proxies_.emplace(proxy_id, std::move(new_proxy)).second;
|
|
|
|
CHECK(is_inserted);
|
2018-05-14 21:00:38 +02:00
|
|
|
G()->td_db()->get_binlog_pmc()->set(get_proxy_database_key(proxy_id),
|
|
|
|
log_event_store(proxies_[proxy_id]).as_slice().str());
|
2018-05-14 21:11:48 +02:00
|
|
|
return proxy_id;
|
2018-05-14 21:00:38 +02:00
|
|
|
}();
|
|
|
|
if (enable) {
|
|
|
|
enable_proxy_impl(proxy_id);
|
|
|
|
}
|
|
|
|
promise.set_value(get_proxy_object(proxy_id));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConnectionCreator::enable_proxy(int32 proxy_id, Promise<Unit> promise) {
|
|
|
|
if (proxies_.count(proxy_id) == 0) {
|
|
|
|
return promise.set_error(Status::Error(400, "Unknown proxy identifier"));
|
|
|
|
}
|
|
|
|
|
|
|
|
enable_proxy_impl(proxy_id);
|
|
|
|
promise.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConnectionCreator::disable_proxy(Promise<Unit> promise) {
|
2018-05-15 14:21:09 +02:00
|
|
|
save_proxy_last_used_date(0);
|
2018-05-14 21:00:38 +02:00
|
|
|
disable_proxy_impl();
|
|
|
|
promise.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConnectionCreator::remove_proxy(int32 proxy_id, Promise<Unit> promise) {
|
|
|
|
if (proxies_.count(proxy_id) == 0) {
|
|
|
|
return promise.set_error(Status::Error(400, "Unknown proxy identifier"));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (proxy_id == active_proxy_id_) {
|
|
|
|
disable_proxy_impl();
|
|
|
|
}
|
|
|
|
|
|
|
|
proxies_.erase(proxy_id);
|
|
|
|
|
|
|
|
G()->td_db()->get_binlog_pmc()->erase(get_proxy_database_key(proxy_id));
|
|
|
|
G()->td_db()->get_binlog_pmc()->erase(get_proxy_used_database_key(proxy_id));
|
|
|
|
promise.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConnectionCreator::get_proxies(Promise<td_api::object_ptr<td_api::proxies>> promise) {
|
|
|
|
promise.set_value(td_api::make_object<td_api::proxies>(
|
|
|
|
transform(proxies_, [this](const std::pair<int32, Proxy> &proxy) { return get_proxy_object(proxy.first); })));
|
|
|
|
}
|
|
|
|
|
2018-05-17 20:08:51 +02:00
|
|
|
void ConnectionCreator::get_proxy_link(int32 proxy_id, Promise<string> promise) {
|
2022-02-09 22:59:52 +01:00
|
|
|
auto it = proxies_.find(proxy_id);
|
|
|
|
if (it == proxies_.end()) {
|
2018-05-17 20:08:51 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Unknown proxy identifier"));
|
|
|
|
}
|
|
|
|
|
2023-03-02 00:41:22 +01:00
|
|
|
promise.set_result(LinkManager::get_proxy_link(it->second, false));
|
2018-05-17 20:08:51 +02:00
|
|
|
}
|
|
|
|
|
2019-02-04 23:32:37 +01:00
|
|
|
ActorId<GetHostByNameActor> ConnectionCreator::get_dns_resolver() {
|
2022-08-17 15:11:13 +02:00
|
|
|
if (G()->get_option_boolean("expect_blocking", true)) {
|
2019-02-04 23:32:37 +01:00
|
|
|
if (block_get_host_by_name_actor_.empty()) {
|
|
|
|
VLOG(connections) << "Init block bypass DNS resolver";
|
|
|
|
GetHostByNameActor::Options options;
|
|
|
|
options.scheduler_id = G()->get_gc_scheduler_id();
|
|
|
|
options.resolver_types = {GetHostByNameActor::ResolverType::Google, GetHostByNameActor::ResolverType::Native};
|
|
|
|
options.ok_timeout = 60;
|
|
|
|
options.error_timeout = 0;
|
|
|
|
block_get_host_by_name_actor_ = create_actor<GetHostByNameActor>("BlockDnsResolverActor", std::move(options));
|
|
|
|
}
|
2019-02-04 23:24:46 +01:00
|
|
|
return block_get_host_by_name_actor_.get();
|
|
|
|
} else {
|
2019-02-04 23:32:37 +01:00
|
|
|
if (get_host_by_name_actor_.empty()) {
|
|
|
|
VLOG(connections) << "Init DNS resolver";
|
|
|
|
GetHostByNameActor::Options options;
|
|
|
|
options.scheduler_id = G()->get_gc_scheduler_id();
|
|
|
|
options.ok_timeout = 5 * 60 - 1;
|
|
|
|
options.error_timeout = 0;
|
|
|
|
get_host_by_name_actor_ = create_actor<GetHostByNameActor>("DnsResolverActor", std::move(options));
|
|
|
|
}
|
2019-02-04 23:24:46 +01:00
|
|
|
return get_host_by_name_actor_.get();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-14 21:00:38 +02:00
|
|
|
void ConnectionCreator::ping_proxy(int32 proxy_id, Promise<double> promise) {
|
2019-08-30 03:56:01 +02:00
|
|
|
CHECK(!close_flag_);
|
2018-06-02 14:20:35 +02:00
|
|
|
if (proxy_id == 0) {
|
2021-10-05 23:55:22 +02:00
|
|
|
auto main_dc_id = G()->net_query_dispatcher().get_main_dc_id();
|
2022-08-17 15:11:13 +02:00
|
|
|
bool prefer_ipv6 = G()->get_option_boolean("prefer_ipv6");
|
2018-07-27 02:54:25 +02:00
|
|
|
auto infos = dc_options_set_.find_all_connections(main_dc_id, false, false, prefer_ipv6, false);
|
2018-06-04 17:55:38 +02:00
|
|
|
if (infos.empty()) {
|
|
|
|
return promise.set_error(Status::Error(400, "Can't find valid DC address"));
|
|
|
|
}
|
|
|
|
const size_t MAX_CONNECTIONS = 10;
|
|
|
|
if (infos.size() > MAX_CONNECTIONS) {
|
|
|
|
infos.resize(MAX_CONNECTIONS);
|
2018-06-02 14:20:35 +02:00
|
|
|
}
|
2018-06-04 17:55:38 +02:00
|
|
|
|
|
|
|
auto token = next_token();
|
|
|
|
auto &request = ping_main_dc_requests_[token];
|
|
|
|
request.promise = std::move(promise);
|
|
|
|
request.left_queries = infos.size();
|
|
|
|
request.result = Status::Error(400, "Failed to ping");
|
|
|
|
|
|
|
|
for (auto &info : infos) {
|
2019-07-22 13:32:21 +02:00
|
|
|
auto r_transport_type = get_transport_type(Proxy(), info);
|
2018-06-04 17:55:38 +02:00
|
|
|
if (r_transport_type.is_error()) {
|
|
|
|
LOG(ERROR) << r_transport_type.error();
|
|
|
|
on_ping_main_dc_result(token, r_transport_type.move_as_error());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-03-27 03:19:22 +01:00
|
|
|
auto ip_address = info.option->get_ip_address();
|
|
|
|
auto r_socket_fd = SocketFd::open(ip_address);
|
2018-06-04 17:55:38 +02:00
|
|
|
if (r_socket_fd.is_error()) {
|
|
|
|
LOG(DEBUG) << "Failed to open socket: " << r_socket_fd.error();
|
|
|
|
on_ping_main_dc_result(token, r_socket_fd.move_as_error());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-10-22 18:59:15 +02:00
|
|
|
ping_proxy_buffered_socket_fd(std::move(ip_address), BufferedFd<SocketFd>(r_socket_fd.move_as_ok()),
|
|
|
|
r_transport_type.move_as_ok(), PSTRING() << info.option->get_ip_address(),
|
|
|
|
PromiseCreator::lambda([actor_id = actor_id(this), token](Result<double> result) {
|
|
|
|
send_closure(actor_id, &ConnectionCreator::on_ping_main_dc_result, token,
|
|
|
|
std::move(result));
|
|
|
|
}));
|
2018-06-04 17:55:38 +02:00
|
|
|
}
|
|
|
|
return;
|
2018-06-02 14:20:35 +02:00
|
|
|
}
|
|
|
|
|
2018-05-18 22:43:38 +02:00
|
|
|
auto it = proxies_.find(proxy_id);
|
|
|
|
if (it == proxies_.end()) {
|
2018-05-14 21:00:38 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Unknown proxy identifier"));
|
|
|
|
}
|
2018-05-19 17:05:38 +02:00
|
|
|
const Proxy &proxy = it->second;
|
2022-08-17 15:11:13 +02:00
|
|
|
bool prefer_ipv6 = G()->get_option_boolean("prefer_ipv6");
|
2019-02-04 23:32:37 +01:00
|
|
|
send_closure(get_dns_resolver(), &GetHostByNameActor::run, proxy.server().str(), proxy.port(), prefer_ipv6,
|
2018-05-18 22:43:38 +02:00
|
|
|
PromiseCreator::lambda([actor_id = actor_id(this), promise = std::move(promise),
|
|
|
|
proxy_id](Result<IPAddress> result) mutable {
|
|
|
|
if (result.is_error()) {
|
2019-06-18 02:17:14 +02:00
|
|
|
return promise.set_error(Status::Error(400, result.error().public_message()));
|
2018-05-18 22:43:38 +02:00
|
|
|
}
|
|
|
|
send_closure(actor_id, &ConnectionCreator::ping_proxy_resolved, proxy_id, result.move_as_ok(),
|
|
|
|
std::move(promise));
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConnectionCreator::ping_proxy_resolved(int32 proxy_id, IPAddress ip_address, Promise<double> promise) {
|
|
|
|
auto it = proxies_.find(proxy_id);
|
|
|
|
if (it == proxies_.end()) {
|
|
|
|
return promise.set_error(Status::Error(400, "Unknown proxy identifier"));
|
|
|
|
}
|
2019-07-22 13:32:21 +02:00
|
|
|
const Proxy &proxy = it->second;
|
2021-10-05 23:55:22 +02:00
|
|
|
auto main_dc_id = G()->net_query_dispatcher().get_main_dc_id();
|
2018-05-18 22:43:38 +02:00
|
|
|
FindConnectionExtra extra;
|
2019-07-22 04:42:19 +02:00
|
|
|
auto r_socket_fd = find_connection(proxy, ip_address, main_dc_id, false, extra);
|
2018-05-18 22:43:38 +02:00
|
|
|
if (r_socket_fd.is_error()) {
|
2019-06-18 02:17:14 +02:00
|
|
|
return promise.set_error(Status::Error(400, r_socket_fd.error().public_message()));
|
2018-05-18 22:43:38 +02:00
|
|
|
}
|
|
|
|
auto socket_fd = r_socket_fd.move_as_ok();
|
|
|
|
|
2020-06-10 19:34:45 +02:00
|
|
|
auto connection_promise = PromiseCreator::lambda(
|
2023-07-11 16:53:43 +02:00
|
|
|
[actor_id = actor_id(this), ip_address, promise = std::move(promise), transport_type = extra.transport_type,
|
2021-11-10 18:05:41 +01:00
|
|
|
debug_str = extra.debug_str](Result<ConnectionData> r_connection_data) mutable {
|
2019-07-20 15:13:36 +02:00
|
|
|
if (r_connection_data.is_error()) {
|
|
|
|
return promise.set_error(Status::Error(400, r_connection_data.error().public_message()));
|
2018-05-18 22:43:38 +02:00
|
|
|
}
|
2021-10-22 18:59:15 +02:00
|
|
|
auto connection_data = r_connection_data.move_as_ok();
|
|
|
|
send_closure(actor_id, &ConnectionCreator::ping_proxy_buffered_socket_fd, ip_address,
|
|
|
|
std::move(connection_data.buffered_socket_fd), std::move(transport_type), std::move(debug_str),
|
2021-03-26 16:04:01 +01:00
|
|
|
std::move(promise));
|
2018-05-18 22:43:38 +02:00
|
|
|
});
|
|
|
|
CHECK(proxy.use_proxy());
|
2019-07-20 15:13:36 +02:00
|
|
|
auto token = next_token();
|
2021-03-26 16:04:01 +01:00
|
|
|
auto ref = prepare_connection(extra.ip_address, std::move(socket_fd), proxy, extra.mtproto_ip_address,
|
|
|
|
extra.transport_type, "Ping", extra.debug_str, nullptr, create_reference(token), false,
|
|
|
|
std::move(connection_promise));
|
2019-07-20 15:13:36 +02:00
|
|
|
if (!ref.empty()) {
|
|
|
|
children_[token] = {false, std::move(ref)};
|
2018-05-18 22:43:38 +02:00
|
|
|
}
|
|
|
|
}
|
2018-05-14 21:00:38 +02:00
|
|
|
|
2021-10-22 18:59:15 +02:00
|
|
|
void ConnectionCreator::ping_proxy_buffered_socket_fd(IPAddress ip_address, BufferedFd<SocketFd> buffered_socket_fd,
|
|
|
|
mtproto::TransportType transport_type, string debug_str,
|
|
|
|
Promise<double> promise) {
|
2018-05-18 22:43:38 +02:00
|
|
|
auto token = next_token();
|
2021-03-26 16:04:01 +01:00
|
|
|
auto raw_connection =
|
2021-10-22 18:59:15 +02:00
|
|
|
mtproto::RawConnection::create(ip_address, std::move(buffered_socket_fd), std::move(transport_type), nullptr);
|
2019-05-06 18:59:49 +02:00
|
|
|
children_[token] = {
|
2021-10-18 18:26:14 +02:00
|
|
|
false, create_ping_actor(debug_str, std::move(raw_connection), nullptr,
|
2019-05-06 18:59:49 +02:00
|
|
|
PromiseCreator::lambda([promise = std::move(promise)](
|
|
|
|
Result<unique_ptr<mtproto::RawConnection>> result) mutable {
|
|
|
|
if (result.is_error()) {
|
2019-06-18 02:17:14 +02:00
|
|
|
return promise.set_error(Status::Error(400, result.error().public_message()));
|
2019-05-06 18:59:49 +02:00
|
|
|
}
|
2021-03-26 16:04:01 +01:00
|
|
|
auto ping_time = result.ok()->extra().rtt;
|
2019-05-06 18:59:49 +02:00
|
|
|
promise.set_value(std::move(ping_time));
|
|
|
|
}),
|
|
|
|
create_reference(token))};
|
2018-05-14 21:00:38 +02:00
|
|
|
}
|
|
|
|
|
2018-09-17 20:15:11 +02:00
|
|
|
void ConnectionCreator::set_active_proxy_id(int32 proxy_id, bool from_binlog) {
|
|
|
|
active_proxy_id_ = proxy_id;
|
|
|
|
if (proxy_id == 0) {
|
2022-08-17 15:11:13 +02:00
|
|
|
G()->set_option_empty("enabled_proxy_id");
|
2018-09-17 20:15:11 +02:00
|
|
|
} else {
|
2022-08-17 15:11:13 +02:00
|
|
|
G()->set_option_integer("enabled_proxy_id", proxy_id);
|
2018-09-17 20:15:11 +02:00
|
|
|
}
|
|
|
|
if (!from_binlog) {
|
|
|
|
if (proxy_id == 0) {
|
|
|
|
G()->td_db()->get_binlog_pmc()->erase("proxy_active_id");
|
2022-06-06 19:36:15 +02:00
|
|
|
send_closure(G()->config_manager(), &ConfigManager::request_config, false);
|
2018-09-17 20:15:11 +02:00
|
|
|
} else {
|
|
|
|
G()->td_db()->get_binlog_pmc()->set("proxy_active_id", to_string(proxy_id));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-14 21:00:38 +02:00
|
|
|
void ConnectionCreator::enable_proxy_impl(int32 proxy_id) {
|
|
|
|
CHECK(proxies_.count(proxy_id) == 1);
|
|
|
|
if (proxy_id == active_proxy_id_) {
|
2018-02-19 22:44:52 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-14 21:00:38 +02:00
|
|
|
if ((active_proxy_id_ != 0 && proxies_[active_proxy_id_].type() == Proxy::Type::Mtproto) ||
|
|
|
|
proxies_[proxy_id].type() == Proxy::Type::Mtproto) {
|
2018-05-16 21:35:27 +02:00
|
|
|
update_mtproto_header(proxies_[proxy_id]);
|
2018-05-10 20:02:24 +02:00
|
|
|
}
|
2018-05-15 14:21:09 +02:00
|
|
|
save_proxy_last_used_date(0);
|
2018-05-10 20:02:24 +02:00
|
|
|
|
2018-09-17 20:15:11 +02:00
|
|
|
set_active_proxy_id(proxy_id);
|
2018-05-10 20:02:24 +02:00
|
|
|
|
2018-05-14 21:00:38 +02:00
|
|
|
on_proxy_changed(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConnectionCreator::disable_proxy_impl() {
|
|
|
|
if (active_proxy_id_ == 0) {
|
2020-04-30 20:00:58 +02:00
|
|
|
send_closure(G()->messages_manager(), &MessagesManager::remove_sponsored_dialog);
|
2023-03-22 17:52:06 +01:00
|
|
|
send_closure(G()->td(), &Td::reload_promo_data);
|
2018-05-14 21:00:38 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
CHECK(proxies_.count(active_proxy_id_) == 1);
|
|
|
|
|
|
|
|
if (proxies_[active_proxy_id_].type() == Proxy::Type::Mtproto) {
|
2018-05-16 21:35:27 +02:00
|
|
|
update_mtproto_header(Proxy());
|
2018-05-14 21:00:38 +02:00
|
|
|
}
|
|
|
|
|
2018-09-17 20:15:11 +02:00
|
|
|
set_active_proxy_id(0);
|
2018-05-14 21:00:38 +02:00
|
|
|
|
|
|
|
on_proxy_changed(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConnectionCreator::on_proxy_changed(bool from_db) {
|
2018-05-23 17:51:58 +02:00
|
|
|
send_closure(G()->state_manager(), &StateManager::on_proxy,
|
2018-08-17 18:03:07 +02:00
|
|
|
active_proxy_id_ != 0 && proxies_[active_proxy_id_].type() != Proxy::Type::Mtproto &&
|
|
|
|
proxies_[active_proxy_id_].type() != Proxy::Type::HttpCaching);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
if (!from_db) {
|
|
|
|
for (auto &child : children_) {
|
2018-05-19 17:05:38 +02:00
|
|
|
if (child.second.first) {
|
|
|
|
child.second.second.reset();
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-27 02:54:25 +02:00
|
|
|
VLOG(connections) << "Drop proxy IP address " << proxy_ip_address_;
|
2018-12-31 20:04:05 +01:00
|
|
|
resolve_proxy_query_token_ = 0;
|
|
|
|
resolve_proxy_timestamp_ = Timestamp();
|
|
|
|
proxy_ip_address_ = IPAddress();
|
2018-05-08 22:02:15 +02:00
|
|
|
|
2018-05-14 21:00:38 +02:00
|
|
|
if (active_proxy_id_ == 0 || !from_db) {
|
2020-04-30 20:00:58 +02:00
|
|
|
send_closure(G()->messages_manager(), &MessagesManager::remove_sponsored_dialog);
|
2018-05-08 22:02:15 +02:00
|
|
|
}
|
2023-03-22 17:52:06 +01:00
|
|
|
send_closure(G()->td(), &Td::reload_promo_data);
|
2018-05-14 21:00:38 +02:00
|
|
|
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
|
|
|
|
string ConnectionCreator::get_proxy_database_key(int32 proxy_id) {
|
|
|
|
CHECK(proxy_id > 0);
|
|
|
|
if (proxy_id == 1) {
|
|
|
|
return "proxy";
|
|
|
|
}
|
|
|
|
return PSTRING() << "proxy" << proxy_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2018-05-14 21:00:38 +02:00
|
|
|
string ConnectionCreator::get_proxy_used_database_key(int32 proxy_id) {
|
|
|
|
CHECK(proxy_id > 0);
|
|
|
|
return PSTRING() << "proxy_used" << proxy_id;
|
|
|
|
}
|
|
|
|
|
2018-05-15 14:21:09 +02:00
|
|
|
void ConnectionCreator::save_proxy_last_used_date(int32 delay) {
|
|
|
|
if (active_proxy_id_ == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CHECK(delay >= 0);
|
|
|
|
int32 date = proxy_last_used_date_[active_proxy_id_];
|
|
|
|
int32 &saved_date = proxy_last_used_saved_date_[active_proxy_id_];
|
|
|
|
if (date <= saved_date + delay) {
|
|
|
|
return;
|
|
|
|
}
|
2018-05-15 14:21:53 +02:00
|
|
|
LOG(DEBUG) << "Save proxy last used date " << date;
|
2018-05-15 14:21:09 +02:00
|
|
|
|
|
|
|
saved_date = date;
|
|
|
|
G()->td_db()->get_binlog_pmc()->set(get_proxy_used_database_key(active_proxy_id_), to_string(date));
|
|
|
|
}
|
|
|
|
|
2018-05-14 21:00:38 +02:00
|
|
|
td_api::object_ptr<td_api::proxy> ConnectionCreator::get_proxy_object(int32 proxy_id) const {
|
|
|
|
auto it = proxies_.find(proxy_id);
|
|
|
|
CHECK(it != proxies_.end());
|
|
|
|
const Proxy &proxy = it->second;
|
|
|
|
td_api::object_ptr<td_api::ProxyType> type;
|
|
|
|
switch (proxy.type()) {
|
|
|
|
case Proxy::Type::Socks5:
|
|
|
|
type = make_tl_object<td_api::proxyTypeSocks5>(proxy.user().str(), proxy.password().str());
|
|
|
|
break;
|
2018-07-27 02:54:25 +02:00
|
|
|
case Proxy::Type::HttpTcp:
|
|
|
|
type = make_tl_object<td_api::proxyTypeHttp>(proxy.user().str(), proxy.password().str(), false);
|
|
|
|
break;
|
|
|
|
case Proxy::Type::HttpCaching:
|
|
|
|
type = make_tl_object<td_api::proxyTypeHttp>(proxy.user().str(), proxy.password().str(), true);
|
2018-07-26 15:49:18 +02:00
|
|
|
break;
|
2018-05-14 21:00:38 +02:00
|
|
|
case Proxy::Type::Mtproto:
|
2019-07-09 04:01:12 +02:00
|
|
|
type = make_tl_object<td_api::proxyTypeMtproto>(proxy.secret().get_encoded_secret());
|
2018-05-14 21:00:38 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
auto last_used_date_it = proxy_last_used_date_.find(proxy_id);
|
|
|
|
auto last_used_date = last_used_date_it == proxy_last_used_date_.end() ? 0 : last_used_date_it->second;
|
|
|
|
return make_tl_object<td_api::proxy>(proxy_id, proxy.server().str(), proxy.port(), last_used_date,
|
|
|
|
proxy_id == active_proxy_id_, std::move(type));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConnectionCreator::on_network(bool network_flag, uint32 network_generation) {
|
2018-07-27 02:54:25 +02:00
|
|
|
VLOG(connections) << "Receive network flag " << network_flag << " with generation " << network_generation;
|
2018-12-31 20:04:05 +01:00
|
|
|
network_flag_ = network_flag;
|
|
|
|
auto old_generation = network_generation_;
|
|
|
|
network_generation_ = network_generation;
|
|
|
|
if (network_flag_) {
|
2018-08-06 17:28:56 +02:00
|
|
|
VLOG(connections) << "Set proxy query token to 0: " << old_generation << " " << network_generation_;
|
|
|
|
resolve_proxy_query_token_ = 0;
|
|
|
|
resolve_proxy_timestamp_ = Timestamp();
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
for (auto &client : clients_) {
|
|
|
|
client.second.backoff.clear();
|
2020-08-05 19:07:28 +02:00
|
|
|
client.second.sanity_flood_control.clear_events();
|
2018-12-31 20:04:05 +01:00
|
|
|
client.second.flood_control.clear_events();
|
|
|
|
client.second.flood_control_online.clear_events();
|
|
|
|
client_loop(client.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (old_generation != network_generation_) {
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-05-08 22:02:15 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void ConnectionCreator::on_online(bool online_flag) {
|
2018-07-27 02:54:25 +02:00
|
|
|
VLOG(connections) << "Receive online flag " << online_flag;
|
2019-03-31 21:29:34 +02:00
|
|
|
bool need_drop_flood_control = online_flag || !online_flag_;
|
2018-12-31 20:04:05 +01:00
|
|
|
online_flag_ = online_flag;
|
2019-03-31 21:29:34 +02:00
|
|
|
if (need_drop_flood_control) {
|
2018-12-31 20:04:05 +01:00
|
|
|
for (auto &client : clients_) {
|
|
|
|
client.second.backoff.clear();
|
2020-08-05 19:36:13 +02:00
|
|
|
client.second.sanity_flood_control.clear_events();
|
2018-12-31 20:04:05 +01:00
|
|
|
client.second.flood_control_online.clear_events();
|
|
|
|
client_loop(client.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-05 18:49:34 +02:00
|
|
|
void ConnectionCreator::on_logging_out(bool is_logging_out) {
|
2020-08-05 19:36:13 +02:00
|
|
|
if (is_logging_out_ == is_logging_out) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-05 18:49:34 +02:00
|
|
|
VLOG(connections) << "Receive logging out flag " << is_logging_out;
|
|
|
|
is_logging_out_ = is_logging_out;
|
|
|
|
for (auto &client : clients_) {
|
|
|
|
client.second.backoff.clear();
|
2020-08-05 19:36:13 +02:00
|
|
|
client.second.sanity_flood_control.clear_events();
|
2020-08-05 18:49:34 +02:00
|
|
|
client.second.flood_control_online.clear_events();
|
|
|
|
client_loop(client.second);
|
|
|
|
}
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-11-23 17:37:32 +01:00
|
|
|
void ConnectionCreator::on_pong(uint32 hash) {
|
2019-12-25 14:57:18 +01:00
|
|
|
G()->save_server_time();
|
2018-05-15 14:21:09 +02:00
|
|
|
if (active_proxy_id_ != 0) {
|
|
|
|
auto now = G()->unix_time();
|
|
|
|
int32 &last_used = proxy_last_used_date_[active_proxy_id_];
|
|
|
|
if (now > last_used) {
|
|
|
|
last_used = now;
|
|
|
|
save_proxy_last_used_date(MAX_PROXY_LAST_USED_SAVE_DELAY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-23 17:37:32 +01:00
|
|
|
void ConnectionCreator::on_mtproto_error(uint32 hash) {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto &client = clients_[hash];
|
|
|
|
client.hash = hash;
|
2022-11-21 19:07:56 +01:00
|
|
|
client.mtproto_error_flood_control.add_event(Time::now_cached());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConnectionCreator::request_raw_connection(DcId dc_id, bool allow_media_only, bool is_media,
|
2022-11-23 17:37:32 +01:00
|
|
|
Promise<unique_ptr<mtproto::RawConnection>> promise, uint32 hash,
|
2019-05-06 18:59:49 +02:00
|
|
|
unique_ptr<mtproto::AuthData> auth_data) {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto &client = clients_[hash];
|
|
|
|
if (!client.inited) {
|
|
|
|
client.inited = true;
|
|
|
|
client.hash = hash;
|
|
|
|
client.dc_id = dc_id;
|
|
|
|
client.allow_media_only = allow_media_only;
|
|
|
|
client.is_media = is_media;
|
|
|
|
} else {
|
|
|
|
CHECK(client.hash == hash);
|
|
|
|
CHECK(client.dc_id == dc_id);
|
|
|
|
CHECK(client.allow_media_only == allow_media_only);
|
|
|
|
CHECK(client.is_media == is_media);
|
|
|
|
}
|
2019-05-06 20:53:39 +02:00
|
|
|
client.auth_data = std::move(auth_data);
|
2019-05-09 21:57:35 +02:00
|
|
|
client.auth_data_generation++;
|
2018-05-14 21:00:38 +02:00
|
|
|
VLOG(connections) << "Request connection for " << tag("client", format::as_hex(client.hash)) << " to " << dc_id << " "
|
2018-12-31 20:04:05 +01:00
|
|
|
<< tag("allow_media_only", allow_media_only);
|
|
|
|
client.queries.push_back(std::move(promise));
|
|
|
|
|
|
|
|
client_loop(client);
|
|
|
|
}
|
|
|
|
|
2019-07-09 16:45:38 +02:00
|
|
|
void ConnectionCreator::request_raw_connection_by_ip(IPAddress ip_address, mtproto::TransportType transport_type,
|
2018-09-27 03:19:03 +02:00
|
|
|
Promise<unique_ptr<mtproto::RawConnection>> promise) {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto r_socket_fd = SocketFd::open(ip_address);
|
|
|
|
if (r_socket_fd.is_error()) {
|
|
|
|
return promise.set_error(r_socket_fd.move_as_error());
|
|
|
|
}
|
2019-07-09 16:45:38 +02:00
|
|
|
auto socket_fd = r_socket_fd.move_as_ok();
|
|
|
|
|
2023-07-11 16:53:43 +02:00
|
|
|
auto connection_promise = PromiseCreator::lambda([actor_id = actor_id(this), promise = std::move(promise),
|
2021-03-26 16:04:01 +01:00
|
|
|
transport_type, network_generation = network_generation_,
|
|
|
|
ip_address](Result<ConnectionData> r_connection_data) mutable {
|
|
|
|
if (r_connection_data.is_error()) {
|
|
|
|
return promise.set_error(Status::Error(400, r_connection_data.error().public_message()));
|
|
|
|
}
|
2021-10-22 18:59:15 +02:00
|
|
|
auto connection_data = r_connection_data.move_as_ok();
|
|
|
|
auto raw_connection = mtproto::RawConnection::create(ip_address, std::move(connection_data.buffered_socket_fd),
|
|
|
|
transport_type, nullptr);
|
2021-03-26 16:04:01 +01:00
|
|
|
raw_connection->extra().extra = network_generation;
|
|
|
|
promise.set_value(std::move(raw_connection));
|
|
|
|
});
|
2019-07-09 16:45:38 +02:00
|
|
|
|
2019-07-20 15:13:36 +02:00
|
|
|
auto token = next_token();
|
2021-03-26 16:04:01 +01:00
|
|
|
auto ref = prepare_connection(ip_address, std::move(socket_fd), Proxy(), IPAddress(), transport_type, "Raw",
|
2019-07-22 04:55:01 +02:00
|
|
|
PSTRING() << "to IP address " << ip_address, nullptr, create_reference(token), false,
|
|
|
|
std::move(connection_promise));
|
2019-07-20 15:13:36 +02:00
|
|
|
if (!ref.empty()) {
|
|
|
|
children_[token] = {false, std::move(ref)};
|
2019-07-09 16:45:38 +02:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-07-22 13:32:21 +02:00
|
|
|
Result<mtproto::TransportType> ConnectionCreator::get_transport_type(const Proxy &proxy,
|
2018-06-04 00:17:59 +02:00
|
|
|
const DcOptionsSet::ConnectionInfo &info) {
|
|
|
|
int32 int_dc_id = info.option->get_dc_id().get_raw_id();
|
2018-05-18 22:43:38 +02:00
|
|
|
if (G()->is_test_dc()) {
|
|
|
|
int_dc_id += 10000;
|
|
|
|
}
|
2021-10-19 17:11:16 +02:00
|
|
|
auto raw_dc_id = narrow_cast<int16>(info.option->is_media_only() ? -int_dc_id : int_dc_id);
|
2018-05-18 22:43:38 +02:00
|
|
|
|
|
|
|
if (proxy.use_mtproto_proxy()) {
|
2019-07-22 13:32:21 +02:00
|
|
|
return mtproto::TransportType{mtproto::TransportType::ObfuscatedTcp, raw_dc_id, proxy.secret()};
|
2018-05-18 22:43:38 +02:00
|
|
|
}
|
2018-07-27 02:54:25 +02:00
|
|
|
if (proxy.use_http_caching_proxy()) {
|
|
|
|
CHECK(info.option != nullptr);
|
|
|
|
string proxy_authorization;
|
2019-07-22 13:32:21 +02:00
|
|
|
if (!proxy.user().empty() || !proxy.password().empty()) {
|
2019-09-15 05:19:46 +02:00
|
|
|
proxy_authorization = "|basic " + base64_encode(PSLICE() << proxy.user() << ':' << proxy.password());
|
2018-07-27 02:54:25 +02:00
|
|
|
}
|
2020-05-16 22:12:52 +02:00
|
|
|
return mtproto::TransportType{mtproto::TransportType::Http, 0,
|
|
|
|
mtproto::ProxySecret::from_raw(
|
|
|
|
PSTRING() << info.option->get_ip_address().get_ip_host() << proxy_authorization)};
|
2018-07-27 02:54:25 +02:00
|
|
|
}
|
2018-05-18 22:43:38 +02:00
|
|
|
|
|
|
|
if (info.use_http) {
|
2019-07-09 04:01:12 +02:00
|
|
|
return mtproto::TransportType{mtproto::TransportType::Http, 0, mtproto::ProxySecret()};
|
2018-05-18 22:43:38 +02:00
|
|
|
} else {
|
2019-07-09 04:01:12 +02:00
|
|
|
return mtproto::TransportType{mtproto::TransportType::ObfuscatedTcp, raw_dc_id, info.option->get_secret()};
|
2018-06-04 00:17:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-22 13:32:21 +02:00
|
|
|
Result<SocketFd> ConnectionCreator::find_connection(const Proxy &proxy, const IPAddress &proxy_ip_address, DcId dc_id,
|
|
|
|
bool allow_media_only, FindConnectionExtra &extra) {
|
2020-05-16 22:32:32 +02:00
|
|
|
extra.debug_str = PSTRING() << "Failed to find valid IP address for " << dc_id;
|
2022-08-17 15:11:13 +02:00
|
|
|
bool prefer_ipv6 = G()->get_option_boolean("prefer_ipv6") || (proxy.use_proxy() && proxy_ip_address.is_ipv6());
|
2018-07-27 02:54:25 +02:00
|
|
|
bool only_http = proxy.use_http_caching_proxy();
|
2021-09-24 12:41:22 +02:00
|
|
|
#if TD_DARWIN_WATCH_OS
|
2021-03-26 16:04:01 +01:00
|
|
|
only_http = true;
|
|
|
|
#endif
|
2018-07-27 02:54:25 +02:00
|
|
|
TRY_RESULT(info, dc_options_set_.find_connection(
|
|
|
|
dc_id, allow_media_only, proxy.use_proxy() && proxy.use_socks5_proxy(), prefer_ipv6, only_http));
|
2018-06-04 00:17:59 +02:00
|
|
|
extra.stat = info.stat;
|
2019-12-08 07:57:33 +01:00
|
|
|
TRY_RESULT_ASSIGN(extra.transport_type, get_transport_type(proxy, info));
|
2018-06-04 00:17:59 +02:00
|
|
|
|
2018-08-12 11:51:24 +02:00
|
|
|
extra.debug_str = PSTRING() << " to " << (info.option->is_media_only() ? "MEDIA " : "") << dc_id
|
2018-06-04 00:17:59 +02:00
|
|
|
<< (info.use_http ? " over HTTP" : "");
|
|
|
|
|
|
|
|
if (proxy.use_mtproto_proxy()) {
|
2019-10-26 15:58:38 +02:00
|
|
|
extra.debug_str = PSTRING() << "MTProto " << proxy_ip_address << extra.debug_str;
|
2018-06-04 00:17:59 +02:00
|
|
|
|
2020-02-22 16:53:56 +01:00
|
|
|
VLOG(connections) << "Create: " << extra.debug_str;
|
2019-07-22 04:42:19 +02:00
|
|
|
return SocketFd::open(proxy_ip_address);
|
2018-05-18 22:43:38 +02:00
|
|
|
}
|
2018-06-04 00:17:59 +02:00
|
|
|
|
2018-05-18 22:43:38 +02:00
|
|
|
extra.check_mode |= info.should_check;
|
|
|
|
|
2018-07-27 02:54:25 +02:00
|
|
|
if (proxy.use_proxy()) {
|
2020-05-16 22:32:32 +02:00
|
|
|
extra.mtproto_ip_address = info.option->get_ip_address();
|
2021-03-26 16:04:01 +01:00
|
|
|
extra.ip_address = proxy_ip_address;
|
2018-08-17 16:02:28 +02:00
|
|
|
extra.debug_str = PSTRING() << (proxy.use_socks5_proxy() ? "Socks5" : (only_http ? "HTTP_ONLY" : "HTTP_TCP")) << ' '
|
2020-05-16 22:32:32 +02:00
|
|
|
<< proxy_ip_address << " --> " << extra.mtproto_ip_address << extra.debug_str;
|
2018-05-18 22:43:38 +02:00
|
|
|
} else {
|
2021-03-26 16:04:01 +01:00
|
|
|
extra.ip_address = info.option->get_ip_address();
|
2018-06-04 00:17:59 +02:00
|
|
|
extra.debug_str = PSTRING() << info.option->get_ip_address() << extra.debug_str;
|
2018-05-18 22:43:38 +02:00
|
|
|
}
|
2021-03-26 16:04:01 +01:00
|
|
|
VLOG(connections) << "Create: " << extra.debug_str;
|
|
|
|
return SocketFd::open(extra.ip_address);
|
2018-05-18 22:43:38 +02:00
|
|
|
}
|
|
|
|
|
2021-03-26 16:04:01 +01:00
|
|
|
ActorOwn<> ConnectionCreator::prepare_connection(IPAddress ip_address, SocketFd socket_fd, const Proxy &proxy,
|
|
|
|
const IPAddress &mtproto_ip_address,
|
2021-10-19 17:11:16 +02:00
|
|
|
const mtproto::TransportType &transport_type, Slice actor_name_prefix,
|
2021-03-26 16:04:01 +01:00
|
|
|
Slice debug_str,
|
|
|
|
unique_ptr<mtproto::RawConnection::StatsCallback> stats_callback,
|
|
|
|
ActorShared<> parent, bool use_connection_token,
|
|
|
|
Promise<ConnectionData> promise) {
|
2019-07-20 15:13:36 +02:00
|
|
|
if (proxy.use_socks5_proxy() || proxy.use_http_tcp_proxy() || transport_type.secret.emulate_tls()) {
|
2019-07-22 04:04:18 +02:00
|
|
|
VLOG(connections) << "Create new transparent proxy connection " << debug_str;
|
2021-07-04 04:58:54 +02:00
|
|
|
class Callback final : public TransparentProxy::Callback {
|
2019-07-20 15:13:36 +02:00
|
|
|
public:
|
2021-03-26 16:04:01 +01:00
|
|
|
explicit Callback(Promise<ConnectionData> promise, IPAddress ip_address,
|
2020-01-07 09:02:36 +01:00
|
|
|
unique_ptr<mtproto::RawConnection::StatsCallback> stats_callback, bool use_connection_token,
|
|
|
|
bool was_connected)
|
2019-07-20 15:13:36 +02:00
|
|
|
: promise_(std::move(promise))
|
2021-03-26 16:04:01 +01:00
|
|
|
, ip_address_(std::move(ip_address))
|
2019-07-20 15:13:36 +02:00
|
|
|
, stats_callback_(std::move(stats_callback))
|
2020-01-07 09:02:36 +01:00
|
|
|
, use_connection_token_(use_connection_token)
|
|
|
|
, was_connected_(was_connected) {
|
2019-07-20 15:13:36 +02:00
|
|
|
}
|
2021-10-22 18:59:15 +02:00
|
|
|
void set_result(Result<BufferedFd<SocketFd>> r_buffered_socket_fd) final {
|
|
|
|
if (r_buffered_socket_fd.is_error()) {
|
2019-07-20 15:13:36 +02:00
|
|
|
if (use_connection_token_) {
|
2021-09-16 18:09:39 +02:00
|
|
|
connection_token_ = mtproto::ConnectionManager::ConnectionToken();
|
2019-07-20 15:13:36 +02:00
|
|
|
}
|
|
|
|
if (was_connected_ && stats_callback_) {
|
|
|
|
stats_callback_->on_error();
|
|
|
|
}
|
2021-10-22 18:59:15 +02:00
|
|
|
promise_.set_error(Status::Error(400, r_buffered_socket_fd.error().public_message()));
|
2019-07-20 15:13:36 +02:00
|
|
|
} else {
|
|
|
|
ConnectionData data;
|
2021-03-26 16:04:01 +01:00
|
|
|
data.ip_address = ip_address_;
|
2021-10-22 18:59:15 +02:00
|
|
|
data.buffered_socket_fd = r_buffered_socket_fd.move_as_ok();
|
2019-07-20 15:13:36 +02:00
|
|
|
data.connection_token = std::move(connection_token_);
|
|
|
|
data.stats_callback = std::move(stats_callback_);
|
|
|
|
promise_.set_value(std::move(data));
|
|
|
|
}
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_connected() final {
|
2019-07-20 15:13:36 +02:00
|
|
|
if (use_connection_token_) {
|
2021-09-16 18:09:39 +02:00
|
|
|
connection_token_ = mtproto::ConnectionManager::connection_proxy(
|
|
|
|
static_cast<ActorId<mtproto::ConnectionManager>>(G()->state_manager()));
|
2019-07-20 15:13:36 +02:00
|
|
|
}
|
|
|
|
was_connected_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Promise<ConnectionData> promise_;
|
2021-09-16 18:09:39 +02:00
|
|
|
mtproto::ConnectionManager::ConnectionToken connection_token_;
|
2021-03-26 16:04:01 +01:00
|
|
|
IPAddress ip_address_;
|
2019-07-20 15:13:36 +02:00
|
|
|
unique_ptr<mtproto::RawConnection::StatsCallback> stats_callback_;
|
2021-10-22 18:59:15 +02:00
|
|
|
bool use_connection_token_{false};
|
2020-01-07 09:02:36 +01:00
|
|
|
bool was_connected_{false};
|
2019-07-20 15:13:36 +02:00
|
|
|
};
|
2020-02-22 16:53:56 +01:00
|
|
|
VLOG(connections) << "Start "
|
|
|
|
<< (proxy.use_socks5_proxy() ? "Socks5" : (proxy.use_http_tcp_proxy() ? "HTTP" : "TLS")) << ": "
|
|
|
|
<< debug_str;
|
2021-03-26 16:04:01 +01:00
|
|
|
auto callback = make_unique<Callback>(std::move(promise), ip_address, std::move(stats_callback),
|
|
|
|
use_connection_token, !proxy.use_socks5_proxy());
|
2019-07-20 15:13:36 +02:00
|
|
|
if (proxy.use_socks5_proxy()) {
|
2019-07-22 04:55:01 +02:00
|
|
|
return ActorOwn<>(create_actor<Socks5>(PSLICE() << actor_name_prefix << "Socks5", std::move(socket_fd),
|
2020-05-16 22:32:32 +02:00
|
|
|
mtproto_ip_address, proxy.user().str(), proxy.password().str(),
|
2019-07-22 04:55:01 +02:00
|
|
|
std::move(callback), std::move(parent)));
|
2019-07-20 15:13:36 +02:00
|
|
|
} else if (proxy.use_http_tcp_proxy()) {
|
2019-07-22 04:55:01 +02:00
|
|
|
return ActorOwn<>(create_actor<HttpProxy>(PSLICE() << actor_name_prefix << "HttpProxy", std::move(socket_fd),
|
2020-05-16 22:32:32 +02:00
|
|
|
mtproto_ip_address, proxy.user().str(), proxy.password().str(),
|
2019-07-20 15:13:36 +02:00
|
|
|
std::move(callback), std::move(parent)));
|
|
|
|
} else if (transport_type.secret.emulate_tls()) {
|
2019-07-22 04:55:01 +02:00
|
|
|
return ActorOwn<>(create_actor<mtproto::TlsInit>(
|
|
|
|
PSLICE() << actor_name_prefix << "TlsInit", std::move(socket_fd), transport_type.secret.get_domain(),
|
|
|
|
transport_type.secret.get_proxy_secret().str(), std::move(callback), std::move(parent),
|
|
|
|
G()->get_dns_time_difference()));
|
2019-07-20 15:13:36 +02:00
|
|
|
} else {
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
} else {
|
2019-07-22 04:04:18 +02:00
|
|
|
VLOG(connections) << "Create new direct connection " << debug_str;
|
2019-07-20 15:13:36 +02:00
|
|
|
|
|
|
|
ConnectionData data;
|
2021-03-26 16:04:01 +01:00
|
|
|
data.ip_address = ip_address;
|
2021-10-22 18:59:15 +02:00
|
|
|
data.buffered_socket_fd = BufferedFd<SocketFd>(std::move(socket_fd));
|
2019-07-20 15:13:36 +02:00
|
|
|
data.stats_callback = std::move(stats_callback);
|
|
|
|
promise.set_result(std::move(data));
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void ConnectionCreator::client_loop(ClientInfo &client) {
|
|
|
|
CHECK(client.hash != 0);
|
|
|
|
if (!network_flag_) {
|
2018-07-27 02:54:25 +02:00
|
|
|
VLOG(connections) << "Exit client_loop, because there is no network";
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (close_flag_) {
|
2018-07-27 02:54:25 +02:00
|
|
|
VLOG(connections) << "Exit client_loop, because of closing";
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-05-14 21:00:38 +02:00
|
|
|
|
2019-07-22 13:32:21 +02:00
|
|
|
Proxy proxy = active_proxy_id_ == 0 ? Proxy() : proxies_[active_proxy_id_];
|
|
|
|
|
2019-07-22 04:42:19 +02:00
|
|
|
if (proxy.use_proxy() && !proxy_ip_address_.is_valid()) {
|
|
|
|
VLOG(connections) << "Exit client_loop, because there is no valid IP address for proxy: " << proxy_ip_address_;
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-21 16:58:20 +01:00
|
|
|
VLOG(connections) << "In client_loop: " << tag("client", format::as_hex(client.hash));
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
// Remove expired ready connections
|
2019-10-21 15:25:56 +02:00
|
|
|
td::remove_if(client.ready_connections,
|
|
|
|
[&, expires_at = Time::now_cached() - ClientInfo::READY_CONNECTIONS_TIMEOUT](auto &v) {
|
|
|
|
bool drop = v.second < expires_at;
|
|
|
|
VLOG_IF(connections, drop) << "Drop expired " << tag("connection", v.first.get());
|
|
|
|
return drop;
|
|
|
|
});
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
// Send ready connections into promises
|
|
|
|
{
|
|
|
|
auto begin = client.queries.begin();
|
|
|
|
auto it = begin;
|
|
|
|
while (it != client.queries.end() && !client.ready_connections.empty()) {
|
2021-05-31 20:51:48 +02:00
|
|
|
if (!it->is_canceled()) {
|
2018-06-13 18:20:42 +02:00
|
|
|
VLOG(connections) << "Send to promise " << tag("connection", client.ready_connections.back().first.get());
|
|
|
|
it->set_value(std::move(client.ready_connections.back().first));
|
|
|
|
client.ready_connections.pop_back();
|
|
|
|
}
|
2018-04-23 00:14:49 +02:00
|
|
|
++it;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
client.queries.erase(begin, it);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Main loop. Create new connections till needed
|
2018-05-18 22:43:38 +02:00
|
|
|
bool check_mode = client.checking_connections != 0 && !proxy.use_proxy();
|
2018-12-31 20:04:05 +01:00
|
|
|
while (true) {
|
|
|
|
// Check if we need new connections
|
|
|
|
if (client.queries.empty()) {
|
|
|
|
if (!client.ready_connections.empty()) {
|
|
|
|
client_set_timeout_at(client, Time::now() + ClientInfo::READY_CONNECTIONS_TIMEOUT);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (check_mode) {
|
|
|
|
if (client.checking_connections >= 3) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (client.pending_connections >= client.queries.size()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-05 18:49:34 +02:00
|
|
|
bool act_as_if_online = online_flag_ || is_logging_out_;
|
2018-12-31 20:04:05 +01:00
|
|
|
// Check flood
|
2020-08-05 18:49:34 +02:00
|
|
|
auto &flood_control = act_as_if_online ? client.flood_control_online : client.flood_control;
|
2018-02-12 11:37:54 +01:00
|
|
|
auto wakeup_at = max(flood_control.get_wakeup_at(), client.mtproto_error_flood_control.get_wakeup_at());
|
2020-08-05 19:07:28 +02:00
|
|
|
wakeup_at = max(client.sanity_flood_control.get_wakeup_at(), wakeup_at);
|
|
|
|
|
2020-08-05 18:49:34 +02:00
|
|
|
if (!act_as_if_online) {
|
2022-11-21 19:07:56 +01:00
|
|
|
wakeup_at = max(wakeup_at, static_cast<double>(client.backoff.get_wakeup_at()));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (wakeup_at > Time::now()) {
|
|
|
|
return client_set_timeout_at(client, wakeup_at);
|
|
|
|
}
|
2022-11-21 19:07:56 +01:00
|
|
|
client.sanity_flood_control.add_event(Time::now());
|
2020-08-05 19:36:13 +02:00
|
|
|
if (!act_as_if_online) {
|
|
|
|
client.backoff.add_event(static_cast<int32>(Time::now()));
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
// Create new RawConnection
|
|
|
|
// sync part
|
2018-05-18 22:43:38 +02:00
|
|
|
FindConnectionExtra extra;
|
2019-07-22 04:42:19 +02:00
|
|
|
auto r_socket_fd = find_connection(proxy, proxy_ip_address_, client.dc_id, client.allow_media_only, extra);
|
2018-05-18 22:43:38 +02:00
|
|
|
check_mode |= extra.check_mode;
|
2018-12-31 20:04:05 +01:00
|
|
|
if (r_socket_fd.is_error()) {
|
2018-05-24 17:09:27 +02:00
|
|
|
LOG(WARNING) << extra.debug_str << ": " << r_socket_fd.error();
|
2018-05-18 22:43:38 +02:00
|
|
|
if (extra.stat) {
|
|
|
|
extra.stat->on_error(); // TODO: different kind of error
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
return client_set_timeout_at(client, Time::now() + 0.1);
|
|
|
|
}
|
|
|
|
|
2020-08-05 19:07:28 +02:00
|
|
|
// Events with failed socket creation are ignored
|
2022-11-21 19:07:56 +01:00
|
|
|
flood_control.add_event(Time::now());
|
2020-08-05 19:07:28 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
auto socket_fd = r_socket_fd.move_as_ok();
|
2023-06-16 12:08:50 +02:00
|
|
|
#if !TD_DARWIN_WATCH_OS
|
2018-12-31 20:04:05 +01:00
|
|
|
IPAddress debug_ip;
|
|
|
|
auto debug_ip_status = debug_ip.init_socket_address(socket_fd);
|
|
|
|
if (debug_ip_status.is_ok()) {
|
2018-05-18 22:43:38 +02:00
|
|
|
extra.debug_str = PSTRING() << extra.debug_str << " from " << debug_ip;
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
|
|
|
LOG(ERROR) << debug_ip_status;
|
|
|
|
}
|
2023-06-16 12:08:50 +02:00
|
|
|
#endif
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
client.pending_connections++;
|
|
|
|
if (check_mode) {
|
2018-05-18 22:43:38 +02:00
|
|
|
if (extra.stat) {
|
|
|
|
extra.stat->on_check();
|
2018-05-08 16:48:30 +02:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
client.checking_connections++;
|
|
|
|
}
|
|
|
|
|
2021-11-11 15:39:09 +01:00
|
|
|
auto promise = PromiseCreator::lambda(
|
|
|
|
[actor_id = actor_id(this), check_mode, transport_type = extra.transport_type, hash = client.hash,
|
|
|
|
debug_str = extra.debug_str,
|
|
|
|
network_generation = network_generation_](Result<ConnectionData> r_connection_data) mutable {
|
|
|
|
send_closure(actor_id, &ConnectionCreator::client_create_raw_connection, std::move(r_connection_data),
|
|
|
|
check_mode, std::move(transport_type), hash, std::move(debug_str), network_generation);
|
|
|
|
});
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-09-27 03:19:03 +02:00
|
|
|
auto stats_callback =
|
|
|
|
td::make_unique<detail::StatsCallback>(client.is_media ? media_net_stats_callback_ : common_net_stats_callback_,
|
|
|
|
actor_id(this), client.hash, extra.stat);
|
2019-07-20 15:13:36 +02:00
|
|
|
auto token = next_token();
|
2021-03-26 16:04:01 +01:00
|
|
|
auto ref = prepare_connection(extra.ip_address, std::move(socket_fd), proxy, extra.mtproto_ip_address,
|
|
|
|
extra.transport_type, Slice(), extra.debug_str, std::move(stats_callback),
|
|
|
|
create_reference(token), true, std::move(promise));
|
2019-07-20 15:13:36 +02:00
|
|
|
if (!ref.empty()) {
|
|
|
|
children_[token] = {true, std::move(ref)};
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConnectionCreator::client_create_raw_connection(Result<ConnectionData> r_connection_data, bool check_mode,
|
2022-11-23 17:37:32 +01:00
|
|
|
mtproto::TransportType transport_type, uint32 hash,
|
2018-04-30 19:01:18 +02:00
|
|
|
string debug_str, uint32 network_generation) {
|
2019-05-09 21:57:35 +02:00
|
|
|
unique_ptr<mtproto::AuthData> auth_data;
|
|
|
|
uint64 auth_data_generation{0};
|
2023-05-14 22:00:56 +02:00
|
|
|
uint64 session_id{0};
|
2019-05-09 21:57:35 +02:00
|
|
|
if (check_mode) {
|
|
|
|
auto it = clients_.find(hash);
|
|
|
|
CHECK(it != clients_.end());
|
|
|
|
const auto &auth_data_ptr = it->second.auth_data;
|
|
|
|
if (auth_data_ptr && auth_data_ptr->use_pfs() && auth_data_ptr->has_auth_key(Time::now_cached())) {
|
|
|
|
auth_data = make_unique<mtproto::AuthData>(*auth_data_ptr);
|
|
|
|
auth_data_generation = it->second.auth_data_generation;
|
2019-05-10 18:27:18 +02:00
|
|
|
session_id = it->second.extract_session_id();
|
|
|
|
auth_data->set_session_id(session_id);
|
2019-05-09 21:57:35 +02:00
|
|
|
}
|
|
|
|
}
|
2019-05-10 18:27:18 +02:00
|
|
|
auto promise = PromiseCreator::lambda([actor_id = actor_id(this), hash, check_mode, auth_data_generation, session_id,
|
2018-09-27 03:19:03 +02:00
|
|
|
debug_str](Result<unique_ptr<mtproto::RawConnection>> result) mutable {
|
2018-12-17 17:10:03 +01:00
|
|
|
if (result.is_ok()) {
|
|
|
|
VLOG(connections) << "Ready connection (" << (check_mode ? "" : "un") << "checked) " << result.ok().get() << ' '
|
2021-03-26 16:04:01 +01:00
|
|
|
<< tag("rtt", format::as_time(result.ok()->extra().rtt)) << ' ' << debug_str;
|
2018-12-17 17:10:03 +01:00
|
|
|
} else {
|
|
|
|
VLOG(connections) << "Failed connection (" << (check_mode ? "" : "un") << "checked) " << result.error() << ' '
|
|
|
|
<< debug_str;
|
|
|
|
}
|
2021-11-11 15:39:09 +01:00
|
|
|
send_closure(actor_id, &ConnectionCreator::client_add_connection, hash, std::move(result), check_mode,
|
2019-05-10 18:27:18 +02:00
|
|
|
auth_data_generation, session_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
if (r_connection_data.is_error()) {
|
|
|
|
return promise.set_error(r_connection_data.move_as_error());
|
|
|
|
}
|
|
|
|
|
|
|
|
auto connection_data = r_connection_data.move_as_ok();
|
2021-03-26 16:04:01 +01:00
|
|
|
auto raw_connection =
|
2021-10-22 18:59:15 +02:00
|
|
|
mtproto::RawConnection::create(connection_data.ip_address, std::move(connection_data.buffered_socket_fd),
|
2021-03-26 16:04:01 +01:00
|
|
|
std::move(transport_type), std::move(connection_data.stats_callback));
|
2018-12-31 20:04:05 +01:00
|
|
|
raw_connection->set_connection_token(std::move(connection_data.connection_token));
|
|
|
|
|
2021-03-26 16:04:01 +01:00
|
|
|
raw_connection->extra().extra = network_generation;
|
|
|
|
raw_connection->extra().debug_str = debug_str;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
if (check_mode) {
|
2019-05-09 21:57:35 +02:00
|
|
|
VLOG(connections) << "Start check: " << debug_str << " " << (auth_data ? "with" : "without") << " auth data";
|
2018-12-31 20:04:05 +01:00
|
|
|
auto token = next_token();
|
2019-05-06 20:53:39 +02:00
|
|
|
children_[token] = {true, create_ping_actor(debug_str, std::move(raw_connection), std::move(auth_data),
|
|
|
|
std::move(promise), create_reference(token))};
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
|
|
|
promise.set_value(std::move(raw_connection));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConnectionCreator::client_set_timeout_at(ClientInfo &client, double wakeup_at) {
|
|
|
|
if (!client.slot.has_event()) {
|
|
|
|
client.slot.set_event(self_closure(this, &ConnectionCreator::client_wakeup, client.hash));
|
|
|
|
}
|
|
|
|
client.slot.set_timeout_at(wakeup_at);
|
|
|
|
VLOG(connections) << tag("client", format::as_hex(client.hash)) << " set timeout in "
|
|
|
|
<< wakeup_at - Time::now_cached();
|
|
|
|
}
|
|
|
|
|
2022-11-23 17:37:32 +01:00
|
|
|
void ConnectionCreator::client_add_connection(uint32 hash, Result<unique_ptr<mtproto::RawConnection>> r_raw_connection,
|
2023-05-14 22:00:56 +02:00
|
|
|
bool check_flag, uint64 auth_data_generation, uint64 session_id) {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto &client = clients_[hash];
|
2019-05-10 18:27:18 +02:00
|
|
|
client.add_session_id(session_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(client.pending_connections > 0);
|
|
|
|
client.pending_connections--;
|
|
|
|
if (check_flag) {
|
|
|
|
CHECK(client.checking_connections > 0);
|
|
|
|
client.checking_connections--;
|
|
|
|
}
|
|
|
|
if (r_raw_connection.is_ok()) {
|
2018-11-20 10:09:52 +01:00
|
|
|
VLOG(connections) << "Add ready connection " << r_raw_connection.ok().get() << " for "
|
|
|
|
<< tag("client", format::as_hex(hash));
|
2018-12-31 20:04:05 +01:00
|
|
|
client.backoff.clear();
|
2020-07-13 22:20:16 +02:00
|
|
|
client.ready_connections.emplace_back(r_raw_connection.move_as_ok(), Time::now_cached());
|
2019-05-09 21:57:35 +02:00
|
|
|
} else {
|
|
|
|
if (r_raw_connection.error().code() == -404 && client.auth_data &&
|
|
|
|
client.auth_data_generation == auth_data_generation) {
|
|
|
|
VLOG(connections) << "Drop auth data from " << tag("client", format::as_hex(hash));
|
|
|
|
client.auth_data = nullptr;
|
|
|
|
client.auth_data_generation++;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
client_loop(client);
|
|
|
|
}
|
|
|
|
|
2022-11-23 17:37:32 +01:00
|
|
|
void ConnectionCreator::client_wakeup(uint32 hash) {
|
2020-02-22 16:53:56 +01:00
|
|
|
VLOG(connections) << tag("hash", format::as_hex(hash)) << " wakeup";
|
2019-12-25 14:57:18 +01:00
|
|
|
G()->save_server_time();
|
2018-12-31 20:04:05 +01:00
|
|
|
client_loop(clients_[hash]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConnectionCreator::on_dc_options(DcOptions new_dc_options) {
|
2020-02-22 16:53:56 +01:00
|
|
|
VLOG(connections) << "SAVE " << new_dc_options;
|
2018-12-31 20:04:05 +01:00
|
|
|
G()->td_db()->get_binlog_pmc()->set("dc_options", serialize(new_dc_options));
|
|
|
|
dc_options_set_.reset();
|
|
|
|
dc_options_set_.add_dc_options(get_default_dc_options(G()->is_test_dc()));
|
|
|
|
#if !TD_EMSCRIPTEN // FIXME
|
|
|
|
dc_options_set_.add_dc_options(std::move(new_dc_options));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConnectionCreator::on_dc_update(DcId dc_id, string ip_port, Promise<> promise) {
|
2021-10-19 17:11:16 +02:00
|
|
|
if (!dc_id.is_exact()) {
|
|
|
|
return promise.set_error(Status::Error("Invalid dc_id"));
|
|
|
|
}
|
|
|
|
|
|
|
|
IPAddress ip_address;
|
|
|
|
TRY_STATUS_PROMISE(promise, ip_address.init_host_port(ip_port));
|
|
|
|
DcOptions options;
|
|
|
|
options.dc_options.emplace_back(dc_id, ip_address);
|
|
|
|
send_closure(G()->config_manager(), &ConfigManager::on_dc_options_update, std::move(options));
|
|
|
|
promise.set_value(Unit());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2018-05-16 21:35:27 +02:00
|
|
|
void ConnectionCreator::update_mtproto_header(const Proxy &proxy) {
|
|
|
|
if (G()->have_mtproto_header()) {
|
|
|
|
G()->mtproto_header().set_proxy(proxy);
|
|
|
|
}
|
|
|
|
if (G()->have_net_query_dispatcher()) {
|
|
|
|
G()->net_query_dispatcher().update_mtproto_header();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void ConnectionCreator::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:
|
2018-05-08 22:02:15 +02:00
|
|
|
explicit StateCallback(ActorId<ConnectionCreator> connection_creator)
|
|
|
|
: connection_creator_(std::move(connection_creator)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
bool on_network(NetType network_type, uint32 generation) final {
|
2018-05-08 22:02:15 +02:00
|
|
|
send_closure(connection_creator_, &ConnectionCreator::on_network, network_type != NetType::None, generation);
|
|
|
|
return connection_creator_.is_alive();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
bool on_online(bool online_flag) final {
|
2018-05-08 22:02:15 +02:00
|
|
|
send_closure(connection_creator_, &ConnectionCreator::on_online, online_flag);
|
|
|
|
return connection_creator_.is_alive();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
bool on_logging_out(bool is_logging_out) final {
|
2020-08-05 18:49:34 +02:00
|
|
|
send_closure(connection_creator_, &ConnectionCreator::on_logging_out, is_logging_out);
|
|
|
|
return connection_creator_.is_alive();
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
private:
|
2018-05-08 22:02:15 +02:00
|
|
|
ActorId<ConnectionCreator> connection_creator_;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
send_closure(G()->state_manager(), &StateManager::add_callback, make_unique<StateCallback>(actor_id(this)));
|
|
|
|
|
|
|
|
auto serialized_dc_options = G()->td_db()->get_binlog_pmc()->get("dc_options");
|
|
|
|
DcOptions dc_options;
|
|
|
|
auto status = unserialize(dc_options, serialized_dc_options);
|
|
|
|
if (status.is_error()) {
|
|
|
|
on_dc_options(DcOptions());
|
|
|
|
} else {
|
|
|
|
on_dc_options(std::move(dc_options));
|
|
|
|
}
|
|
|
|
|
2023-10-12 22:41:46 +02:00
|
|
|
if (G()->td_db()->get_binlog_pmc()->get("proxy_max_id") != "2" ||
|
|
|
|
!G()->td_db()->get_binlog_pmc()->get(get_proxy_database_key(1)).empty()) {
|
|
|
|
// don't need to init proxies if they have never been added
|
|
|
|
init_proxies();
|
2023-10-14 02:06:01 +02:00
|
|
|
} else {
|
|
|
|
max_proxy_id_ = 2;
|
2023-10-12 22:41:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ref_cnt_guard_ = create_reference(-1);
|
|
|
|
|
|
|
|
is_inited_ = true;
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConnectionCreator::init_proxies() {
|
2018-05-14 21:00:38 +02:00
|
|
|
auto proxy_info = G()->td_db()->get_binlog_pmc()->prefix_get("proxy");
|
2020-04-23 19:27:46 +02:00
|
|
|
auto it = proxy_info.find("_max_id");
|
2018-05-14 21:00:38 +02:00
|
|
|
if (it != proxy_info.end()) {
|
|
|
|
max_proxy_id_ = to_integer<int32>(it->second);
|
|
|
|
proxy_info.erase(it);
|
|
|
|
}
|
2020-04-23 19:27:46 +02:00
|
|
|
it = proxy_info.find("_active_id");
|
2018-05-14 21:00:38 +02:00
|
|
|
if (it != proxy_info.end()) {
|
2018-09-17 20:15:11 +02:00
|
|
|
set_active_proxy_id(to_integer<int32>(it->second), true);
|
2018-05-14 21:00:38 +02:00
|
|
|
proxy_info.erase(it);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto &info : proxy_info) {
|
2020-04-23 19:27:46 +02:00
|
|
|
if (begins_with(info.first, "_used")) {
|
2021-10-19 17:11:16 +02:00
|
|
|
auto proxy_id = to_integer_safe<int32>(Slice(info.first).substr(5)).move_as_ok();
|
|
|
|
auto last_used = to_integer_safe<int32>(info.second).move_as_ok();
|
2022-02-09 22:59:52 +01:00
|
|
|
CHECK(proxy_id > 0);
|
2018-05-14 21:00:38 +02:00
|
|
|
proxy_last_used_date_[proxy_id] = last_used;
|
2018-05-15 14:21:09 +02:00
|
|
|
proxy_last_used_saved_date_[proxy_id] = last_used;
|
2018-05-14 21:00:38 +02:00
|
|
|
} else {
|
2019-02-12 17:17:20 +01:00
|
|
|
LOG_CHECK(!ends_with(info.first, "_max_id")) << info.first;
|
2021-10-19 17:11:16 +02:00
|
|
|
auto proxy_id = info.first.empty() ? static_cast<int32>(1) : to_integer_safe<int32>(info.first).move_as_ok();
|
2022-02-09 22:59:52 +01:00
|
|
|
CHECK(proxy_id > 0);
|
2018-05-14 21:00:38 +02:00
|
|
|
CHECK(proxies_.count(proxy_id) == 0);
|
|
|
|
log_event_parse(proxies_[proxy_id], info.second).ensure();
|
2018-05-15 18:44:24 +02:00
|
|
|
if (proxies_[proxy_id].type() == Proxy::Type::None) {
|
|
|
|
LOG_IF(ERROR, proxy_id != 1) << "Have empty proxy " << proxy_id;
|
2023-10-12 22:48:40 +02:00
|
|
|
G()->td_db()->get_binlog_pmc()->erase(get_proxy_database_key(proxy_id));
|
|
|
|
G()->td_db()->get_binlog_pmc()->erase(get_proxy_used_database_key(proxy_id));
|
2018-05-15 18:44:24 +02:00
|
|
|
proxies_.erase(proxy_id);
|
|
|
|
if (active_proxy_id_ == proxy_id) {
|
2018-09-17 20:15:11 +02:00
|
|
|
set_active_proxy_id(0);
|
2018-05-15 18:44:24 +02:00
|
|
|
}
|
|
|
|
}
|
2018-05-14 21:00:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (max_proxy_id_ == 0) {
|
|
|
|
// legacy one-proxy version
|
|
|
|
max_proxy_id_ = 2;
|
|
|
|
if (!proxies_.empty()) {
|
|
|
|
CHECK(proxies_.begin()->first == 1);
|
2018-09-17 20:15:11 +02:00
|
|
|
set_active_proxy_id(1);
|
2018-05-14 21:00:38 +02:00
|
|
|
}
|
|
|
|
G()->td_db()->get_binlog_pmc()->set("proxy_max_id", "2");
|
|
|
|
} else if (max_proxy_id_ < 2) {
|
|
|
|
LOG(ERROR) << "Found wrong max_proxy_id = " << max_proxy_id_;
|
|
|
|
max_proxy_id_ = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (active_proxy_id_ != 0) {
|
|
|
|
if (proxies_[active_proxy_id_].type() == Proxy::Type::Mtproto) {
|
2018-05-16 21:35:27 +02:00
|
|
|
update_mtproto_header(proxies_[active_proxy_id_]);
|
2018-05-14 21:00:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
on_proxy_changed(true);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConnectionCreator::hangup_shared() {
|
|
|
|
ref_cnt_--;
|
|
|
|
children_.erase(get_link_token());
|
|
|
|
if (ref_cnt_ == 0) {
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ActorShared<ConnectionCreator> ConnectionCreator::create_reference(int64 token) {
|
|
|
|
CHECK(token != 0);
|
|
|
|
ref_cnt_++;
|
|
|
|
return actor_shared(this, token);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConnectionCreator::hangup() {
|
|
|
|
close_flag_ = true;
|
2018-05-15 14:21:09 +02:00
|
|
|
save_proxy_last_used_date(0);
|
2018-12-31 20:04:05 +01:00
|
|
|
ref_cnt_guard_.reset();
|
|
|
|
for (auto &child : children_) {
|
2018-05-19 17:05:38 +02:00
|
|
|
child.second.second.reset();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DcOptions ConnectionCreator::get_default_dc_options(bool is_test) {
|
|
|
|
DcOptions res;
|
2018-09-13 05:08:49 +02:00
|
|
|
enum class HostType : int32 { IPv4, IPv6, Url };
|
2023-04-19 09:07:57 +02:00
|
|
|
auto add_ip_ports = [&res](int32 dc_id, vector<string> ip_address_strings, const vector<int> &ports,
|
2018-09-03 18:53:52 +02:00
|
|
|
HostType type = HostType::IPv4) {
|
2018-12-31 20:04:05 +01:00
|
|
|
IPAddress ip_address;
|
2023-04-19 09:07:57 +02:00
|
|
|
Random::shuffle(ip_address_strings);
|
2020-05-29 09:20:29 +02:00
|
|
|
for (auto port : ports) {
|
2023-04-19 09:07:57 +02:00
|
|
|
for (auto &ip_address_string : ip_address_strings) {
|
2018-09-03 18:53:52 +02:00
|
|
|
switch (type) {
|
|
|
|
case HostType::IPv4:
|
2023-04-19 09:07:57 +02:00
|
|
|
ip_address.init_ipv4_port(ip_address_string, port).ensure();
|
2018-09-03 18:53:52 +02:00
|
|
|
break;
|
|
|
|
case HostType::IPv6:
|
2023-04-19 09:07:57 +02:00
|
|
|
ip_address.init_ipv6_port(ip_address_string, port).ensure();
|
2018-09-03 18:53:52 +02:00
|
|
|
break;
|
|
|
|
case HostType::Url:
|
2023-04-19 09:07:57 +02:00
|
|
|
ip_address.init_host_port(ip_address_string, port).ensure();
|
2018-09-03 18:53:52 +02:00
|
|
|
break;
|
2018-03-14 13:27:55 +01:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
res.dc_options.emplace_back(DcId::internal(dc_id), ip_address);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
vector<int> ports = {443, 80, 5222};
|
2018-09-04 17:51:28 +02:00
|
|
|
#if TD_EMSCRIPTEN
|
|
|
|
if (is_test) {
|
|
|
|
add_ip_ports(1, {"pluto.web.telegram.org/apiws_test"}, {443}, HostType::Url);
|
|
|
|
add_ip_ports(2, {"venus.web.telegram.org/apiws_test"}, {443}, HostType::Url);
|
|
|
|
add_ip_ports(3, {"aurora.web.telegram.org/apiws_test"}, {443}, HostType::Url);
|
|
|
|
} else {
|
|
|
|
add_ip_ports(1, {"pluto.web.telegram.org/apiws"}, {443}, HostType::Url);
|
|
|
|
add_ip_ports(2, {"venus.web.telegram.org/apiws"}, {443}, HostType::Url);
|
|
|
|
add_ip_ports(3, {"aurora.web.telegram.org/apiws"}, {443}, HostType::Url);
|
|
|
|
add_ip_ports(4, {"vesta.web.telegram.org/apiws"}, {443}, HostType::Url);
|
|
|
|
add_ip_ports(5, {"flora.web.telegram.org/apiws"}, {443}, HostType::Url);
|
|
|
|
}
|
|
|
|
#else
|
2018-12-31 20:04:05 +01:00
|
|
|
if (is_test) {
|
|
|
|
add_ip_ports(1, {"149.154.175.10"}, ports);
|
|
|
|
add_ip_ports(2, {"149.154.167.40"}, ports);
|
|
|
|
add_ip_ports(3, {"149.154.175.117"}, ports);
|
2018-03-14 13:27:55 +01:00
|
|
|
|
2018-09-03 18:53:52 +02:00
|
|
|
add_ip_ports(1, {"2001:b28:f23d:f001::e"}, ports, HostType::IPv6);
|
|
|
|
add_ip_ports(2, {"2001:67c:4e8:f002::e"}, ports, HostType::IPv6);
|
|
|
|
add_ip_ports(3, {"2001:b28:f23d:f003::e"}, ports, HostType::IPv6);
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
|
|
|
add_ip_ports(1, {"149.154.175.50"}, ports);
|
2020-05-29 09:20:29 +02:00
|
|
|
add_ip_ports(2, {"149.154.167.51", "95.161.76.100"}, ports);
|
2018-12-31 20:04:05 +01:00
|
|
|
add_ip_ports(3, {"149.154.175.100"}, ports);
|
|
|
|
add_ip_ports(4, {"149.154.167.91"}, ports);
|
|
|
|
add_ip_ports(5, {"149.154.171.5"}, ports);
|
2018-03-14 13:27:55 +01:00
|
|
|
|
2018-09-03 18:53:52 +02:00
|
|
|
add_ip_ports(1, {"2001:b28:f23d:f001::a"}, ports, HostType::IPv6);
|
|
|
|
add_ip_ports(2, {"2001:67c:4e8:f002::a"}, ports, HostType::IPv6);
|
|
|
|
add_ip_ports(3, {"2001:b28:f23d:f003::a"}, ports, HostType::IPv6);
|
|
|
|
add_ip_ports(4, {"2001:67c:4e8:f004::a"}, ports, HostType::IPv6);
|
|
|
|
add_ip_ports(5, {"2001:b28:f23f:f005::a"}, ports, HostType::IPv6);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-09-04 17:51:28 +02:00
|
|
|
#endif
|
2018-12-31 20:04:05 +01:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConnectionCreator::loop() {
|
2022-08-10 12:57:41 +02:00
|
|
|
if (G()->close_flag()) {
|
2018-05-14 21:00:38 +02:00
|
|
|
return;
|
|
|
|
}
|
2022-08-10 12:57:41 +02:00
|
|
|
if (!is_inited_) {
|
2018-08-06 17:28:56 +02:00
|
|
|
return;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!network_flag_) {
|
|
|
|
return;
|
|
|
|
}
|
2018-05-08 22:02:15 +02:00
|
|
|
|
|
|
|
Timestamp timeout;
|
2018-05-14 21:00:38 +02:00
|
|
|
if (active_proxy_id_ != 0) {
|
2018-05-08 22:02:15 +02:00
|
|
|
if (resolve_proxy_timestamp_.is_in_past()) {
|
|
|
|
if (resolve_proxy_query_token_ == 0) {
|
|
|
|
resolve_proxy_query_token_ = next_token();
|
2018-05-14 21:00:38 +02:00
|
|
|
const Proxy &proxy = proxies_[active_proxy_id_];
|
2022-08-17 15:11:13 +02:00
|
|
|
bool prefer_ipv6 = G()->get_option_boolean("prefer_ipv6");
|
2018-07-27 02:54:25 +02:00
|
|
|
VLOG(connections) << "Resolve IP address " << resolve_proxy_query_token_ << " of " << proxy.server();
|
2018-05-08 22:02:15 +02:00
|
|
|
send_closure(
|
2019-02-04 23:32:37 +01:00
|
|
|
get_dns_resolver(), &GetHostByNameActor::run, proxy.server().str(), proxy.port(), prefer_ipv6,
|
2018-05-08 22:02:15 +02:00
|
|
|
PromiseCreator::lambda([actor_id = create_reference(resolve_proxy_query_token_)](Result<IPAddress> result) {
|
2021-11-11 15:39:09 +01:00
|
|
|
send_closure(actor_id, &ConnectionCreator::on_proxy_resolved, std::move(result), false);
|
2018-05-08 22:02:15 +02:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
CHECK(resolve_proxy_query_token_ == 0);
|
|
|
|
timeout.relax(resolve_proxy_timestamp_);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-05-08 22:02:15 +02:00
|
|
|
|
|
|
|
if (timeout) {
|
|
|
|
set_timeout_at(timeout.at());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-08 22:02:15 +02:00
|
|
|
void ConnectionCreator::on_proxy_resolved(Result<IPAddress> r_ip_address, bool dummy) {
|
2018-08-06 17:28:56 +02:00
|
|
|
SCOPE_EXIT {
|
|
|
|
loop();
|
|
|
|
};
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
if (get_link_token() != resolve_proxy_query_token_) {
|
2018-07-27 02:54:25 +02:00
|
|
|
VLOG(connections) << "Ignore unneeded proxy IP address " << get_link_token() << ", expected "
|
|
|
|
<< resolve_proxy_query_token_;
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-05-08 22:02:15 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
resolve_proxy_query_token_ = 0;
|
|
|
|
if (r_ip_address.is_error()) {
|
2018-07-27 02:54:25 +02:00
|
|
|
VLOG(connections) << "Receive error for resolving proxy IP address: " << r_ip_address.error();
|
2018-05-08 16:48:30 +02:00
|
|
|
resolve_proxy_timestamp_ = Timestamp::in(1 * 60);
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
proxy_ip_address_ = r_ip_address.move_as_ok();
|
2018-07-27 02:54:25 +02:00
|
|
|
VLOG(connections) << "Set proxy IP address to " << proxy_ip_address_;
|
2018-05-08 16:48:30 +02:00
|
|
|
resolve_proxy_timestamp_ = Timestamp::in(5 * 60);
|
2018-12-31 20:04:05 +01:00
|
|
|
for (auto &client : clients_) {
|
|
|
|
client_loop(client.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-04 17:55:38 +02:00
|
|
|
void ConnectionCreator::on_ping_main_dc_result(uint64 token, Result<double> result) {
|
|
|
|
auto &request = ping_main_dc_requests_[token];
|
|
|
|
CHECK(request.left_queries > 0);
|
|
|
|
if (result.is_error()) {
|
|
|
|
LOG(DEBUG) << "Receive ping error " << result.error();
|
|
|
|
if (request.result.is_error()) {
|
|
|
|
request.result = std::move(result);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
LOG(DEBUG) << "Receive ping result " << result.ok();
|
|
|
|
if (request.result.is_error() || request.result.ok() > result.ok()) {
|
|
|
|
request.result = result.ok();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (--request.left_queries == 0) {
|
|
|
|
if (request.result.is_error()) {
|
2019-06-18 02:17:14 +02:00
|
|
|
request.promise.set_error(Status::Error(400, request.result.error().public_message()));
|
2018-06-04 17:55:38 +02:00
|
|
|
} else {
|
|
|
|
request.promise.set_value(request.result.move_as_ok());
|
|
|
|
}
|
|
|
|
ping_main_dc_requests_.erase(token);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|