-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquery.cc
140 lines (125 loc) · 4.67 KB
/
query.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include "query.h"
#include "dns.h"
#include "resolvers.h"
#include <QComboBox>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QTextEdit>
#include <QFormLayout>
#include <QSettings>
#include <QLineEdit>
#include <QLabel>
#include <QSizePolicy>
/*
* A panel that'll lookup a DNS record via a chosen
* resolver and display it prettily.
*/
Query::Query(Resolvers *res, QWidget *parent) : QWidget(parent), resolvers(res) {
auto inputForm = new QFormLayout;
inputForm->setFieldGrowthPolicy(QFormLayout::AllNonFixedFieldsGrow);
resolver = new QComboBox;
resolver->setEditable(true);
resolver->setObjectName("query_resolver");
connect(resolvers, &Resolvers::resolversChanged, [=](){
resolvers->populateCombo(resolver);
});
resolvers->populateCombo(resolver);
resolverAnno = new QLabel;
inputForm->addRow(tr("Resolver"), resolver);
inputForm->addRow(QString(), resolverAnno);
type = new QComboBox;
type->setEditable(true);
type->setObjectName("query_type");
type->addItem(tr("A+AAAA"), QString("A+AAAA"));
type->addItem(tr("A"), QString("A"));
type->addItem(tr("AAAA"), QString("AAAA"));
type->addItem(tr("CNAME"), QString("CNAME"));
type->addItem(tr("MX"), QString("MX"));
type->addItem(tr("NS"), QString("NS"));
type->addItem(tr("PTR"), QString("PTR"));
type->addItem(tr("SRV"), QString("SRV"));
type->addItem(tr("TXT"), QString("TXT"));
inputForm->addRow(tr("Query type"), type);
name = new QComboBox;
name->setEditable(true);
name->setObjectName("query_name");
name->setInsertPolicy(QComboBox::InsertAtTop);
name->lineEdit()->setPlaceholderText(tr("e.g. github.com"));
name->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
inputForm->addRow(tr("Query name"), name);
auto buttonsLayout = new QHBoxLayout;
go = new QPushButton(tr("Go"));
connect(go, &QPushButton::clicked, this, &Query::run);
buttonsLayout->addStretch(1);
buttonsLayout->addWidget(go);
results = new QTextEdit;
results->setReadOnly(true);
auto topLayout = new QVBoxLayout;
topLayout->addLayout(inputForm);
topLayout->addLayout(buttonsLayout);
topLayout->addWidget(results, 1);
setLayout(topLayout);
QSettings s;
s.beginGroup("query");
resolver->setCurrentIndex(s.value("resolver", 0).toInt());
type->setCurrentIndex(s.value("type", 0).toInt());
name->setEditText(s.value("name").toString());
s.endGroup();
updateResolverAnno();
connect(resolver, &QComboBox::currentTextChanged, this, &Query::updateResolverAnno);
}
void Query::saveState() {
QSettings s;
s.beginGroup("query");
s.setValue("resolver", resolver->currentIndex());
s.setValue("type", type->currentIndex());
s.setValue("name", name->currentText());
s.endGroup();
}
void Query::updateResolverAnno() {
QString u = url();
if (u == resolver->currentText()) {
resolverAnno->clear();
return;
}
resolverAnno->setText(tr("<small>(%1)</small>").arg(u.toHtmlEscaped()));
}
QString Query::url() {
// We pull the resolver URL from the userdata for preloaded
// resolvers. But if the user has entered something manually
// then we use that instead. There's no great way to detect
// that, so we look to see if the displayed text is the same
// as the text that was set at this index.
if (!resolver->currentData().isNull() && resolver->currentText() == resolver->itemText(resolver->currentIndex())) {
return resolver->currentData().toString();
}
return resolver->currentText();
}
void Query::run() {
QString resolverUrl = url();
QString queryType = type->currentText();
if (!type->currentData().isNull()) {
queryType = type->currentData().toString();
}
if (queryType == "A+AAAA") {
auto qa = new DnsLookup("A", name->currentText(), resolverUrl, this);
auto qaaaa = new DnsLookup("AAAA", name->currentText(), resolverUrl, this);
connect(qa, &DnsLookup::finished, [=](){
qaaaa->lookup();
});
connect(qaaaa, &DnsLookup::finished, [=](){
results->setText(qa->ErrorString() + qa->PrettyResults() + QString("\n") + qaaaa->ErrorString() + qaaaa->PrettyResults());
qa->deleteLater();
qaaaa->deleteLater();
});
qa->lookup();
return;
}
auto q = new DnsLookup(queryType, name->currentText(), resolverUrl, this);
connect(q, &DnsLookup::finished, [=](){
results->setText(tr("Resolver: %1\nResponse time %2 ms\n\n").arg(resolverUrl).arg(static_cast<double>(q->responseTime()) / 1000000.0, 0, 'f', 3) + q->ErrorString() + q->PrettyResults());
q->deleteLater();
});
q->lookup();
}