From 471beb1cceb7acb88b241e3b4307e68eaa71ec4f Mon Sep 17 00:00:00 2001 From: "Leroy.H.Y" Date: Mon, 1 Jul 2019 23:43:27 +0800 Subject: [PATCH] [Fix] Continue refactoring the project. Signed-off-by: Leroy.H.Y --- .gitignore | 1 + Hv2ray.pro | 139 +++++++++++++++------------------ src/utils.cpp | 9 --- src/utils.h | 1 - src/vinteract.cpp | 25 ++++-- src/vinteract.h | 18 ++++- src/w_ConnectionEditWindow.cpp | 2 +- src/w_ImportConfig.cpp | 7 +- src/w_MainWindow.cpp | 17 ++-- src/w_MainWindow.h | 2 +- src/w_MainWindow.ui | 5 +- src/w_PrefrencesWindow.cpp | 5 +- 12 files changed, 120 insertions(+), 111 deletions(-) diff --git a/.gitignore b/.gitignore index 55f8ccb3..ba5bbdc4 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ v2ctl.sig build/ python37/ tools/python-3.7.3.exe +./.vscode diff --git a/Hv2ray.pro b/Hv2ray.pro index 62efcd61..3d2e4fc6 100644 --- a/Hv2ray.pro +++ b/Hv2ray.pro @@ -24,36 +24,34 @@ DEFINES += QT_DEPRECATED_WARNINGS CONFIG += c++11 -VPATH += ./src - SOURCES += \ - w_MainWindow.cpp \ - w_ConnectionEditWindow.cpp \ - w_ImportConfig.cpp \ - w_PrefrencesWindow.cpp \ - main.cpp \ - vinteract.cpp \ - utils.cpp \ - runguard.cpp + ./src/w_MainWindow.cpp \ + ./src/w_ConnectionEditWindow.cpp \ + ./src/w_ImportConfig.cpp \ + ./src/w_PrefrencesWindow.cpp \ + ./src/main.cpp \ + ./src/vinteract.cpp \ + ./src/utils.cpp \ + ./src/runguard.cpp HEADERS += \ - w_MainWindow.h \ - w_ConnectionEditWindow.h \ - w_ImportConfig.h \ - w_PrefrencesWindow.h \ - constants.h \ - vinteract.h \ - utils.h \ - runguard.h + ./src/w_MainWindow.h \ + ./src/w_ConnectionEditWindow.h \ + ./src/w_ImportConfig.h \ + ./src/w_PrefrencesWindow.h \ + ./src/constants.h \ + ./src/vinteract.h \ + ./src/utils.h \ + ./src/runguard.h FORMS += \ - w_MainWindow.ui \ - w_ConnectionEditWindow.ui \ - w_ImportConfig.ui \ - w_PrefrencesWindow.ui + ./src/w_MainWindow.ui \ + ./src/w_ConnectionEditWindow.ui \ + ./src/w_ImportConfig.ui \ + ./src/w_PrefrencesWindow.ui RESOURCES += \ - resources.qrc + ./resources.qrc TRANSLATIONS += \ @@ -68,66 +66,57 @@ else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target WITH_PYTHON = no +PYTHONVER = null unix:!macx { - exists( "/usr/include/python3.7m/Python.h" ) { - equals(WITH_PYTHON, "no") { - message("Will build with python lib version 3.7.") - INCLUDEPATH += /usr/include/python3.7m/ - LIBS += -lpython3.7m - WITH_PYTHON = yes - } - } -} - -unix:!macx { - exists( "/usr/include/python3.6m/Python.h" ) { - equals(WITH_PYTHON, "no") { - message("Will build with python lib version 3.6.") - INCLUDEPATH += /usr/include/python3.6m/ - LIBS += -lpython3.6m - WITH_PYTHON = yes - } - } -} - -unix:!macx { + # Python Headers check. exists( "/usr/include/python3.5m/Python.h" ) { - equals(WITH_PYTHON, "no") { - message("Will build with python lib version 3.5.") - INCLUDEPATH += /usr/include/python3.5m/ - LIBS += -lpython3.5m - WITH_PYTHON = yes - } + PYTHONVER = 3.5 + WITH_PYTHON = yes + } + exists( "/usr/include/python3.6m/Python.h" ) { + PYTHONVER = 3.6 + WITH_PYTHON = yes + } + exists( "/usr/include/python3.7m/Python.h" ) { + PYTHONVER = 3.7 + WITH_PYTHON = yes + } + + equals( WITH_PYTHON, "yes" ) { + INCLUDEPATH += /usr/include/python$${PYTHONVER}m/ + LIBS += -lpython$${PYTHONVER}m + message("Will build with python lib version $$PYTHONVER") } } macx { - PYTHON_ROOT=/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions - exists( "$$PYTHON_ROOT/3.7/include/python3.7m/Python.h" ) { - equals(WITH_PYTHON, "no") { - message("Will build with python lib version 3.7.3.") - INCLUDEPATH += $$PYTHON_ROOT/3.7/include/python3.7m/ - LIBS += -L$$PYTHON_ROOT/3.7/lib/python3.7/config-3.7m-darwin/ -lpython3.7m - WITH_PYTHON = yes - } + PYTHON_ROOT=/usr/local/Cellar/python + exists( "$${PYTHON_ROOT}/3.6.5_1/" ) { + PYTHONVER = 3.6 + PYLDPATH=$${PYTHON_ROOT}/3.6.5_1/Frameworks/Python.framework/Versions/$${PYTHONVER} + WITH_PYTHON = yes + } + + exists( "$$PYTHON_ROOT/3.7.3/" ) { + PYTHONVER = 3.7 + PYLDPATH=$${PYTHON_ROOT}/3.7.3/Frameworks/Python.framework/Versions/$${PYTHONVER} + WITH_PYTHON = yes + } + + INCLUDEPATH += $${PYLDPATH}/include/python$${PYTHONVER}m/ + LIBS += -L$${PYLDPATH}/lib/python$${PYTHONVER}/config-$${PYTHONVER}m-darwin/ -lpython$${PYTHONVER}m + message("Will build with python lib version $$PYTHONVER") +} + +win32 { + exists( "$$PWD/python37/libs/libpython37_mingw.a" ) { + LIBS += -L$$PWD/python37/libs/ -lpython37_mingw + INCLUDEPATH += $$PWD/python37/include + WITH_PYTHON = yes } } -macx { - PYTHON_ROOT=/usr/local/Cellar/python/3.6.5_1/Frameworks/Python.framework/Versions - exists( "$$PYTHON_ROOT/3.6/include/python3.6m/Python.h" ) { - equals(WITH_PYTHON, "no") { - message("Will build with python lib version 3.6.5_1.") - INCLUDEPATH += $$PYTHON_ROOT/3.6/include/python3.6m/ - LIBS += -L$$PYTHON_ROOT/3.6/lib/python3.6/config-3.6m-darwin/ -lpython3.6m - WITH_PYTHON = yes - } - } +equals(WITH_PYTHON, "no") { + error("No Python3 libs found, did you install dev packages such as python3-dev ?") } -unix: equals(WITH_PYTHON, "no") { - error("No python libs found, did you install python3 dev package?") -} - -win32: LIBS += -L$$PWD/python37/libs/ -lpython37_mingw -win32: INCLUDEPATH += $$PWD/python37/include diff --git a/src/utils.cpp b/src/utils.cpp index 30621892..7b2a2e6f 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -48,15 +48,6 @@ bool getRootEnabled() return loadRootObjFromConf().value("v2suidEnabled").toBool(); } -bool checkVCoreExes() -{ - if (QFileInfo("v2ray").exists() && QFileInfo("geoip.dat").exists() && QFileInfo("geosite.dat").exists() && QFileInfo("v2ctl").exists()) { - return true; - } else { - showWarnMessageBox(nullptr, QObject::tr("CoreNotFound"), QObject::tr("CoreFileNotFoundExplaination")); - return false; - } -} void showWarnMessageBox(QWidget* parent, QString title, QString text) { diff --git a/src/utils.h b/src/utils.h index ec8000aa..9b3adf2c 100644 --- a/src/utils.h +++ b/src/utils.h @@ -8,7 +8,6 @@ QJsonObject switchJsonArrayObject(QJsonObject objest, QString value); QJsonObject findValueFromJsonArray(QJsonArray arr, QString key, QString val); QJsonObject loadRootObjFromConf(); QJsonArray getInbounds(); -bool checkVCoreExes(); void showWarnMessageBox(QWidget* parent, QString title, QString text); void overrideInbounds(QString path); int getIndexByValue(QJsonArray array, QString key, QString val); diff --git a/src/vinteract.cpp b/src/vinteract.cpp index 623b5b3c..407202dc 100644 --- a/src/vinteract.cpp +++ b/src/vinteract.cpp @@ -7,7 +7,7 @@ #include "w_MainWindow.h" #include "vinteract.h" -bool validationCheck(QString path) +bool v2Instance::checkConfigFile(QString path) { if(checkVCoreExes()) { QProcessEnvironment env = QProcessEnvironment::systemEnvironment(); @@ -33,17 +33,24 @@ bool validationCheck(QString path) else return false; } -v2Instance::v2Instance() +v2Instance::v2Instance(QWidget *parent) { this->vProcess = new QProcess(); + QObject::connect(vProcess, SIGNAL(readyReadStandardOutput()), parent, SLOT(updateLog())); } -v2Instance::~v2Instance() +bool v2Instance::checkVCoreExes() { - this->stop(); + if (QFileInfo("v2ray").exists() && QFileInfo("geoip.dat").exists() && QFileInfo("geosite.dat").exists() && QFileInfo("v2ctl").exists()) { + return true; + } + else { + showWarnMessageBox(nullptr, QObject::tr("CoreNotFound"), QObject::tr("CoreFileNotFoundExplaination")); + return false; + } } -bool v2Instance::start(QWidget *parent) +bool v2Instance::start() { if(this->vProcess->state() == QProcess::Running) { this->stop(); @@ -54,7 +61,7 @@ bool v2Instance::start(QWidget *parent) this->vProcess->setProcessEnvironment(env); this->vProcess->start("./v2ray", QStringList() << "-config" << "config.json", QIODevice::ReadWrite | QIODevice::Text); this->vProcess->waitForStarted(); - QObject::connect(vProcess, SIGNAL(readyReadStandardOutput()), parent, SLOT(updateLog())); + processStatus = STARTED; return true; } else return false; @@ -63,4 +70,10 @@ bool v2Instance::start(QWidget *parent) void v2Instance::stop() { this->vProcess->close(); + processStatus = STOPPED; +} + +v2Instance::~v2Instance() +{ + this->stop(); } diff --git a/src/vinteract.h b/src/vinteract.h index cdc9d5d6..0170d063 100644 --- a/src/vinteract.h +++ b/src/vinteract.h @@ -3,17 +3,27 @@ #include #include -bool validationCheck(QString path); +enum V2RAY_INSTANCE_STARTUP_STATUS { + STOPPED, + STARTING, + STARTED +}; class v2Instance { public: - explicit v2Instance(); - bool start(QWidget *parent); + explicit v2Instance(QWidget *parent); + + bool start(); void stop(); void restart(); - QProcess *vProcess; + + static bool checkVCoreExes(); + static bool checkConfigFile(QString path); ~v2Instance(); + QProcess *vProcess; +private: + V2RAY_INSTANCE_STARTUP_STATUS processStatus; }; #endif // VINTERACT_H diff --git a/src/w_ConnectionEditWindow.cpp b/src/w_ConnectionEditWindow.cpp index ca8b5db6..6222ec64 100644 --- a/src/w_ConnectionEditWindow.cpp +++ b/src/w_ConnectionEditWindow.cpp @@ -1,5 +1,5 @@ #include "w_ConnectionEditWindow.h" -#include "ui_ConnectionEditWindow.h" +#include "ui_w_ConnectionEditWindow.h" #include #include "w_MainWindow.h" #include diff --git a/src/w_ImportConfig.cpp b/src/w_ImportConfig.cpp index aad660e6..d3e8f381 100644 --- a/src/w_ImportConfig.cpp +++ b/src/w_ImportConfig.cpp @@ -14,7 +14,8 @@ #include "vinteract.h" #include "utils.h" #include "w_ImportConfig.h" -#include "ui_ImportConfig.h" +#include "ui_w_ImportConfig.h" + ImportConfig::ImportConfig(QWidget *parent) : QDialog(parent), @@ -87,7 +88,7 @@ void ImportConfig::on_buttonBox_accepted() if(ui->importSourceCombo->currentIndex() == 0) // From File... { QString path = ui->fileLineTxt->text(); - bool isValid = validationCheck(path); + bool isValid = v2Instance::checkConfigFile(path); if(isValid) { savefromFile(path, alias); } @@ -109,7 +110,7 @@ void ImportConfig::on_buttonBox_accepted() Py_Finalize(); if(QFile::exists(QCoreApplication::applicationDirPath() + "/config.json.tmp")) { ImportConfig *im = new ImportConfig(this->parentWidget()); - if (validationCheck(QCoreApplication::applicationDirPath() + "/config.json.tmp")) { + if (v2Instance::checkConfigFile(QCoreApplication::applicationDirPath() + "/config.json.tmp")) { im->savefromFile("config.json.tmp", alias); } QFile::remove("config.json.tmp"); diff --git a/src/w_MainWindow.cpp b/src/w_MainWindow.cpp index 3c5fe542..f2febf0f 100644 --- a/src/w_MainWindow.cpp +++ b/src/w_MainWindow.cpp @@ -8,7 +8,7 @@ #include #include -#include "ui_MainWindow.h" +#include "ui_w_MainWindow.h" #include "w_PrefrencesWindow.h" #include "w_MainWindow.h" #include "w_ConnectionEditWindow.h" @@ -64,10 +64,10 @@ MainWindow::MainWindow(QWidget *parent) : UpdateConfigTable(); // ui->configTable->setContextMenuPolicy(Qt::CustomContextMenu); // connect(ui->configTable, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(showMenu(QPoint))); - this->v2instance = new v2Instance(); + this->vinstance = new v2Instance(this); CreateTrayIcon(); if(QFileInfo("config.json").exists()) { - v2instance->start(this); + vinstance->start(); } // QAction *select = new QAction("Select", ui->configTable); @@ -87,7 +87,7 @@ MainWindow::~MainWindow() { hTray->hide(); delete this->hTray; - delete this->v2instance; + delete this->vinstance; delete ui; } @@ -107,6 +107,7 @@ void MainWindow::on_actionExisting_config_triggered() void MainWindow::showMenu(QPoint pos) { + Q_UNUSED(pos) // if(ui->configTable->indexAt(pos).column() != -1) { // popMenu->move(cursor().pos()); // popMenu->show(); @@ -147,13 +148,13 @@ void MainWindow::GenerateConfig(int idIntable) } void MainWindow::UpdateLog() { - ui->logText->insertPlainText(this->v2instance->vProcess->readAllStandardOutput()); + ui->logText->insertPlainText(this->vinstance->vProcess->readAllStandardOutput()); } void MainWindow::on_startButton_clicked() { ui->logText->clear(); - bool startFlag = this->v2instance->start(this); + bool startFlag = this->vinstance->start(); trayMenu->actions()[2]->setEnabled(!startFlag); trayMenu->actions()[3]->setEnabled(startFlag); trayMenu->actions()[4]->setEnabled(startFlag); @@ -161,7 +162,7 @@ void MainWindow::on_startButton_clicked() void MainWindow::on_stopButton_clicked() { - this->v2instance->stop(); + this->vinstance->stop(); ui->logText->clear(); trayMenu->actions()[2]->setEnabled(true); trayMenu->actions()[3]->setEnabled(false); @@ -209,7 +210,7 @@ void MainWindow::on_activatedTray(QSystemTrayIcon::ActivationReason reason) case QSystemTrayIcon::MiddleClick: // TODO: Check if an alert message box is present. // If so, do nothing but please wait for the message box to be closed. - if(this->v2instance->vProcess->state() == QProcess::ProcessState::Running) { + if(this->vinstance->vProcess->state() == QProcess::ProcessState::Running) { on_stopButton_clicked(); } else { on_startButton_clicked(); diff --git a/src/w_MainWindow.h b/src/w_MainWindow.h index a4f3247c..ac29b0a6 100644 --- a/src/w_MainWindow.h +++ b/src/w_MainWindow.h @@ -19,7 +19,7 @@ class MainWindow : public QMainWindow public: explicit MainWindow(QWidget *parent = nullptr); - v2Instance *v2instance; + v2Instance *vinstance; QSystemTrayIcon *hTray; QMenu *trayMenu = new QMenu(this); QMenu *popMenu = new QMenu(this); diff --git a/src/w_MainWindow.ui b/src/w_MainWindow.ui index 7d890a69..e561718e 100644 --- a/src/w_MainWindow.ui +++ b/src/w_MainWindow.ui @@ -143,6 +143,9 @@ QFormLayout::ExpandingFieldsGrow + + 10 + @@ -241,7 +244,7 @@ 0 0 568 - 21 + 29 diff --git a/src/w_PrefrencesWindow.cpp b/src/w_PrefrencesWindow.cpp index 648bf5f9..461b5cf2 100644 --- a/src/w_PrefrencesWindow.cpp +++ b/src/w_PrefrencesWindow.cpp @@ -8,9 +8,10 @@ #include #include #include -#include +#include #include "utils.h" +#include "vinteract.h" #include "w_PrefrencesWindow.h" PrefrencesWindow::PrefrencesWindow(QWidget *parent) : @@ -48,7 +49,7 @@ PrefrencesWindow::~PrefrencesWindow() void PrefrencesWindow::on_buttonBox_accepted() { - if(checkVCoreExes()) { + if(v2Instance::checkVCoreExes()) { if(ui->httpPortLE->text().toInt() != ui->socksPortLE->text().toInt()) { QJsonArray inbounds; QJsonDocument modifiedDoc;