mirror of
https://github.com/amnezia-vpn/amnezia-client.git
synced 2026-06-22 02:01:08 +07:00
90 lines
2.0 KiB
C++
90 lines
2.0 KiB
C++
#include "proxyserver.h"
|
|
#include "settings.h"
|
|
|
|
#include <QDebug>
|
|
|
|
ProxyServer::ProxyServer(const std::shared_ptr<Settings> &settings, QObject *parent)
|
|
: QObject(parent)
|
|
, m_settings(settings)
|
|
, m_service(new ProxyService(settings, this))
|
|
{
|
|
}
|
|
|
|
ProxyServer::~ProxyServer()
|
|
{
|
|
stop();
|
|
}
|
|
|
|
bool ProxyServer::start(quint16 port)
|
|
{
|
|
if (m_isRunning) {
|
|
if (m_currentApiPort == 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()));
|
|
const bool apiStarted = m_api->start(port);
|
|
if (!apiStarted) {
|
|
qWarning() << "Local proxy: port is busy:" << port;
|
|
m_api.reset();
|
|
m_isRunning = false;
|
|
m_currentApiPort = 0;
|
|
return false;
|
|
}
|
|
|
|
m_isRunning = true;
|
|
m_currentApiPort = port;
|
|
|
|
return true;
|
|
}
|
|
|
|
void ProxyServer::stop()
|
|
{
|
|
stopXrayProcess();
|
|
if (m_api) {
|
|
m_api->stop();
|
|
m_api.reset();
|
|
}
|
|
m_isRunning = false;
|
|
m_currentApiPort = 0;
|
|
m_currentProxyPort = 0;
|
|
}
|
|
|
|
bool ProxyServer::startXrayProcess()
|
|
{
|
|
return m_service->startXray();
|
|
}
|
|
|
|
void ProxyServer::stopXrayProcess()
|
|
{
|
|
m_service->stopXray();
|
|
}
|
|
|
|
void ProxyServer::syncSettings()
|
|
{
|
|
if (!m_isRunning) {
|
|
qDebug() << "Local proxy: syncSettings called but server is not running";
|
|
return;
|
|
}
|
|
|
|
const quint16 newProxyPort = m_settings ? m_settings->localProxyPort() : 0;
|
|
const bool xrayRunning = m_service->isXrayRunning();
|
|
|
|
if (!xrayRunning) {
|
|
qInfo() << "Local proxy: starting Xray on port" << newProxyPort;
|
|
m_currentProxyPort = newProxyPort;
|
|
startXrayProcess();
|
|
return;
|
|
}
|
|
|
|
if (m_currentProxyPort != newProxyPort) {
|
|
qInfo() << "Local proxy: proxy port changed from" << m_currentProxyPort << "to" << newProxyPort;
|
|
m_currentProxyPort = newProxyPort;
|
|
m_service->restartXray();
|
|
}
|
|
} |