mirror of
https://github.com/amnezia-vpn/amnezia-client.git
synced 2026-05-08 14:33:23 +00:00
* Add network status check for AWG/WG protocol * Use service for PingSender * Cleanup unused code * Use networkchecker for all protocols * fix android build * add delay for ping checker stop * handle for interafe problems on windows * Restart IpcClient after OS suspend * Add DBus network checker for Linux * Use ping check for tun interfce * Windows suspend mode handler * MacOS suspend mode handler draft * Add delay for Linux wakeup reconnect * Add delay for Linux wakeup reconnect * Fix macOS wakeup/sleep prob Fix macOS not receiving wakeup/sleep events * fix done * Update deploy.yml fix CICD * Update vpnconnection.cpp update fix build CICD * Update vpnconnection.cpp update fix build cicd macos * Update deploy.yml fix CICD build macos * Update deploy.yml fix CICD macos * feat: implement SCP write buffer, improve network check and refactor macOS OpenGL support * feat: add tunnel addresses updated signal and handle network check based on gateway and local address availability * refactor: improve IpcClient connection handling and instance management * fix: scp revert. * fix: cmake reverted. * fix: submodules updated --------- Co-authored-by: Mykola Baibuz <mykola.baibuz@gmail.com> Co-authored-by: Yaroslav Yashin <yaroslav.yashin@gmail.com> Co-authored-by: vkamn <vk@amnezia.org>
154 lines
4.8 KiB
C++
154 lines
4.8 KiB
C++
#include "ipcclient.h"
|
|
#include <QRemoteObjectNode>
|
|
|
|
IpcClient *IpcClient::m_instance = nullptr;
|
|
|
|
IpcClient::IpcClient(QObject *parent) : QObject(parent)
|
|
{
|
|
}
|
|
|
|
IpcClient::~IpcClient()
|
|
{
|
|
if (m_localSocket)
|
|
m_localSocket->close();
|
|
}
|
|
|
|
bool IpcClient::isSocketConnected() const
|
|
{
|
|
return m_isSocketConnected;
|
|
}
|
|
|
|
void IpcClient::closeAndResetInstance(bool deleteSelf)
|
|
{
|
|
if (m_localSocket)
|
|
{
|
|
m_localSocket->disconnectFromServer();
|
|
m_localSocket->deleteLater();
|
|
m_localSocket.clear();
|
|
}
|
|
m_ipcClient.reset();
|
|
m_Tun2SocksClient.reset();
|
|
m_isSocketConnected = false;
|
|
if (deleteSelf) {
|
|
m_instance = nullptr;
|
|
}
|
|
}
|
|
|
|
IpcClient *IpcClient::Instance()
|
|
{
|
|
return m_instance;
|
|
}
|
|
|
|
QSharedPointer<IpcInterfaceReplica> IpcClient::Interface()
|
|
{
|
|
if (!Instance())
|
|
return nullptr;
|
|
return Instance()->m_ipcClient;
|
|
}
|
|
|
|
QSharedPointer<IpcProcessTun2SocksReplica> IpcClient::InterfaceTun2Socks()
|
|
{
|
|
if (!Instance())
|
|
return nullptr;
|
|
return Instance()->m_Tun2SocksClient;
|
|
}
|
|
|
|
bool IpcClient::init(IpcClient *instance)
|
|
{
|
|
if (m_instance && m_instance != instance) {
|
|
m_instance->closeAndResetInstance(false);
|
|
m_instance->deleteLater();
|
|
}
|
|
m_instance = instance;
|
|
|
|
Instance()->m_localSocket = new QLocalSocket(Instance());
|
|
connect(Instance()->m_localSocket.data(), &QLocalSocket::connected, &Instance()->m_ClientNode, []() {
|
|
Instance()->m_ClientNode.addClientSideConnection(Instance()->m_localSocket.data());
|
|
auto cliNode = Instance()->m_ClientNode.acquire<IpcInterfaceReplica>();
|
|
cliNode->waitForSource(5000);
|
|
Instance()->m_ipcClient.reset(cliNode);
|
|
|
|
if (!Instance()->m_ipcClient) {
|
|
qWarning() << "IpcClient is not ready!";
|
|
}
|
|
|
|
Instance()->m_ipcClient->waitForSource(1000);
|
|
|
|
if (!Instance()->m_ipcClient->isReplicaValid()) {
|
|
qWarning() << "IpcClient replica is not connected!";
|
|
}
|
|
|
|
auto t2sNode = Instance()->m_ClientNode.acquire<IpcProcessTun2SocksReplica>();
|
|
t2sNode->waitForSource(5000);
|
|
Instance()->m_Tun2SocksClient.reset(t2sNode);
|
|
|
|
if (!Instance()->m_Tun2SocksClient) {
|
|
qWarning() << "IpcClient::m_Tun2SocksClient is not ready!";
|
|
}
|
|
|
|
Instance()->m_Tun2SocksClient->waitForSource(1000);
|
|
|
|
if (!Instance()->m_Tun2SocksClient->isReplicaValid()) {
|
|
qWarning() << "IpcClient::m_Tun2SocksClient replica is not connected!";
|
|
}
|
|
});
|
|
|
|
connect(Instance()->m_localSocket, &QLocalSocket::disconnected,
|
|
[instance]() { instance->m_isSocketConnected = false; });
|
|
|
|
Instance()->m_localSocket->connectToServer(amnezia::getIpcServiceUrl());
|
|
Instance()->m_localSocket->waitForConnected();
|
|
|
|
if (!Instance()->m_ipcClient) {
|
|
qDebug() << "IpcClient::init failed";
|
|
return false;
|
|
}
|
|
|
|
qDebug() << "IpcClient::init succeed";
|
|
instance->m_isSocketConnected = (Instance()->m_ipcClient->isReplicaValid() && Instance()->m_Tun2SocksClient->isReplicaValid());
|
|
|
|
return Instance()->isSocketConnected();
|
|
}
|
|
|
|
QSharedPointer<PrivilegedProcess> IpcClient::CreatePrivilegedProcess()
|
|
{
|
|
if (!Instance()->m_ipcClient || !Instance()->m_ipcClient->isReplicaValid()) {
|
|
qWarning() << "IpcClient::createPrivilegedProcess : IpcClient IpcClient replica is not valid";
|
|
return nullptr;
|
|
}
|
|
|
|
QRemoteObjectPendingReply<int> futureResult = Instance()->m_ipcClient->createPrivilegedProcess();
|
|
futureResult.waitForFinished(5000);
|
|
|
|
int pid = futureResult.returnValue();
|
|
|
|
auto pd = QSharedPointer<ProcessDescriptor>(new ProcessDescriptor());
|
|
Instance()->m_processNodes.insert(pid, pd);
|
|
|
|
pd->localSocket.reset(new QLocalSocket(pd->replicaNode.data()));
|
|
|
|
connect(pd->localSocket.data(), &QLocalSocket::connected, pd->replicaNode.data(), [pd]() {
|
|
pd->replicaNode->addClientSideConnection(pd->localSocket.data());
|
|
|
|
IpcProcessInterfaceReplica *repl = pd->replicaNode->acquire<IpcProcessInterfaceReplica>();
|
|
PrivilegedProcess *priv = static_cast<PrivilegedProcess *>(repl);
|
|
pd->ipcProcess.reset(priv);
|
|
if (!pd->ipcProcess) {
|
|
qWarning() << "Acquire PrivilegedProcess failed";
|
|
} else {
|
|
pd->ipcProcess->waitForSource(1000);
|
|
if (!pd->ipcProcess->isReplicaValid()) {
|
|
qWarning() << "PrivilegedProcess replica is not connected!";
|
|
}
|
|
|
|
QObject::connect(pd->ipcProcess.data(), &PrivilegedProcess::destroyed, pd->ipcProcess.data(),
|
|
[pd]() { pd->replicaNode->deleteLater(); });
|
|
}
|
|
});
|
|
pd->localSocket->connectToServer(amnezia::getIpcProcessUrl(pid));
|
|
pd->localSocket->waitForConnected();
|
|
|
|
auto processReplica = QSharedPointer<PrivilegedProcess>(pd->ipcProcess);
|
|
return processReplica;
|
|
}
|