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
|
|
|
|
|
2018-06-26 01:43:11 +02:00
|
|
|
#include "td/telegram/net/DcId.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/net/DcOptions.h"
|
|
|
|
#include "td/telegram/net/DcOptionsSet.h"
|
2018-05-08 22:02:15 +02:00
|
|
|
#include "td/telegram/net/NetQuery.h"
|
2019-07-09 04:13:10 +02:00
|
|
|
#include "td/telegram/net/Proxy.h"
|
2021-10-27 16:32:09 +02:00
|
|
|
#include "td/telegram/td_api.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-05-06 18:59:49 +02:00
|
|
|
#include "td/mtproto/AuthData.h"
|
2021-09-16 18:09:39 +02:00
|
|
|
#include "td/mtproto/ConnectionManager.h"
|
2019-07-20 15:13:36 +02:00
|
|
|
#include "td/mtproto/RawConnection.h"
|
2019-07-22 04:04:18 +02:00
|
|
|
#include "td/mtproto/TransportType.h"
|
2018-04-30 19:01:18 +02:00
|
|
|
|
2019-07-09 04:13:10 +02:00
|
|
|
#include "td/net/NetStats.h"
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/actor/actor.h"
|
|
|
|
#include "td/actor/SignalSlot.h"
|
|
|
|
|
2021-10-22 18:59:15 +02:00
|
|
|
#include "td/utils/BufferedFd.h"
|
2019-02-12 22:26:36 +01:00
|
|
|
#include "td/utils/common.h"
|
2022-02-07 22:04:34 +01:00
|
|
|
#include "td/utils/FlatHashMap.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/FloodControlStrict.h"
|
2018-09-27 15:37:15 +02:00
|
|
|
#include "td/utils/logging.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/port/IPAddress.h"
|
|
|
|
#include "td/utils/port/SocketFd.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/Slice.h"
|
|
|
|
#include "td/utils/Status.h"
|
|
|
|
#include "td/utils/Time.h"
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
#include <memory>
|
2019-05-22 20:17:24 +02:00
|
|
|
#include <set>
|
2018-12-31 20:04:05 +01:00
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
namespace td {
|
2019-07-22 04:04:18 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
namespace detail {
|
|
|
|
class StatsCallback;
|
2019-07-22 04:04:18 +02:00
|
|
|
} // namespace detail
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
class GetHostByNameActor;
|
2019-07-22 04:04:18 +02:00
|
|
|
|
2018-09-26 01:59:29 +02:00
|
|
|
extern int VERBOSITY_NAME(connections);
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class ConnectionCreator final : public NetQueryCallback {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
|
|
|
explicit ConnectionCreator(ActorShared<> parent);
|
|
|
|
ConnectionCreator(ConnectionCreator &&other);
|
2021-10-24 01:09:36 +02:00
|
|
|
ConnectionCreator &operator=(ConnectionCreator &&other);
|
2021-07-03 22:51:36 +02:00
|
|
|
~ConnectionCreator() final;
|
2018-05-15 14:21:09 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void on_dc_options(DcOptions new_dc_options);
|
|
|
|
void on_dc_update(DcId dc_id, string ip_port, Promise<> promise);
|
2022-11-23 17:37:32 +01:00
|
|
|
void on_pong(uint32 hash);
|
|
|
|
void on_mtproto_error(uint32 hash);
|
2018-12-31 20:04:05 +01:00
|
|
|
void 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 = 0,
|
2019-05-06 18:59:49 +02:00
|
|
|
unique_ptr<mtproto::AuthData> auth_data = {});
|
2019-07-09 16:45:38 +02:00
|
|
|
void request_raw_connection_by_ip(IPAddress ip_address, mtproto::TransportType transport_type,
|
|
|
|
Promise<unique_ptr<mtproto::RawConnection>> promise);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
void set_net_stats_callback(std::shared_ptr<NetStatsCallback> common_callback,
|
|
|
|
std::shared_ptr<NetStatsCallback> media_callback);
|
|
|
|
|
2018-07-27 17:10:45 +02:00
|
|
|
void add_proxy(int32 old_proxy_id, string server, int32 port, bool enable,
|
|
|
|
td_api::object_ptr<td_api::ProxyType> proxy_type, Promise<td_api::object_ptr<td_api::proxy>> promise);
|
2018-05-14 21:00:38 +02:00
|
|
|
void enable_proxy(int32 proxy_id, Promise<Unit> promise);
|
|
|
|
void disable_proxy(Promise<Unit> promise);
|
|
|
|
void remove_proxy(int32 proxy_id, Promise<Unit> promise);
|
|
|
|
void get_proxies(Promise<td_api::object_ptr<td_api::proxies>> promise);
|
2018-05-17 20:08:51 +02:00
|
|
|
void get_proxy_link(int32 proxy_id, Promise<string> promise);
|
2018-05-14 21:00:38 +02:00
|
|
|
void ping_proxy(int32 proxy_id, Promise<double> promise);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-07-20 15:13:36 +02:00
|
|
|
struct ConnectionData {
|
2021-03-26 16:04:01 +01:00
|
|
|
IPAddress ip_address;
|
2021-10-22 18:59:15 +02:00
|
|
|
BufferedFd<SocketFd> buffered_socket_fd;
|
2021-09-16 18:09:39 +02:00
|
|
|
mtproto::ConnectionManager::ConnectionToken connection_token;
|
2019-07-20 15:13:36 +02:00
|
|
|
unique_ptr<mtproto::RawConnection::StatsCallback> stats_callback;
|
|
|
|
};
|
|
|
|
|
2019-07-22 04:04:18 +02:00
|
|
|
static DcOptions get_default_dc_options(bool is_test);
|
|
|
|
|
2021-03-26 16:52:45 +01:00
|
|
|
static ActorOwn<> prepare_connection(IPAddress ip_address, SocketFd socket_fd, const Proxy &proxy,
|
2021-10-19 17:11:16 +02:00
|
|
|
const IPAddress &mtproto_ip_address,
|
|
|
|
const mtproto::TransportType &transport_type, Slice actor_name_prefix,
|
|
|
|
Slice debug_str,
|
2019-07-20 15:13:36 +02:00
|
|
|
unique_ptr<mtproto::RawConnection::StatsCallback> stats_callback,
|
|
|
|
ActorShared<> parent, bool use_connection_token,
|
|
|
|
Promise<ConnectionData> promise);
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
private:
|
|
|
|
ActorShared<> parent_;
|
|
|
|
DcOptionsSet dc_options_set_;
|
|
|
|
bool network_flag_ = false;
|
|
|
|
uint32 network_generation_ = 0;
|
|
|
|
bool online_flag_ = false;
|
2020-08-05 18:49:34 +02:00
|
|
|
bool is_logging_out_ = false;
|
2018-05-14 21:00:38 +02:00
|
|
|
bool is_inited_ = false;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-05-15 14:21:09 +02:00
|
|
|
static constexpr int32 MAX_PROXY_LAST_USED_SAVE_DELAY = 60;
|
2018-05-14 21:00:38 +02:00
|
|
|
std::map<int32, Proxy> proxies_;
|
2022-02-07 20:41:07 +01:00
|
|
|
FlatHashMap<int32, int32> proxy_last_used_date_;
|
|
|
|
FlatHashMap<int32, int32> proxy_last_used_saved_date_;
|
2018-05-14 21:00:38 +02:00
|
|
|
int32 max_proxy_id_ = 0;
|
|
|
|
int32 active_proxy_id_ = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
ActorOwn<GetHostByNameActor> get_host_by_name_actor_;
|
2019-02-04 23:24:46 +01:00
|
|
|
ActorOwn<GetHostByNameActor> block_get_host_by_name_actor_;
|
2018-12-31 20:04:05 +01:00
|
|
|
IPAddress proxy_ip_address_;
|
|
|
|
Timestamp resolve_proxy_timestamp_;
|
|
|
|
uint64 resolve_proxy_query_token_{0};
|
|
|
|
|
|
|
|
struct ClientInfo {
|
|
|
|
class Backoff {
|
2024-03-11 20:47:54 +01:00
|
|
|
#if TD_ANDROID || TD_DARWIN_IOS || TD_DARWIN_VISION_OS || TD_DARWIN_WATCH_OS || TD_TIZEN
|
2018-12-31 20:04:05 +01:00
|
|
|
static constexpr int32 MAX_BACKOFF = 300;
|
|
|
|
#else
|
|
|
|
static constexpr int32 MAX_BACKOFF = 16;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
public:
|
|
|
|
void add_event(int32 now) {
|
|
|
|
wakeup_at_ = now + next_delay_;
|
2018-02-12 11:37:54 +01:00
|
|
|
next_delay_ = min(MAX_BACKOFF, next_delay_ * 2);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
int32 get_wakeup_at() const {
|
|
|
|
return wakeup_at_;
|
|
|
|
}
|
|
|
|
void clear() {
|
|
|
|
*this = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
int32 wakeup_at_{0};
|
|
|
|
int32 next_delay_ = 1;
|
|
|
|
};
|
|
|
|
ClientInfo();
|
2023-05-14 22:00:56 +02:00
|
|
|
uint64 extract_session_id();
|
|
|
|
void add_session_id(uint64 session_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
Backoff backoff;
|
2020-08-05 19:07:28 +02:00
|
|
|
FloodControlStrict sanity_flood_control;
|
2018-12-31 20:04:05 +01:00
|
|
|
FloodControlStrict flood_control;
|
|
|
|
FloodControlStrict flood_control_online;
|
|
|
|
FloodControlStrict mtproto_error_flood_control;
|
|
|
|
Slot slot;
|
|
|
|
size_t pending_connections{0};
|
|
|
|
size_t checking_connections{0};
|
2018-09-27 03:19:03 +02:00
|
|
|
std::vector<std::pair<unique_ptr<mtproto::RawConnection>, double>> ready_connections;
|
|
|
|
std::vector<Promise<unique_ptr<mtproto::RawConnection>>> queries;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
static constexpr double READY_CONNECTIONS_TIMEOUT = 10;
|
|
|
|
|
|
|
|
bool inited{false};
|
2022-11-23 17:37:32 +01:00
|
|
|
uint32 hash{0};
|
2018-12-31 20:04:05 +01:00
|
|
|
DcId dc_id;
|
2021-11-11 15:39:09 +01:00
|
|
|
bool allow_media_only{false};
|
|
|
|
bool is_media{false};
|
2023-05-14 22:00:56 +02:00
|
|
|
std::set<uint64> session_ids_;
|
2019-05-06 18:59:49 +02:00
|
|
|
unique_ptr<mtproto::AuthData> auth_data;
|
2019-05-09 21:57:35 +02:00
|
|
|
uint64 auth_data_generation{0};
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
2022-11-23 17:37:32 +01:00
|
|
|
std::map<uint32, ClientInfo> clients_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
std::shared_ptr<NetStatsCallback> media_net_stats_callback_;
|
|
|
|
std::shared_ptr<NetStatsCallback> common_net_stats_callback_;
|
|
|
|
|
2022-10-01 15:29:56 +02:00
|
|
|
ActorShared<ConnectionCreator> ref_cnt_guard_;
|
2018-12-31 20:04:05 +01:00
|
|
|
int ref_cnt_{0};
|
|
|
|
bool close_flag_{false};
|
2018-05-08 22:02:15 +02:00
|
|
|
uint64 current_token_ = 0;
|
2018-06-04 17:55:38 +02:00
|
|
|
std::map<uint64, std::pair<bool, ActorShared<>>> children_;
|
|
|
|
|
|
|
|
struct PingMainDcRequest {
|
|
|
|
Promise<double> promise;
|
|
|
|
size_t left_queries = 0;
|
|
|
|
Result<double> result;
|
|
|
|
};
|
|
|
|
std::map<uint64, PingMainDcRequest> ping_main_dc_requests_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-05-08 22:02:15 +02:00
|
|
|
uint64 next_token() {
|
2018-12-31 20:04:05 +01:00
|
|
|
return ++current_token_;
|
|
|
|
}
|
2018-05-14 21:00:38 +02:00
|
|
|
|
2022-10-01 15:29:56 +02:00
|
|
|
ActorShared<ConnectionCreator> create_reference(int64 token);
|
|
|
|
|
2018-09-17 20:15:11 +02:00
|
|
|
void set_active_proxy_id(int32 proxy_id, bool from_binlog = false);
|
2018-05-14 21:00:38 +02:00
|
|
|
void enable_proxy_impl(int32 proxy_id);
|
|
|
|
void disable_proxy_impl();
|
|
|
|
void on_proxy_changed(bool from_db);
|
|
|
|
static string get_proxy_database_key(int32 proxy_id);
|
|
|
|
static string get_proxy_used_database_key(int32 proxy_id);
|
2018-05-15 14:21:09 +02:00
|
|
|
void save_proxy_last_used_date(int32 delay);
|
2018-05-14 21:00:38 +02:00
|
|
|
td_api::object_ptr<td_api::proxy> get_proxy_object(int32 proxy_id) const;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final;
|
|
|
|
void hangup_shared() final;
|
|
|
|
void hangup() final;
|
|
|
|
void loop() final;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2023-10-12 22:41:46 +02:00
|
|
|
void init_proxies();
|
2024-02-28 11:47:17 +01:00
|
|
|
void add_dc_options(DcOptions &&new_dc_options);
|
2018-12-31 20:04:05 +01:00
|
|
|
Result<SocketFd> do_request_connection(DcId dc_id, bool allow_media_only);
|
2018-09-27 03:19:03 +02:00
|
|
|
Result<std::pair<unique_ptr<mtproto::RawConnection>, bool>> do_request_raw_connection(DcId dc_id,
|
|
|
|
bool allow_media_only,
|
2022-11-23 17:37:32 +01:00
|
|
|
bool is_media, uint32 hash);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
void on_network(bool network_flag, uint32 network_generation);
|
|
|
|
void on_online(bool online_flag);
|
2020-08-05 18:49:34 +02:00
|
|
|
void on_logging_out(bool is_logging_out);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-05-16 21:35:27 +02:00
|
|
|
static void update_mtproto_header(const Proxy &proxy);
|
|
|
|
|
2022-11-23 17:37:32 +01:00
|
|
|
void client_wakeup(uint32 hash);
|
2018-12-31 20:04:05 +01:00
|
|
|
void client_loop(ClientInfo &client);
|
2018-04-30 19:01:18 +02:00
|
|
|
void 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, string debug_str,
|
2018-04-30 19:01:18 +02:00
|
|
|
uint32 network_generation);
|
2022-11-23 17:37:32 +01:00
|
|
|
void client_add_connection(uint32 hash, Result<unique_ptr<mtproto::RawConnection>> r_raw_connection, bool check_flag,
|
2023-05-14 22:00:56 +02:00
|
|
|
uint64 auth_data_generation, uint64 session_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
void client_set_timeout_at(ClientInfo &client, double wakeup_at);
|
|
|
|
|
|
|
|
void on_proxy_resolved(Result<IPAddress> ip_address, bool dummy);
|
|
|
|
|
2018-05-18 22:43:38 +02:00
|
|
|
struct FindConnectionExtra {
|
|
|
|
DcOptionsSet::Stat *stat{nullptr};
|
|
|
|
mtproto::TransportType transport_type;
|
|
|
|
string debug_str;
|
2021-03-26 16:04:01 +01:00
|
|
|
IPAddress ip_address;
|
2020-05-16 22:32:32 +02:00
|
|
|
IPAddress mtproto_ip_address;
|
2018-05-18 22:43:38 +02:00
|
|
|
bool check_mode{false};
|
|
|
|
};
|
2018-06-04 00:17:59 +02:00
|
|
|
|
2019-07-22 13:32:21 +02:00
|
|
|
static Result<mtproto::TransportType> get_transport_type(const Proxy &proxy,
|
2018-06-04 00:17:59 +02:00
|
|
|
const DcOptionsSet::ConnectionInfo &info);
|
|
|
|
|
2019-07-22 13:32:21 +02:00
|
|
|
Result<SocketFd> find_connection(const Proxy &proxy, const IPAddress &proxy_ip_address, DcId dc_id,
|
2019-07-22 04:42:19 +02:00
|
|
|
bool allow_media_only, FindConnectionExtra &extra);
|
2018-06-04 00:17:59 +02:00
|
|
|
|
2019-02-04 23:32:37 +01:00
|
|
|
ActorId<GetHostByNameActor> get_dns_resolver();
|
2019-02-04 23:24:46 +01:00
|
|
|
|
2018-05-18 22:43:38 +02:00
|
|
|
void ping_proxy_resolved(int32 proxy_id, IPAddress ip_address, Promise<double> promise);
|
2018-06-04 00:17:59 +02:00
|
|
|
|
2021-10-22 18:59:15 +02:00
|
|
|
void ping_proxy_buffered_socket_fd(IPAddress ip_address, BufferedFd<SocketFd> buffered_socket_fd,
|
|
|
|
mtproto::TransportType transport_type, string debug_str, Promise<double> promise);
|
2018-06-04 17:55:38 +02:00
|
|
|
|
|
|
|
void on_ping_main_dc_result(uint64 token, Result<double> result);
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace td
|