From d462e21d2023a3497f7b0bcc7d2f6d61a346f6d9 Mon Sep 17 00:00:00 2001 From: levlam Date: Tue, 19 Nov 2019 18:12:51 +0300 Subject: [PATCH] Simplify get_simple_config_dns. GitOrigin-RevId: 4488e1241c2678199e173deb5548d904ec5e6afd --- td/telegram/ConfigManager.cpp | 99 +++++++++++++++-------------------- 1 file changed, 41 insertions(+), 58 deletions(-) diff --git a/td/telegram/ConfigManager.cpp b/td/telegram/ConfigManager.cpp index 53de4b62..66843b91 100644 --- a/td/telegram/ConfigManager.cpp +++ b/td/telegram/ConfigManager.cpp @@ -198,7 +198,7 @@ Result decode_config(Slice input) { } static ActorOwn<> get_simple_config_impl(Promise promise, int32 scheduler_id, string url, - string host, bool prefer_ipv6, + string host, std::vector> headers, bool prefer_ipv6, std::function(HttpQuery &)> get_config, string content = string(), string content_type = string()) { VLOG(config_recoverer) << "Request simple config from " << url; @@ -207,9 +207,10 @@ static ActorOwn<> get_simple_config_impl(Promise promise, in #else const int timeout = 10; const int ttl = 3; - static const string user_agent = - "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 " - "Safari/537.36"; + headers.emplace_back("Host", std::move(host)); + headers.emplace_back("User-Agent", + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) " + "Chrome/77.0.3865.90 Safari/537.36"); return ActorOwn<>(create_actor_on_scheduler( "Wget", scheduler_id, PromiseCreator::lambda([get_config = std::move(get_config), @@ -227,8 +228,8 @@ static ActorOwn<> get_simple_config_impl(Promise promise, in return std::move(res); }()); }), - std::move(url), std::vector>({{"Host", std::move(host)}, {"User-Agent", user_agent}}), - timeout, ttl, prefer_ipv6, SslStream::VerifyPeer::Off, std::move(content), std::move(content_type))); + std::move(url), std::move(headers), timeout, ttl, prefer_ipv6, SslStream::VerifyPeer::Off, std::move(content), + std::move(content_type))); #endif } @@ -237,66 +238,48 @@ ActorOwn<> get_simple_config_azure(Promise promise, const Co string url = PSTRING() << "https://software-download.microsoft.com/" << (is_test ? "test" : "prod") << "v2/config.txt"; const bool prefer_ipv6 = shared_config == nullptr ? false : shared_config->get_option_boolean("prefer_ipv6"); - return get_simple_config_impl(std::move(promise), scheduler_id, std::move(url), "tcdnb.azureedge.net", prefer_ipv6, - [](HttpQuery &http_query) { return http_query.content_.str(); }); + return get_simple_config_impl(std::move(promise), scheduler_id, std::move(url), "tcdnb.azureedge.net", {}, + prefer_ipv6, [](HttpQuery &http_query) { return http_query.content_.str(); }); } static ActorOwn<> get_simple_config_dns(Slice address, Slice host, Promise promise, const ConfigShared *shared_config, bool is_test, int32 scheduler_id) { - VLOG(config_recoverer) << "Request simple config from DNS"; -#if TD_EMSCRIPTEN // FIXME - return ActorOwn<>(); -#else string name = shared_config == nullptr ? string() : shared_config->get_option_string("dc_txt_domain_name"); - const int timeout = 10; - const int ttl = 3; const bool prefer_ipv6 = shared_config == nullptr ? false : shared_config->get_option_boolean("prefer_ipv6"); if (name.empty() || true) { name = is_test ? "tapv3.stel.com" : "apv3.stel.com"; } - return ActorOwn<>(create_actor_on_scheduler( - "Wget", scheduler_id, - PromiseCreator::lambda([promise = std::move(promise)](Result> r_query) mutable { - promise.set_result([&]() -> Result { - TRY_RESULT(http_query, std::move(r_query)); - - SimpleConfigResult res; - res.r_http_date = HttpDate::parse_http_date(http_query->get_header("date").str()); - res.r_config = [&]() -> Result { - TRY_RESULT(json, json_decode(http_query->content_)); - if (json.type() != JsonValue::Type::Object) { - return Status::Error("JSON error"); - } - auto &answer_object = json.get_object(); - TRY_RESULT(answer, get_json_object_field(answer_object, "Answer", JsonValue::Type::Array, false)); - auto &answer_array = answer.get_array(); - vector parts; - for (auto &v : answer_array) { - if (v.type() != JsonValue::Type::Object) { - return Status::Error("JSON error"); - } - auto &data_object = v.get_object(); - TRY_RESULT(part, get_json_object_string_field(data_object, "data", false)); - parts.push_back(std::move(part)); - } - if (parts.size() != 2) { - return Status::Error("Expected data in two parts"); - } - string data; - if (parts[0].size() < parts[1].size()) { - data = parts[1] + parts[0]; - } else { - data = parts[0] + parts[1]; - } - return decode_config(data); - }(); - return std::move(res); - }()); - }), - PSTRING() << "https://" << address << "?name=" << url_encode(name) << "&type=16", - std::vector>({{"Host", host.str()}, {"Accept", "application/dns-json"}}), timeout, ttl, - prefer_ipv6, SslStream::VerifyPeer::Off)); -#endif + auto get_config = [](HttpQuery &http_query) -> Result { + TRY_RESULT(json, json_decode(http_query.content_)); + if (json.type() != JsonValue::Type::Object) { + return Status::Error("JSON error"); + } + auto &answer_object = json.get_object(); + TRY_RESULT(answer, get_json_object_field(answer_object, "Answer", JsonValue::Type::Array, false)); + auto &answer_array = answer.get_array(); + vector parts; + for (auto &v : answer_array) { + if (v.type() != JsonValue::Type::Object) { + return Status::Error("JSON error"); + } + auto &data_object = v.get_object(); + TRY_RESULT(part, get_json_object_string_field(data_object, "data", false)); + parts.push_back(std::move(part)); + } + if (parts.size() != 2) { + return Status::Error("Expected data in two parts"); + } + string data; + if (parts[0].size() < parts[1].size()) { + data = parts[1] + parts[0]; + } else { + data = parts[0] + parts[1]; + } + return data; + }; + return get_simple_config_impl(std::move(promise), scheduler_id, + PSTRING() << "https://" << address << "?name=" << url_encode(name) << "&type=16", + host.str(), {{"Accept", "application/dns-json"}}, prefer_ipv6, std::move(get_config)); } ActorOwn<> get_simple_config_google_dns(Promise promise, const ConfigShared *shared_config, @@ -344,7 +327,7 @@ ActorOwn<> get_simple_config_firebase_remote_config(Promise return std::move(config); }; return get_simple_config_impl(std::move(promise), scheduler_id, std::move(url), "firebaseremoteconfig.googleapis.com", - prefer_ipv6, get_config, payload, "application/json"); + {}, prefer_ipv6, std::move(get_config), payload, "application/json"); } ActorOwn<> get_full_config(DcOption option, Promise promise, ActorShared<> parent) {