Support prefer_ipv6 in Wget.

GitOrigin-RevId: 80740a20f38174235160e05b1854e7023ebe3677
This commit is contained in:
levlam 2018-07-01 04:45:25 +03:00
parent 9b0a138dd1
commit 5a11dd6c58
4 changed files with 22 additions and 8 deletions

View File

@ -10,6 +10,7 @@
#include "td/net/HttpQuery.h"
#include "td/net/Wget.h"
#include "td/utils/common.h"
#include "td/utils/logging.h"
#include "td/utils/Status.h"
@ -21,6 +22,9 @@ int main(int argc, char *argv[]) {
td::VERBOSITY_NAME(fd) = VERBOSITY_NAME(INFO);
std::string url = (argc > 1 ? argv[1] : "https://telegram.org");
auto timeout = 10;
auto ttl = 3;
auto prefer_ipv6 = (argc > 2 && std::string(argv[2]) == "-6");
auto scheduler = std::make_unique<td::ConcurrentScheduler>();
scheduler->init(0);
scheduler
@ -28,7 +32,7 @@ int main(int argc, char *argv[]) {
LOG(ERROR) << *res.ok();
td::Scheduler::instance()->finish();
}),
url)
url, td::Auto(), timeout, ttl, prefer_ipv6)
.release();
scheduler->start();
while (scheduler->run_main(10)) {

View File

@ -109,11 +109,14 @@ Result<SimpleConfig> decode_config(Slice input) {
return std::move(config);
}
static ActorOwn<> get_simple_config_impl(Promise<SimpleConfig> promise, int32 scheduler_id, string url, string host) {
static ActorOwn<> get_simple_config_impl(Promise<SimpleConfig> promise, int32 scheduler_id, string url, string host,
bool prefer_ipv6) {
VLOG(config_recoverer) << "Request simple config from " << url;
#if TD_EMSCRIPTEN // FIXME
return ActorOwn<>();
#else
const int timeout = 10;
const int ttl = 3;
return ActorOwn<>(create_actor_on_scheduler<Wget>(
"Wget", scheduler_id,
PromiseCreator::lambda([promise = std::move(promise)](Result<HttpQueryPtr> r_query) mutable {
@ -122,7 +125,7 @@ static ActorOwn<> get_simple_config_impl(Promise<SimpleConfig> promise, int32 sc
return decode_config(http_query->content_);
}());
}),
std::move(url), std::vector<std::pair<string, string>>({{"Host", std::move(host)}}), 10 /*timeout*/, 3 /*ttl*/,
std::move(url), std::vector<std::pair<string, string>>({{"Host", std::move(host)}}), timeout, ttl, prefer_ipv6,
SslFd::VerifyPeer::Off));
#endif
}
@ -131,7 +134,8 @@ ActorOwn<> get_simple_config_azure(Promise<SimpleConfig> promise, const ConfigSh
int32 scheduler_id) {
string url = PSTRING() << "https://software-download.microsoft.com/" << (is_test ? "test" : "prod")
<< "v2/config.txt";
return get_simple_config_impl(std::move(promise), scheduler_id, std::move(url), "tcdnb.azureedge.net");
const bool prefer_ipv6 = 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);
}
ActorOwn<> get_simple_config_google_dns(Promise<SimpleConfig> promise, const ConfigShared *shared_config, bool is_test,
@ -141,6 +145,9 @@ ActorOwn<> get_simple_config_google_dns(Promise<SimpleConfig> promise, const Con
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->get_option_boolean("prefer_ipv6");
if (name.empty()) {
name = is_test ? "tapv2.stel.com" : "apv2.stel.com";
}
@ -178,7 +185,7 @@ ActorOwn<> get_simple_config_google_dns(Promise<SimpleConfig> promise, const Con
}());
}),
PSTRING() << "https://www.google.com/resolve?name=" << url_encode(name) << "&type=16",
std::vector<std::pair<string, string>>({{"Host", "dns.google.com"}}), 10 /*timeout*/, 3 /*ttl*/,
std::vector<std::pair<string, string>>({{"Host", "dns.google.com"}}), timeout, ttl, prefer_ipv6,
SslFd::VerifyPeer::Off));
#endif
}

View File

@ -23,12 +23,13 @@
namespace td {
Wget::Wget(Promise<HttpQueryPtr> promise, string url, std::vector<std::pair<string, string>> headers, int32 timeout_in,
int32 ttl, SslFd::VerifyPeer verify_peer)
int32 ttl, bool prefer_ipv6, SslFd::VerifyPeer verify_peer)
: promise_(std::move(promise))
, input_url_(std::move(url))
, headers_(std::move(headers))
, timeout_in_(timeout_in)
, ttl_(ttl)
, prefer_ipv6_(prefer_ipv6)
, verify_peer_(verify_peer) {
}
@ -61,7 +62,7 @@ Status Wget::try_init() {
TRY_RESULT(header, hc.finish());
IPAddress addr;
TRY_STATUS(addr.init_host_port(url.host_, url.port_));
TRY_STATUS(addr.init_host_port(url.host_, url.port_, prefer_ipv6_));
TRY_RESULT(fd, SocketFd::open(addr));
if (url.protocol_ == HttpUrl::Protocol::HTTP) {

View File

@ -22,7 +22,8 @@ namespace td {
class Wget : public HttpOutboundConnection::Callback {
public:
explicit Wget(Promise<HttpQueryPtr> promise, string url, std::vector<std::pair<string, string>> headers = {},
int32 timeout_in = 10, int32 ttl = 3, SslFd::VerifyPeer verify_peer = SslFd::VerifyPeer::On);
int32 timeout_in = 10, int32 ttl = 3, bool prefer_ipv6 = false,
SslFd::VerifyPeer verify_peer = SslFd::VerifyPeer::On);
private:
Status try_init();
@ -42,6 +43,7 @@ class Wget : public HttpOutboundConnection::Callback {
std::vector<std::pair<string, string>> headers_;
int32 timeout_in_;
int32 ttl_;
bool prefer_ipv6_ = false;
SslFd::VerifyPeer verify_peer_;
};