diff --git a/td/telegram/Global.h b/td/telegram/Global.h index 34e54e2e9..e48fdf22a 100644 --- a/td/telegram/Global.h +++ b/td/telegram/Global.h @@ -94,6 +94,10 @@ class Global : public ActorContext { return *net_query_dispatcher_; } + bool have_net_query_dispatcher() const { + return net_query_dispatcher_ != nullptr; + } + void set_shared_config(std::unique_ptr shared_config); ConfigShared &shared_config() { @@ -223,6 +227,9 @@ class Global : public ActorContext { MtprotoHeader &mtproto_header(); void set_mtproto_header(std::unique_ptr mtproto_header); + bool have_mtproto_header() const { + return mtproto_header_ != nullptr; + } const TdParameters ¶meters() const { return parameters_; diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 2d00807d4..41d4e96d9 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -4037,7 +4037,7 @@ void Td::request(uint64 id, tl_object_ptr function) { } if (is_preauthentication_request(function_id)) { pending_preauthentication_requests_.emplace_back(id, std::move(function)); - // return; + return; } return send_error_raw(id, 401, "Initialization parameters are needed"); } @@ -4070,7 +4070,7 @@ void Td::request(uint64 id, tl_object_ptr function) { } if (is_preauthentication_request(function_id)) { pending_preauthentication_requests_.emplace_back(id, std::move(function)); - // return; + return; } return send_error_raw(id, 401, "Database encryption key is needed"); } @@ -4586,6 +4586,16 @@ class Td::UploadFileCallback : public FileManager::UploadCallback { int VERBOSITY_NAME(td_init) = VERBOSITY_NAME(DEBUG) + 3; +template +void Td::complete_pending_preauthentication_requests(const T &func) { + for (auto &request : pending_preauthentication_requests_) { + if (request.second != nullptr && func(request.second->get_id())) { + downcast_call(*request.second, [this, id = request.first](auto &request) { this->on_request(id, request); }); + request.second = nullptr; + } + } +} + Status Td::init(DbKey key) { auto current_scheduler_id = Scheduler::instance()->sched_id(); auto scheduler_count = Scheduler::instance()->sched_count(); @@ -4631,6 +4641,25 @@ Status Td::init(DbKey key) { G()->set_connection_creator(std::move(connection_creator)); net_stats_manager_ = std::move(net_stats_manager); + + complete_pending_preauthentication_requests([](int32 id) { + switch (id) { + case td_api::processDcUpdate::ID: + case td_api::setNetworkType::ID: + case td_api::getNetworkStatistics::ID: + case td_api::addNetworkStatistics::ID: + case td_api::resetNetworkStatistics::ID: + case td_api::addProxy::ID: + case td_api::enableProxy::ID: + case td_api::disableProxy::ID: + case td_api::removeProxy::ID: + case td_api::getProxies::ID: + case td_api::pingProxy::ID: + return true; + default: + return false; + } + }); } VLOG(td_init) << "Create TempAuthKeyWatchdog"; @@ -4653,6 +4682,16 @@ Status Td::init(DbKey key) { config_manager_ = create_actor("ConfigManager", create_reference()); G()->set_config_manager(config_manager_.get()); + complete_pending_preauthentication_requests([](int32 id) { + switch (id) { + case td_api::getOption::ID: + case td_api::setOption::ID: + return true; + default: + return false; + } + }); + VLOG(td_init) << "Create NetQueryDispatcher"; auto net_query_dispatcher = std::make_unique([&] { return create_reference(); }); G()->set_net_query_dispatcher(std::move(net_query_dispatcher)); @@ -4797,6 +4836,8 @@ Status Td::init(DbKey key) { updates_manager_->get_difference("init"); } + complete_pending_preauthentication_requests([](int32 id) { return true; }); + state_ = State::Run; return Status::OK(); } diff --git a/td/telegram/Td.h b/td/telegram/Td.h index 82faf610d..9c80c5f75 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -247,6 +247,9 @@ class Td final : public NetQueryCallback { vector>> pending_preauthentication_requests_; + template + void complete_pending_preauthentication_requests(const T &func); + static void on_alarm_timeout_callback(void *td_ptr, int64 alarm_id); void on_alarm_timeout(int64 alarm_id); diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index 27188172b..1ad16612a 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -677,6 +677,16 @@ class CliClient final : public Actor { send_request(td_api::make_object( "@telegram /test_command https://telegram.org telegram.me @gif @test")); + send_request(td_api::make_object("use_pfs")); + send_request(td_api::make_object( + "use_pfs", td_api::make_object(std::time(nullptr) / 86400 % 2 == 0))); + send_request(td_api::make_object("use_storage_optimizer", + td_api::make_object(false))); + + send_request(td_api::make_object(td_api::make_object())); + send_request(td_api::make_object()); + send_request(td_api::make_object()); + auto bad_parameters = td_api::make_object(); bad_parameters->database_directory_ = "/.."; bad_parameters->api_id_ = api_id_; diff --git a/td/telegram/net/ConnectionCreator.cpp b/td/telegram/net/ConnectionCreator.cpp index 4cd2b423e..9e7d29c32 100644 --- a/td/telegram/net/ConnectionCreator.cpp +++ b/td/telegram/net/ConnectionCreator.cpp @@ -339,8 +339,7 @@ void ConnectionCreator::enable_proxy_impl(int32 proxy_id) { if ((active_proxy_id_ != 0 && proxies_[active_proxy_id_].type() == Proxy::Type::Mtproto) || proxies_[proxy_id].type() == Proxy::Type::Mtproto) { - G()->mtproto_header().set_proxy(proxies_[proxy_id]); - G()->net_query_dispatcher().update_mtproto_header(); + update_mtproto_header(proxies_[proxy_id]); } save_proxy_last_used_date(0); @@ -358,8 +357,7 @@ void ConnectionCreator::disable_proxy_impl() { CHECK(proxies_.count(active_proxy_id_) == 1); if (proxies_[active_proxy_id_].type() == Proxy::Type::Mtproto) { - G()->mtproto_header().set_proxy(Proxy()); - G()->net_query_dispatcher().update_mtproto_header(); + update_mtproto_header(Proxy()); } active_proxy_id_ = 0; @@ -813,6 +811,15 @@ void ConnectionCreator::on_dc_update(DcId dc_id, string ip_port, Promise<> promi }()); } +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(); + } +} + void ConnectionCreator::start_up() { class StateCallback : public StateManager::Callback { public: @@ -891,8 +898,7 @@ void ConnectionCreator::start_up() { if (active_proxy_id_ != 0) { if (proxies_[active_proxy_id_].type() == Proxy::Type::Mtproto) { - G()->mtproto_header().set_proxy(proxies_[active_proxy_id_]); - G()->net_query_dispatcher().update_mtproto_header(); + update_mtproto_header(proxies_[active_proxy_id_]); } on_proxy_changed(true); diff --git a/td/telegram/net/ConnectionCreator.h b/td/telegram/net/ConnectionCreator.h index f8a79ce75..85a87840a 100644 --- a/td/telegram/net/ConnectionCreator.h +++ b/td/telegram/net/ConnectionCreator.h @@ -252,6 +252,8 @@ class ConnectionCreator : public NetQueryCallback { void on_network(bool network_flag, uint32 network_generation); void on_online(bool online_flag); + static void update_mtproto_header(const Proxy &proxy); + void client_wakeup(size_t hash); void client_loop(ClientInfo &client); struct ConnectionData {