From 01b2f3ac967a9918ba4e6845d8664c4939a65d8b Mon Sep 17 00:00:00 2001 From: "Leroy.H.Y" Date: Mon, 6 Jan 2020 21:42:32 +0800 Subject: [PATCH] [add] Added --noAPI flag in commandline --- Build.Counter | 2 +- Qv2ray.pro | 6 ++- src/Qv2rayBase.hpp | 4 -- src/QvCoreConfigOperations.cpp | 12 ----- src/QvCoreConfigOperations.hpp | 1 - src/components/QvCore/QvCommandLineArgs.cpp | 45 +++++++++++++++++++ src/components/QvCore/QvCommandLineArgs.hpp | 44 ++++++++++++++++++ ...eractions.cpp => QvKernelInteractions.cpp} | 43 +++++++++--------- ...eractions.hpp => QvKernelInteractions.hpp} | 12 +++-- src/main.cpp | 31 +++++++++++-- src/ui/w_ImportConfig.cpp | 4 +- src/ui/w_MainWindow.cpp | 12 ++--- src/ui/w_MainWindow.hpp | 4 +- src/ui/w_PreferencesWindow.cpp | 2 +- src/utils/QvHelpers.hpp | 1 - 15 files changed, 160 insertions(+), 63 deletions(-) create mode 100644 src/components/QvCore/QvCommandLineArgs.cpp create mode 100644 src/components/QvCore/QvCommandLineArgs.hpp rename src/components/{QvCoreInteractions.cpp => QvKernelInteractions.cpp} (87%) rename src/components/{QvCoreInteractions.hpp => QvKernelInteractions.hpp} (87%) diff --git a/Build.Counter b/Build.Counter index 5475b168..aa1bbf69 100644 --- a/Build.Counter +++ b/Build.Counter @@ -1 +1 @@ -2555 +2573 diff --git a/Qv2ray.pro b/Qv2ray.pro index c4eae0f4..44b37a39 100644 --- a/Qv2ray.pro +++ b/Qv2ray.pro @@ -35,12 +35,13 @@ CONFIG += lrelease embed_translations SOURCES += \ src/components/QvComponentsHandler.cpp \ + src/components/QvCore/QvCommandLineArgs.cpp \ + src/components/QvKernelInteractions.cpp \ src/components/QvLaunchAtLoginConfigurator.cpp \ src/components/QvPACHandler.cpp \ src/components/QvSystemProxyConfigurator.cpp \ src/components/QvTCPing.cpp \ src/main.cpp \ - src/components/QvCoreInteractions.cpp \ src/components/QvGFWPACConverter.cpp \ src/components/QvHTTPRequestHelper.cpp \ src/components/QvLogHighlighter.cpp \ @@ -84,8 +85,9 @@ HEADERS += \ src/QvCoreConfigOperations.hpp \ src/QvUtils.hpp \ src/components/QvComponentsHandler.hpp \ - src/components/QvCoreInteractions.hpp \ + src/components/QvCore/QvCommandLineArgs.hpp \ src/components/QvHTTPRequestHelper.hpp \ + src/components/QvKernelInteractions.hpp \ src/components/QvLaunchAtLoginConfigurator.hpp \ src/components/QvLogHighlighter.hpp \ src/components/QvNetSpeedPlugin.hpp \ diff --git a/src/Qv2rayBase.hpp b/src/Qv2rayBase.hpp index 708e0cb4..9b0f0530 100644 --- a/src/Qv2rayBase.hpp +++ b/src/Qv2rayBase.hpp @@ -74,10 +74,6 @@ const int QV2RAY_CONFIG_VERSION = 6; #define NEWLINE "\r\n" -#ifndef MAX -# define MAX(a, b) (((a) > (b)) ? (a) : (b)) -#endif - using namespace std; using namespace std::chrono; diff --git a/src/QvCoreConfigOperations.cpp b/src/QvCoreConfigOperations.cpp index f5abfaa8..a52d26b4 100644 --- a/src/QvCoreConfigOperations.cpp +++ b/src/QvCoreConfigOperations.cpp @@ -69,17 +69,5 @@ namespace Qv2ray bool cRules = cRule && root["routing"].toObject()["rules"].toArray().count() > 0; return cRules; } - - int FindIndexByTag(INOUTLIST list, const QString &tag) - { - for (int i = 0; i < list.count(); i++) { - auto value = list[i].toObject(); - - if (value.contains("tag") && value["tag"].toString() == tag) - return i; - } - - return -1; - } } } diff --git a/src/QvCoreConfigOperations.hpp b/src/QvCoreConfigOperations.hpp index 7d63abc4..74d48c9a 100644 --- a/src/QvCoreConfigOperations.hpp +++ b/src/QvCoreConfigOperations.hpp @@ -20,7 +20,6 @@ namespace Qv2ray QMap GetSubscriptionConnection(QString subscription); QMap> GetSubscriptionConnections(QStringList subscriptions); bool CheckIsComplexConfig(CONFIGROOT root); - int FindIndexByTag(INOUTLIST list, const QString &tag); // // -------------------------- BEGIN CONFIG CONVERSIONS -------------------------- diff --git a/src/components/QvCore/QvCommandLineArgs.cpp b/src/components/QvCore/QvCommandLineArgs.cpp new file mode 100644 index 00000000..4ec502af --- /dev/null +++ b/src/components/QvCore/QvCommandLineArgs.cpp @@ -0,0 +1,45 @@ +#include "QvCommandLineArgs.hpp" +#include "Qv2rayBase.hpp" +#include + + +namespace Qv2ray +{ + namespace CommandArgOperations + { + // Instantiation + QvStartupOptions StartupOption = QvStartupOptions{}; + + QvCommandArgParser::QvCommandArgParser() : QObject(), + noAPIOption("FAKE"), helpOption("FAKE"), versionOption("FAKE") + { + parser.setApplicationDescription(QObject::tr("Qv2ray - An cross-platform Qt frontend for V2ray.")); + parser.setSingleDashWordOptionMode(QCommandLineParser::ParseAsLongOptions); + // + noAPIOption = QCommandLineOption("noAPI", QObject::tr("Disable gRPC API subsystems.")); + parser.addOption(noAPIOption); + helpOption = parser.addHelpOption(); + versionOption = parser.addVersionOption(); + } + + CommandLineParseResult QvCommandArgParser::ParseCommandLine(QString *errorMessage) + { + if (!parser.parse(QCoreApplication::arguments())) { + *errorMessage = parser.errorText(); + return CommandLineError; + } + + if (parser.isSet(versionOption)) + return CommandLineVersionRequested; + + if (parser.isSet(helpOption)) + return CommandLineHelpRequested; + + if (parser.isSet(noAPIOption)) { + StartupOption.noAPI = true; + } + + return CommandLineOk; + } + } +} diff --git a/src/components/QvCore/QvCommandLineArgs.hpp b/src/components/QvCore/QvCommandLineArgs.hpp new file mode 100644 index 00000000..afe12bbc --- /dev/null +++ b/src/components/QvCore/QvCommandLineArgs.hpp @@ -0,0 +1,44 @@ +#ifndef QVCOMMANDLINEARGS_HPP +#define QVCOMMANDLINEARGS_HPP + +#include "Qv2rayBase.hpp" + +namespace Qv2ray +{ + namespace CommandArgOperations + { + struct QvStartupOptions { + /// No API subsystem + bool noAPI; + }; + enum CommandLineParseResult { + CommandLineOk, + CommandLineError, + CommandLineVersionRequested, + CommandLineHelpRequested + }; + // + extern QvStartupOptions StartupOption; + + class QvCommandArgParser : public QObject + { + Q_OBJECT + public: + QvCommandArgParser(); + CommandLineParseResult ParseCommandLine(QString *errorMessage); + const QCommandLineParser *Parser() + { + return &parser; + } + + private: + QCommandLineParser parser; + QCommandLineOption noAPIOption; + QCommandLineOption helpOption; + QCommandLineOption versionOption; + }; + } +} + +using namespace Qv2ray::CommandArgOperations; +#endif diff --git a/src/components/QvCoreInteractions.cpp b/src/components/QvKernelInteractions.cpp similarity index 87% rename from src/components/QvCoreInteractions.cpp rename to src/components/QvKernelInteractions.cpp index e96434f1..04701e45 100644 --- a/src/components/QvCoreInteractions.cpp +++ b/src/components/QvKernelInteractions.cpp @@ -1,10 +1,9 @@ #include #include #include -#include "QvCoreInteractions.hpp" +#include "QvKernelInteractions.hpp" #include "QvCoreConfigOperations.hpp" - -#include "QvTinyLog.hpp" +#include "QvCore/QvCommandLineArgs.hpp" using namespace v2ray::core::app::stats::command; using grpc::Channel; @@ -16,9 +15,9 @@ using grpc::Status; namespace Qv2ray { - namespace QvCoreInteration + namespace QvKernelInterations { - bool ConnectionInstance::ValidateConfig(const QString &path) + bool V2rayKernelInstance::ValidateConfig(const QString &path) { auto conf = GetGlobalConfig(); @@ -53,7 +52,7 @@ namespace Qv2ray } } - ConnectionInstance::ConnectionInstance() + V2rayKernelInstance::V2rayKernelInstance() { auto proc = new QProcess(); vProcess = proc; @@ -63,7 +62,7 @@ namespace Qv2ray ConnectionStatus = STOPPED; } - bool ConnectionInstance::StartConnection(CONFIGROOT root, int apiPort) + bool V2rayKernelInstance::StartConnection(CONFIGROOT root, int apiPort) { inboundTags.clear(); @@ -96,7 +95,10 @@ namespace Qv2ray vProcess->start(GetGlobalConfig().v2CorePath, QStringList() << "-config" << filePath, QIODevice::ReadWrite | QIODevice::Text); vProcess->waitForStarted(); ConnectionStatus = STARTED; - { + + if (StartupOption.noAPI) { + LOG(MODULE_VCORE, "API is disabled by the command line arg \"--noAPI\"") + } else { // Config API apiFailedCounter = 0; this->apiPort = apiPort; @@ -106,6 +108,7 @@ namespace Qv2ray apiTimerId = startTimer(1000); LOG(MODULE_VCORE, "API Worker started.") } + return true; } else { ConnectionStatus = STOPPED; @@ -113,7 +116,7 @@ namespace Qv2ray } } - void ConnectionInstance::timerEvent(QTimerEvent *event) + void V2rayKernelInstance::timerEvent(QTimerEvent *event) { QObject::timerEvent(event); @@ -134,7 +137,7 @@ namespace Qv2ray } } - void ConnectionInstance::StopConnection() + void V2rayKernelInstance::StopConnection() { vProcess->close(); killTimer(apiTimerId); @@ -144,7 +147,7 @@ namespace Qv2ray ConnectionStatus = STOPPED; } - ConnectionInstance::~ConnectionInstance() + V2rayKernelInstance::~V2rayKernelInstance() { if (ConnectionStatus != STOPPED) { StopConnection(); @@ -153,7 +156,7 @@ namespace Qv2ray delete vProcess; } - long ConnectionInstance::CallStatsAPIByName(QString name) + long V2rayKernelInstance::CallStatsAPIByName(QString name) { if (ConnectionStatus != STARTED) { LOG(MODULE_VCORE, "Invalid connection status when calling API") @@ -186,23 +189,23 @@ namespace Qv2ray return response.stat().value(); } // ------------------------------------------------------------- API FUNCTIONS -------------------------- - long ConnectionInstance::getTagSpeedUp(const QString &tag) + long V2rayKernelInstance::getTagSpeedUp(const QString &tag) { return transferSpeed[tag + "_up"]; } - long ConnectionInstance::getTagSpeedDown(const QString &tag) + long V2rayKernelInstance::getTagSpeedDown(const QString &tag) { return transferSpeed[tag + "_down"]; } - long ConnectionInstance::getTagDataUp(const QString &tag) + long V2rayKernelInstance::getTagDataUp(const QString &tag) { return transferData[tag + "_up"]; } - long ConnectionInstance::getTagDataDown(const QString &tag) + long V2rayKernelInstance::getTagDataDown(const QString &tag) { return transferData[tag + "_down"]; } - long ConnectionInstance::getAllDataUp() + long V2rayKernelInstance::getAllDataUp() { long val = 0; @@ -212,7 +215,7 @@ namespace Qv2ray return val; } - long ConnectionInstance::getAllDataDown() + long V2rayKernelInstance::getAllDataDown() { long val = 0; @@ -222,7 +225,7 @@ namespace Qv2ray return val; } - long ConnectionInstance::getAllSpeedUp() + long V2rayKernelInstance::getAllSpeedUp() { long val = 0; @@ -232,7 +235,7 @@ namespace Qv2ray return val; } - long ConnectionInstance::getAllSpeedDown() + long V2rayKernelInstance::getAllSpeedDown() { long val = 0; diff --git a/src/components/QvCoreInteractions.hpp b/src/components/QvKernelInteractions.hpp similarity index 87% rename from src/components/QvCoreInteractions.hpp rename to src/components/QvKernelInteractions.hpp index 5d123aba..d0479a18 100644 --- a/src/components/QvCoreInteractions.hpp +++ b/src/components/QvKernelInteractions.hpp @@ -1,8 +1,6 @@ #ifndef VINTERACT_H #define VINTERACT_H #include -#include -#include "Qv2rayBase.hpp" #include #include "QvUtils.hpp" #include "v2ray_api_commands.pb.h" @@ -10,7 +8,7 @@ namespace Qv2ray { - namespace QvCoreInteration + namespace QvKernelInterations { enum QvInstanceStatus { STOPPED, @@ -18,12 +16,12 @@ namespace Qv2ray STARTED }; - class ConnectionInstance : public QObject + class V2rayKernelInstance : public QObject { Q_OBJECT public: - explicit ConnectionInstance(); - ~ConnectionInstance() override; + explicit V2rayKernelInstance(); + ~V2rayKernelInstance() override; // // Speed long getTagSpeedUp(const QString &tag); @@ -63,6 +61,6 @@ namespace Qv2ray } } -using namespace Qv2ray::QvCoreInteration; +using namespace Qv2ray::QvKernelInterations; #endif // VINTERACT_H diff --git a/src/main.cpp b/src/main.cpp index a92dfed3..517a52de 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,6 +11,8 @@ #include "w_MainWindow.hpp" +#include "QvCore/QvCommandLineArgs.hpp" + bool verifyConfigAvaliability(QString path, bool checkExistingConfig) { // Does not exist. @@ -207,6 +209,28 @@ int main(int argc, char *argv[]) LOG(MODULE_UI, "Installing a tranlator from OS: " + _lang.toStdString() + " -- " + (_result_ ? "OK" : "Failed")) } + QvCommandArgParser parser; + QString errorMessage; + + switch (parser.ParseCommandLine(&errorMessage)) { + case CommandLineOk: + break; + + case CommandLineError: + cout << errorMessage.toStdString() << endl; + cout << parser.Parser()->helpText().toStdString() << endl; + return 1; + + case CommandLineVersionRequested: + LOG(QCoreApplication::applicationName().toStdString(), QCoreApplication::applicationVersion().toStdString()); + return 0; + + case CommandLineHelpRequested: + cout << parser.Parser()->helpText().toStdString() << endl; + return 0; + } + + // LOG("LICENCE", NEWLINE "This program comes with ABSOLUTELY NO WARRANTY." NEWLINE "This is free software, and you are welcome to redistribute it" NEWLINE "under certain conditions." NEWLINE NEWLINE @@ -250,7 +274,6 @@ int main(int argc, char *argv[]) // Load the config for upgrade, but do not parse it to the struct. auto conf = JsonFromString(StringFromFile(new QFile(QV2RAY_CONFIG_FILE))); - // auto confVersion = conf["config_version"].toVariant().toString().toInt(); if (confVersion > QV2RAY_CONFIG_VERSION) { @@ -261,7 +284,7 @@ int main(int argc, char *argv[]) QObject::tr("Please check if there's an issue explaining about it.") + NEWLINE + QObject::tr("Or submit a new issue if you think this is an error.") + NEWLINE + NEWLINE + QObject::tr("Qv2ray will now exit.")); - return -3; + return -2; } if (confVersion < QV2RAY_CONFIG_VERSION) { @@ -313,7 +336,7 @@ int main(int argc, char *argv[]) QObject::tr("Technical Details") + "\r\n" + "OSsl.Rq.V=" + osslReqVersion + "\r\n" + "OSsl.Cr.V=" + osslCurVersion); - return -2; + return -3; } #ifdef Q_OS_WIN @@ -386,7 +409,7 @@ int main(int argc, char *argv[]) } catch (...) { QvMessageBox(nullptr, "ERROR", "There's something wrong happened and Qv2ray will quit now."); LOG(MODULE_INIT, "EXCEPTION THROWN: " __FILE__) - return -9; + return -99; } #endif diff --git a/src/ui/w_ImportConfig.cpp b/src/ui/w_ImportConfig.cpp index 5548629f..f7530c13 100644 --- a/src/ui/w_ImportConfig.cpp +++ b/src/ui/w_ImportConfig.cpp @@ -8,7 +8,7 @@ #include "qzxing/src/QZXing.h" #include "QvUtils.hpp" -#include "QvCoreInteractions.hpp" +#include "QvKernelInteractions.hpp" #include "QvCoreConfigOperations.hpp" #include "w_ScreenShot_Core.hpp" @@ -70,7 +70,7 @@ void ImportConfigWindow::on_beginImportBtn_clicked() bool keepInBound = keepImportedInboundCheckBox->isChecked(); QString path = fileLineTxt->text(); - if (!ConnectionInstance::ValidateConfig(path)) { + if (!V2rayKernelInstance::ValidateConfig(path)) { QvMessageBox(this, tr("Import config file"), tr("Failed to check the validity of the config file.")); return; } diff --git a/src/ui/w_MainWindow.cpp b/src/ui/w_MainWindow.cpp index 2b4733e0..bef466d7 100644 --- a/src/ui/w_MainWindow.cpp +++ b/src/ui/w_MainWindow.cpp @@ -62,8 +62,8 @@ MainWindow::MainWindow(QWidget *parent): { MainWindow::mwInstance = this; currentConfig = GetGlobalConfig(); - vinstance = new ConnectionInstance(); - connect(vinstance, &ConnectionInstance::onProcessOutputReadyRead, this, &MainWindow::UpdateVCoreLog); + vinstance = new V2rayKernelInstance(); + connect(vinstance, &V2rayKernelInstance::onProcessOutputReadyRead, this, &MainWindow::UpdateVCoreLog); setupUi(this); // // Two browsers @@ -957,13 +957,13 @@ void MainWindow::timerEvent(QTimerEvent *event) auto _totalDataUp = vinstance->getAllDataUp(); auto _totalDataDown = vinstance->getAllDataDown(); // - double max = 0; + double _max = 0; double historyMax = 0; auto graphVUp = _totalSpeedUp / 1024; auto graphVDown = _totalSpeedDown / 1024; for (auto i = 0; i < 29; i++) { - historyMax = MAX(historyMax, MAX(uploadList[i + 1], downloadList[i + 1])); + historyMax = max(historyMax, max(uploadList[i + 1], downloadList[i + 1])); uploadList[i] = uploadList[i + 1]; downloadList[i] = downloadList[i + 1]; uploadSerie->replace(i, i, uploadList[i + 1]); @@ -975,8 +975,8 @@ void MainWindow::timerEvent(QTimerEvent *event) uploadSerie->replace(29, 29, graphVUp); downloadSerie->replace(29, 29, graphVDown); // - max = MAX(MAX(graphVUp, graphVDown), historyMax); - speedChartObj->axes(Qt::Vertical).first()->setRange(0, max * 1.2); + _max = max(historyMax, double(max(graphVUp, graphVDown))); + speedChartObj->axes(Qt::Vertical).first()->setRange(0, _max * 1.2); // auto totalSpeedUp = FormatBytes(_totalSpeedUp) + "/s"; auto totalSpeedDown = FormatBytes(_totalSpeedDown) + "/s"; diff --git a/src/ui/w_MainWindow.hpp b/src/ui/w_MainWindow.hpp index e5715466..97df53ec 100644 --- a/src/ui/w_MainWindow.hpp +++ b/src/ui/w_MainWindow.hpp @@ -12,7 +12,7 @@ #include "ui_w_MainWindow.h" #include "QvUtils.hpp" -#include "QvCoreInteractions.hpp" +#include "QvKernelInteractions.hpp" #include "QvCoreConfigOperations.hpp" #include "QvHTTPRequestHelper.hpp" #include "QvPACHandler.hpp" @@ -74,7 +74,7 @@ class MainWindow : public QMainWindow, Ui::MainWindow public: static MainWindow *mwInstance; QString CurrentConnectionName = ""; - ConnectionInstance *vinstance; + V2rayKernelInstance *vinstance; protected: void mouseReleaseEvent(QMouseEvent *e) override; diff --git a/src/ui/w_PreferencesWindow.cpp b/src/ui/w_PreferencesWindow.cpp index 2cfd70eb..e528d012 100644 --- a/src/ui/w_PreferencesWindow.cpp +++ b/src/ui/w_PreferencesWindow.cpp @@ -6,7 +6,7 @@ #include #include "QvUtils.hpp" -#include "QvCoreInteractions.hpp" +#include "QvKernelInteractions.hpp" #include "QvNetSpeedPlugin.hpp" #include "QvCoreConfigOperations.hpp" diff --git a/src/utils/QvHelpers.hpp b/src/utils/QvHelpers.hpp index 9ea4b325..f3c0a892 100644 --- a/src/utils/QvHelpers.hpp +++ b/src/utils/QvHelpers.hpp @@ -9,7 +9,6 @@ namespace Qv2ray { namespace Utils { - QTranslator *getTranslator(const QString &lang); QStringList GetFileList(QDir dir); QString Base64Encode(QString string);