mirror of
https://github.com/amnezia-vpn/amnezia-client.git
synced 2026-06-23 02:00:20 +07:00
feat: add dynamic local proxy lifecycle in depence of settings
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
#include "coreController.h"
|
#include "coreController.h"
|
||||||
|
|
||||||
#include <QDirIterator>
|
#include <QDirIterator>
|
||||||
|
#include <QDebug>
|
||||||
#include <QTranslator>
|
#include <QTranslator>
|
||||||
#include <QStandardPaths>
|
#include <QStandardPaths>
|
||||||
|
|
||||||
@@ -36,13 +37,39 @@ CoreController::CoreController(const QSharedPointer<VpnConnection> &vpnConnectio
|
|||||||
void CoreController::initLocalProxy()
|
void CoreController::initLocalProxy()
|
||||||
{
|
{
|
||||||
#if !defined(Q_OS_ANDROID) && !defined(Q_OS_IOS)
|
#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));
|
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
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -52,7 +52,6 @@
|
|||||||
|
|
||||||
#if !defined(Q_OS_ANDROID) && !defined(Q_OS_IOS)
|
#if !defined(Q_OS_ANDROID) && !defined(Q_OS_IOS)
|
||||||
#include "core/local-proxy/proxyserver.h"
|
#include "core/local-proxy/proxyserver.h"
|
||||||
#include "core/local-proxy/proxylogger.h"
|
|
||||||
#include "ui/notificationhandler.h"
|
#include "ui/notificationhandler.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
@@ -5,11 +5,19 @@
|
|||||||
#include <QDateTime>
|
#include <QDateTime>
|
||||||
#include <QDir>
|
#include <QDir>
|
||||||
#include <QFileInfo>
|
#include <QFileInfo>
|
||||||
|
#include <QDebug>
|
||||||
|
#include <QStandardPaths>
|
||||||
|
|
||||||
|
#include "proxylogger.h"
|
||||||
|
|
||||||
ProxyServer::ProxyServer(QObject *parent)
|
ProxyServer::ProxyServer(QObject *parent)
|
||||||
: QObject(parent)
|
: QObject(parent)
|
||||||
, m_service(new ProxyService(this))
|
, 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);
|
connect(m_service.data(), &ProxyService::configsChanged, this, &ProxyServer::startXrayProcess);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -20,10 +28,23 @@ ProxyServer::~ProxyServer()
|
|||||||
|
|
||||||
bool ProxyServer::start(quint16 port)
|
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()));
|
m_api.reset(new HttpApi(m_service.toWeakRef()));
|
||||||
bool apiStarted = m_api->start(port);
|
const bool apiStarted = m_api->start(port);
|
||||||
if (!apiStarted) {
|
if (!apiStarted) {
|
||||||
qWarning() << "Local proxy: port is busy:" << port;
|
qWarning() << "Local proxy: port is busy:" << port;
|
||||||
|
m_api.reset();
|
||||||
|
m_isRunning = false;
|
||||||
|
m_currentPort = 0;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -35,6 +56,9 @@ bool ProxyServer::start(quint16 port)
|
|||||||
qDebug() << "No config found, Xray will not start automatically";
|
qDebug() << "No config found, Xray will not start automatically";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
m_isRunning = true;
|
||||||
|
m_currentPort = port;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,7 +67,10 @@ void ProxyServer::stop()
|
|||||||
stopXrayProcess();
|
stopXrayProcess();
|
||||||
if (m_api) {
|
if (m_api) {
|
||||||
m_api->stop();
|
m_api->stop();
|
||||||
|
m_api.reset();
|
||||||
}
|
}
|
||||||
|
m_isRunning = false;
|
||||||
|
m_currentPort = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ProxyServer::startXrayProcess()
|
bool ProxyServer::startXrayProcess()
|
||||||
|
|||||||
@@ -23,4 +23,6 @@ private:
|
|||||||
|
|
||||||
QScopedPointer<HttpApi> m_api;
|
QScopedPointer<HttpApi> m_api;
|
||||||
QSharedPointer<ProxyService> m_service;
|
QSharedPointer<ProxyService> m_service;
|
||||||
|
bool m_isRunning {false};
|
||||||
|
quint16 m_currentPort {0};
|
||||||
};
|
};
|
||||||
@@ -114,9 +114,13 @@ PageType {
|
|||||||
PageController.showNotificationMessage(qsTr("Failed to enable local proxy. Check the port (%1-%2).")
|
PageController.showNotificationMessage(qsTr("Failed to enable local proxy. Check the port (%1-%2).")
|
||||||
.arg(root.localProxyPortMin)
|
.arg(root.localProxyPortMin)
|
||||||
.arg(root.localProxyPortMax))
|
.arg(root.localProxyPortMax))
|
||||||
|
localProxySwitch.syncState()
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
localProxySwitch.syncState()
|
||||||
} else {
|
} else {
|
||||||
SettingsController.disableLocalProxy()
|
SettingsController.disableLocalProxy()
|
||||||
|
localProxySwitch.syncState()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -192,16 +196,5 @@ PageType {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Connections {
|
|
||||||
target: ServersModel
|
|
||||||
|
|
||||||
function onProcessedServerChanged() {
|
|
||||||
localProxySwitch.syncState()
|
|
||||||
if (!portField.textField.activeFocus) {
|
|
||||||
portField.syncPortValue()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user