-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadfiles.cc
179 lines (160 loc) · 5.58 KB
/
readfiles.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
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
/*
* NESCA-VIEWER
* by oldteam & lomaster
* license CC-BY-NC-SA 4.0
* Сделано от души 2023.
*/
#include "readfiles.h"
#include <QXmlStreamReader>
#include <QFile>
#include <QList>
#include <QDebug>
datablock parse_data_block_nmap(QXmlStreamReader &xml)
{
datablock block;
__details details;
port currentPort;
bool no_open = false;
while (!xml.atEnd() && !xml.hasError())
{
xml.readNext();
if (xml.isStartElement())
{
QString elementName = xml.name().toString();
if (elementName == "address")
{
if (xml.attributes().hasAttribute("addrtype") && xml.attributes().value("addrtype").toString() == "ipv4")
{
block.ip_address = xml.attributes().value("addr").toString();
}
}
else if (elementName == "hostname")
{
if (xml.attributes().hasAttribute("name"))
{
details.dns_name = xml.attributes().value("name").toString();
}
}
else if (elementName == "times")
{
if (xml.attributes().hasAttribute("rttvar"))
{
QString rttvarStr = xml.attributes().value("rttvar").toString();
// Replace comma with period in the string and then convert to double
rttvarStr.replace(",", ".");
details.rtt = xml.attributes().value("rttvar").toString().toDouble() / 1000.0;
}
}
else if (elementName == "port")
{
currentPort.port = xml.attributes().value("portid").toString().toInt();
currentPort.protocol = xml.attributes().value("protocol").toString();
}
else if (elementName == "state")
{
currentPort.state = xml.attributes().value("state").toString();
}
}
else if (xml.isEndElement())
{
QString elementName = xml.name().toString();
if (elementName == "port")
{
details.ports.append(currentPort);
currentPort = port();
}
else if (elementName == "host")
{
block._details = details;
break; // Exit the loop after processing a host
}
}
}
return block;
}
QList<datablock> parse_xml_files(QString& file_path)
{
QList<datablock> dataBlocks;
QFile file(file_path);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
qDebug() << "Failed to open the file:" << file_path;
file.close();
return {};
}
QXmlStreamReader xml(&file);
while (!xml.atEnd() && !xml.hasError())
{
xml.readNextStartElement();
if (xml.isStartElement() && xml.name().toString() == "host")
{
datablock block = parse_data_block_nmap(xml);
// Check if the state is "open" before adding to dataBlocks
if (!block._details.ports.isEmpty() && block._details.ports.first().state == "open")
{
dataBlocks.append(block);
}
}
}
file.close();
return dataBlocks;
}
datablock parse_data_block(const QJsonObject& json_object)
{
datablock dataBlock;
dataBlock.ip_address = json_object["ip_address"].toString();
if (json_object.contains("details") && json_object["details"].isObject()) {
QJsonObject detailsJson = json_object["details"].toObject();
dataBlock._details.dns_name = detailsJson["dns_name"].toString();
dataBlock._details.rtt = detailsJson["rtt"].toDouble();
if (detailsJson.contains("ports") && detailsJson["ports"].isArray()) {
QJsonArray portsJsonArray = detailsJson["ports"].toArray();
for (const QJsonValue& portValue : portsJsonArray) {
if (portValue.isObject()) {
QJsonObject portJsonObject = portValue.toObject();
port port;
port.port = portJsonObject["port"].toInt();
port.protocol = portJsonObject["protocol"].toString();
port.http_title = portJsonObject["http_title"].toString();
port.description = portJsonObject["description"].toString();
port.screenshot = portJsonObject["screenshot"].toString();
port.content = portJsonObject["content"].toString();
port.passwd = portJsonObject["passwd"].toString();
dataBlock._details.ports.append(port);
}
}
}
}
return dataBlock;
}
QList<datablock> parse_json_files(QString& file_path)
{
QList<datablock> dataBlocks;
QFile file(file_path);
if (!file.open(QIODevice::ReadOnly))
{
qDebug() << "Unable to open file:" << file_path;
file.close();
return {};
}
QJsonParseError error;
QByteArray jsonData = file.readAll();
file.close();
QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonData, &error);
if (error.error != QJsonParseError::NoError)
{
file.close();
return {};
}
if (jsonDoc.isArray()) {
QJsonArray jsonArray = jsonDoc.array();
for (const QJsonValue& value : jsonArray) {
if (value.isObject()) {
QJsonObject jsonObject = value.toObject();
datablock dataBlock = parse_data_block(jsonObject);
dataBlocks.append(dataBlock);
}
}
}
return dataBlocks;
}