Files
amnezia-client/ipc/ipcserver.cpp
T

211 lines
4.3 KiB
C++
Raw Normal View History

2021-02-02 01:47:40 +03:00
#include "ipcserver.h"
#include <QDateTime>
#include <QFileInfo>
2024-09-09 20:53:44 +04:00
#include <QLocalSocket>
#include <QObject>
2021-02-02 01:47:40 +03:00
#include "logger.h"
2024-09-09 20:53:44 +04:00
#include "router.h"
2022-01-30 17:35:57 +03:00
2024-12-30 22:50:33 +02:00
#include "killswitch.h"
2021-02-18 15:00:41 +03:00
#ifdef Q_OS_WIN
2024-09-09 20:53:44 +04:00
#include "tapcontroller_win.h"
2023-12-16 09:19:04 -05:00
#endif
2023-12-23 12:51:55 +02:00
2024-09-09 20:53:44 +04:00
IpcServer::IpcServer(QObject *parent) : IpcInterfaceSource(parent)
2024-04-01 18:45:00 +07:00
2024-09-09 20:53:44 +04:00
{
}
2021-02-02 01:47:40 +03:00
int IpcServer::createPrivilegedProcess()
{
2023-08-08 16:41:00 -07:00
#ifdef MZ_DEBUG
qDebug() << "IpcServer::createPrivilegedProcess";
#endif
2021-02-02 01:47:40 +03:00
m_localpid++;
2021-02-18 15:00:41 +03:00
ProcessDescriptor pd(this);
2021-02-03 15:42:36 +03:00
pd.localServer->setSocketOptions(QLocalServer::WorldAccessOption);
if (!pd.localServer->listen(amnezia::getIpcProcessUrl(m_localpid))) {
qDebug() << QString("Unable to start the server: %1.").arg(pd.localServer->errorString());
return -1;
}
// Make sure any connections are handed to QtRO
QObject::connect(pd.localServer.data(), &QLocalServer::newConnection, this, [pd]() {
2022-01-30 17:35:57 +03:00
qDebug() << "IpcServer new connection";
2021-02-03 15:42:36 +03:00
if (pd.serverNode) {
pd.serverNode->addHostSideConnection(pd.localServer->nextPendingConnection());
pd.serverNode->enableRemoting(pd.ipcProcess.data());
}
});
2021-02-02 01:47:40 +03:00
2024-09-09 20:53:44 +04:00
QObject::connect(pd.serverNode.data(), &QRemoteObjectHost::error, this,
[pd](QRemoteObjectNode::ErrorCode errorCode) { qDebug() << "QRemoteObjectHost::error" << errorCode; });
2021-06-01 18:18:09 +03:00
2024-09-09 20:53:44 +04:00
QObject::connect(pd.serverNode.data(), &QRemoteObjectHost::destroyed, this, [pd]() { qDebug() << "QRemoteObjectHost::destroyed"; });
2021-06-01 18:18:09 +03:00
2021-02-02 01:47:40 +03:00
m_processes.insert(m_localpid, pd);
return m_localpid;
}
2021-02-18 15:00:41 +03:00
int IpcServer::routeAddList(const QString &gw, const QStringList &ips)
{
2023-08-08 16:41:00 -07:00
#ifdef MZ_DEBUG
qDebug() << "IpcServer::routeAddList";
#endif
2021-02-21 09:44:53 -08:00
return Router::routeAddList(gw, ips);
2021-02-18 15:00:41 +03:00
}
bool IpcServer::clearSavedRoutes()
{
2023-08-08 16:41:00 -07:00
#ifdef MZ_DEBUG
qDebug() << "IpcServer::clearSavedRoutes";
#endif
2021-02-21 09:44:53 -08:00
return Router::clearSavedRoutes();
2021-02-18 15:00:41 +03:00
}
2021-06-01 18:18:09 +03:00
bool IpcServer::routeDeleteList(const QString &gw, const QStringList &ips)
2021-02-18 15:00:41 +03:00
{
2023-08-08 16:41:00 -07:00
#ifdef MZ_DEBUG
qDebug() << "IpcServer::routeDeleteList";
#endif
2024-09-09 20:53:44 +04:00
return Router::routeDeleteList(gw, ips);
2021-02-18 15:00:41 +03:00
}
void IpcServer::flushDns()
{
2023-08-08 16:41:00 -07:00
#ifdef MZ_DEBUG
qDebug() << "IpcServer::flushDns";
#endif
2021-02-21 09:44:53 -08:00
return Router::flushDns();
2021-02-18 15:00:41 +03:00
}
2021-12-15 14:53:07 +03:00
void IpcServer::resetIpStack()
{
2023-08-08 16:41:00 -07:00
#ifdef MZ_DEBUG
qDebug() << "IpcServer::resetIpStack";
#endif
2021-12-15 14:53:07 +03:00
Router::resetIpStack();
}
2021-02-18 15:00:41 +03:00
bool IpcServer::checkAndInstallDriver()
{
2023-08-08 16:41:00 -07:00
#ifdef MZ_DEBUG
qDebug() << "IpcServer::checkAndInstallDriver";
#endif
2021-02-18 15:00:41 +03:00
#ifdef Q_OS_WIN
return TapController::checkAndSetup();
#else
return true;
#endif
}
QStringList IpcServer::getTapList()
{
2023-08-08 16:41:00 -07:00
#ifdef MZ_DEBUG
qDebug() << "IpcServer::getTapList";
#endif
2021-02-18 15:00:41 +03:00
#ifdef Q_OS_WIN
return TapController::getTapList();
#else
return QStringList();
#endif
}
2022-01-30 17:35:57 +03:00
void IpcServer::cleanUp()
{
2023-08-08 16:41:00 -07:00
#ifdef MZ_DEBUG
2022-01-30 17:35:57 +03:00
qDebug() << "IpcServer::cleanUp";
2023-08-08 16:41:00 -07:00
#endif
2024-09-09 20:53:44 +04:00
Logger::deInit();
Logger::cleanUp();
2022-01-30 17:35:57 +03:00
}
2024-09-09 20:53:44 +04:00
void IpcServer::clearLogs()
{
Logger::clearLogs(true);
}
2024-03-27 11:02:34 +00:00
bool IpcServer::createTun(const QString &dev, const QString &subnet)
{
return Router::createTun(dev, subnet);
}
bool IpcServer::deleteTun(const QString &dev)
{
return Router::deleteTun(dev);
}
2024-09-09 20:53:44 +04:00
bool IpcServer::updateResolvers(const QString &ifname, const QList<QHostAddress> &resolvers)
2024-03-27 11:02:34 +00:00
{
return Router::updateResolvers(ifname, resolvers);
}
void IpcServer::StartRoutingIpv6()
{
Router::StartRoutingIpv6();
}
void IpcServer::StopRoutingIpv6()
{
Router::StopRoutingIpv6();
}
2022-01-30 17:35:57 +03:00
void IpcServer::setLogsEnabled(bool enabled)
{
2023-08-08 16:41:00 -07:00
#ifdef MZ_DEBUG
qDebug() << "IpcServer::setLogsEnabled";
#endif
2022-01-30 17:35:57 +03:00
if (enabled) {
2024-09-09 20:53:44 +04:00
Logger::init(true);
} else {
Logger::deInit();
2022-01-30 17:35:57 +03:00
}
}
2024-12-31 19:33:12 +02:00
bool IpcServer::allowTrafficTo(QStringList ranges)
{
return KillSwitch::instance()->allowTrafficTo(ranges);
}
2024-12-30 22:50:33 +02:00
bool IpcServer::disableAllTraffic()
{
2024-12-30 22:50:33 +02:00
return KillSwitch::instance()->disableAllTraffic();
}
2023-12-23 12:51:55 +02:00
2024-12-30 22:50:33 +02:00
bool IpcServer::enableKillSwitch(const QJsonObject &configStr, int vpnAdapterIndex)
{
return KillSwitch::instance()->enableKillSwitch(configStr, vpnAdapterIndex);
}
bool IpcServer::disableKillSwitch()
{
2024-12-30 22:50:33 +02:00
return KillSwitch::instance()->disableKillSwitch();
}
bool IpcServer::enablePeerTraffic(const QJsonObject &configStr)
{
2024-12-30 22:50:33 +02:00
return KillSwitch::instance()->enablePeerTraffic(configStr);
}
2025-02-17 22:43:38 +02:00
2025-02-18 22:26:56 +02:00
bool IpcServer::refreshKillSwitch(bool enabled)
2025-02-17 22:43:38 +02:00
{
2025-02-18 22:26:56 +02:00
return KillSwitch::instance()->refresh(enabled);
2025-02-17 22:43:38 +02:00
}