add logs and relevant errors

This commit is contained in:
aiamnezia 2024-12-09 13:09:12 +04:00
parent 525685b613
commit a86b0872c7
2 changed files with 41 additions and 21 deletions

View file

@ -4,12 +4,16 @@
#include <QJsonDocument> #include <QJsonDocument>
#include <QJsonObject> #include <QJsonObject>
#include <QUuid> #include <QUuid>
#include <qlogging.h> #include "logger.h"
#include "containers/containers_defs.h" #include "containers/containers_defs.h"
#include "core/controllers/serverController.h" #include "core/controllers/serverController.h"
#include "core/scripts_registry.h" #include "core/scripts_registry.h"
namespace {
Logger logger("XrayConfigurator");
}
XrayConfigurator::XrayConfigurator(std::shared_ptr<Settings> settings, const QSharedPointer<ServerController> &serverController, QObject *parent) XrayConfigurator::XrayConfigurator(std::shared_ptr<Settings> settings, const QSharedPointer<ServerController> &serverController, QObject *parent)
: ConfiguratorBase(settings, serverController, parent) : ConfiguratorBase(settings, serverController, parent)
{ {
@ -26,12 +30,14 @@ QString XrayConfigurator::prepareServerConfig(const ServerCredentials &credentia
container, credentials, amnezia::protocols::xray::serverConfigPath, errorCode); container, credentials, amnezia::protocols::xray::serverConfigPath, errorCode);
if (errorCode != ErrorCode::NoError) { if (errorCode != ErrorCode::NoError) {
logger.error() << "Failed to get server config file";
return ""; return "";
} }
// Parse current config as JSON // Parse current config as JSON
QJsonDocument doc = QJsonDocument::fromJson(currentConfig.toUtf8()); QJsonDocument doc = QJsonDocument::fromJson(currentConfig.toUtf8());
if (doc.isNull() || !doc.isObject()) { if (doc.isNull() || !doc.isObject()) {
logger.error() << "Failed to parse server config JSON";
errorCode = ErrorCode::InternalError; errorCode = ErrorCode::InternalError;
return ""; return "";
} }
@ -40,24 +46,28 @@ QString XrayConfigurator::prepareServerConfig(const ServerCredentials &credentia
// Validate server config structure // Validate server config structure
if (!serverConfig.contains("inbounds")) { if (!serverConfig.contains("inbounds")) {
logger.error() << "Server config missing 'inbounds' field";
errorCode = ErrorCode::InternalError; errorCode = ErrorCode::InternalError;
return ""; return "";
} }
QJsonArray inbounds = serverConfig["inbounds"].toArray(); QJsonArray inbounds = serverConfig["inbounds"].toArray();
if (inbounds.isEmpty()) { if (inbounds.isEmpty()) {
logger.error() << "Server config has empty 'inbounds' array";
errorCode = ErrorCode::InternalError; errorCode = ErrorCode::InternalError;
return ""; return "";
} }
QJsonObject inbound = inbounds[0].toObject(); QJsonObject inbound = inbounds[0].toObject();
if (!inbound.contains("settings")) { if (!inbound.contains("settings")) {
logger.error() << "Inbound missing 'settings' field";
errorCode = ErrorCode::InternalError; errorCode = ErrorCode::InternalError;
return ""; return "";
} }
QJsonObject settings = inbound["settings"].toObject(); QJsonObject settings = inbound["settings"].toObject();
if (!settings.contains("clients")) { if (!settings.contains("clients")) {
logger.error() << "Settings missing 'clients' field";
errorCode = ErrorCode::InternalError; errorCode = ErrorCode::InternalError;
return ""; return "";
} }
@ -88,6 +98,7 @@ QString XrayConfigurator::prepareServerConfig(const ServerCredentials &credentia
libssh::ScpOverwriteMode::ScpOverwriteExisting libssh::ScpOverwriteMode::ScpOverwriteExisting
); );
if (errorCode != ErrorCode::NoError) { if (errorCode != ErrorCode::NoError) {
logger.error() << "Failed to upload updated config";
return ""; return "";
} }
@ -99,6 +110,7 @@ QString XrayConfigurator::prepareServerConfig(const ServerCredentials &credentia
); );
if (errorCode != ErrorCode::NoError) { if (errorCode != ErrorCode::NoError) {
logger.error() << "Failed to restart container";
return ""; return "";
} }
@ -111,6 +123,7 @@ QString XrayConfigurator::createConfig(const ServerCredentials &credentials, Doc
// Get client ID from prepareServerConfig // Get client ID from prepareServerConfig
QString xrayClientId = prepareServerConfig(credentials, container, containerConfig, errorCode); QString xrayClientId = prepareServerConfig(credentials, container, containerConfig, errorCode);
if (errorCode != ErrorCode::NoError || xrayClientId.isEmpty()) { if (errorCode != ErrorCode::NoError || xrayClientId.isEmpty()) {
logger.error() << "Failed to prepare server config";
errorCode = ErrorCode::InternalError; errorCode = ErrorCode::InternalError;
return ""; return "";
} }
@ -119,6 +132,7 @@ QString XrayConfigurator::createConfig(const ServerCredentials &credentials, Doc
m_serverController->genVarsForScript(credentials, container, containerConfig)); m_serverController->genVarsForScript(credentials, container, containerConfig));
if (config.isEmpty()) { if (config.isEmpty()) {
logger.error() << "Failed to get config template";
errorCode = ErrorCode::InternalError; errorCode = ErrorCode::InternalError;
return ""; return "";
} }
@ -126,6 +140,7 @@ QString XrayConfigurator::createConfig(const ServerCredentials &credentials, Doc
QString xrayPublicKey = QString xrayPublicKey =
m_serverController->getTextFileFromContainer(container, credentials, amnezia::protocols::xray::PublicKeyPath, errorCode); m_serverController->getTextFileFromContainer(container, credentials, amnezia::protocols::xray::PublicKeyPath, errorCode);
if (errorCode != ErrorCode::NoError || xrayPublicKey.isEmpty()) { if (errorCode != ErrorCode::NoError || xrayPublicKey.isEmpty()) {
logger.error() << "Failed to get public key";
errorCode = ErrorCode::InternalError; errorCode = ErrorCode::InternalError;
return ""; return "";
} }
@ -134,6 +149,7 @@ QString XrayConfigurator::createConfig(const ServerCredentials &credentials, Doc
QString xrayShortId = QString xrayShortId =
m_serverController->getTextFileFromContainer(container, credentials, amnezia::protocols::xray::shortidPath, errorCode); m_serverController->getTextFileFromContainer(container, credentials, amnezia::protocols::xray::shortidPath, errorCode);
if (errorCode != ErrorCode::NoError || xrayShortId.isEmpty()) { if (errorCode != ErrorCode::NoError || xrayShortId.isEmpty()) {
logger.error() << "Failed to get short ID";
errorCode = ErrorCode::InternalError; errorCode = ErrorCode::InternalError;
return ""; return "";
} }
@ -141,6 +157,10 @@ QString XrayConfigurator::createConfig(const ServerCredentials &credentials, Doc
// Validate all required variables are present // Validate all required variables are present
if (!config.contains("$XRAY_CLIENT_ID") || !config.contains("$XRAY_PUBLIC_KEY") || !config.contains("$XRAY_SHORT_ID")) { if (!config.contains("$XRAY_CLIENT_ID") || !config.contains("$XRAY_PUBLIC_KEY") || !config.contains("$XRAY_SHORT_ID")) {
logger.error() << "Config template missing required variables:"
<< "XRAY_CLIENT_ID:" << !config.contains("$XRAY_CLIENT_ID")
<< "XRAY_PUBLIC_KEY:" << !config.contains("$XRAY_PUBLIC_KEY")
<< "XRAY_SHORT_ID:" << !config.contains("$XRAY_SHORT_ID");
errorCode = ErrorCode::InternalError; errorCode = ErrorCode::InternalError;
return ""; return "";
} }

View file

@ -554,8 +554,8 @@ ErrorCode ClientManagementModel::revokeClient(const int row, const DockerContain
break; break;
} }
default: { default: {
logger.warning() << "Unknown container type was received"; logger.error() << "Internal error: received unexpected container type";
break; return ErrorCode::InternalError;
} }
} }
@ -610,8 +610,8 @@ ErrorCode ClientManagementModel::revokeClient(const QJsonObject &containerConfig
break; break;
} }
default: { default: {
logger.warning() << "Unknown container type was received"; logger.error() << "Internal error: received unexpected container type";
return ErrorCode::NoError; return ErrorCode::InternalError;
} }
} }
@ -686,8 +686,8 @@ ErrorCode ClientManagementModel::revokeClient(const QJsonObject &containerConfig
break; break;
} }
default: default:
logger.warning() << "Unknown container type was received"; logger.error() << "Internal error: received unexpected container type";
break; return ErrorCode::InternalError;
} }
return errorCode; return errorCode;