mirror of
https://github.com/Qv2ray/Qv2ray.git
synced 2025-05-20 19:00:22 +08:00
refactor: main.cpp refactor, fixed a crash
This commit is contained in:
parent
fce84ece23
commit
22e853f04c
@ -1 +1 @@
|
|||||||
5579
|
5580
|
||||||
|
@ -168,32 +168,30 @@ namespace Qv2ray
|
|||||||
bool Qv2rayApplication::CheckSettingsPathAvailability(const QString &_path, bool checkExistingConfig)
|
bool Qv2rayApplication::CheckSettingsPathAvailability(const QString &_path, bool checkExistingConfig)
|
||||||
{
|
{
|
||||||
auto path = _path;
|
auto path = _path;
|
||||||
|
|
||||||
if (!path.endsWith("/"))
|
if (!path.endsWith("/"))
|
||||||
{
|
|
||||||
path.append("/");
|
path.append("/");
|
||||||
}
|
|
||||||
// Does not exist.
|
// Does not exist.
|
||||||
if (!QDir(path).exists())
|
if (!QDir(path).exists())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
// A temp file used to test file permissions in that folder.
|
{
|
||||||
QFile testFile(path + ".qv2ray_file_write_test_file" + QSTRN(QTime::currentTime().msecsSinceStartOfDay()));
|
// A temp file used to test file permissions in that folder.
|
||||||
bool opened = testFile.open(QFile::OpenModeFlag::ReadWrite);
|
QFile testFile(path + ".qv2ray_test_file" + QSTRN(QTime::currentTime().msecsSinceStartOfDay()));
|
||||||
|
|
||||||
|
if (!testFile.open(QFile::OpenModeFlag::ReadWrite))
|
||||||
|
{
|
||||||
|
LOG(MODULE_SETTINGS, "Directory at: " + path + " cannot be used as a valid config file path.")
|
||||||
|
LOG(MODULE_SETTINGS, "---> Cannot create a new file or open a file for writing.")
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if (!opened)
|
|
||||||
{
|
|
||||||
LOG(MODULE_SETTINGS, "Directory at: " + path + " cannot be used as a valid config file path.")
|
|
||||||
LOG(MODULE_SETTINGS, "---> Cannot create a new file or open a file for writing.")
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
testFile.write("Qv2ray test file, feel free to remove.");
|
testFile.write("Qv2ray test file, feel free to remove.");
|
||||||
testFile.flush();
|
testFile.flush();
|
||||||
testFile.close();
|
testFile.close();
|
||||||
bool removed = testFile.remove();
|
|
||||||
|
|
||||||
if (!removed)
|
if (!testFile.remove())
|
||||||
{
|
{
|
||||||
// This is rare, as we can create a file but failed to remove it.
|
// This is rare, as we can create a file but failed to remove it.
|
||||||
LOG(MODULE_SETTINGS, "Directory at: " + path + " cannot be used as a valid config file path.")
|
LOG(MODULE_SETTINGS, "Directory at: " + path + " cannot be used as a valid config file path.")
|
||||||
@ -202,89 +200,64 @@ namespace Qv2ray
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (checkExistingConfig)
|
if (!checkExistingConfig)
|
||||||
{
|
|
||||||
// Check if an existing config is found.
|
|
||||||
QFile configFile(path + "Qv2ray.conf");
|
|
||||||
|
|
||||||
// No such config file.
|
|
||||||
if (!configFile.exists())
|
|
||||||
return false;
|
|
||||||
|
|
||||||
bool opened2 = configFile.open(QIODevice::ReadWrite);
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
if (opened2)
|
|
||||||
{
|
|
||||||
// Verify if the config can be loaded.
|
|
||||||
// Failed to parse if we changed the file structure...
|
|
||||||
// Original:
|
|
||||||
// -- auto conf =
|
|
||||||
// StructFromJsonString<Qv2rayConfig>(configFile.readAll());
|
|
||||||
//
|
|
||||||
// Verify JSON file format. (only) because this file version may
|
|
||||||
// not be upgraded and may contain unsupported structure.
|
|
||||||
auto err = VerifyJsonString(StringFromFile(configFile));
|
|
||||||
|
|
||||||
if (!err.isEmpty())
|
|
||||||
{
|
|
||||||
LOG(MODULE_INIT, "Json parse returns: " + err)
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// If the file format is valid.
|
|
||||||
auto conf = JsonFromString(StringFromFile(configFile));
|
|
||||||
LOG(MODULE_SETTINGS,
|
|
||||||
"Path: " + path + " contains a config file, in version " + conf["config_version"].toVariant().toString())
|
|
||||||
configFile.close();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
LOG(MODULE_SETTINGS, "File: " + configFile.fileName() + " cannot be opened!")
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (...)
|
|
||||||
{
|
|
||||||
LOG(MODULE_SETTINGS, "Exception raised when checking config: " + configFile.fileName())
|
|
||||||
// LOG(INIT, e->what())
|
|
||||||
QvMessageBoxWarn(nullptr, tr("Warning"), tr("Qv2ray cannot load the config file from here:") + NEWLINE + configFile.fileName());
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
|
// Just pass the test
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if an existing config is found.
|
||||||
|
QFile configFile(path + "Qv2ray.conf");
|
||||||
|
|
||||||
|
// No such config file.
|
||||||
|
if (!configFile.exists())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (!configFile.open(QIODevice::ReadWrite))
|
||||||
|
{
|
||||||
|
LOG(MODULE_SETTINGS, "File: " + configFile.fileName() + " cannot be opened!")
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto err = VerifyJsonString(StringFromFile(configFile));
|
||||||
|
if (!err.isEmpty())
|
||||||
|
{
|
||||||
|
LOG(MODULE_INIT, "Json parse returns: " + err)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the file format is valid.
|
||||||
|
const auto conf = JsonFromString(StringFromFile(configFile));
|
||||||
|
LOG(MODULE_SETTINGS, "Found a config file, v=" + conf["config_version"].toString() + " path=" + path)
|
||||||
|
configFile.close();
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Qv2rayApplication::PreInitilize(int argc, char *argv[])
|
bool Qv2rayApplication::PreInitilize(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
QCoreApplication consoleApp(argc, argv);
|
|
||||||
QString errorMessage;
|
QString errorMessage;
|
||||||
|
|
||||||
const auto &args = consoleApp.arguments();
|
|
||||||
Qv2rayProcessArgument.path = args.first();
|
|
||||||
Qv2rayProcessArgument.version = QV2RAY_VERSION_STRING;
|
|
||||||
Qv2rayProcessArgument.data = args.join(" ");
|
|
||||||
switch (ParseCommandLine(&errorMessage))
|
|
||||||
{
|
{
|
||||||
case QUIT:
|
QCoreApplication coreApp(argc, argv);
|
||||||
|
const auto &args = coreApp.arguments();
|
||||||
|
Qv2rayProcessArgument.path = args.first();
|
||||||
|
Qv2rayProcessArgument.version = QV2RAY_VERSION_STRING;
|
||||||
|
Qv2rayProcessArgument.data = args.join(" ");
|
||||||
|
switch (ParseCommandLine(&errorMessage))
|
||||||
{
|
{
|
||||||
return false;
|
case QUIT:
|
||||||
}
|
{
|
||||||
case ERROR:
|
return false;
|
||||||
{
|
}
|
||||||
LOG(MODULE_INIT, errorMessage)
|
case ERROR:
|
||||||
return false;
|
{
|
||||||
}
|
LOG(MODULE_INIT, errorMessage)
|
||||||
case CONTINUE:
|
return false;
|
||||||
{
|
}
|
||||||
break;
|
case CONTINUE:
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// noScaleFactors = disable HiDPI
|
// noScaleFactors = disable HiDPI
|
||||||
|
11
src/main.cpp
11
src/main.cpp
@ -47,7 +47,10 @@ int main(int argc, char *argv[])
|
|||||||
#endif
|
#endif
|
||||||
//
|
//
|
||||||
// parse the command line before starting as a Qt application
|
// parse the command line before starting as a Qt application
|
||||||
Qv2rayApplication::PreInitilize(argc, argv);
|
if (!Qv2rayApplication::PreInitilize(argc, argv))
|
||||||
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
Qv2rayApplication app(argc, argv);
|
Qv2rayApplication app(argc, argv);
|
||||||
if (app.SetupQv2ray())
|
if (app.SetupQv2ray())
|
||||||
{
|
{
|
||||||
@ -210,8 +213,8 @@ int main(int argc, char *argv[])
|
|||||||
{
|
{
|
||||||
// Initialise Connection Handler
|
// Initialise Connection Handler
|
||||||
PluginHost = new QvPluginHost();
|
PluginHost = new QvPluginHost();
|
||||||
ConnectionManager = new QvConfigHandler(qvApp);
|
ConnectionManager = new QvConfigHandler();
|
||||||
RouteManager = new RouteHandler(qvApp);
|
RouteManager = new RouteHandler();
|
||||||
|
|
||||||
#ifdef Q_OS_LINUX
|
#ifdef Q_OS_LINUX
|
||||||
qvApp->setFallbackSessionManagementEnabled(false);
|
qvApp->setFallbackSessionManagementEnabled(false);
|
||||||
@ -234,6 +237,8 @@ int main(int argc, char *argv[])
|
|||||||
signal(SIGUSR2, [](int) { ConnectionManager->StopConnection(); });
|
signal(SIGUSR2, [](int) { ConnectionManager->StopConnection(); });
|
||||||
#endif
|
#endif
|
||||||
auto rcode = app.exec();
|
auto rcode = app.exec();
|
||||||
|
delete ConnectionManager;
|
||||||
|
delete RouteManager;
|
||||||
delete PluginHost;
|
delete PluginHost;
|
||||||
LOG(MODULE_INIT, "Quitting normally")
|
LOG(MODULE_INIT, "Quitting normally")
|
||||||
return rcode;
|
return rcode;
|
||||||
|
Loading…
Reference in New Issue
Block a user