2018-12-31 20:04:05 +01:00
|
|
|
//
|
2018-12-31 23:02:34 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2019
|
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-04 01:12:40 +01:00
|
|
|
#include "td/net/SslStream.h"
|
2019-01-24 18:08:29 +01:00
|
|
|
#include "td/net/Wget.h"
|
|
|
|
|
|
|
|
#include "td/utils/JsonBuilder.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/logging.h"
|
2019-02-04 01:12:40 +01:00
|
|
|
#include "td/utils/Slice.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
|
|
|
|
2019-01-24 10:47:54 +01:00
|
|
|
class GoogleDnsResolver : public Actor {
|
|
|
|
public:
|
2019-01-24 18:08:29 +01:00
|
|
|
GoogleDnsResolver(std::string host, GetHostByNameActor::ResolveOptions options, Promise<IPAddress> promise)
|
2019-01-24 13:18:23 +01:00
|
|
|
: host_(std::move(host)), options_(std::move(options)), 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-01-24 14:07:11 +01:00
|
|
|
GetHostByNameActor::ResolveOptions options_;
|
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
|
|
|
|
2019-01-24 13:18:23 +01:00
|
|
|
void start_up() override {
|
|
|
|
const int timeout = 10;
|
|
|
|
const int ttl = 3;
|
2019-02-04 00:29:37 +01:00
|
|
|
begin_time_ = Time::now();
|
|
|
|
auto wget_promise = PromiseCreator::lambda([actor_id = actor_id(this)](Result<HttpQueryPtr> r_http_query) {
|
|
|
|
send_closure(actor_id, &GoogleDnsResolver::on_result, std::move(r_http_query));
|
|
|
|
});
|
|
|
|
wget_ = create_actor<Wget>("GoogleDnsResolver", std::move(wget_promise),
|
2019-01-24 13:18:23 +01:00
|
|
|
PSTRING() << "https://www.google.com/resolve?name=" << url_encode(host_)
|
|
|
|
<< "&type=" << (options_.prefer_ipv6 ? 28 : 1),
|
|
|
|
std::vector<std::pair<string, string>>({{"Host", "dns.google.com"}}), timeout, ttl,
|
|
|
|
options_.prefer_ipv6, SslStream::VerifyPeer::Off);
|
|
|
|
}
|
|
|
|
|
2019-02-04 00:29:37 +01:00
|
|
|
static Result<IPAddress> get_ip_address(Result<HttpQueryPtr> r_http_query) {
|
|
|
|
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();
|
|
|
|
if (array.size() == 0) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_result(Result<HttpQueryPtr> r_http_query) {
|
|
|
|
auto end_time = Time::now();
|
|
|
|
auto result = get_ip_address(std::move(r_http_query));
|
|
|
|
LOG(WARNING) << "Init host = " << host_ << " in " << end_time - begin_time_ << " seconds to "
|
|
|
|
<< (result.is_ok() ? (PSLICE() << result.ok()) : CSlice("[invalid]"));
|
|
|
|
promise_.set_result(std::move(result));
|
|
|
|
stop();
|
2019-01-24 10:47:54 +01:00
|
|
|
}
|
|
|
|
};
|
2019-01-24 18:08:29 +01:00
|
|
|
|
2019-01-24 13:18:23 +01:00
|
|
|
class NativeDnsResolver : public Actor {
|
|
|
|
public:
|
2019-01-24 18:08:29 +01:00
|
|
|
NativeDnsResolver(std::string host, GetHostByNameActor::ResolveOptions options, Promise<IPAddress> promise)
|
2019-01-24 13:18:23 +01:00
|
|
|
: host_(std::move(host)), options_(std::move(options)), promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string host_;
|
2019-01-24 14:07:11 +01:00
|
|
|
GetHostByNameActor::ResolveOptions options_;
|
2019-01-24 13:18:23 +01:00
|
|
|
Promise<IPAddress> promise_;
|
|
|
|
|
|
|
|
void start_up() override {
|
|
|
|
IPAddress ip;
|
2019-01-24 18:08:29 +01:00
|
|
|
auto begin_time = Time::now();
|
2019-01-24 13:18:23 +01:00
|
|
|
auto status = ip.init_host_port(host_, 0, options_.prefer_ipv6);
|
2019-01-24 18:08:29 +01:00
|
|
|
auto end_time = Time::now();
|
2019-01-24 13:18:23 +01:00
|
|
|
LOG(WARNING) << "Init host = " << host_ << " in " << end_time - begin_time << " seconds to " << ip;
|
|
|
|
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 13:18:23 +01:00
|
|
|
class DnsResolver : public Actor {
|
|
|
|
public:
|
2019-01-24 18:08:29 +01:00
|
|
|
DnsResolver(std::string host, GetHostByNameActor::ResolveOptions options, Promise<IPAddress> promise)
|
2019-01-24 13:18:23 +01:00
|
|
|
: host_(std::move(host)), options_(std::move(options)), promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string host_;
|
2019-01-24 14:07:11 +01:00
|
|
|
GetHostByNameActor::ResolveOptions options_;
|
2019-01-24 13:18:23 +01:00
|
|
|
Promise<IPAddress> promise_;
|
|
|
|
ActorOwn<> query_;
|
|
|
|
size_t pos_ = 0;
|
2019-01-24 18:21:00 +01:00
|
|
|
GetHostByNameActor::ResolveType types[2] = {GetHostByNameActor::ResolveType::Google,
|
|
|
|
GetHostByNameActor::ResolveType::Native};
|
2019-01-24 13:18:23 +01:00
|
|
|
|
|
|
|
void loop() override {
|
|
|
|
if (!query_.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (pos_ == 2) {
|
2019-02-04 00:29:37 +01:00
|
|
|
promise_.set_error(Status::Error("Failed to resolve IP address"));
|
2019-01-24 13:18:23 +01:00
|
|
|
return stop();
|
|
|
|
}
|
|
|
|
options_.type = types[pos_];
|
2019-01-24 18:07:24 +01:00
|
|
|
pos_++;
|
2019-01-24 13:18:23 +01:00
|
|
|
query_ = GetHostByNameActor::resolve(host_, options_,
|
|
|
|
PromiseCreator::lambda([actor_id = actor_id(this)](Result<IPAddress> res) {
|
|
|
|
send_closure(actor_id, &DnsResolver::on_result, std::move(res));
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_result(Result<IPAddress> res) {
|
|
|
|
query_.reset();
|
|
|
|
if (res.is_ok() || pos_ == 2) {
|
|
|
|
promise_.set_result(std::move(res));
|
|
|
|
return stop();
|
|
|
|
}
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
};
|
2019-02-04 01:12:40 +01:00
|
|
|
|
2019-01-24 10:47:54 +01:00
|
|
|
} // namespace detail
|
|
|
|
|
2019-01-24 14:07:11 +01:00
|
|
|
ActorOwn<> GetHostByNameActor::resolve(std::string host, ResolveOptions options, Promise<IPAddress> promise) {
|
2019-01-24 13:18:23 +01:00
|
|
|
switch (options.type) {
|
2019-01-24 18:21:00 +01:00
|
|
|
case ResolveType::Native:
|
2019-01-24 13:18:23 +01:00
|
|
|
return ActorOwn<>(create_actor_on_scheduler<detail::NativeDnsResolver>(
|
|
|
|
"NativeDnsResolver", options.scheduler_id, std::move(host), options, std::move(promise)));
|
2019-01-24 18:21:00 +01:00
|
|
|
case ResolveType::Google:
|
2019-01-24 13:18:23 +01:00
|
|
|
return ActorOwn<>(create_actor_on_scheduler<detail::GoogleDnsResolver>(
|
|
|
|
"GoogleDnsResolver", options.scheduler_id, std::move(host), options, std::move(promise)));
|
2019-01-24 18:21:00 +01:00
|
|
|
case ResolveType::All:
|
2019-01-24 13:18:23 +01:00
|
|
|
return ActorOwn<>(create_actor_on_scheduler<detail::DnsResolver>("DnsResolver", options.scheduler_id,
|
|
|
|
std::move(host), options, std::move(promise)));
|
2019-01-24 18:08:29 +01:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return ActorOwn<>();
|
2019-01-24 13:18:23 +01:00
|
|
|
}
|
2019-01-24 10:47:54 +01:00
|
|
|
}
|
2018-05-18 18:44:46 +02:00
|
|
|
|
2019-02-04 01:12:40 +01:00
|
|
|
GetHostByNameActor::GetHostByNameActor(Options options) : options_(std::move(options)) {
|
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) {
|
2018-07-01 03:12:20 +02:00
|
|
|
auto &value = cache_[prefer_ipv6].emplace(host, Value{{}, 0}).first->second;
|
2019-01-24 18:08:29 +01:00
|
|
|
auto begin_time = Time::now();
|
2018-12-31 20:04:05 +01:00
|
|
|
if (value.expire_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-01-24 13:18:23 +01:00
|
|
|
value.promises.emplace_back(port, std::move(promise));
|
|
|
|
if (value.query.empty()) {
|
2019-02-04 00:29:37 +01:00
|
|
|
CHECK(value.promises.size() == 1);
|
|
|
|
|
2019-01-24 14:07:11 +01:00
|
|
|
ResolveOptions options;
|
|
|
|
options.type = options_.type;
|
|
|
|
options.scheduler_id = options_.scheduler_id;
|
2019-01-24 13:18:23 +01:00
|
|
|
options.prefer_ipv6 = prefer_ipv6;
|
|
|
|
value.query =
|
|
|
|
resolve(host, options,
|
|
|
|
PromiseCreator::lambda([actor_id = actor_id(this), host, prefer_ipv6](Result<IPAddress> res) mutable {
|
|
|
|
send_closure(actor_id, &GetHostByNameActor::on_result, std::move(host), prefer_ipv6, std::move(res));
|
|
|
|
}));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
2018-05-18 18:44:46 +02:00
|
|
|
|
2019-02-04 01:12:40 +01:00
|
|
|
void GetHostByNameActor::on_result(std::string host, bool prefer_ipv6, Result<IPAddress> res) {
|
|
|
|
auto value_it = cache_[prefer_ipv6].find(host);
|
|
|
|
CHECK(value_it != cache_[prefer_ipv6].end());
|
|
|
|
auto &value = value_it->second;
|
|
|
|
CHECK(!value.promises.empty());
|
|
|
|
CHECK(!value.query.empty());
|
|
|
|
|
|
|
|
auto promises = std::move(value.promises);
|
|
|
|
auto end_time = Time::now() + (res.is_ok() ? options_.ok_timeout : options_.error_timeout);
|
|
|
|
value = Value{std::move(res), end_time};
|
|
|
|
CHECK(value.promises.empty());
|
|
|
|
CHECK(value.query.empty());
|
|
|
|
|
|
|
|
for (auto &promise : promises) {
|
|
|
|
promise.second.set_result(value.get_ip_port(promise.first));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|