-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdns_https_get.cc
73 lines (60 loc) · 2.24 KB
/
dns_https_get.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include "dns_https_get.h"
#include "dnswire.h"
#include "network_manager.h"
#include "thirdparty/uritemplate.h"
#include <QUrl>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QNetworkAccessManager>
using UriTemplate = uritemplatecpp::UriTemplate;
DohGetImplementation::DohGetImplementation(ushort type, const QString& name, const QString& url, QObject *parent) : DnsImplementation(parent), request(WireRequest(type, name)), resolverUrl(url) {
}
QString DohGetImplementation::ErrorString() const {
return errorString;
}
QList<QHostAddress> DohGetImplementation::Addresses() const {
return WireToAddresses(response);
}
QString DohGetImplementation::PrettyResults() const {
return WireToPretty(response);
}
void DohGetImplementation::lookup() {
if (request.isEmpty()) {
errorString = tr("Failed to serialize query");
emit finished();
return;
}
QByteArray b64request = request.toBase64(QByteArray::Base64UrlEncoding | QByteArray::OmitTrailingEquals);
UriTemplate uri(resolverUrl.toStdString());
uri.set("dns", b64request.toStdString());
QString targetUrl = QString::fromStdString(uri.build());
QUrl u(targetUrl);
if (!u.isValid() || u.host().isEmpty()) {
errorString = tr("'%1' doesn't look like the name of a dns over https resolver").arg(resolverUrl);
emit finished();
return;
}
QNetworkRequest req;
req.setUrl(u);
req.setRawHeader("Accept", "application/dns-message");
timer.start();
QNetworkReply *repl = networkManager->get(req);
connect(repl, &QNetworkReply::encrypted, this, [=](){
timer.start();
});
connect(repl, &QNetworkReply::finished, this, [=](){
responseTime = timer.nsecsElapsed();
if (repl->error() != QNetworkReply::NoError) {
errorString = repl->errorString();
} else {
QVariant ct = repl->header(QNetworkRequest::ContentTypeHeader);
if (ct.isValid() && !ct.toString().toLower().contains("application/dns-message")) {
errorString = tr("Unexpected content-type: %1").arg(ct.toString());
} else {
response = repl->readAll();
}
}
emit finished();
repl->deleteLater();
});
}