[Fix] Continue refactoring the project.

Signed-off-by: Leroy.H.Y <lhy20010403@hotmail.com>
This commit is contained in:
Leroy.H.Y 2019-07-01 23:43:27 +08:00
parent adae62393f
commit 471beb1cce
No known key found for this signature in database
GPG Key ID: 6AC1673B587DC37D
12 changed files with 120 additions and 111 deletions

1
.gitignore vendored
View File

@ -15,3 +15,4 @@ v2ctl.sig
build/ build/
python37/ python37/
tools/python-3.7.3.exe tools/python-3.7.3.exe
./.vscode

View File

@ -24,36 +24,34 @@ DEFINES += QT_DEPRECATED_WARNINGS
CONFIG += c++11 CONFIG += c++11
VPATH += ./src
SOURCES += \ SOURCES += \
w_MainWindow.cpp \ ./src/w_MainWindow.cpp \
w_ConnectionEditWindow.cpp \ ./src/w_ConnectionEditWindow.cpp \
w_ImportConfig.cpp \ ./src/w_ImportConfig.cpp \
w_PrefrencesWindow.cpp \ ./src/w_PrefrencesWindow.cpp \
main.cpp \ ./src/main.cpp \
vinteract.cpp \ ./src/vinteract.cpp \
utils.cpp \ ./src/utils.cpp \
runguard.cpp ./src/runguard.cpp
HEADERS += \ HEADERS += \
w_MainWindow.h \ ./src/w_MainWindow.h \
w_ConnectionEditWindow.h \ ./src/w_ConnectionEditWindow.h \
w_ImportConfig.h \ ./src/w_ImportConfig.h \
w_PrefrencesWindow.h \ ./src/w_PrefrencesWindow.h \
constants.h \ ./src/constants.h \
vinteract.h \ ./src/vinteract.h \
utils.h \ ./src/utils.h \
runguard.h ./src/runguard.h
FORMS += \ FORMS += \
w_MainWindow.ui \ ./src/w_MainWindow.ui \
w_ConnectionEditWindow.ui \ ./src/w_ConnectionEditWindow.ui \
w_ImportConfig.ui \ ./src/w_ImportConfig.ui \
w_PrefrencesWindow.ui ./src/w_PrefrencesWindow.ui
RESOURCES += \ RESOURCES += \
resources.qrc ./resources.qrc
TRANSLATIONS += \ TRANSLATIONS += \
@ -68,66 +66,57 @@ else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target !isEmpty(target.path): INSTALLS += target
WITH_PYTHON = no WITH_PYTHON = no
PYTHONVER = null
unix:!macx { unix:!macx {
exists( "/usr/include/python3.7m/Python.h" ) { # Python Headers check.
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 {
exists( "/usr/include/python3.5m/Python.h" ) { exists( "/usr/include/python3.5m/Python.h" ) {
equals(WITH_PYTHON, "no") { PYTHONVER = 3.5
message("Will build with python lib version 3.5.") WITH_PYTHON = yes
INCLUDEPATH += /usr/include/python3.5m/ }
LIBS += -lpython3.5m exists( "/usr/include/python3.6m/Python.h" ) {
WITH_PYTHON = yes 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 { macx {
PYTHON_ROOT=/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions PYTHON_ROOT=/usr/local/Cellar/python
exists( "$$PYTHON_ROOT/3.7/include/python3.7m/Python.h" ) { exists( "$${PYTHON_ROOT}/3.6.5_1/" ) {
equals(WITH_PYTHON, "no") { PYTHONVER = 3.6
message("Will build with python lib version 3.7.3.") PYLDPATH=$${PYTHON_ROOT}/3.6.5_1/Frameworks/Python.framework/Versions/$${PYTHONVER}
INCLUDEPATH += $$PYTHON_ROOT/3.7/include/python3.7m/ WITH_PYTHON = yes
LIBS += -L$$PYTHON_ROOT/3.7/lib/python3.7/config-3.7m-darwin/ -lpython3.7m }
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 { equals(WITH_PYTHON, "no") {
PYTHON_ROOT=/usr/local/Cellar/python/3.6.5_1/Frameworks/Python.framework/Versions error("No Python3 libs found, did you install dev packages such as python3-dev ?")
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
}
}
} }
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

View File

@ -48,15 +48,6 @@ bool getRootEnabled()
return loadRootObjFromConf().value("v2suidEnabled").toBool(); 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) void showWarnMessageBox(QWidget* parent, QString title, QString text)
{ {

View File

@ -8,7 +8,6 @@ QJsonObject switchJsonArrayObject(QJsonObject objest, QString value);
QJsonObject findValueFromJsonArray(QJsonArray arr, QString key, QString val); QJsonObject findValueFromJsonArray(QJsonArray arr, QString key, QString val);
QJsonObject loadRootObjFromConf(); QJsonObject loadRootObjFromConf();
QJsonArray getInbounds(); QJsonArray getInbounds();
bool checkVCoreExes();
void showWarnMessageBox(QWidget* parent, QString title, QString text); void showWarnMessageBox(QWidget* parent, QString title, QString text);
void overrideInbounds(QString path); void overrideInbounds(QString path);
int getIndexByValue(QJsonArray array, QString key, QString val); int getIndexByValue(QJsonArray array, QString key, QString val);

View File

@ -7,7 +7,7 @@
#include "w_MainWindow.h" #include "w_MainWindow.h"
#include "vinteract.h" #include "vinteract.h"
bool validationCheck(QString path) bool v2Instance::checkConfigFile(QString path)
{ {
if(checkVCoreExes()) { if(checkVCoreExes()) {
QProcessEnvironment env = QProcessEnvironment::systemEnvironment(); QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
@ -33,17 +33,24 @@ bool validationCheck(QString path)
else return false; else return false;
} }
v2Instance::v2Instance() v2Instance::v2Instance(QWidget *parent)
{ {
this->vProcess = new QProcess(); 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) { if(this->vProcess->state() == QProcess::Running) {
this->stop(); this->stop();
@ -54,7 +61,7 @@ bool v2Instance::start(QWidget *parent)
this->vProcess->setProcessEnvironment(env); this->vProcess->setProcessEnvironment(env);
this->vProcess->start("./v2ray", QStringList() << "-config" << "config.json", QIODevice::ReadWrite | QIODevice::Text); this->vProcess->start("./v2ray", QStringList() << "-config" << "config.json", QIODevice::ReadWrite | QIODevice::Text);
this->vProcess->waitForStarted(); this->vProcess->waitForStarted();
QObject::connect(vProcess, SIGNAL(readyReadStandardOutput()), parent, SLOT(updateLog())); processStatus = STARTED;
return true; return true;
} }
else return false; else return false;
@ -63,4 +70,10 @@ bool v2Instance::start(QWidget *parent)
void v2Instance::stop() void v2Instance::stop()
{ {
this->vProcess->close(); this->vProcess->close();
processStatus = STOPPED;
}
v2Instance::~v2Instance()
{
this->stop();
} }

View File

@ -3,17 +3,27 @@
#include <QString> #include <QString>
#include <QProcess> #include <QProcess>
bool validationCheck(QString path); enum V2RAY_INSTANCE_STARTUP_STATUS {
STOPPED,
STARTING,
STARTED
};
class v2Instance class v2Instance
{ {
public: public:
explicit v2Instance(); explicit v2Instance(QWidget *parent);
bool start(QWidget *parent);
bool start();
void stop(); void stop();
void restart(); void restart();
QProcess *vProcess;
static bool checkVCoreExes();
static bool checkConfigFile(QString path);
~v2Instance(); ~v2Instance();
QProcess *vProcess;
private:
V2RAY_INSTANCE_STARTUP_STATUS processStatus;
}; };
#endif // VINTERACT_H #endif // VINTERACT_H

View File

@ -1,5 +1,5 @@
#include "w_ConnectionEditWindow.h" #include "w_ConnectionEditWindow.h"
#include "ui_ConnectionEditWindow.h" #include "ui_w_ConnectionEditWindow.h"
#include <iostream> #include <iostream>
#include "w_MainWindow.h" #include "w_MainWindow.h"
#include <QDebug> #include <QDebug>

View File

@ -14,7 +14,8 @@
#include "vinteract.h" #include "vinteract.h"
#include "utils.h" #include "utils.h"
#include "w_ImportConfig.h" #include "w_ImportConfig.h"
#include "ui_ImportConfig.h" #include "ui_w_ImportConfig.h"
ImportConfig::ImportConfig(QWidget *parent) : ImportConfig::ImportConfig(QWidget *parent) :
QDialog(parent), QDialog(parent),
@ -87,7 +88,7 @@ void ImportConfig::on_buttonBox_accepted()
if(ui->importSourceCombo->currentIndex() == 0) // From File... if(ui->importSourceCombo->currentIndex() == 0) // From File...
{ {
QString path = ui->fileLineTxt->text(); QString path = ui->fileLineTxt->text();
bool isValid = validationCheck(path); bool isValid = v2Instance::checkConfigFile(path);
if(isValid) { if(isValid) {
savefromFile(path, alias); savefromFile(path, alias);
} }
@ -109,7 +110,7 @@ void ImportConfig::on_buttonBox_accepted()
Py_Finalize(); Py_Finalize();
if(QFile::exists(QCoreApplication::applicationDirPath() + "/config.json.tmp")) { if(QFile::exists(QCoreApplication::applicationDirPath() + "/config.json.tmp")) {
ImportConfig *im = new ImportConfig(this->parentWidget()); 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); im->savefromFile("config.json.tmp", alias);
} }
QFile::remove("config.json.tmp"); QFile::remove("config.json.tmp");

View File

@ -8,7 +8,7 @@
#include <QFileInfo> #include <QFileInfo>
#include <QInputDialog> #include <QInputDialog>
#include "ui_MainWindow.h" #include "ui_w_MainWindow.h"
#include "w_PrefrencesWindow.h" #include "w_PrefrencesWindow.h"
#include "w_MainWindow.h" #include "w_MainWindow.h"
#include "w_ConnectionEditWindow.h" #include "w_ConnectionEditWindow.h"
@ -64,10 +64,10 @@ MainWindow::MainWindow(QWidget *parent) :
UpdateConfigTable(); UpdateConfigTable();
// ui->configTable->setContextMenuPolicy(Qt::CustomContextMenu); // ui->configTable->setContextMenuPolicy(Qt::CustomContextMenu);
// connect(ui->configTable, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(showMenu(QPoint))); // connect(ui->configTable, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(showMenu(QPoint)));
this->v2instance = new v2Instance(); this->vinstance = new v2Instance(this);
CreateTrayIcon(); CreateTrayIcon();
if(QFileInfo("config.json").exists()) { if(QFileInfo("config.json").exists()) {
v2instance->start(this); vinstance->start();
} }
// QAction *select = new QAction("Select", ui->configTable); // QAction *select = new QAction("Select", ui->configTable);
@ -87,7 +87,7 @@ MainWindow::~MainWindow()
{ {
hTray->hide(); hTray->hide();
delete this->hTray; delete this->hTray;
delete this->v2instance; delete this->vinstance;
delete ui; delete ui;
} }
@ -107,6 +107,7 @@ void MainWindow::on_actionExisting_config_triggered()
void MainWindow::showMenu(QPoint pos) void MainWindow::showMenu(QPoint pos)
{ {
Q_UNUSED(pos)
// if(ui->configTable->indexAt(pos).column() != -1) { // if(ui->configTable->indexAt(pos).column() != -1) {
// popMenu->move(cursor().pos()); // popMenu->move(cursor().pos());
// popMenu->show(); // popMenu->show();
@ -147,13 +148,13 @@ void MainWindow::GenerateConfig(int idIntable)
} }
void MainWindow::UpdateLog() void MainWindow::UpdateLog()
{ {
ui->logText->insertPlainText(this->v2instance->vProcess->readAllStandardOutput()); ui->logText->insertPlainText(this->vinstance->vProcess->readAllStandardOutput());
} }
void MainWindow::on_startButton_clicked() void MainWindow::on_startButton_clicked()
{ {
ui->logText->clear(); ui->logText->clear();
bool startFlag = this->v2instance->start(this); bool startFlag = this->vinstance->start();
trayMenu->actions()[2]->setEnabled(!startFlag); trayMenu->actions()[2]->setEnabled(!startFlag);
trayMenu->actions()[3]->setEnabled(startFlag); trayMenu->actions()[3]->setEnabled(startFlag);
trayMenu->actions()[4]->setEnabled(startFlag); trayMenu->actions()[4]->setEnabled(startFlag);
@ -161,7 +162,7 @@ void MainWindow::on_startButton_clicked()
void MainWindow::on_stopButton_clicked() void MainWindow::on_stopButton_clicked()
{ {
this->v2instance->stop(); this->vinstance->stop();
ui->logText->clear(); ui->logText->clear();
trayMenu->actions()[2]->setEnabled(true); trayMenu->actions()[2]->setEnabled(true);
trayMenu->actions()[3]->setEnabled(false); trayMenu->actions()[3]->setEnabled(false);
@ -209,7 +210,7 @@ void MainWindow::on_activatedTray(QSystemTrayIcon::ActivationReason reason)
case QSystemTrayIcon::MiddleClick: case QSystemTrayIcon::MiddleClick:
// TODO: Check if an alert message box is present. // TODO: Check if an alert message box is present.
// If so, do nothing but please wait for the message box to be closed. // 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(); on_stopButton_clicked();
} else { } else {
on_startButton_clicked(); on_startButton_clicked();

View File

@ -19,7 +19,7 @@ class MainWindow : public QMainWindow
public: public:
explicit MainWindow(QWidget *parent = nullptr); explicit MainWindow(QWidget *parent = nullptr);
v2Instance *v2instance; v2Instance *vinstance;
QSystemTrayIcon *hTray; QSystemTrayIcon *hTray;
QMenu *trayMenu = new QMenu(this); QMenu *trayMenu = new QMenu(this);
QMenu *popMenu = new QMenu(this); QMenu *popMenu = new QMenu(this);

View File

@ -143,6 +143,9 @@
<property name="fieldGrowthPolicy"> <property name="fieldGrowthPolicy">
<enum>QFormLayout::ExpandingFieldsGrow</enum> <enum>QFormLayout::ExpandingFieldsGrow</enum>
</property> </property>
<property name="verticalSpacing">
<number>10</number>
</property>
<item row="0" column="0"> <item row="0" column="0">
<widget class="QLabel" name="label"> <widget class="QLabel" name="label">
<property name="text"> <property name="text">
@ -241,7 +244,7 @@
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>568</width> <width>568</width>
<height>21</height> <height>29</height>
</rect> </rect>
</property> </property>
<widget class="QMenu" name="menuFile"> <widget class="QMenu" name="menuFile">

View File

@ -8,9 +8,10 @@
#include <QFileInfo> #include <QFileInfo>
#include <QProcess> #include <QProcess>
#include <unistd.h> #include <unistd.h>
#include <ui_PrefrencesWindow.h> #include <ui_w_PrefrencesWindow.h>
#include "utils.h" #include "utils.h"
#include "vinteract.h"
#include "w_PrefrencesWindow.h" #include "w_PrefrencesWindow.h"
PrefrencesWindow::PrefrencesWindow(QWidget *parent) : PrefrencesWindow::PrefrencesWindow(QWidget *parent) :
@ -48,7 +49,7 @@ PrefrencesWindow::~PrefrencesWindow()
void PrefrencesWindow::on_buttonBox_accepted() void PrefrencesWindow::on_buttonBox_accepted()
{ {
if(checkVCoreExes()) { if(v2Instance::checkVCoreExes()) {
if(ui->httpPortLE->text().toInt() != ui->socksPortLE->text().toInt()) { if(ui->httpPortLE->text().toInt() != ui->socksPortLE->text().toInt()) {
QJsonArray inbounds; QJsonArray inbounds;
QJsonDocument modifiedDoc; QJsonDocument modifiedDoc;