2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-01-01 01:35:39 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
|
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/net/GetHostByNameActor.h"
|
|
|
|
|
2019-02-12 17:48:52 +01:00
|
|
|
#include "td/net/HttpQuery.h"
|
2019-02-04 01:12:40 +01:00
|
|
|
#include "td/net/SslStream.h"
|
2019-01-24 18:08:29 +01:00
|
|
|
#include "td/net/Wget.h"
|
|
|
|
|
2019-02-12 22:26:36 +01:00
|
|
|
#include "td/utils/common.h"
|
2019-01-24 18:08:29 +01:00
|
|
|
#include "td/utils/JsonBuilder.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/logging.h"
|
2019-02-12 17:48:52 +01:00
|
|
|
#include "td/utils/misc.h"
|
2019-02-04 01:12:40 +01:00
|
|
|
#include "td/utils/Slice.h"
|
2021-05-17 14:21:11 +02:00
|
|
|
#include "td/utils/SliceBuilder.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Time.h"
|
|
|
|
|
|
|
|
namespace td {
|
2019-01-24 10:47:54 +01:00
|
|
|
namespace detail {
|
2019-02-04 00:29:37 +01:00
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class GoogleDnsResolver final : public Actor {
|
2019-01-24 10:47:54 +01:00
|
|
|
public:
|
2019-02-04 02:32:32 +01:00
|
|
|
GoogleDnsResolver(std::string host, bool prefer_ipv6, Promise<IPAddress> promise)
|
|
|
|
: host_(std::move(host)), prefer_ipv6_(prefer_ipv6), promise_(std::move(promise)) {
|
2019-01-24 10:47:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2019-01-24 13:18:23 +01:00
|
|
|
std::string host_;
|
2019-02-04 02:32:32 +01:00
|
|
|
bool prefer_ipv6_;
|
2019-01-24 13:18:23 +01:00
|
|
|
Promise<IPAddress> promise_;
|
2019-01-24 10:47:54 +01:00
|
|
|
ActorOwn<Wget> wget_;
|
2019-02-04 00:29:37 +01:00
|
|
|
double begin_time_ = 0;
|
2019-01-24 10:47:54 +01:00
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final {
|
2020-05-16 16:35:53 +02:00
|
|
|
auto r_address = IPAddress::get_ip_address(host_);
|
2019-02-04 04:32:10 +01:00
|
|
|
if (r_address.is_ok()) {
|
|
|
|
promise_.set_value(r_address.move_as_ok());
|
|
|
|
return stop();
|
|
|
|
}
|
|
|
|
|
2019-01-24 13:18:23 +01:00
|
|
|
const int timeout = 10;
|
|
|
|
const int ttl = 3;
|
2019-02-04 00:29:37 +01:00
|
|
|
begin_time_ = Time::now();
|
2019-06-17 18:12:54 +02:00
|
|
|
auto wget_promise = PromiseCreator::lambda([actor_id = actor_id(this)](Result<unique_ptr<HttpQuery>> r_http_query) {
|
2019-02-04 00:29:37 +01:00
|
|
|
send_closure(actor_id, &GoogleDnsResolver::on_result, std::move(r_http_query));
|
|
|
|
});
|
2019-02-04 02:32:32 +01:00
|
|
|
wget_ = create_actor<Wget>(
|
|
|
|
"GoogleDnsResolver", std::move(wget_promise),
|
2020-01-25 21:36:49 +01:00
|
|
|
PSTRING() << "https://dns.google/resolve?name=" << url_encode(host_) << "&type=" << (prefer_ipv6_ ? 28 : 1),
|
|
|
|
std::vector<std::pair<string, string>>({{"Host", "dns.google"}}), timeout, ttl, prefer_ipv6_,
|
2019-02-04 02:32:32 +01:00
|
|
|
SslStream::VerifyPeer::Off);
|
2019-01-24 13:18:23 +01:00
|
|
|
}
|
|
|
|
|
2019-06-17 18:12:54 +02:00
|
|
|
static Result<IPAddress> get_ip_address(Result<unique_ptr<HttpQuery>> r_http_query) {
|
2019-02-04 00:29:37 +01:00
|
|
|
TRY_RESULT(http_query, std::move(r_http_query));
|
|
|
|
TRY_RESULT(json_value, json_decode(http_query->content_));
|
|
|
|
if (json_value.type() != JsonValue::Type::Object) {
|
|
|
|
return Status::Error("Failed to parse DNS result: not an object");
|
|
|
|
}
|
|
|
|
TRY_RESULT(answer, get_json_object_field(json_value.get_object(), "Answer", JsonValue::Type::Array, false));
|
|
|
|
auto &array = answer.get_array();
|
2021-10-18 18:26:14 +02:00
|
|
|
if (array.empty()) {
|
2019-02-04 00:29:37 +01:00
|
|
|
return Status::Error("Failed to parse DNS result: Answer is an empty array");
|
|
|
|
}
|
|
|
|
if (array[0].type() != JsonValue::Type::Object) {
|
|
|
|
return Status::Error("Failed to parse DNS result: Answer[0] is not an object");
|
|
|
|
}
|
|
|
|
auto &answer_0 = array[0].get_object();
|
|
|
|
TRY_RESULT(ip_str, get_json_object_string_field(answer_0, "data", false));
|
|
|
|
IPAddress ip;
|
|
|
|
TRY_STATUS(ip.init_host_port(ip_str, 0));
|
|
|
|
return ip;
|
|
|
|
}
|
|
|
|
|
2019-06-17 18:12:54 +02:00
|
|
|
void on_result(Result<unique_ptr<HttpQuery>> r_http_query) {
|
2019-02-04 00:29:37 +01:00
|
|
|
auto end_time = Time::now();
|
|
|
|
auto result = get_ip_address(std::move(r_http_query));
|
2019-02-04 15:02:21 +01:00
|
|
|
VLOG(dns_resolver) << "Init IPv" << (prefer_ipv6_ ? "6" : "4") << " host = " << host_ << " in "
|
|
|
|
<< end_time - begin_time_ << " seconds to "
|
|
|
|
<< (result.is_ok() ? (PSLICE() << result.ok()) : CSlice("[invalid]"));
|
2019-02-04 00:29:37 +01:00
|
|
|
promise_.set_result(std::move(result));
|
|
|
|
stop();
|
2019-01-24 10:47:54 +01:00
|
|
|
}
|
|
|
|
};
|
2019-01-24 18:08:29 +01:00
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class NativeDnsResolver final : public Actor {
|
2019-01-24 13:18:23 +01:00
|
|
|
public:
|
2019-02-04 02:32:32 +01:00
|
|
|
NativeDnsResolver(std::string host, bool prefer_ipv6, Promise<IPAddress> promise)
|
|
|
|
: host_(std::move(host)), prefer_ipv6_(prefer_ipv6), promise_(std::move(promise)) {
|
2019-01-24 13:18:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string host_;
|
2019-02-04 02:32:32 +01:00
|
|
|
bool prefer_ipv6_;
|
2019-01-24 13:18:23 +01:00
|
|
|
Promise<IPAddress> promise_;
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final {
|
2019-01-24 13:18:23 +01:00
|
|
|
IPAddress ip;
|
2019-01-24 18:08:29 +01:00
|
|
|
auto begin_time = Time::now();
|
2019-02-04 02:32:32 +01:00
|
|
|
auto status = ip.init_host_port(host_, 0, prefer_ipv6_);
|
2019-01-24 18:08:29 +01:00
|
|
|
auto end_time = Time::now();
|
2019-02-04 15:02:21 +01:00
|
|
|
VLOG(dns_resolver) << "Init host = " << host_ << " in " << end_time - begin_time << " seconds to " << ip;
|
2019-01-24 13:18:23 +01:00
|
|
|
if (status.is_error()) {
|
|
|
|
promise_.set_error(std::move(status));
|
2019-02-04 00:29:37 +01:00
|
|
|
} else {
|
|
|
|
promise_.set_value(std::move(ip));
|
2019-01-24 13:18:23 +01:00
|
|
|
}
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
};
|
2019-01-24 18:08:29 +01:00
|
|
|
|
2019-01-24 10:47:54 +01:00
|
|
|
} // namespace detail
|
|
|
|
|
2019-02-04 15:02:21 +01:00
|
|
|
int VERBOSITY_NAME(dns_resolver) = VERBOSITY_NAME(DEBUG);
|
|
|
|
|
2019-02-04 01:12:40 +01:00
|
|
|
GetHostByNameActor::GetHostByNameActor(Options options) : options_(std::move(options)) {
|
2019-02-04 02:36:13 +01:00
|
|
|
CHECK(!options_.resolver_types.empty());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-01-24 13:18:23 +01:00
|
|
|
void GetHostByNameActor::run(string host, int port, bool prefer_ipv6, Promise<IPAddress> promise) {
|
2019-02-04 03:39:20 +01:00
|
|
|
if (host.empty()) {
|
|
|
|
return promise.set_error(Status::Error("Host is empty"));
|
|
|
|
}
|
|
|
|
|
|
|
|
auto r_ascii_host = idn_to_ascii(host);
|
|
|
|
if (r_ascii_host.is_error()) {
|
|
|
|
return promise.set_error(r_ascii_host.move_as_error());
|
|
|
|
}
|
|
|
|
auto ascii_host = r_ascii_host.move_as_ok();
|
|
|
|
|
2019-01-24 18:08:29 +01:00
|
|
|
auto begin_time = Time::now();
|
2020-11-21 14:34:39 +01:00
|
|
|
auto &value = cache_[prefer_ipv6].emplace(ascii_host, Value{{}, begin_time - 1.0}).first->second;
|
2019-02-12 17:48:52 +01:00
|
|
|
if (value.expires_at > begin_time) {
|
2019-01-24 13:18:23 +01:00
|
|
|
return promise.set_result(value.get_ip_port(port));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-02-04 03:39:20 +01:00
|
|
|
auto &query = active_queries_[prefer_ipv6][ascii_host];
|
2019-02-04 01:19:31 +01:00
|
|
|
query.promises.emplace_back(port, std::move(promise));
|
|
|
|
if (query.query.empty()) {
|
|
|
|
CHECK(query.promises.size() == 1);
|
2019-02-04 03:39:20 +01:00
|
|
|
query.real_host = std::move(host);
|
|
|
|
query.begin_time = Time::now();
|
|
|
|
run_query(std::move(ascii_host), prefer_ipv6, query);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
2018-05-18 18:44:46 +02:00
|
|
|
|
2019-02-04 02:32:32 +01:00
|
|
|
void GetHostByNameActor::run_query(std::string host, bool prefer_ipv6, Query &query) {
|
|
|
|
auto promise = PromiseCreator::lambda([actor_id = actor_id(this), host, prefer_ipv6](Result<IPAddress> res) mutable {
|
|
|
|
send_closure(actor_id, &GetHostByNameActor::on_query_result, std::move(host), prefer_ipv6, std::move(res));
|
|
|
|
});
|
|
|
|
|
|
|
|
CHECK(query.query.empty());
|
2019-02-04 02:36:13 +01:00
|
|
|
CHECK(query.pos < options_.resolver_types.size());
|
|
|
|
auto resolver_type = options_.resolver_types[query.pos++];
|
2019-02-04 02:32:32 +01:00
|
|
|
query.query = [&] {
|
|
|
|
switch (resolver_type) {
|
2019-02-04 02:36:13 +01:00
|
|
|
case ResolverType::Native:
|
2019-02-04 02:32:32 +01:00
|
|
|
return ActorOwn<>(create_actor_on_scheduler<detail::NativeDnsResolver>(
|
|
|
|
"NativeDnsResolver", options_.scheduler_id, std::move(host), prefer_ipv6, std::move(promise)));
|
2019-02-04 02:36:13 +01:00
|
|
|
case ResolverType::Google:
|
2019-02-04 02:32:32 +01:00
|
|
|
return ActorOwn<>(create_actor_on_scheduler<detail::GoogleDnsResolver>(
|
|
|
|
"GoogleDnsResolver", options_.scheduler_id, std::move(host), prefer_ipv6, std::move(promise)));
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return ActorOwn<>();
|
|
|
|
}
|
|
|
|
}();
|
|
|
|
}
|
|
|
|
|
2019-02-04 03:39:20 +01:00
|
|
|
void GetHostByNameActor::on_query_result(std::string host, bool prefer_ipv6, Result<IPAddress> result) {
|
2019-02-04 01:19:31 +01:00
|
|
|
auto query_it = active_queries_[prefer_ipv6].find(host);
|
|
|
|
CHECK(query_it != active_queries_[prefer_ipv6].end());
|
|
|
|
auto &query = query_it->second;
|
|
|
|
CHECK(!query.promises.empty());
|
|
|
|
CHECK(!query.query.empty());
|
2019-02-04 01:12:40 +01:00
|
|
|
|
2019-02-04 03:39:20 +01:00
|
|
|
if (result.is_error() && query.pos < options_.resolver_types.size()) {
|
2019-02-04 02:32:32 +01:00
|
|
|
query.query.reset();
|
|
|
|
return run_query(std::move(host), prefer_ipv6, query);
|
|
|
|
}
|
|
|
|
|
2019-02-04 03:39:20 +01:00
|
|
|
auto end_time = Time::now();
|
2019-02-04 15:02:21 +01:00
|
|
|
VLOG(dns_resolver) << "Init host = " << query.real_host << " in total of " << end_time - query.begin_time
|
|
|
|
<< " seconds to " << (result.is_ok() ? (PSLICE() << result.ok()) : CSlice("[invalid]"));
|
2019-02-04 03:39:20 +01:00
|
|
|
|
2019-02-04 01:19:31 +01:00
|
|
|
auto promises = std::move(query.promises);
|
2019-02-04 02:32:32 +01:00
|
|
|
auto value_it = cache_[prefer_ipv6].find(host);
|
|
|
|
CHECK(value_it != cache_[prefer_ipv6].end());
|
2019-02-04 03:39:20 +01:00
|
|
|
auto cache_timeout = result.is_ok() ? options_.ok_timeout : options_.error_timeout;
|
|
|
|
value_it->second = Value{std::move(result), end_time + cache_timeout};
|
2019-02-04 01:19:31 +01:00
|
|
|
active_queries_[prefer_ipv6].erase(query_it);
|
2019-02-04 01:12:40 +01:00
|
|
|
|
|
|
|
for (auto &promise : promises) {
|
2019-02-04 02:32:32 +01:00
|
|
|
promise.second.set_result(value_it->second.get_ip_port(promise.first));
|
2019-02-04 01:12:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|