-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.cpp
executable file
·496 lines (428 loc) · 15.8 KB
/
MainWindow.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
#include "Application.h"
#include "MainWindow.h"
#include "ui_MainWindow.h"
#include "Utils.h"
#include <QCloseEvent>
#include <QDesktopServices>
#include <QDir>
#include <QJsonDocument>
#include <QJsonObject>
#include <QNetworkReply>
#include <QTimer>
#include <Windows.h>
#include <openssl/bio.h>
#include <openssl/evp.h>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
m_sizeAdjusted(false),
m_sysShutdown(false),
m_settings(getFilePath(PathType::IniFile), QSettings::IniFormat),
m_actionGroup(nullptr)
{
ui->setupUi(this);
ui->statusBar->addPermanentWidget(&m_trafficStats);
ui->statusBar->addPermanentWidget(&m_connStats);
ui->statusBar->setStyleSheet(QStringLiteral("QLabel { padding-left: 10px; padding-right: 10px; }"));
Application::instance()->installNativeEventFilter(this);
QStringList profiles = m_settings.value(QStringLiteral("profile/profiles")).toStringList();
profiles.removeAll(QString());
for (const QString &prof : qAsConst(profiles)) {
QString text("Profile \"");
text += prof;
text += '\"';
QAction *action = m_trayIconMenu.addAction(text);
action->setData(prof);
action->setCheckable(true);
m_actionGroup.addAction(action);
}
connect(&m_actionGroup, &QActionGroup::triggered, this, &MainWindow::actionGroupTriggered);
m_trayIconMenu.addSeparator();
QAction *actionOpenCfg = m_trayIconMenu.addAction(QStringLiteral("Open Config"));
QAction *actionOpenClashCfg = m_trayIconMenu.addAction(QStringLiteral("Open Clash Config"));
QAction *actionOpenWorkDir = m_trayIconMenu.addAction(QStringLiteral("Open Working Directory"));
connect(actionOpenCfg, &QAction::triggered, this, &MainWindow::openCfgTriggered);
connect(actionOpenClashCfg, &QAction::triggered, this, &MainWindow::openClashCfgTriggered);
connect(actionOpenWorkDir, &QAction::triggered, this, &MainWindow::openWorkDirTriggered);
m_trayIconMenu.addSeparator();
QAction *actionAutoStart = m_trayIconMenu.addAction(QStringLiteral("Auto Start"));
QAction *actionQuit = m_trayIconMenu.addAction(QStringLiteral("Quit"));
actionAutoStart->setCheckable(true);
actionAutoStart->setChecked(isAutoStart());
connect(actionQuit, &QAction::triggered, this, &MainWindow::close);
connect(actionAutoStart, &QAction::triggered, this, &MainWindow::autoStartTriggered);
setIcon(QIcon::Disabled);
m_trayIcon.setContextMenu(&m_trayIconMenu);
m_trayIcon.show();
connect(&m_trayIcon, &QSystemTrayIcon::activated, this, &MainWindow::trayIconActivated);
m_clash.setWorkingDirectory(getFilePath(PathType::BaseDir));
m_clash.setArguments({ "-d", "." });
m_clash.setProgram(getFilePath(PathType::ClashExecutable));
m_clash.closeReadChannel(QProcess::StandardError);
m_clash.closeWriteChannel();
connect(&m_clash, &QProcess::errorOccurred, this, &MainWindow::clashErrorOccurred);
connect(&m_clash, qOverload<int, QProcess::ExitStatus>(&QProcess::finished), this, &MainWindow::clashFinished);
connect(&m_clash, &QProcess::readyReadStandardOutput, this, &MainWindow::clashStdoutReady);
connect(&m_clash, &QProcess::started, this, &MainWindow::clashStarted);
if (profiles.isEmpty()) {
ui->logPage->appendLog(LogLevel::Error, "no profile");
} else {
auto actions = m_actionGroup.actions();
int profIdx = m_settings.value(QStringLiteral("state/profile"), 0).toInt();
if (profIdx < 0 || profIdx >= actions.size()) {
profIdx = 0;
}
actions.at(profIdx)->setChecked(true);
fetchConfig(profiles.at(profIdx));
}
}
MainWindow::~MainWindow()
{
if (m_clash.state() == QProcess::Running) {
sendCtrlCToProcess(m_clash.processId());
m_clash.waitForFinished();
}
delete ui;
}
void MainWindow::updateTrafficStats(double ulTraffic, double dlTraffic)
{
QString text("UL: ");
text += prettyBytes(ulTraffic);
text += " DL: ";
text += prettyBytes(dlTraffic);
m_trafficStats.setText(text);
}
void MainWindow::updateConnStats(double ulTotal, double dlTotal, int numOfConns)
{
QString text("UL Total: ");
text += prettyBytes(ulTotal);
text += " DL Total: ";
text += prettyBytes(dlTotal);
text += " Conn: ";
text += QString::number(numOfConns);
m_connStats.setText(text);
}
bool MainWindow::nativeEventFilter(const QByteArray & /*eventType*/, void *message, long * /*result*/)
{
if (static_cast<MSG *>(message)->message == WM_QUERYENDSESSION) {
m_sysShutdown = true;
}
return false;
}
void MainWindow::showEvent(QShowEvent *event)
{
// showEvent will be called twice with spontaneous set to true and false. See
// description of QShowEvent.
if (!event->spontaneous() && m_clash.state() == QProcess::Running) {
emit clashApiReady();
}
QMainWindow::showEvent(event);
}
void MainWindow::closeEvent(QCloseEvent *event)
{
if (event->spontaneous() && !m_sysShutdown) {
event->ignore();
showMinimized();
} else {
event->accept();
auto actions = m_actionGroup.actions();
for (int i = 0; i < actions.size(); ++i) {
if (actions.at(i)->isChecked()) {
m_settings.setValue(QStringLiteral("state/profile"), i);
break;
}
}
}
}
bool MainWindow::event(QEvent *event)
{
switch (event->type()) {
case QEvent::WindowStateChange:
if (isMinimized()) {
if (!event->spontaneous()) {
QTimer::singleShot(150, this, &MainWindow::hide);
}
}
break;
case QEvent::ShowToParent:
if (!m_sizeAdjusted) {
m_sizeAdjusted = true;
QSizeF newSize;
newSize.setWidth(ui->logPage->avgCharWidth() * 160);
newSize.setHeight(newSize.width() * 9 / 16);
QSizeF oldSize = size();
int dx = qRound((newSize.width() - oldSize.width())/2.0);
int dy = qRound((newSize.height() - oldSize.height())/2.0);
setGeometry(geometry().adjusted(-dx, -dy, dx, dy));
}
break;
default:
break;
}
return QMainWindow::event(event);
}
void MainWindow::fetchConfig(const QString &profile)
{
QString tooltip("Profile: ");
tooltip += profile;
m_trayIcon.setToolTip(tooltip);
QUrl url(m_settings.value(QStringLiteral("profile/url")).toString());
if (!url.isValid()) {
ui->logPage->appendLog(LogLevel::Error, url.errorString());
return;
}
QString path('/');
path += profile.toLower();
snprintf(m_iv, sizeof(m_iv), "%08d", int(time(nullptr) % 100000000));
url.setPath(path);
url.setQuery(m_iv);
QNetworkRequest request(url);
request.setHeader(QNetworkRequest::UserAgentHeader, "ClashQ");
if (m_settings.value(QStringLiteral("profile/insecure")).toBool()) {
QSslConfiguration sslConfig = QSslConfiguration::defaultConfiguration();
sslConfig.setPeerVerifyMode(QSslSocket::VerifyNone);
request.setSslConfiguration(sslConfig);
}
QNetworkReply *reply = Application::netMgmr().get(request);
connect(reply, &QNetworkReply::finished, this, &MainWindow::fetchCfgReplyFinished);
QString logText("fetching configuration for profile \"");
logText += profile;
logText += '\"';
ui->logPage->appendLog(LogLevel::Info, logText);
}
void MainWindow::fetchClashVer()
{
QUrl url(Application::clashApiUrl());
url.setPath(QStringLiteral("/version"));
QNetworkRequest request(url);
QNetworkReply *reply = Application::netMgmr().get(request);
connect(reply, &QNetworkReply::finished, this, &MainWindow::getVerReplyFinished);
}
QByteArray MainWindow::decryptConfig(const QByteArray &ba)
{
QByteArray result;
QString key = m_settings.value(QStringLiteral("profile/key")).toString();
if (key.isEmpty()) {
ui->logPage->appendLog(LogLevel::Error, "key is empty");
return result;
}
const EVP_CIPHER *cipher = EVP_get_cipherbyname("des");
BIO *memBio = BIO_new_mem_buf(ba.data(), ba.size());
BIO *b64Bio = BIO_new(BIO_f_base64());
BIO *cipherBio = BIO_new(BIO_f_cipher());
BIO_set_cipher(cipherBio, cipher, (unsigned char *)key.toStdString().c_str(), (unsigned char *)m_iv, 0);
BIO_push(b64Bio, memBio);
BIO_push(cipherBio, b64Bio);
int len;
char buf[1024];
while ((len = BIO_read(cipherBio, buf, sizeof(buf))) > 0) {
result.append(buf, len);
}
if (BIO_get_cipher_status(cipherBio) <= 0) {
result.clear();
ui->logPage->appendLog(LogLevel::Error, "failed to decrypt configuration");
}
BIO_free_all(cipherBio);
return result;
}
void MainWindow::setIcon(QIcon::Mode mode)
{
QIcon icon(QStringLiteral(":/app.ico"));
if (mode == QIcon::Disabled) {
QIcon disIcon(icon.pixmap(32, 32, mode));
setWindowIcon(disIcon);
m_trayIcon.setIcon(disIcon);
} else {
setWindowIcon(icon);
m_trayIcon.setIcon(icon);
}
}
bool MainWindow::isAutoStart()
{
DWORD type = REG_NONE;
HKEY hKey;
if (RegOpenKeyEx(HKEY_CURRENT_USER, LR"(SOFTWARE\Microsoft\Windows\CurrentVersion\Run)", 0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS) {
if (RegQueryValueExW(hKey, L"ClashQ", 0, &type, nullptr, nullptr) != ERROR_SUCCESS) {
ui->logPage->appendLog(LogLevel::Warning, "failed to query auto start");
}
RegCloseKey(hKey);
}
return type == REG_SZ;
}
void MainWindow::fetchCfgReplyFinished()
{
QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
reply->deleteLater();
if (reply->error() != QNetworkReply::NoError) {
ui->logPage->appendLog(LogLevel::Error, reply->errorString());
return;
}
QFile file(getFilePath(PathType::ClashConfig));
if (!file.open(QIODevice::WriteOnly)) {
ui->logPage->appendLog(LogLevel::Error, file.errorString());
return;
}
QByteArray ba(decryptConfig(reply->readAll()));
if (ba.isEmpty()) {
return;
}
ui->logPage->appendLog(LogLevel::Info, "decrypt configuration successfully");
file.write(ba);
file.close();
m_clash.start(QIODevice::ReadOnly);
}
void MainWindow::getVerReplyFinished()
{
QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
reply->deleteLater();
if (reply->error() != QNetworkReply::NoError) {
QString log("failed to get clash version (");
log += reply->errorString();
log += ')';
ui->logPage->appendLog(LogLevel::Warning, log);
QTimer::singleShot(1000, this, &MainWindow::fetchClashVer);
return;
}
QJsonDocument jsonDoc = QJsonDocument::fromJson(reply->readAll());
QJsonObject jsonObj = jsonDoc.object();
QJsonValue jsonVer = jsonObj[QLatin1String("version")];
if (jsonVer.isString()) {
QString tooltip = m_trayIcon.toolTip();
tooltip += "\nClash: ";
tooltip += jsonVer.toString();
m_trayIcon.setToolTip(tooltip);
}
}
void MainWindow::clashErrorOccurred(QProcess::ProcessError error)
{
switch (error) {
case QProcess::FailedToStart:
ui->logPage->appendLog(LogLevel::Error, "failed to start clash subprocess");
break;
case QProcess::Crashed:
ui->logPage->appendLog(LogLevel::Warning, "clash subprocess crashed");
break;
case QProcess::Timedout:
ui->logPage->appendLog(LogLevel::Warning, "wait for clash subprocess time out");
break;
case QProcess::WriteError:
ui->logPage->appendLog(LogLevel::Warning, "failed to write to clash subprocess");
break;
case QProcess::ReadError:
ui->logPage->appendLog(LogLevel::Warning, "failed to read from clash subprocess");
break;
default:
ui->logPage->appendLog(LogLevel::Warning, "unknown error occurred in clash subprocess");
break;
}
}
void MainWindow::clashFinished(int exitCode, QProcess::ExitStatus exitStatus)
{
LogLevel level;
QString logText("clash ");
if (exitStatus == QProcess::NormalExit) {
level = LogLevel::Info;
logText += "exited with code ";
logText += QString::number(exitCode);
} else {
level = LogLevel::Warning;
logText += "crashed";
}
ui->logPage->appendLog(level, logText);
setIcon(QIcon::Disabled);
}
void MainWindow::clashStdoutReady()
{
QProcess *subprocess = qobject_cast<QProcess *>(sender());
ui->logPage->appendClashLog(subprocess->readAllStandardOutput().trimmed());
}
void MainWindow::clashStarted()
{
setIcon(QIcon::Normal);
QTimer::singleShot(1000, this, &MainWindow::fetchClashVer);
if (isVisible()) {
emit clashApiReady();
}
}
void MainWindow::trayIconActivated(QSystemTrayIcon::ActivationReason reason)
{
if (reason == QSystemTrayIcon::Trigger) {
if (isVisible()) {
if (isMinimized()) {
showNormal();
} else {
hide();
}
} else {
showNormal();
activateWindow();
}
}
}
void MainWindow::actionGroupTriggered(QAction *action)
{
if (m_clash.state() == QProcess::Running) {
sendCtrlCToProcess(m_clash.processId());
m_clash.waitForFinished();
}
if (QGuiApplication::keyboardModifiers().testFlag(Qt::ControlModifier)) {
close();
QProcess::startDetached(Application::applicationFilePath(), QStringList());
} else {
fetchConfig(action->data().toString().toLower());
}
}
void MainWindow::openCfgTriggered()
{
QDesktopServices::openUrl(QUrl::fromLocalFile(getFilePath(PathType::IniFile)));
}
void MainWindow::openClashCfgTriggered()
{
QDesktopServices::openUrl(QUrl::fromLocalFile(getFilePath(PathType::ClashConfig)));
}
void MainWindow::openWorkDirTriggered()
{
QDesktopServices::openUrl(QUrl::fromLocalFile(getFilePath(PathType::BaseDir)));
}
void MainWindow::autoStartTriggered(bool checked)
{
HKEY hKey;
if (RegOpenKeyEx(HKEY_CURRENT_USER, LR"(SOFTWARE\Microsoft\Windows\CurrentVersion\Run)", 0, KEY_SET_VALUE, &hKey) == ERROR_SUCCESS) {
if (checked) {
QString appPath = QCoreApplication::applicationFilePath();
appPath = QDir::toNativeSeparators(appPath);
if (RegSetValueExW(hKey, L"ClashQ", 0, REG_SZ, (BYTE *)appPath.toStdWString().c_str(), (appPath.length() + 1) * sizeof(wchar_t)) != ERROR_SUCCESS) {
ui->logPage->appendLog(LogLevel::Warning, "failed to enable auto start");
}
} else {
if (RegDeleteValueW(hKey, L"ClashQ") != ERROR_SUCCESS) {
ui->logPage->appendLog(LogLevel::Warning, "failed to disable auto start");
}
}
RegCloseKey(hKey);
}
}
QString MainWindow::getFilePath(PathType pt)
{
QDir dir = QDir::home();
dir.cd(QStringLiteral("clash"));
switch (pt) {
case PathType::BaseDir:
return dir.absolutePath();
case PathType::IniFile:
return dir.absoluteFilePath(QStringLiteral("config.ini"));
case PathType::ClashExecutable:
return dir.absoluteFilePath(QStringLiteral("clash.exe"));
case PathType::ClashConfig:
return dir.absoluteFilePath(QStringLiteral("config.yaml"));
default:
return QString();
}
}
void MainWindow::sendCtrlCToProcess(qint64 pid)
{
if (AttachConsole(pid)) {
GenerateConsoleCtrlEvent(CTRL_C_EVENT, pid);
FreeConsole();
}
}