feat: add dynamic local proxy lifecycle in depence of settings

This commit is contained in:
aiamnezia
2025-12-31 18:48:57 +04:00
parent 5ab82e2196
commit 9740e7557a
5 changed files with 67 additions and 19 deletions

View File

@@ -1,6 +1,7 @@
#include "coreController.h"
#include <QDirIterator>
#include <QDebug>
#include <QTranslator>
#include <QStandardPaths>
@@ -36,13 +37,39 @@ CoreController::CoreController(const QSharedPointer<VpnConnection> &vpnConnectio
void CoreController::initLocalProxy()
{
#if !defined(Q_OS_ANDROID) && !defined(Q_OS_IOS)
// Logger and proxy initialization
ProxyLogger::getInstance().init(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/logs/proxy.log");
ProxyLogger::getInstance().setLogLevel(ProxyLogger::LogLevel::Info);
m_proxyServer.reset(new ProxyServer(this));
const quint16 proxyPort = 49490;
m_proxyServer->start(proxyPort);
auto syncLocalProxy = [this]() {
if (!m_proxyServer) {
return;
}
const bool httpEnabled = m_settings->isLocalProxyHttpEnabled();
const quint16 port = m_settings->localProxyPort();
if (!httpEnabled) {
qInfo() << "Local proxy: HTTP API disabled";
m_proxyServer->stop();
return;
}
if (port < 1024) {
qWarning() << "Local proxy: invalid HTTP API port" << port << ", stopping server";
m_proxyServer->stop();
return;
}
if (!m_proxyServer->start(port)) {
qWarning() << "Local proxy: failed to start on port" << port;
return;
}
qInfo() << "Local proxy: running on 127.0.0.1:" << port;
};
syncLocalProxy();
connect(m_settings.get(), &Settings::localProxySettingsChanged, this, syncLocalProxy);
#endif
}

View File

@@ -52,7 +52,6 @@
#if !defined(Q_OS_ANDROID) && !defined(Q_OS_IOS)
#include "core/local-proxy/proxyserver.h"
#include "core/local-proxy/proxylogger.h"
#include "ui/notificationhandler.h"
#endif

View File

@@ -5,11 +5,19 @@
#include <QDateTime>
#include <QDir>
#include <QFileInfo>
#include <QDebug>
#include <QStandardPaths>
#include "proxylogger.h"
ProxyServer::ProxyServer(QObject *parent)
: QObject(parent)
, m_service(new ProxyService(this))
{
const QString logDir = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/logs";
ProxyLogger::getInstance().init(logDir + "/proxy.log");
ProxyLogger::getInstance().setLogLevel(ProxyLogger::LogLevel::Info);
connect(m_service.data(), &ProxyService::configsChanged, this, &ProxyServer::startXrayProcess);
}
@@ -20,10 +28,23 @@ ProxyServer::~ProxyServer()
bool ProxyServer::start(quint16 port)
{
if (m_isRunning) {
if (m_currentPort == port) {
qInfo() << "Local proxy: already running on port" << port;
return true;
}
qInfo() << "Local proxy: restarting on new port" << port;
stop();
}
m_api.reset(new HttpApi(m_service.toWeakRef()));
bool apiStarted = m_api->start(port);
const bool apiStarted = m_api->start(port);
if (!apiStarted) {
qWarning() << "Local proxy: port is busy:" << port;
m_api.reset();
m_isRunning = false;
m_currentPort = 0;
return false;
}
@@ -35,6 +56,9 @@ bool ProxyServer::start(quint16 port)
qDebug() << "No config found, Xray will not start automatically";
}
m_isRunning = true;
m_currentPort = port;
return true;
}
@@ -43,7 +67,10 @@ void ProxyServer::stop()
stopXrayProcess();
if (m_api) {
m_api->stop();
m_api.reset();
}
m_isRunning = false;
m_currentPort = 0;
}
bool ProxyServer::startXrayProcess()

View File

@@ -23,4 +23,6 @@ private:
QScopedPointer<HttpApi> m_api;
QSharedPointer<ProxyService> m_service;
bool m_isRunning {false};
quint16 m_currentPort {0};
};

View File

@@ -114,9 +114,13 @@ PageType {
PageController.showNotificationMessage(qsTr("Failed to enable local proxy. Check the port (%1-%2).")
.arg(root.localProxyPortMin)
.arg(root.localProxyPortMax))
localProxySwitch.syncState()
return
}
localProxySwitch.syncState()
} else {
SettingsController.disableLocalProxy()
localProxySwitch.syncState()
}
}
}
@@ -192,16 +196,5 @@ PageType {
}
}
}
Connections {
target: ServersModel
function onProcessedServerChanged() {
localProxySwitch.syncState()
if (!portField.textField.activeFocus) {
portField.syncPortValue()
}
}
}
}