2026-04-30 14:53:03 +08:00
|
|
|
#include "ipcClient.h"
|
2025-12-16 16:05:31 +01:00
|
|
|
#include "ipc.h"
|
2021-02-02 01:47:40 +03:00
|
|
|
#include <QRemoteObjectNode>
|
2025-12-16 16:05:31 +01:00
|
|
|
#include <QtNetwork/qlocalsocket.h>
|
2021-10-26 12:59:20 +03:00
|
|
|
|
|
|
|
|
IpcClient::IpcClient(QObject *parent) : QObject(parent)
|
|
|
|
|
{
|
2025-12-19 15:44:42 +01:00
|
|
|
m_node.connectToNode(QUrl("local:" + amnezia::getIpcServiceUrl()));
|
|
|
|
|
m_interface.reset(m_node.acquire<IpcInterfaceReplica>());
|
2021-02-02 01:47:40 +03:00
|
|
|
}
|
|
|
|
|
|
2025-12-19 15:44:42 +01:00
|
|
|
IpcClient& IpcClient::Instance()
|
2025-12-02 11:46:24 +07:00
|
|
|
{
|
2025-12-19 15:44:42 +01:00
|
|
|
thread_local IpcClient ipcClient;
|
|
|
|
|
return ipcClient;
|
2021-10-26 12:59:20 +03:00
|
|
|
}
|
2021-02-18 15:00:41 +03:00
|
|
|
|
2021-10-26 12:59:20 +03:00
|
|
|
QSharedPointer<IpcInterfaceReplica> IpcClient::Interface()
|
|
|
|
|
{
|
2025-12-19 15:44:42 +01:00
|
|
|
QSharedPointer<IpcInterfaceReplica> rep = Instance().m_interface;
|
2025-12-18 13:18:11 +01:00
|
|
|
if (rep.isNull()) {
|
|
|
|
|
qCritical() << "IpcClient::Interface(): Failed to acquire replica";
|
2025-02-08 20:30:54 +01:00
|
|
|
return nullptr;
|
2025-12-16 16:05:31 +01:00
|
|
|
}
|
|
|
|
|
if (!rep->waitForSource(1000)) {
|
|
|
|
|
qCritical() << "IpcClient::Interface(): Failed to initialize replica";
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
if (!rep->isReplicaValid()) {
|
|
|
|
|
qWarning() << "IpcClient::Interface(): Replica is invalid";
|
|
|
|
|
}
|
|
|
|
|
return rep;
|
2021-10-26 12:59:20 +03:00
|
|
|
}
|
|
|
|
|
|
2026-02-11 16:47:28 +01:00
|
|
|
QSharedPointer<IpcProcessInterfaceReplica> IpcClient::CreatePrivilegedProcess()
|
2024-09-20 04:12:22 -07:00
|
|
|
{
|
2026-02-11 16:47:28 +01:00
|
|
|
return withInterface([](QSharedPointer<IpcInterfaceReplica> &iface) -> QSharedPointer<IpcProcessInterfaceReplica> {
|
|
|
|
|
auto createPrivilegedProcess = iface->createPrivilegedProcess();
|
|
|
|
|
if (!createPrivilegedProcess.waitForFinished()) {
|
|
|
|
|
qCritical() << "Failed to create privileged process";
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2021-02-03 15:42:36 +03:00
|
|
|
|
2026-02-11 16:47:28 +01:00
|
|
|
const int pid = createPrivilegedProcess.returnValue();
|
2021-02-03 15:42:36 +03:00
|
|
|
|
2026-02-11 16:47:28 +01:00
|
|
|
auto* node = new QRemoteObjectNode();
|
|
|
|
|
node->connectToNode(QUrl(QString("local:%1").arg(amnezia::getIpcProcessUrl(pid))));
|
2021-02-03 15:42:36 +03:00
|
|
|
|
2026-02-11 16:47:28 +01:00
|
|
|
QSharedPointer<IpcProcessInterfaceReplica> rep(
|
|
|
|
|
node->acquire<IpcProcessInterfaceReplica>(),
|
|
|
|
|
[node] (IpcProcessInterfaceReplica *ptr) {
|
|
|
|
|
delete ptr;
|
|
|
|
|
node->deleteLater();
|
2021-02-18 15:00:41 +03:00
|
|
|
}
|
2026-02-11 16:47:28 +01:00
|
|
|
);
|
|
|
|
|
if (rep.isNull()) {
|
|
|
|
|
qCritical() << "IpcClient::CreatePrivilegedProcess(): Failed to acquire replica";
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
if (!rep->waitForSource()) {
|
|
|
|
|
qCritical() << "IpcClient::CreatePrivilegedProcess(): Failed to initialize replica";
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
if (!rep->isReplicaValid()) {
|
|
|
|
|
qCritical() << "IpcClient::CreatePrivilegedProcess(): Replica is invalid";
|
|
|
|
|
return nullptr;
|
2021-02-03 15:42:36 +03:00
|
|
|
}
|
2025-12-18 13:18:11 +01:00
|
|
|
|
2026-02-11 16:47:28 +01:00
|
|
|
return rep;
|
|
|
|
|
},
|
|
|
|
|
[]() -> QSharedPointer<IpcProcessInterfaceReplica> {
|
2025-12-18 13:18:11 +01:00
|
|
|
return nullptr;
|
2026-02-11 16:47:28 +01:00
|
|
|
});
|
2021-02-02 01:47:40 +03:00
|
|
|
}
|