-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedr.cpp
314 lines (253 loc) · 8.5 KB
/
edr.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// Copyright 2020 VMware, Inc.
// SPDX-License-Identifier: MIT
#include "edr.h"
#include "ui_edr.h"
#include <QSettings>
#include <QProcess>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonValueRef>
#include <QTreeView>
#include <QFileDialog>
#include <QMessageBox>
#include "qjsonmodel.h"
#include "eraser.h"
Edr::Edr(QWidget *parent) :
QWidget(parent),
ui(new Ui::Edr),
manager(new QNetworkAccessManager(this))
{
ui->setupUi(this);
ui->table->resizeColumnsToContents();
ui->table->horizontalHeader()->setStretchLastSection(true);
ui->textEdit->setVisible(false);
QStringList inst_list_edr = get_instances_list_edr();
for (int inst=0; inst<inst_list_edr.size(); inst++) {
ui->comboBox_server->addItem(QIcon(":/img/onprem.png"),inst_list_edr[inst]);
}
ui->pushButton_delete->setVisible(false); // TODO
}
QStringList Edr::get_instances_list_edr()
{
QStringList inst_list;
QSettings settings;
int size = settings.beginReadArray("instances_edr");
for (int i = 0; i < size; ++i) {
settings.setArrayIndex(i);
inst_list << settings.value("name").toString();
}
settings.endArray();
return inst_list;
}
Edr::~Edr()
{
delete ui;
}
void Edr::on_pushButton_ping_clicked()
{
QString server = server_url;
ui->checkBox_show_logs->setCheckState(Qt::Checked);
ui->textEdit->append("--------------------------------------------------------------------------------------------------------------------");
ui->textEdit->append("PING server");
ui->textEdit->append(server);
server = server.mid(sizeof("https://")-1); // Remove https://
ui->textEdit->append(server);
server=server.split(":").first();
ui->textEdit->append(server);
QStringList parameters;
#if defined(WIN32)
parameters << "-n" << "1";
#else
parameters << "-c 1";
#endif
parameters << server;
int exitCode = QProcess::execute("ping", parameters);
if (exitCode==0) {
ui->textEdit->append("Server alive\n");
} else {
ui->textEdit->append("Server dead\n");
}
}
void Edr::on_pushButton_sensor_list_clicked()
{
ui->textEdit->append("--------------------------------------------------------------------------------------------------------------------");
ui->textEdit->append("Get Sensor List");
// Example:
// curl -k -H X-Auth-Token:e1638bbf0c868c1d437068498a22d5e7e47c6b7f https://192.168.230.2:443/api/v1/sensor
QString server = server_url;
QString api = server_api;
QString url(server + "/api/v1/sensor");
ui->textEdit->append(url);
QNetworkRequest request=QNetworkRequest(QUrl(url));
request.setRawHeader("X-Auth-Token", api.toLocal8Bit());
reply_error = false;
reply = manager->get(request);
#if QT_VERSION_MAJOR == 5
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(slotError(QNetworkReply::NetworkError)));
#endif
connect(reply, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(slotSslErrors(QList<QSslError>)));
connect(reply, SIGNAL(finished()), this, SLOT(slotFinished()));
}
void Edr::slotError(QNetworkReply::NetworkError)
{
reply_error = true;
ui->textEdit->append("-- BEGIN -- Network error");
ui->textEdit->append(reply->errorString());
ui->textEdit->append("-- END -- Network error");
ui->checkBox_show_logs->setCheckState(Qt::Checked);
QMessageBox::critical(this, QString("Network Error: %0").arg(reply->error()), reply->errorString(), QMessageBox::Ok);
}
void Edr::slotSslErrors(QList<QSslError> le)
{
// reply_error = true; We have always an error the first connection if certificate is self signed
ui->textEdit->append("-- BEGIN -- SSL error");
for (const QSslError &e : le) {
ui->textEdit->append(e.errorString());
}
reply->ignoreSslErrors();
ui->textEdit->append("-- END -- SSL error");
ui->checkBox_show_logs->setCheckState(Qt::Checked);
}
void Edr::slotFinished()
{
QByteArray sensors_latin1 = reply->readAll();
reply->deleteLater();
if (reply_error)
return;
QString s;
s = s.fromLatin1(sensors_latin1);
jsonReports = s.toUtf8();
QJsonParseError *e = new QJsonParseError();
QJsonDocument json_doc = QJsonDocument::fromJson(jsonReports, e);
if (json_doc.isNull()) {
ui->textEdit->append("Failed parse JSON");
ui->textEdit->append(e->errorString());
return;
}
if (!json_doc.isArray()) {
ui->textEdit->append("JSON is not an array.");
return;
}
QJsonArray jsonArray = json_doc.array();
int sensor = 0;
foreach (const QJsonValue & value, jsonArray) {
QTableWidgetItem *item;
QJsonObject obj = value.toObject();
if ((ui->checkBox_show_uninstalled->isChecked()) || (obj["status"].toString() != "Uninstall Pending"))
{
ui->table->setRowCount(sensor+1);
item = new QTableWidgetItem(QString::number(obj["id"].toInt()));
ui->table->setItem(sensor, 0, item);
item = new QTableWidgetItem(obj["computer_dns_name"].toString());
ui->table->setItem(sensor, 1, item);
item = new QTableWidgetItem(obj["registration_time"].toString());
ui->table->setItem(sensor, 2, item);
item = new QTableWidgetItem(obj["last_checkin_time"].toString());
ui->table->setItem(sensor, 3, item);
item = new QTableWidgetItem(obj["status"].toString());
ui->table->setItem(sensor, 4, item);
item = new QTableWidgetItem("");
ui->table->setItem(sensor, 5, item);
sensor++;
}
}
ui->table->resizeColumnsToContents();
ui->table->horizontalHeader()->setStretchLastSection(true);
ui->textEdit->append(QString(tr("Found %1 sensors\n").arg(sensor)));
}
void Edr::on_checkBox_show_logs_stateChanged(int arg1)
{
if (arg1) {
ui->textEdit->setVisible(true);
} else {
ui->textEdit->setVisible(false);
}
}
void Edr::on_pushButton_duplicate_clicked()
{
int found = 0;
int nb = ui->table->rowCount();
for (int i=0; i<nb; i++)
{
QString s_name = ui->table->item(i,1)->text();
for (int j=i+1; j<nb; j++)
{
if (s_name == ui->table->item(j,1)->text())
{
found++;
QTableWidgetItem *item = new QTableWidgetItem("X");
QString date1 = ui->table->item(i,3)->text();
QString date2 = ui->table->item(j,3)->text();
if (date1 < date2)
{
ui->table->setItem(i, 5, item);
}
else
{
ui->table->setItem(j, 5, item);
}
}
}
}
ui->textEdit->append(QString(tr("Found %1 duplicate sensors\n").arg(found)));
}
void Edr::on_checkBox_show_uninstalled_clicked()
{
on_pushButton_sensor_list_clicked();
}
void Edr::on_pushButton_delete_clicked()
{
Eraser *w = new Eraser(this);
w->setModal(true);
w->setTable(ui->table);
w->exec();
on_pushButton_sensor_list_clicked();
}
void Edr::on_comboBox_server_currentIndexChanged(const QString &name)
{
QStringList list;
QSettings settings;
int size = settings.beginReadArray("instances_edr");
for (int i = 0; i < size; ++i) {
settings.setArrayIndex(i);
if (name == settings.value("name").toString())
{
server_name = name;
server_url = settings.value("url").toString();
server_api = settings.value("api").toString();
break;
}
}
settings.endArray();
}
void Edr::on_pushButton_json_view_clicked()
{
QTreeView * view = new QTreeView;
QJsonModel * model = new QJsonModel;
view->setModel(model);
model->loadJson(jsonReports);
view->resize(1024,768);
view->setColumnWidth(0,400);
view->expandAll();
view->setWindowTitle(tr("Sensor list"));
view->setWindowIcon(QIcon(":/img/cb.jpeg"));
view->show();
}
void Edr::on_pushButton_download_clicked()
{
QString fileName = QFileDialog::getSaveFileName(this, tr("Save sensor list"), "", tr("JSON file (*.json);;All Files (*)"));
if (fileName.isEmpty())
return;
if (!fileName.contains(".")) {
fileName += ".json";
}
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly)) {
QMessageBox::information(this, tr("Unable to open file"),
file.errorString());
return;
}
file.write(jsonReports);
file.close();
}