mirror of
https://github.com/amnezia-vpn/amnezia-client.git
synced 2026-06-24 02:00:24 +07:00
feat: enhance local proxy error handling and configuration management
- Added signal for local proxy start failure to Settings. - Updated CoreController to emit failure signals with descriptive messages when local proxy fails to start. - Refactored XrayController to accept JSON configuration directly, improving configuration handling. - Removed unused config file loading logic to streamline the XrayController's functionality.
This commit is contained in:
@@ -56,11 +56,15 @@ void CoreController::initLocalProxy()
|
|||||||
if (port < 1024) {
|
if (port < 1024) {
|
||||||
qWarning() << "Local proxy: invalid HTTP API port" << port << ", stopping server";
|
qWarning() << "Local proxy: invalid HTTP API port" << port << ", stopping server";
|
||||||
m_proxyServer->stop();
|
m_proxyServer->stop();
|
||||||
|
m_settings->setLocalProxyHttpEnabled(false);
|
||||||
|
emit m_settings->localProxyStartFailed(tr("Local proxy disabled: invalid HTTP API port."));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!m_proxyServer->start(port)) {
|
if (!m_proxyServer->start(port)) {
|
||||||
qWarning() << "Local proxy: failed to start on port" << port;
|
qWarning() << "Local proxy: failed to start on port" << port;
|
||||||
|
m_settings->setLocalProxyHttpEnabled(false);
|
||||||
|
emit m_settings->localProxyStartFailed(tr("Local proxy failed to start. Check if the port is available."));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -44,24 +44,14 @@ bool ProxyService::startXray()
|
|||||||
|
|
||||||
QString error;
|
QString error;
|
||||||
const auto configData = m_configManager->buildConfig(error);
|
const auto configData = m_configManager->buildConfig(error);
|
||||||
qDebug() << "configData:" << configData.value().serializedConfig;
|
|
||||||
qDebug() << "configData:" << configData.value().parsedConfig;
|
|
||||||
if (!configData) {
|
if (!configData) {
|
||||||
logConfigError(error);
|
logConfigError(error);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
QString configPath;
|
|
||||||
if (!m_configManager->writeTempConfig(configData->serializedConfig, configPath, error)) {
|
|
||||||
qDebug() << "configPath:" << configPath;
|
|
||||||
logConfigError(error);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
qDebug() << "configPath:" << configPath;
|
|
||||||
|
|
||||||
m_cachedConfig = configData->parsedConfig;
|
m_cachedConfig = configData->parsedConfig;
|
||||||
|
|
||||||
const bool success = m_xrayController->start(configPath);
|
const bool success = m_xrayController->start(configData->serializedConfig);
|
||||||
if (success) {
|
if (success) {
|
||||||
ProxyLogger::getInstance().info("Xray started successfully");
|
ProxyLogger::getInstance().info("Xray started successfully");
|
||||||
emit xrayStatusChanged(true);
|
emit xrayStatusChanged(true);
|
||||||
@@ -78,7 +68,6 @@ bool ProxyService::stopXray()
|
|||||||
const bool stopped = m_xrayController->stop();
|
const bool stopped = m_xrayController->stop();
|
||||||
if (stopped) {
|
if (stopped) {
|
||||||
ProxyLogger::getInstance().info("Xray stopped");
|
ProxyLogger::getInstance().info("Xray stopped");
|
||||||
m_configManager->removeTempConfig();
|
|
||||||
emit xrayStatusChanged(false);
|
emit xrayStatusChanged(false);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,8 +3,6 @@
|
|||||||
#include "proxylogger.h"
|
#include "proxylogger.h"
|
||||||
#include "core/ipcclient.h"
|
#include "core/ipcclient.h"
|
||||||
|
|
||||||
#include <QFile>
|
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
const QString kIpcUnavailableError = QStringLiteral("Failed to communicate with IPC service");
|
const QString kIpcUnavailableError = QStringLiteral("Failed to communicate with IPC service");
|
||||||
}
|
}
|
||||||
@@ -20,9 +18,8 @@ XrayController::~XrayController()
|
|||||||
stop();
|
stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool XrayController::start(const QString &configPath)
|
bool XrayController::start(const QString &configJson)
|
||||||
{
|
{
|
||||||
|
|
||||||
if (m_isRunning) {
|
if (m_isRunning) {
|
||||||
ProxyLogger::getInstance().info("Xray is already running");
|
ProxyLogger::getInstance().info("Xray is already running");
|
||||||
return true;
|
return true;
|
||||||
@@ -32,13 +29,14 @@ bool XrayController::start(const QString &configPath)
|
|||||||
|
|
||||||
m_lastError.clear();
|
m_lastError.clear();
|
||||||
|
|
||||||
QString configContent;
|
if (configJson.trimmed().isEmpty()) {
|
||||||
if (!loadConfigFile(configPath, configContent)) {
|
m_lastError = QStringLiteral("Config content is empty");
|
||||||
|
ProxyLogger::getInstance().error(m_lastError);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const bool ipcResult = IpcClient::withInterface([&](QSharedPointer<IpcInterfaceReplica> iface) {
|
const bool ipcResult = IpcClient::withInterface([&](QSharedPointer<IpcInterfaceReplica> iface) {
|
||||||
iface->xrayStart(configContent);
|
iface->xrayStart(configJson);
|
||||||
return true;
|
return true;
|
||||||
}, []() {
|
}, []() {
|
||||||
return false;
|
return false;
|
||||||
@@ -90,38 +88,3 @@ QString XrayController::getError() const
|
|||||||
{
|
{
|
||||||
return m_lastError;
|
return m_lastError;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool XrayController::loadConfigFile(const QString &configPath, QString &configContent)
|
|
||||||
{
|
|
||||||
if (configPath.isEmpty()) {
|
|
||||||
m_lastError = "Config path is empty";
|
|
||||||
ProxyLogger::getInstance().error(m_lastError);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
QFile configFile(configPath);
|
|
||||||
if (!configFile.exists()) {
|
|
||||||
m_lastError = QString("Config file not found at: %1").arg(configPath);
|
|
||||||
ProxyLogger::getInstance().error(m_lastError);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!configFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
|
||||||
m_lastError = QString("Failed to open config file: %1").arg(configFile.errorString());
|
|
||||||
ProxyLogger::getInstance().error(m_lastError);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
QByteArray data = configFile.readAll();
|
|
||||||
configFile.close();
|
|
||||||
|
|
||||||
if (data.isEmpty()) {
|
|
||||||
m_lastError = QString("Config file is empty: %1").arg(configPath);
|
|
||||||
ProxyLogger::getInstance().error(m_lastError);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
configContent = QString::fromUtf8(data);
|
|
||||||
ProxyLogger::getInstance().debug(QString("Loaded Xray config from %1").arg(configPath));
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
@@ -10,15 +10,13 @@ public:
|
|||||||
explicit XrayController(QObject* parent = nullptr);
|
explicit XrayController(QObject* parent = nullptr);
|
||||||
~XrayController();
|
~XrayController();
|
||||||
|
|
||||||
bool start(const QString& configPath);
|
bool start(const QString& configJson);
|
||||||
bool stop();
|
bool stop();
|
||||||
bool isXrayRunning() const;
|
bool isXrayRunning() const;
|
||||||
qint64 getProcessId() const;
|
qint64 getProcessId() const;
|
||||||
QString getError() const;
|
QString getError() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool loadConfigFile(const QString& configPath, QString& configContent);
|
|
||||||
|
|
||||||
bool m_isRunning {false};
|
bool m_isRunning {false};
|
||||||
QString m_lastError;
|
QString m_lastError;
|
||||||
};
|
};
|
||||||
@@ -253,6 +253,7 @@ signals:
|
|||||||
void serverRemoved(int serverIndex);
|
void serverRemoved(int serverIndex);
|
||||||
void settingsCleared();
|
void settingsCleared();
|
||||||
void localProxySettingsChanged();
|
void localProxySettingsChanged();
|
||||||
|
void localProxyStartFailed(const QString &message);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QVariant value(const QString &key, const QVariant &defaultValue = QVariant()) const;
|
QVariant value(const QString &key, const QVariant &defaultValue = QVariant()) const;
|
||||||
|
|||||||
@@ -56,6 +56,7 @@ SettingsController::SettingsController(const QSharedPointer<ServersModel> &serve
|
|||||||
toggleDevGatewayEnv(m_isDevModeEnabled);
|
toggleDevGatewayEnv(m_isDevModeEnabled);
|
||||||
|
|
||||||
connect(m_settings.get(), &Settings::localProxySettingsChanged, this, &SettingsController::localProxySettingsUpdated);
|
connect(m_settings.get(), &Settings::localProxySettingsChanged, this, &SettingsController::localProxySettingsUpdated);
|
||||||
|
connect(m_settings.get(), &Settings::localProxyStartFailed, this, &SettingsController::localProxyStartFailed);
|
||||||
}
|
}
|
||||||
|
|
||||||
QString getPlatformName()
|
QString getPlatformName()
|
||||||
|
|||||||
@@ -153,6 +153,7 @@ signals:
|
|||||||
void isHomeAdLabelVisibleChanged(bool visible);
|
void isHomeAdLabelVisibleChanged(bool visible);
|
||||||
void startMinimizedChanged();
|
void startMinimizedChanged();
|
||||||
void localProxySettingsUpdated();
|
void localProxySettingsUpdated();
|
||||||
|
void localProxyStartFailed(const QString &message);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QSharedPointer<ServersModel> m_serversModel;
|
QSharedPointer<ServersModel> m_serversModel;
|
||||||
|
|||||||
Reference in New Issue
Block a user