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/Global.h"
|
|
|
|
|
2022-08-17 19:01:03 +02:00
|
|
|
#include "td/telegram/AuthManager.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/net/ConnectionCreator.h"
|
|
|
|
#include "td/telegram/net/NetQueryDispatcher.h"
|
|
|
|
#include "td/telegram/net/TempAuthKeyWatchdog.h"
|
2021-12-16 13:18:51 +01:00
|
|
|
#include "td/telegram/OptionManager.h"
|
2018-10-28 18:30:47 +01:00
|
|
|
#include "td/telegram/StateManager.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/TdDb.h"
|
2024-04-08 19:18:48 +02:00
|
|
|
#include "td/telegram/UpdatesManager.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
#include "td/utils/format.h"
|
2019-02-12 21:48:16 +01:00
|
|
|
#include "td/utils/logging.h"
|
2020-09-24 23:46:16 +02:00
|
|
|
#include "td/utils/misc.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/port/Clocks.h"
|
|
|
|
#include "td/utils/tl_helpers.h"
|
|
|
|
|
2018-08-14 01:11:49 +02:00
|
|
|
#include <cmath>
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
namespace td {
|
|
|
|
|
2023-07-26 20:37:54 +02:00
|
|
|
Global::Global() {
|
|
|
|
auto current_scheduler_id = Scheduler::instance()->sched_id();
|
|
|
|
auto max_scheduler_id = Scheduler::instance()->sched_count() - 1;
|
|
|
|
database_scheduler_id_ = min(current_scheduler_id + 1, max_scheduler_id);
|
|
|
|
gc_scheduler_id_ = min(current_scheduler_id + 2, max_scheduler_id);
|
|
|
|
slow_net_scheduler_id_ = min(current_scheduler_id + 3, max_scheduler_id);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
Global::~Global() = default;
|
|
|
|
|
2022-05-02 19:59:07 +02:00
|
|
|
void Global::log_out(Slice reason) {
|
2022-08-17 19:01:03 +02:00
|
|
|
send_closure(auth_manager_, &AuthManager::on_authorization_lost, reason.str());
|
2022-05-02 19:59:07 +02:00
|
|
|
}
|
|
|
|
|
2023-10-05 20:50:53 +02:00
|
|
|
void Global::close_all(bool destroy_flag, Promise<Unit> on_finished) {
|
|
|
|
td_db_->close(use_sqlite_pmc() ? get_database_scheduler_id() : get_slow_net_scheduler_id(), destroy_flag,
|
|
|
|
std::move(on_finished));
|
2018-12-31 20:04:05 +01:00
|
|
|
state_manager_.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
ActorId<ConnectionCreator> Global::connection_creator() const {
|
|
|
|
return connection_creator_.get();
|
|
|
|
}
|
|
|
|
void Global::set_connection_creator(ActorOwn<ConnectionCreator> connection_creator) {
|
|
|
|
connection_creator_ = std::move(connection_creator);
|
|
|
|
}
|
|
|
|
|
|
|
|
ActorId<TempAuthKeyWatchdog> Global::temp_auth_key_watchdog() const {
|
|
|
|
return temp_auth_key_watchdog_.get();
|
|
|
|
}
|
|
|
|
void Global::set_temp_auth_key_watchdog(ActorOwn<TempAuthKeyWatchdog> actor) {
|
|
|
|
temp_auth_key_watchdog_ = std::move(actor);
|
|
|
|
}
|
|
|
|
|
2018-05-08 22:02:15 +02:00
|
|
|
MtprotoHeader &Global::mtproto_header() {
|
2018-12-31 20:04:05 +01:00
|
|
|
return *mtproto_header_;
|
|
|
|
}
|
2018-09-27 03:19:03 +02:00
|
|
|
void Global::set_mtproto_header(unique_ptr<MtprotoHeader> mtproto_header) {
|
2018-12-31 20:04:05 +01:00
|
|
|
mtproto_header_ = std::move(mtproto_header);
|
|
|
|
}
|
|
|
|
|
2019-12-25 15:36:31 +01:00
|
|
|
struct ServerTimeDiff {
|
2019-12-25 11:44:44 +01:00
|
|
|
double diff;
|
|
|
|
double system_time;
|
|
|
|
|
|
|
|
template <class StorerT>
|
|
|
|
void store(StorerT &storer) const {
|
|
|
|
using td::store;
|
|
|
|
store(diff, storer);
|
|
|
|
store(system_time, storer);
|
|
|
|
}
|
|
|
|
template <class ParserT>
|
|
|
|
void parse(ParserT &parser) {
|
|
|
|
using td::parse;
|
|
|
|
parse(diff, parser);
|
|
|
|
if (parser.get_left_len() != 0) {
|
|
|
|
parse(system_time, parser);
|
|
|
|
} else {
|
|
|
|
system_time = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-03-13 20:29:56 +01:00
|
|
|
Status Global::init(ActorId<Td> td, unique_ptr<TdDb> td_db_ptr) {
|
2018-12-31 20:04:05 +01:00
|
|
|
td_ = td;
|
2019-04-08 00:09:54 +02:00
|
|
|
td_db_ = std::move(td_db_ptr);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-12-25 11:44:44 +01:00
|
|
|
string saved_diff_str = td_db()->get_binlog_pmc()->get("server_time_difference");
|
2019-12-29 22:02:12 +01:00
|
|
|
auto system_time = Clocks::system();
|
2019-12-24 16:51:41 +01:00
|
|
|
auto default_time_difference = system_time - Time::now();
|
2019-12-25 11:44:44 +01:00
|
|
|
if (saved_diff_str.empty()) {
|
2019-07-04 18:21:07 +02:00
|
|
|
server_time_difference_ = default_time_difference;
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
2019-12-25 15:36:31 +01:00
|
|
|
ServerTimeDiff saved_diff;
|
2019-12-25 11:44:44 +01:00
|
|
|
unserialize(saved_diff, saved_diff_str).ensure();
|
2019-12-24 16:51:41 +01:00
|
|
|
|
2020-02-22 17:31:08 +01:00
|
|
|
saved_diff_ = saved_diff.diff;
|
|
|
|
saved_system_time_ = saved_diff.system_time;
|
|
|
|
|
2019-12-25 11:44:44 +01:00
|
|
|
double diff = saved_diff.diff + default_time_difference;
|
|
|
|
if (saved_diff.system_time > system_time) {
|
|
|
|
double time_backwards_fix = saved_diff.system_time - system_time;
|
2019-12-25 16:35:36 +01:00
|
|
|
if (time_backwards_fix > 60) {
|
|
|
|
LOG(WARNING) << "Fix system time which went backwards: " << format::as_time(time_backwards_fix) << " "
|
|
|
|
<< tag("saved_system_time", saved_diff.system_time) << tag("system_time", system_time);
|
|
|
|
}
|
2019-12-24 16:51:41 +01:00
|
|
|
diff += time_backwards_fix;
|
2020-01-23 23:07:53 +01:00
|
|
|
} else if (saved_diff.system_time != 0) {
|
2020-01-06 02:23:59 +01:00
|
|
|
const double MAX_TIME_FORWARD = 367 * 86400; // if more than 1 year has passed, the session is logged out anyway
|
|
|
|
if (saved_diff.system_time + MAX_TIME_FORWARD < system_time) {
|
|
|
|
double time_forward_fix = system_time - (saved_diff.system_time + MAX_TIME_FORWARD);
|
|
|
|
LOG(WARNING) << "Fix system time which went forward: " << format::as_time(time_forward_fix) << " "
|
|
|
|
<< tag("saved_system_time", saved_diff.system_time) << tag("system_time", system_time);
|
|
|
|
diff -= time_forward_fix;
|
|
|
|
}
|
2020-03-10 23:24:07 +01:00
|
|
|
} else if (saved_diff.diff >= 1500000000 && system_time >= 1500000000) { // only for saved_diff.system_time == 0
|
|
|
|
diff = default_time_difference;
|
2019-12-24 16:51:41 +01:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
LOG(DEBUG) << "LOAD: " << tag("server_time_difference", diff);
|
|
|
|
server_time_difference_ = diff;
|
|
|
|
}
|
2019-12-25 15:36:31 +01:00
|
|
|
server_time_difference_was_updated_ = false;
|
2019-07-04 18:21:07 +02:00
|
|
|
dns_time_difference_ = default_time_difference;
|
|
|
|
dns_time_difference_was_updated_ = false;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2023-03-13 20:29:56 +01:00
|
|
|
Slice Global::get_dir() const {
|
|
|
|
return td_db_->get_database_directory();
|
|
|
|
}
|
|
|
|
|
|
|
|
Slice Global::get_files_dir() const {
|
|
|
|
return td_db_->get_files_directory();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Global::is_test_dc() const {
|
|
|
|
return td_db_->is_test_dc();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Global::use_file_database() const {
|
|
|
|
return td_db_->use_file_database();
|
|
|
|
}
|
|
|
|
|
2023-03-13 23:41:04 +01:00
|
|
|
bool Global::use_sqlite_pmc() const {
|
|
|
|
return td_db_->use_sqlite_pmc();
|
|
|
|
}
|
|
|
|
|
2023-03-13 20:29:56 +01:00
|
|
|
bool Global::use_chat_info_database() const {
|
|
|
|
return td_db_->use_chat_info_database();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Global::use_message_database() const {
|
|
|
|
return td_db_->use_message_database();
|
|
|
|
}
|
|
|
|
|
2022-04-12 15:12:22 +02:00
|
|
|
int32 Global::get_retry_after(int32 error_code, Slice error_message) {
|
|
|
|
if (error_code != 429) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Slice retry_after_prefix("Too Many Requests: retry after ");
|
|
|
|
if (!begins_with(error_message, retry_after_prefix)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto r_retry_after = to_integer_safe<int32>(error_message.substr(retry_after_prefix.size()));
|
|
|
|
if (r_retry_after.is_ok() && r_retry_after.ok() > 0) {
|
|
|
|
return r_retry_after.ok();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-02-22 17:31:08 +01:00
|
|
|
int32 Global::to_unix_time(double server_time) const {
|
|
|
|
LOG_CHECK(1.0 <= server_time && server_time <= 2140000000.0)
|
2021-09-16 16:28:33 +02:00
|
|
|
<< server_time << ' ' << Clocks::system() << ' ' << is_server_time_reliable() << ' '
|
|
|
|
<< get_server_time_difference() << ' ' << Time::now() << ' ' << saved_diff_ << ' ' << saved_system_time_;
|
2019-12-25 16:35:36 +01:00
|
|
|
return static_cast<int32>(server_time);
|
|
|
|
}
|
|
|
|
|
2023-05-14 20:58:54 +02:00
|
|
|
void Global::update_server_time_difference(double diff, bool force) {
|
|
|
|
if (force || !server_time_difference_was_updated_ || server_time_difference_ < diff) {
|
2018-12-31 20:04:05 +01:00
|
|
|
server_time_difference_ = diff;
|
|
|
|
server_time_difference_was_updated_ = true;
|
2019-12-25 11:44:44 +01:00
|
|
|
do_save_server_time_difference();
|
2020-01-08 21:41:03 +01:00
|
|
|
|
2022-08-18 17:52:59 +02:00
|
|
|
get_option_manager()->on_update_server_time_difference();
|
2019-12-24 16:51:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-25 14:57:18 +01:00
|
|
|
void Global::save_server_time() {
|
2019-12-24 16:51:41 +01:00
|
|
|
auto t = Time::now();
|
2019-12-25 16:03:08 +01:00
|
|
|
if (server_time_difference_was_updated_ && system_time_saved_at_.load(std::memory_order_relaxed) + 10 < t) {
|
2019-12-24 16:51:41 +01:00
|
|
|
system_time_saved_at_ = t;
|
2019-12-25 11:44:44 +01:00
|
|
|
do_save_server_time_difference();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-25 11:44:44 +01:00
|
|
|
void Global::do_save_server_time_difference() {
|
2022-08-17 18:43:00 +02:00
|
|
|
if (get_option_boolean("disable_time_adjustment_protection")) {
|
2021-06-04 16:18:59 +02:00
|
|
|
td_db()->get_binlog_pmc()->erase("server_time_difference");
|
2020-07-13 16:01:51 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-12-25 11:44:44 +01:00
|
|
|
// diff = server_time - Time::now
|
|
|
|
// fixed_diff = server_time - Clocks::system
|
|
|
|
double system_time = Clocks::system();
|
|
|
|
double fixed_diff = server_time_difference_ + Time::now() - system_time;
|
|
|
|
|
2019-12-25 15:36:31 +01:00
|
|
|
ServerTimeDiff diff;
|
2019-12-25 11:44:44 +01:00
|
|
|
diff.diff = fixed_diff;
|
|
|
|
diff.system_time = system_time;
|
|
|
|
td_db()->get_binlog_pmc()->set("server_time_difference", serialize(diff));
|
|
|
|
}
|
|
|
|
|
2019-07-04 18:21:07 +02:00
|
|
|
void Global::update_dns_time_difference(double diff) {
|
|
|
|
dns_time_difference_ = diff;
|
|
|
|
dns_time_difference_was_updated_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
double Global::get_dns_time_difference() const {
|
|
|
|
bool dns_flag = dns_time_difference_was_updated_;
|
|
|
|
double dns_diff = dns_time_difference_;
|
|
|
|
bool server_flag = server_time_difference_was_updated_;
|
|
|
|
double server_diff = server_time_difference_;
|
|
|
|
if (dns_flag != server_flag) {
|
|
|
|
return dns_flag ? dns_diff : server_diff;
|
|
|
|
}
|
|
|
|
if (dns_flag) {
|
2019-07-23 00:50:12 +02:00
|
|
|
return max(dns_diff, server_diff);
|
2019-07-04 18:21:07 +02:00
|
|
|
}
|
2019-07-22 04:04:18 +02:00
|
|
|
if (td_db_) {
|
|
|
|
return server_diff;
|
|
|
|
}
|
|
|
|
return Clocks::system() - Time::now();
|
2019-07-04 18:21:07 +02:00
|
|
|
}
|
|
|
|
|
2018-06-25 23:10:53 +02:00
|
|
|
DcId Global::get_webfile_dc_id() const {
|
2022-08-17 18:43:00 +02:00
|
|
|
auto dc_id = narrow_cast<int32>(get_option_integer("webfile_dc_id"));
|
2018-06-25 23:10:53 +02:00
|
|
|
if (!DcId::is_valid(dc_id)) {
|
|
|
|
if (is_test_dc()) {
|
|
|
|
dc_id = 2;
|
|
|
|
} else {
|
|
|
|
dc_id = 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
CHECK(DcId::is_valid(dc_id));
|
|
|
|
}
|
|
|
|
|
|
|
|
return DcId::internal(dc_id);
|
|
|
|
}
|
|
|
|
|
2020-07-30 16:28:56 +02:00
|
|
|
void Global::set_net_query_stats(std::shared_ptr<NetQueryStats> net_query_stats) {
|
2020-09-27 01:20:42 +02:00
|
|
|
net_query_creator_.set_create_func(
|
|
|
|
[net_query_stats = std::move(net_query_stats)] { return td::make_unique<NetQueryCreator>(net_query_stats); });
|
2020-07-30 16:28:56 +02:00
|
|
|
}
|
|
|
|
|
2018-09-27 03:19:03 +02:00
|
|
|
void Global::set_net_query_dispatcher(unique_ptr<NetQueryDispatcher> net_query_dispatcher) {
|
2018-12-31 20:04:05 +01:00
|
|
|
net_query_dispatcher_ = std::move(net_query_dispatcher);
|
|
|
|
}
|
2018-06-25 23:10:53 +02:00
|
|
|
|
2022-08-18 13:54:57 +02:00
|
|
|
const OptionManager *Global::get_option_manager() const {
|
2022-08-18 17:52:59 +02:00
|
|
|
CHECK(option_manager_ != nullptr);
|
|
|
|
return option_manager_;
|
2022-08-18 13:54:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
OptionManager *Global::get_option_manager() {
|
2022-08-18 17:52:59 +02:00
|
|
|
CHECK(option_manager_ != nullptr);
|
|
|
|
return option_manager_;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2022-08-17 15:11:13 +02:00
|
|
|
|
|
|
|
void Global::set_option_empty(Slice name) {
|
2022-08-18 13:54:57 +02:00
|
|
|
get_option_manager()->set_option_empty(name);
|
2022-08-17 15:11:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Global::set_option_boolean(Slice name, bool value) {
|
2022-08-18 13:54:57 +02:00
|
|
|
get_option_manager()->set_option_boolean(name, value);
|
2022-08-17 15:11:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Global::set_option_integer(Slice name, int64 value) {
|
2022-08-18 13:54:57 +02:00
|
|
|
get_option_manager()->set_option_integer(name, value);
|
2022-08-17 15:11:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Global::set_option_string(Slice name, Slice value) {
|
2022-08-18 13:54:57 +02:00
|
|
|
get_option_manager()->set_option_string(name, value);
|
2022-08-17 15:11:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Global::have_option(Slice name) const {
|
2022-08-18 13:54:57 +02:00
|
|
|
return get_option_manager()->have_option(name);
|
2022-08-17 15:11:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Global::get_option_boolean(Slice name, bool default_value) const {
|
2022-08-18 13:54:57 +02:00
|
|
|
return get_option_manager()->get_option_boolean(name, default_value);
|
2022-08-17 15:11:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int64 Global::get_option_integer(Slice name, int64 default_value) const {
|
2022-08-18 13:54:57 +02:00
|
|
|
return get_option_manager()->get_option_integer(name, default_value);
|
2022-08-17 15:11:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
string Global::get_option_string(Slice name, string default_value) const {
|
2022-08-18 13:54:57 +02:00
|
|
|
return get_option_manager()->get_option_string(name, std::move(default_value));
|
2022-08-17 15:11:13 +02:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-08-14 01:11:49 +02:00
|
|
|
int64 Global::get_location_key(double latitude, double longitude) {
|
|
|
|
const double PI = 3.14159265358979323846;
|
|
|
|
latitude *= PI / 180;
|
|
|
|
longitude *= PI / 180;
|
|
|
|
|
|
|
|
int64 key = 0;
|
|
|
|
if (latitude < 0) {
|
|
|
|
latitude = -latitude;
|
|
|
|
key = 65536;
|
|
|
|
}
|
|
|
|
|
|
|
|
double f = std::tan(PI / 4 - latitude / 2);
|
|
|
|
key += static_cast<int64>(f * std::cos(longitude) * 128) * 256;
|
|
|
|
key += static_cast<int64>(f * std::sin(longitude) * 128);
|
2022-02-09 22:59:52 +01:00
|
|
|
if (key == 0) {
|
|
|
|
key = 1;
|
|
|
|
}
|
2018-08-14 01:11:49 +02:00
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64 Global::get_location_access_hash(double latitude, double longitude) {
|
|
|
|
auto it = location_access_hashes_.find(get_location_key(latitude, longitude));
|
|
|
|
if (it == location_access_hashes_.end()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Global::add_location_access_hash(double latitude, double longitude, int64 access_hash) {
|
|
|
|
if (access_hash == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
location_access_hashes_[get_location_key(latitude, longitude)] = access_hash;
|
|
|
|
}
|
|
|
|
|
2024-04-08 19:18:48 +02:00
|
|
|
void Global::notify_speed_limited(bool is_upload) {
|
|
|
|
send_closure(updates_manager_, &UpdatesManager::notify_speed_limited, is_upload);
|
|
|
|
}
|
|
|
|
|
2020-10-02 12:22:06 +02:00
|
|
|
double get_global_server_time() {
|
2020-02-01 05:43:54 +01:00
|
|
|
return G()->server_time();
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|