Simplify get_simple_config_dns.

GitOrigin-RevId: 4488e1241c2678199e173deb5548d904ec5e6afd
This commit is contained in:
levlam 2019-11-19 18:12:51 +03:00
parent 9fb3cee419
commit d462e21d20

View File

@ -198,7 +198,7 @@ Result<SimpleConfig> decode_config(Slice input) {
} }
static ActorOwn<> get_simple_config_impl(Promise<SimpleConfigResult> promise, int32 scheduler_id, string url, static ActorOwn<> get_simple_config_impl(Promise<SimpleConfigResult> promise, int32 scheduler_id, string url,
string host, bool prefer_ipv6, string host, std::vector<std::pair<string, string>> headers, bool prefer_ipv6,
std::function<Result<string>(HttpQuery &)> get_config, std::function<Result<string>(HttpQuery &)> get_config,
string content = string(), string content_type = string()) { string content = string(), string content_type = string()) {
VLOG(config_recoverer) << "Request simple config from " << url; VLOG(config_recoverer) << "Request simple config from " << url;
@ -207,9 +207,10 @@ static ActorOwn<> get_simple_config_impl(Promise<SimpleConfigResult> promise, in
#else #else
const int timeout = 10; const int timeout = 10;
const int ttl = 3; const int ttl = 3;
static const string user_agent = headers.emplace_back("Host", std::move(host));
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 " headers.emplace_back("User-Agent",
"Safari/537.36"; "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>( return ActorOwn<>(create_actor_on_scheduler<Wget>(
"Wget", scheduler_id, "Wget", scheduler_id,
PromiseCreator::lambda([get_config = std::move(get_config), PromiseCreator::lambda([get_config = std::move(get_config),
@ -227,8 +228,8 @@ static ActorOwn<> get_simple_config_impl(Promise<SimpleConfigResult> promise, in
return std::move(res); return std::move(res);
}()); }());
}), }),
std::move(url), std::vector<std::pair<string, string>>({{"Host", std::move(host)}, {"User-Agent", user_agent}}), std::move(url), std::move(headers), timeout, ttl, prefer_ipv6, SslStream::VerifyPeer::Off, std::move(content),
timeout, ttl, prefer_ipv6, SslStream::VerifyPeer::Off, std::move(content), std::move(content_type))); std::move(content_type)));
#endif #endif
} }
@ -237,66 +238,48 @@ ActorOwn<> get_simple_config_azure(Promise<SimpleConfigResult> promise, const Co
string url = PSTRING() << "https://software-download.microsoft.com/" << (is_test ? "test" : "prod") string url = PSTRING() << "https://software-download.microsoft.com/" << (is_test ? "test" : "prod")
<< "v2/config.txt"; << "v2/config.txt";
const bool prefer_ipv6 = shared_config == nullptr ? false : shared_config->get_option_boolean("prefer_ipv6"); 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, return get_simple_config_impl(std::move(promise), scheduler_id, std::move(url), "tcdnb.azureedge.net", {},
[](HttpQuery &http_query) { return http_query.content_.str(); }); prefer_ipv6, [](HttpQuery &http_query) { return http_query.content_.str(); });
} }
static ActorOwn<> get_simple_config_dns(Slice address, Slice host, Promise<SimpleConfigResult> promise, static ActorOwn<> get_simple_config_dns(Slice address, Slice host, Promise<SimpleConfigResult> promise,
const ConfigShared *shared_config, bool is_test, int32 scheduler_id) { 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"); 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"); const bool prefer_ipv6 = shared_config == nullptr ? false : shared_config->get_option_boolean("prefer_ipv6");
if (name.empty() || true) { if (name.empty() || true) {
name = is_test ? "tapv3.stel.com" : "apv3.stel.com"; name = is_test ? "tapv3.stel.com" : "apv3.stel.com";
} }
return ActorOwn<>(create_actor_on_scheduler<Wget>( auto get_config = [](HttpQuery &http_query) -> Result<string> {
"Wget", scheduler_id, TRY_RESULT(json, json_decode(http_query.content_));
PromiseCreator::lambda([promise = std::move(promise)](Result<unique_ptr<HttpQuery>> r_query) mutable { if (json.type() != JsonValue::Type::Object) {
promise.set_result([&]() -> Result<SimpleConfigResult> { return Status::Error("JSON error");
TRY_RESULT(http_query, std::move(r_query)); }
auto &answer_object = json.get_object();
SimpleConfigResult res; TRY_RESULT(answer, get_json_object_field(answer_object, "Answer", JsonValue::Type::Array, false));
res.r_http_date = HttpDate::parse_http_date(http_query->get_header("date").str()); auto &answer_array = answer.get_array();
res.r_config = [&]() -> Result<SimpleConfig> { vector<string> parts;
TRY_RESULT(json, json_decode(http_query->content_)); for (auto &v : answer_array) {
if (json.type() != JsonValue::Type::Object) { if (v.type() != JsonValue::Type::Object) {
return Status::Error("JSON error"); return Status::Error("JSON error");
} }
auto &answer_object = json.get_object(); auto &data_object = v.get_object();
TRY_RESULT(answer, get_json_object_field(answer_object, "Answer", JsonValue::Type::Array, false)); TRY_RESULT(part, get_json_object_string_field(data_object, "data", false));
auto &answer_array = answer.get_array(); parts.push_back(std::move(part));
vector<string> parts; }
for (auto &v : answer_array) { if (parts.size() != 2) {
if (v.type() != JsonValue::Type::Object) { return Status::Error("Expected data in two parts");
return Status::Error("JSON error"); }
} string data;
auto &data_object = v.get_object(); if (parts[0].size() < parts[1].size()) {
TRY_RESULT(part, get_json_object_string_field(data_object, "data", false)); data = parts[1] + parts[0];
parts.push_back(std::move(part)); } else {
} data = parts[0] + parts[1];
if (parts.size() != 2) { }
return Status::Error("Expected data in two parts"); return data;
} };
string data; return get_simple_config_impl(std::move(promise), scheduler_id,
if (parts[0].size() < parts[1].size()) { PSTRING() << "https://" << address << "?name=" << url_encode(name) << "&type=16",
data = parts[1] + parts[0]; host.str(), {{"Accept", "application/dns-json"}}, prefer_ipv6, std::move(get_config));
} 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<std::pair<string, string>>({{"Host", host.str()}, {"Accept", "application/dns-json"}}), timeout, ttl,
prefer_ipv6, SslStream::VerifyPeer::Off));
#endif
} }
ActorOwn<> get_simple_config_google_dns(Promise<SimpleConfigResult> promise, const ConfigShared *shared_config, ActorOwn<> get_simple_config_google_dns(Promise<SimpleConfigResult> promise, const ConfigShared *shared_config,
@ -344,7 +327,7 @@ ActorOwn<> get_simple_config_firebase_remote_config(Promise<SimpleConfigResult>
return std::move(config); return std::move(config);
}; };
return get_simple_config_impl(std::move(promise), scheduler_id, std::move(url), "firebaseremoteconfig.googleapis.com", 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<FullConfig> promise, ActorShared<> parent) { ActorOwn<> get_full_config(DcOption option, Promise<FullConfig> promise, ActorShared<> parent) {