-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConnectionModel.cpp
executable file
·235 lines (210 loc) · 7.2 KB
/
ConnectionModel.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
#include "Application.h"
#include "ConnectionModel.h"
#include "Utils.h"
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QNetworkReply>
ConnectionModel::ConnectionModel(QObject *parent) :
QAbstractTableModel(parent)
{
m_timer.setInterval(1000);
connect(&m_timer, &QTimer::timeout, this, &ConnectionModel::sendRequest);
connect(&Application::mainWindow(), &MainWindow::clashApiReady, &m_timer, qOverload<>(&QTimer::start));
}
QVariant ConnectionModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation != Qt::Horizontal) {
return QVariant();
}
if (role == Qt::InitialSortOrderRole) {
switch (section) {
case HeaderUpload:
case HeaderDownload:
case HeaderTime:
return Qt::DescendingOrder;
default:
return Qt::AscendingOrder;
}
}
if (role == Qt::DisplayRole) {
switch (section) {
case HeaderHost:
return "Host";
case HeaderUpload:
return "Upload";
case HeaderDownload:
return "Download";
case HeaderChains:
return "Chains";
case HeaderRule:
return "Rule";
case HeaderTime:
return "Time";
case HeaderSource:
return "Source";
case HeaderDestIp:
return "Destination IP";
case HeaderType:
return "Type";
case HeaderProcess:
return "Process";
default:
return QVariant();
}
}
return QVariant();
}
int ConnectionModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid()) {
return 0;
}
return m_connInfos.size();
}
int ConnectionModel::columnCount(const QModelIndex &parent) const
{
if (parent.isValid()) {
return 0;
}
return HeaderCount;
}
QVariant ConnectionModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid()) {
return QVariant();
}
const ConnInfo &info = m_connInfos[index.row()];
// Sort role
if (role == Qt::UserRole) {
switch (index.column()) {
case HeaderUpload:
return info.upload;
case HeaderDownload:
return info.download;
case HeaderTime:
return info.start.secsTo(QDateTime::currentDateTime());
default:
return QVariant();
}
}
if (role == Qt::DisplayRole) {
switch (index.column()) {
case HeaderHost:
return info.host;
case HeaderUpload:
return prettyBytes(info.upload);
case HeaderDownload:
return prettyBytes(info.download);
case HeaderChains:
return info.chains;
case HeaderRule:
return info.rule;
case HeaderTime:
return prettyDuration(info.start.secsTo(QDateTime::currentDateTime()));
case HeaderSource:
return info.source;
case HeaderDestIp:
return info.destIp;
case HeaderType:
return info.type;
case HeaderProcess:
return info.process;
default:
return QVariant();
}
}
if (role == Qt::TextAlignmentRole) {
switch (index.column()) {
case HeaderUpload:
case HeaderDownload:
case HeaderTime:
return Qt::AlignRight;
default:
return QVariant();
}
}
return QVariant();
}
void ConnectionModel::sendRequest()
{
QUrl url(Application::clashApiUrl());
url.setPath(QStringLiteral("/connections"));
QNetworkRequest request(url);
QNetworkReply *reply = Application::netMgmr().get(request);
connect(reply, &QNetworkReply::finished, this, &ConnectionModel::replyFinished);
}
void ConnectionModel::replyFinished()
{
QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
reply->deleteLater();
QJsonDocument jsonDoc(QJsonDocument::fromJson(reply->readAll()));
QJsonObject jsonObj(jsonDoc.object());
QJsonArray conns = jsonObj[QLatin1String("connections")].toArray();
double ulTotal = jsonObj[QLatin1String("uploadTotal")].toDouble();
double dlTotal = jsonObj[QLatin1String("downloadTotal")].toDouble();
Application::mainWindow().updateConnStats(ulTotal, dlTotal, conns.count());
QVector<int> indexes(m_connInfos.size());
std::iota(indexes.begin(), indexes.end(), 0);
for (int i = 0; i < conns.count(); ++i) {
QJsonObject connObj = conns[i].toObject();
QJsonObject metaObj = connObj[QLatin1String("metadata")].toObject();
QJsonArray chains = connObj[QLatin1String("chains")].toArray();
QStringList chainsStrs;
for (int i = chains.count() - 1; i >= 0; --i) {
chainsStrs.append(chains[i].toString());
}
ConnInfo info;
info.id = connObj[QLatin1String("id")].toString();
info.host = metaObj[QLatin1String("host")].toString();
info.upload = connObj[QLatin1String("upload")].toDouble();
info.download = connObj[QLatin1String("download")].toDouble();
info.chains = chainsStrs.join('/');
info.rule = connObj[QLatin1String("rule")].toString();
QString rulePayload(connObj[QLatin1String("rulePayload")].toString());
if (!rulePayload.isEmpty()) {
info.rule += '(';
info.rule += rulePayload;
info.rule += ')';
}
info.start = QDateTime::fromString(connObj[QLatin1String("start")].toString(), Qt::ISODateWithMs);
info.source = metaObj[QLatin1String("sourceIP")].toString();
info.source += ':';
info.source += metaObj[QLatin1String("sourcePort")].toString();
info.destIp = metaObj[QLatin1String("destinationIP")].toString();
info.type = metaObj[QLatin1String("type")].toString();
info.type += '(';
info.type += metaObj[QLatin1String("network")].toString();
info.type += ')';
info.process = metaObj[QLatin1String("processPath")].toString();
if (info.host.isEmpty()) {
info.host = info.destIp;
}
info.host += ':';
info.host += metaObj[QLatin1String("destinationPort")].toString();
int idx = -1;
for (int i = 0; i < indexes.size(); ++i) {
if (m_connInfos[indexes[i]].id == info.id) {
idx = indexes.takeAt(i);
break;
}
}
if (idx < 0) {
beginInsertRows(QModelIndex(), m_connInfos.size(), m_connInfos.size());
m_connInfos.append(std::move(info));
endInsertRows();
} else {
m_connInfos[idx] = info;
emit dataChanged(index(idx, 0), index(idx, HeaderCount - 1));
}
}
for (int i = 0; i < indexes.size(); ++i) {
int idx = indexes[i] - i;
beginRemoveRows(QModelIndex(), idx, idx);
m_connInfos.remove(idx);
endRemoveRows();
}
if (!Application::mainWindow().isVisible()) {
m_timer.stop();
}
}