-
Notifications
You must be signed in to change notification settings - Fork 0
/
iplist.cpp
177 lines (154 loc) · 5 KB
/
iplist.cpp
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright 2020 VMware, Inc.
// SPDX-License-Identifier: MIT
#include "iplist.h"
#include "ui_iplist.h"
#include <QTimer>
#include <QSettings>
#include <QMessageBox>
#include "iplistadd.h"
IpList::IpList(QWidget *parent) :
QWidget(parent),
ui(new Ui::IpList),
lookup_in_progress(false)
{
ui->setupUi(this);
timer = new QTimer(this);
updateServerList();
updateComboBox();
on_comboBox_region_currentIndexChanged(0);
}
IpList::~IpList()
{
delete ui;
}
void IpList::on_pushButton_lookup_clicked()
{
if (!lookup_in_progress)
{
lookup_in_progress = true;
on_comboBox_region_currentIndexChanged(0);
connect(timer, SIGNAL(timeout()), this, SLOT(lookUp()));
timer->start(30*1000);
ui->pushButton_lookup->setText(tr("Lookup in loop every 30s in progress...")); // QT internal cache is updated every 30s.
ui->comboBox_region->setEnabled(false);
lookUp(); // Don't wait 30s for first lookup
} else {
lookup_in_progress = false;
timer->stop();
ui->pushButton_lookup->setText(tr("Lookup"));
ui->comboBox_region->setEnabled(true);
}
}
void IpList::lookUp()
{
QString host = ui->comboBox_region->currentText();
QHostInfo::lookupHost(host, this, SLOT(lookedUp(QHostInfo)));
}
void IpList::lookedUp(const QHostInfo &host)
{
bool newIP = false;
if (host.error() != QHostInfo::NoError) {
qDebug() << tr("Lookup failed:") << host.errorString();
QMessageBox::critical(this, tr("Lookup failed"), host.errorString(), QMessageBox::Ok);
return;
}
const auto addresses = host.addresses();
for (const QHostAddress &address : addresses) {
QString s = address.toString();
if (!ipList.contains(s)) {
newIP = true;
ipList.append(s);
ipList_new.append(s);
}
}
if (newIP) {
int count = 0;
ipList.sort();
ui->plainTextEdit->clear();
for (const QString &s : ipList) {
ui->plainTextEdit->appendPlainText(s);
count++;
}
ui->plainTextEdit->appendPlainText(QString("%0 IP addresses").arg(count));
ui->plainTextEdit->appendPlainText("\n------- ");
ui->plainTextEdit->appendPlainText(tr("New addresses"));
ui->plainTextEdit->appendPlainText(" -------- \n");
count = 0;
ipList_new.sort();
for (const QString &s : ipList_new) {
ui->plainTextEdit->appendPlainText(s);
count++;
}
ui->plainTextEdit->appendPlainText(QString("%0 new IP addresses").arg(count));
}
QSettings settings;
settings.setValue(ui->comboBox_region->currentText() + "_iplist", ipList);
}
void IpList::on_comboBox_region_currentIndexChanged(int index)
{
int count = 0;
QSettings settings;
Q_UNUSED(index);
ui->plainTextEdit->clear();
ipList_new.clear();
ipList = settings.value(ui->comboBox_region->currentText() + "_iplist").toStringList();
for (const QString &s : ipList) {
ui->plainTextEdit->appendPlainText(s);
count++;
}
ui->plainTextEdit->appendPlainText(QString("%0 IP addresses").arg(count));
}
void IpList::on_pushButton_add_clicked()
{
auto d = new ipListAdd(this);
int res = d->exec();
if (res == QDialog::Accepted)
{
addServer(d->getServer());
updateComboBox();
ui->comboBox_region->setCurrentText(d->getServer());
}
}
void IpList::addServer(const QString &newServer)
{
QSettings settings;
ipList_servers << newServer;
ipList_servers.removeDuplicates();
ipList_servers.sort();
settings.setValue("ipList_servers", ipList_servers);
}
void IpList::updateServerList()
{
ipList_servers.clear();
ipList_servers << "api-prod06.conferdeploy.net"
<< "api-prodnrt.conferdeploy.net"
<< "api-prodsyd.conferdeploy.net"
<< "api.confer.net"
<< "api5.conferdeploy.net"
<< "content.carbonblack.io"
<< "crl.godaddy.com"
<< "dashboard.confer.net"
<< "defense-eu.conferdeploy.net"
<< "defense-prodnrt.conferdeploy.net"
<< "defense-prodsyd.conferdeploy.net"
<< "defense.conferdeploy.net"
<< "dev-prod05.conferdeploy.net"
<< "dev-prod06.conferdeploy.net"
<< "dev-prodnrt.conferdeploy.net"
<< "dev-prodsyd.conferdeploy.net"
<< "dev5.conferdeploy.net"
<< "devices.confer.net"
<< "ocsp.godaddy.com"
<< "updates2.cdc.carbonblack.io";
QSettings settings;
ipList_servers << settings.value("ipList_servers").toStringList();
ipList_servers.removeDuplicates();
ipList_servers.sort();
}
void IpList::updateComboBox()
{
ui->comboBox_region->clear();
for (const QString &server : ipList_servers) {
ui->comboBox_region->addItem(server);
}
}