From 92aba4970552bebb6695c4a55b8c88468a934329 Mon Sep 17 00:00:00 2001 From: Yaroslav Gurov <31506978+ygurov@users.noreply.github.com> Date: Fri, 19 Dec 2025 15:44:42 +0100 Subject: [PATCH] fix: cannot connect to IPC on Windows (#2083) * fix: replace localsocket by QtRO-embedded one * fix: make IpcClient initialization lazy --- client/core/ipcclient.cpp | 41 ++++++++------------------------------- client/core/ipcclient.h | 24 ++++++++--------------- 2 files changed, 16 insertions(+), 49 deletions(-) diff --git a/client/core/ipcclient.cpp b/client/core/ipcclient.cpp index d9ab13f9b..4586b3522 100644 --- a/client/core/ipcclient.cpp +++ b/client/core/ipcclient.cpp @@ -3,41 +3,22 @@ #include #include -namespace -{ - thread_local IpcClient ipcClient; -} - IpcClient::IpcClient(QObject *parent) : QObject(parent) { - connect(&m_localSocket, &QLocalSocket::connected, this, [this]() { - m_ClientNode.reset(new QRemoteObjectNode); - m_ClientNode->addClientSideConnection(&m_localSocket); - m_ipcClient.reset(m_ClientNode->acquire()); - m_Tun2SocksClient.reset(m_ClientNode->acquire()); - m_isSocketConnected = true; - }); - - connect(&m_localSocket, &QLocalSocket::disconnected, this, [this]() { - m_ClientNode.clear(); - m_ipcClient.clear(); - m_Tun2SocksClient.clear(); - m_isSocketConnected = false; - }); + m_node.connectToNode(QUrl("local:" + amnezia::getIpcServiceUrl())); + m_interface.reset(m_node.acquire()); + m_tun2socks.reset(m_node.acquire()); } -IpcClient *IpcClient::Instance() +IpcClient& IpcClient::Instance() { - if (!ipcClient.m_isSocketConnected) { - ipcClient.establishConnection(); - } - - return &ipcClient; + thread_local IpcClient ipcClient; + return ipcClient; } QSharedPointer IpcClient::Interface() { - QSharedPointer rep = Instance()->m_ipcClient; + QSharedPointer rep = Instance().m_interface; if (rep.isNull()) { qCritical() << "IpcClient::Interface(): Failed to acquire replica"; return nullptr; @@ -54,7 +35,7 @@ QSharedPointer IpcClient::Interface() QSharedPointer IpcClient::InterfaceTun2Socks() { - QSharedPointer rep = Instance()->m_Tun2SocksClient; + QSharedPointer rep = Instance().m_tun2socks; if (rep.isNull()) { qCritical() << "IpcClient::InterfaceTun2Socks: Replica is undefined"; return nullptr; @@ -69,12 +50,6 @@ QSharedPointer IpcClient::InterfaceTun2Socks() return rep; } -bool IpcClient::establishConnection() -{ - m_localSocket.connectToServer(amnezia::getIpcServiceUrl()); - return m_localSocket.waitForConnected(); -} - QSharedPointer IpcClient::CreatePrivilegedProcess() { QSharedPointer rep = Interface(); diff --git a/client/core/ipcclient.h b/client/core/ipcclient.h index f797a3407..74f1bf29d 100644 --- a/client/core/ipcclient.h +++ b/client/core/ipcclient.h @@ -15,7 +15,7 @@ class IpcClient : public QObject public: explicit IpcClient(QObject *parent = nullptr); - static IpcClient *Instance(); + static IpcClient& Instance(); static QSharedPointer Interface(); static QSharedPointer InterfaceTun2Socks(); @@ -24,10 +24,10 @@ public: template static auto withInterface(Func func) { - QSharedPointer iface = Instance()->m_ipcClient; + QSharedPointer iface = Instance().m_interface; using ReturnType = decltype(func(std::declval>())); - if (iface.isNull() || !iface->isReplicaValid()) { + if (iface.isNull() || !iface->waitForSource(1000) || !iface->isReplicaValid()) { qWarning() << "IpcClient::withInterface(): Service is not running"; if constexpr (std::is_void_v) @@ -42,25 +42,19 @@ public: template static auto withInterface(OnSuccess onSuccess, OnFailure onFailure) { - QSharedPointer iface = Instance()->m_ipcClient; - - if (iface.isNull() || !iface->isReplicaValid()) { + QSharedPointer iface = Instance().m_interface; + if (iface.isNull() || !iface->waitForSource(1000) || !iface->isReplicaValid()) { return onFailure(); } return onSuccess(iface); } - - bool isSocketConnected() const; signals: private: - bool establishConnection(); - - QLocalSocket m_localSocket; - QSharedPointer m_ClientNode; - QSharedPointer m_ipcClient; - QSharedPointer m_Tun2SocksClient; + QRemoteObjectNode m_node; + QSharedPointer m_interface; + QSharedPointer m_tun2socks; struct ProcessDescriptor { ProcessDescriptor () { @@ -72,8 +66,6 @@ private: QSharedPointer replicaNode; QSharedPointer localSocket; }; - - bool m_isSocketConnected {false}; }; #endif // IPCCLIENT_H