forked from Shadowsocks-NET/Qv2ray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQv2rayApplication.cpp
405 lines (355 loc) · 13.6 KB
/
Qv2rayApplication.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
#include "Qv2rayApplication.hpp"
#include "GuiPluginHost/GuiPluginHost.hpp"
#include "Qv2rayBase/Common/Utils.hpp"
#include "Qv2rayBase/Interfaces/IStorageProvider.hpp"
#include "Qv2rayBase/Profile/KernelManager.hpp"
#include "Qv2rayBase/Profile/ProfileManager.hpp"
#include "StyleManager/StyleManager.hpp"
#include "models/SettingsModels.hpp"
#include "ui/windows/w_MainWindow.hpp"
#include <QCommandLineParser>
#include <QDesktopServices>
#include <QSessionManager>
#include <QSettings>
#include <QSslSocket>
#include <QUrl>
#include <QUrlQuery>
#include <QVersionNumber>
#include <QtPlugin>
#include <openssl/ssl.h>
constexpr auto QV2RAY_GUI_EXTRASETTINGS_KEY = "qv2ray-gui-settings";
#ifdef QT_DEBUG
const static inline auto QV2RAY_URL_SCHEME = "qv2ray-debug";
#else
const static inline auto QV2RAY_URL_SCHEME = "qv2ray";
#endif
Q_IMPORT_PLUGIN(Qv2rayInternalPlugin);
Qv2rayApplication::Qv2rayApplication(int &argc, char *argv[]) : SingleApplication(argc, argv, true, User | ExcludeAppPath | ExcludeAppVersion)
{
// These no-op function calls ensures libssl and libcrypto are linked against Qv2ray.
// Forcing the deployment of libssl and libcrypto libraries.
// So that the OpenSSL TLS backend can be used by Qt Network.
// TODO Find a workaround without this hack.
Q_UNUSED(SSL_new(nullptr));
baseLibrary = new Qv2rayBase::Qv2rayBaseLibrary;
Qv2rayLogo = QPixmap{ u":/qv2ray.png"_qs };
installEventFilter(this);
}
Qv2rayApplication::~Qv2rayApplication()
{
delete baseLibrary;
}
Qv2rayExitReason Qv2rayApplication::GetExitReason() const
{
return exitReason;
}
bool Qv2rayApplication::Initialize()
{
QString errorMessage;
bool canContinue;
const auto hasError = parseCommandLine(&errorMessage, &canContinue);
qInfo() << "Command line:" << errorMessage;
if (hasError && !canContinue)
{
qInfo() << "Fatal, Qv2ray cannot continue.";
exitReason = EXIT_INITIALIZATION_FAILED;
return false;
}
Qv2rayBase::Interfaces::StorageContext ctx;
#ifdef QT_DEBUG
ctx << Qv2rayBase::Interfaces::StorageContextFlags::STORAGE_CTX_IS_DEBUG;
#endif
#ifdef Q_OS_WINDOWS
ctx << Qv2rayBase::Interfaces::StorageContextFlags::STORAGE_CTX_HAS_ASIDE_CONFIGURATION;
#endif
const auto result = baseLibrary->Initialize(StartupArguments.noPlugins ? Qv2rayBase::START_NO_PLUGINS : Qv2rayBase::START_NORMAL, ctx, this);
if (result != Qv2rayBase::NORMAL)
{
exitReason = EXIT_INITIALIZATION_FAILED;
return false;
}
#ifdef Q_OS_WIN
const auto appPath = QDir::toNativeSeparators(QCoreApplication::applicationFilePath());
const auto regPath = "HKEY_CURRENT_USER\\Software\\Classes\\" + QString::fromUtf8(QV2RAY_URL_SCHEME);
QSettings reg(regPath, QSettings::NativeFormat);
reg.setValue("Default", "Qv2ray");
reg.setValue("URL Protocol", "");
reg.beginGroup("DefaultIcon");
reg.setValue("Default", QString("%1,1").arg(appPath));
reg.endGroup();
reg.beginGroup("shell");
reg.beginGroup("open");
reg.beginGroup("command");
reg.setValue("Default", appPath + " %1");
#endif
connect(this, &QApplication::aboutToQuit, this, &Qv2rayApplication::quitInternal);
connect(this, &SingleApplication::receivedMessage, this, &Qv2rayApplication::onMessageReceived, Qt::QueuedConnection);
if (isSecondary())
{
StartupArguments.version = QV2RAY_VERSION_STRING;
StartupArguments.fullArgs = arguments();
if (StartupArguments.arguments.isEmpty())
StartupArguments.arguments << Qv2rayStartupArguments::NORMAL;
bool status = sendMessage(JsonToString(StartupArguments.toJson(), QJsonDocument::Compact).toUtf8());
if (!status)
qInfo() << "Cannot send message.";
exitReason = EXIT_SECONDARY_INSTANCE;
return false;
}
connect(this, &QGuiApplication::commitDataRequest, this, &Qv2rayApplication::SaveQv2raySettings, Qt::DirectConnection);
#ifdef Q_OS_WIN
SetCurrentDirectory(QCoreApplication::applicationDirPath().toStdWString().c_str());
#endif
GlobalConfig = new Qv2rayApplicationConfigObject;
GlobalConfig->loadJson(QvStorageProvider->GetExtraSettings(QString::fromUtf8(QV2RAY_GUI_EXTRASETTINGS_KEY)));
GUIPluginHost = new GuiPluginHost::GuiPluginAPIHost;
UIMessageBus = new MessageBus::QvMessageBusObject;
StyleManager = new QvStyleManager::QvStyleManager;
StyleManager->ApplyStyle(GlobalConfig->appearanceConfig->UITheme);
setQuitOnLastWindowClosed(false);
return true;
}
Qv2rayExitReason Qv2rayApplication::RunQv2ray()
{
trayManager = new Qv2ray::ui::TrayManager;
mainWindow = new MainWindow();
connect(trayManager, &TrayManager::TrayActivated, mainWindow, &MainWindow::OnTrayIconActivated);
connect(trayManager, &TrayManager::VisibilityToggled, mainWindow, &MainWindow::MWToggleVisibility);
if (StartupArguments.arguments.contains(Qv2rayStartupArguments::QV2RAY_LINK))
{
for (const auto &link : StartupArguments.links)
{
const auto url = QUrl::fromUserInput(link);
const auto command = url.host();
auto subcommands = url.path().split(u"/"_qs);
subcommands.removeAll("");
QMap<QString, QString> args;
for (const auto &kvp : QUrlQuery(url).queryItems())
{
args.insert(kvp.first, kvp.second);
}
if (command == u"open"_qs)
{
mainWindow->ProcessCommand(command, subcommands, args);
}
}
}
return (Qv2rayExitReason) exec();
}
void Qv2rayApplication::quitInternal()
{
delete mainWindow;
delete trayManager;
delete StyleManager;
delete GUIPluginHost;
SaveQv2raySettings();
QvBaselib->Shutdown();
}
bool Qv2rayApplication::parseCommandLine(QString *errorMessage, bool *canContinue)
{
*canContinue = true;
QStringList filteredArgs;
filteredArgs.reserve(QCoreApplication::arguments().size());
for (const auto &arg : QCoreApplication::arguments())
{
#ifdef Q_OS_MACOS
if (arg.contains("-psn"))
continue;
#endif
filteredArgs << arg;
}
QCommandLineParser parser;
QCommandLineOption noAPIOption(u"noAPI"_qs, QObject::tr("Disable gRPC API subsystem"));
QCommandLineOption noPluginsOption(u"noPlugin"_qs, QObject::tr("Disable plugins feature"));
QCommandLineOption debugLogOption(u"debug"_qs, QObject::tr("Enable debug output"));
QCommandLineOption noAutoConnectionOption(u"noAutoConnection"_qs, QObject::tr("Do not automatically connect"));
QCommandLineOption disconnectOption(u"disconnect"_qs, QObject::tr("Stop current connection"));
QCommandLineOption reconnectOption(u"reconnect"_qs, QObject::tr("Reconnect last connection"));
QCommandLineOption exitOption(u"exit"_qs, QObject::tr("Exit Qv2ray"));
parser.setApplicationDescription(QObject::tr("Qv2ray - A cross-platform Qt frontend for V2Ray."));
parser.setSingleDashWordOptionMode(QCommandLineParser::ParseAsLongOptions);
parser.addOption(noAPIOption);
parser.addOption(noPluginsOption);
parser.addOption(debugLogOption);
parser.addOption(noAutoConnectionOption);
parser.addOption(disconnectOption);
parser.addOption(reconnectOption);
parser.addOption(exitOption);
const auto helpOption = parser.addHelpOption();
const auto versionOption = parser.addVersionOption();
if (!parser.parse(filteredArgs))
{
*canContinue = true;
*errorMessage = parser.errorText();
return false;
}
if (parser.isSet(versionOption))
{
parser.showVersion();
return true;
}
if (parser.isSet(helpOption))
{
parser.showHelp();
return true;
}
for (const auto &arg : parser.positionalArguments())
{
if (arg.startsWith(QString(QV2RAY_URL_SCHEME) + "://"))
{
StartupArguments.arguments << Qv2rayStartupArguments::QV2RAY_LINK;
StartupArguments.links << arg;
}
}
if (parser.isSet(exitOption))
{
qDebug() << "exitOption is set.";
StartupArguments.arguments << Qv2rayStartupArguments::EXIT;
}
if (parser.isSet(disconnectOption))
{
qDebug() << "disconnectOption is set.";
StartupArguments.arguments << Qv2rayStartupArguments::DISCONNECT;
}
if (parser.isSet(reconnectOption))
{
qDebug() << "reconnectOption is set.";
StartupArguments.arguments << Qv2rayStartupArguments::RECONNECT;
}
#define ProcessExtraStartupOptions(option) \
qDebug() << "Startup Options:" << #option << parser.isSet(option##Option); \
StartupArguments.option = parser.isSet(option##Option);
ProcessExtraStartupOptions(noAPI);
ProcessExtraStartupOptions(debugLog);
ProcessExtraStartupOptions(noAutoConnection);
ProcessExtraStartupOptions(noPlugins);
return true;
}
void Qv2rayApplication::p_OpenURL(const QUrl &url)
{
QDesktopServices::openUrl(url);
}
bool Qv2rayApplication::eventFilter(QObject *watched, QEvent *event)
{
#ifdef Q_OS_MACOS
static Qt::ApplicationState _prevAppState;
if (watched == this && event->type() == QEvent::ApplicationStateChange)
{
auto ev = static_cast<QApplicationStateChangeEvent *>(event);
if (_prevAppState == Qt::ApplicationActive && ev->applicationState() == Qt::ApplicationActive)
{
mainWindow->MWShowWindow();
}
_prevAppState = ev->applicationState();
}
#endif // Q_OS_MACOS
return QApplication::eventFilter(watched, event);
}
void Qv2rayApplication::p_MessageBoxWarn(const QString &title, const QString &text)
{
QMessageBox::warning(nullptr, title, text, QMessageBox::Ok);
}
void Qv2rayApplication::p_MessageBoxInfo(const QString &title, const QString &text)
{
QMessageBox::information(nullptr, title, text, QMessageBox::Ok);
}
Qv2rayBase::MessageOpt Qv2rayApplication::p_MessageBoxAsk(const QString &title, const QString &text, const QList<Qv2rayBase::MessageOpt> &buttons)
{
QFlags<QMessageBox::StandardButton> btns;
for (const auto &b : buttons)
{
btns.setFlag(MessageBoxButtonMap.value(b));
}
return MessageBoxButtonMap.key(QMessageBox::question(nullptr, title, text, btns));
}
MainWindow *Qv2rayApplication::GetMainWindow() const
{
return mainWindow;
}
TrayManager *Qv2rayApplication::GetTrayManager() const
{
return trayManager;
}
void Qv2rayApplication::SaveQv2raySettings()
{
QvBaselib->SaveConfigurations();
QvStorageProvider->StoreExtraSettings(QString::fromUtf8(QV2RAY_GUI_EXTRASETTINGS_KEY), GlobalConfig->toJson());
}
void Qv2rayApplication::onMessageReceived(quint32 clientId, const QByteArray &_msg)
{
// Sometimes SingleApplication sends message with clientId == current id, ignore that.
if (clientId == instanceId())
return;
Qv2rayStartupArguments msg;
msg.loadJson(JsonFromString(_msg));
qInfo() << "Received message, version:" << msg.version << "From client ID:" << clientId;
qInfo() << _msg;
if (QVersionNumber::fromString(msg.version) > QVersionNumber::fromString(QV2RAY_VERSION_STRING))
{
const auto newPath = msg.fullArgs.constFirst();
QString message;
message += tr("A new version of Qv2ray is starting:") + NEWLINE;
message += '\n';
message += tr("New version information: ") + '\n';
message += tr("Version: %1").arg(msg.version) + '\n';
message += tr("Path: %1").arg(newPath) + '\n';
message += '\n';
message += tr("Do you want to exit and launch that new version?");
const auto result = p_MessageBoxAsk(tr("New version detected"), message, { Qv2rayBase::MessageOpt::Yes, Qv2rayBase::MessageOpt::No });
if (result == Qv2rayBase::MessageOpt::Yes)
{
StartupArguments._qvNewVersionPath = newPath;
exitReason = EXIT_NEW_VERSION_TRIGGER;
QCoreApplication::quit();
}
}
for (const auto &argument : msg.arguments)
{
switch (argument)
{
case Qv2rayStartupArguments::EXIT:
{
exitReason = EXIT_NORMAL;
quit();
break;
}
case Qv2rayStartupArguments::NORMAL:
{
mainWindow->show();
mainWindow->raise();
mainWindow->activateWindow();
break;
}
case Qv2rayStartupArguments::RECONNECT:
{
QvProfileManager->StartConnection(QvKernelManager->CurrentConnection());
break;
}
case Qv2rayStartupArguments::DISCONNECT:
{
QvProfileManager->StopConnection();
break;
}
case Qv2rayStartupArguments::QV2RAY_LINK:
{
for (const auto &link : msg.links)
{
const auto url = QUrl::fromUserInput(link);
const auto command = url.host();
auto subcommands = url.path().split('/');
subcommands.removeAll("");
QMap<QString, QString> args;
for (const auto &kvp : QUrlQuery(url).queryItems())
{
args.insert(kvp.first, kvp.second);
}
if (command == u"open"_qs)
{
mainWindow->ProcessCommand(command, subcommands, args);
}
}
break;
}
}
}
}