2025-02-06 12:03:51 +00:00
|
|
|
/*
|
|
|
|
|
* Deskflow -- mouse and keyboard sharing utility
|
|
|
|
|
* SPDX-FileCopyrightText: (C) 2025 Symless Ltd.
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "DaemonIpcClient.h"
|
|
|
|
|
|
2025-03-19 22:14:27 +00:00
|
|
|
#include "common/Constants.h"
|
2025-02-06 12:03:51 +00:00
|
|
|
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
#include <QLocalSocket>
|
|
|
|
|
#include <QObject>
|
2025-02-07 17:02:20 +00:00
|
|
|
#include <QString>
|
2025-02-06 12:03:51 +00:00
|
|
|
|
|
|
|
|
namespace deskflow::gui::ipc {
|
|
|
|
|
|
|
|
|
|
const auto kTimeout = 1000;
|
2025-04-18 12:56:37 +00:00
|
|
|
const auto kRetryLimit = 3;
|
2025-02-06 12:03:51 +00:00
|
|
|
|
2025-02-10 11:22:04 +00:00
|
|
|
DaemonIpcClient::DaemonIpcClient(QObject *parent)
|
2025-02-10 12:58:23 +00:00
|
|
|
: QObject(parent),
|
|
|
|
|
m_socket{new QLocalSocket(this)} // NOSONAR - Qt memory
|
2025-02-06 12:03:51 +00:00
|
|
|
{
|
2025-02-10 18:00:34 +00:00
|
|
|
connect(m_socket, &QLocalSocket::disconnected, this, &DaemonIpcClient::handleDisconnected);
|
|
|
|
|
connect(m_socket, &QLocalSocket::errorOccurred, this, &DaemonIpcClient::handleErrorOccurred);
|
2025-02-06 12:03:51 +00:00
|
|
|
}
|
|
|
|
|
|
2025-02-07 17:02:20 +00:00
|
|
|
bool DaemonIpcClient::connectToServer()
|
2025-02-06 12:03:51 +00:00
|
|
|
{
|
2025-04-18 12:56:37 +00:00
|
|
|
if (m_state == State::Connecting) {
|
|
|
|
|
qWarning() << "daemon ipc client already connecting to server";
|
2025-04-15 20:52:36 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
2025-02-06 13:36:12 +00:00
|
|
|
|
2025-04-18 12:56:37 +00:00
|
|
|
if (m_state != State::Unconnected) {
|
|
|
|
|
qDebug() << "daemon ipc client not in unconnected state, disconnecting";
|
|
|
|
|
disconnectFromServer();
|
|
|
|
|
}
|
2025-04-15 20:52:36 +00:00
|
|
|
|
2025-04-18 12:56:37 +00:00
|
|
|
if (m_socket->state() != QLocalSocket::UnconnectedState) {
|
|
|
|
|
qWarning() << "daemon ipc client socket not in unconnected state, disconnecting";
|
|
|
|
|
disconnectFromServer();
|
2025-02-06 12:03:51 +00:00
|
|
|
}
|
|
|
|
|
|
2025-04-18 12:56:37 +00:00
|
|
|
for (int i = 0; i < kRetryLimit; ++i) {
|
|
|
|
|
if (i == 0) {
|
|
|
|
|
qDebug() << "daemon ipc client connecting to server:" << kDaemonIpcName;
|
|
|
|
|
} else {
|
|
|
|
|
qDebug() << "daemon ipc client retrying connection, attempt:" << i + 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_state = State::Connecting;
|
|
|
|
|
m_socket->connectToServer(kDaemonIpcName);
|
|
|
|
|
|
|
|
|
|
if (!m_socket->waitForConnected(kTimeout)) {
|
|
|
|
|
qWarning() << "daemon ipc client failed to connect";
|
|
|
|
|
disconnectFromServer();
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!sendMessage("hello", "hello", false)) {
|
|
|
|
|
qWarning() << "daemon ipc client failed to send hello";
|
|
|
|
|
disconnectFromServer();
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_state = State::Connected;
|
|
|
|
|
qDebug() << "daemon ipc client connected";
|
|
|
|
|
Q_EMIT connected();
|
|
|
|
|
return true;
|
2025-02-07 17:02:20 +00:00
|
|
|
}
|
2025-02-06 13:31:18 +00:00
|
|
|
|
2025-04-18 12:56:37 +00:00
|
|
|
qWarning() << "daemon ipc client failed to connect after" << kRetryLimit << "attempts";
|
|
|
|
|
disconnectFromServer();
|
|
|
|
|
Q_EMIT connectionFailed();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2025-04-15 20:52:36 +00:00
|
|
|
|
2025-04-18 12:56:37 +00:00
|
|
|
void DaemonIpcClient::disconnectFromServer()
|
|
|
|
|
{
|
|
|
|
|
m_state = State::Disconnecting;
|
|
|
|
|
qDebug() << "daemon ipc client disconnecting from server";
|
|
|
|
|
m_socket->disconnectFromServer();
|
|
|
|
|
|
|
|
|
|
if (m_socket->state() != QLocalSocket::UnconnectedState) {
|
|
|
|
|
qDebug() << "daemon ipc client waiting for socket to disconnect";
|
|
|
|
|
m_socket->waitForDisconnected(kTimeout);
|
|
|
|
|
qDebug() << "daemon ipc client disconnected from server";
|
|
|
|
|
} else {
|
|
|
|
|
qDebug() << "daemon ipc client socket already disconnected";
|
|
|
|
|
}
|
2025-02-10 14:12:27 +00:00
|
|
|
|
2025-04-18 12:56:37 +00:00
|
|
|
m_state = State::Unconnected;
|
2025-02-06 13:31:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DaemonIpcClient::handleDisconnected()
|
|
|
|
|
{
|
2025-04-18 12:56:37 +00:00
|
|
|
qDebug() << "daemon ipc client disconnected from server";
|
|
|
|
|
if (m_state == State::Connected) {
|
|
|
|
|
Q_EMIT connectionFailed();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_state = State::Unconnected;
|
2025-02-06 13:31:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DaemonIpcClient::handleErrorOccurred()
|
|
|
|
|
{
|
2025-03-14 11:31:24 +00:00
|
|
|
qWarning() << "daemon ipc client error:" << m_socket->errorString();
|
2025-04-18 12:56:37 +00:00
|
|
|
disconnectFromServer();
|
|
|
|
|
|
|
|
|
|
if (m_state == State::Connected) {
|
|
|
|
|
Q_EMIT connectionFailed();
|
|
|
|
|
}
|
2025-02-06 13:31:18 +00:00
|
|
|
}
|
|
|
|
|
|
2025-02-10 12:58:23 +00:00
|
|
|
bool DaemonIpcClient::sendMessage(const QString &message, const QString &expectAck, const bool expectConnected)
|
|
|
|
|
{
|
2025-04-18 12:56:37 +00:00
|
|
|
if (expectConnected && !isConnected()) {
|
2025-02-10 14:12:27 +00:00
|
|
|
qWarning() << "cannot send command, ipc client not connected";
|
2025-02-10 12:58:23 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QByteArray messageData = message.toUtf8() + "\n";
|
|
|
|
|
m_socket->write(messageData);
|
|
|
|
|
if (!m_socket->waitForBytesWritten(kTimeout)) {
|
2025-03-14 11:31:24 +00:00
|
|
|
qWarning() << "daemon ipc client failed to write command";
|
2025-02-10 12:58:23 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!expectAck.isEmpty()) {
|
2025-03-14 11:31:24 +00:00
|
|
|
qDebug() << "daemon ipc client waiting for ack: " << expectAck;
|
2025-02-10 12:58:23 +00:00
|
|
|
|
|
|
|
|
if (!m_socket->waitForReadyRead(kTimeout)) {
|
2025-04-18 12:56:37 +00:00
|
|
|
qWarning() << "daemon ipc client socket ready read timed out";
|
2025-02-10 12:58:23 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QByteArray response = m_socket->readAll();
|
|
|
|
|
if (response.isEmpty()) {
|
2025-03-14 11:31:24 +00:00
|
|
|
qWarning() << "daemon ipc client got empty response";
|
2025-02-10 12:58:23 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString responseData = QString::fromUtf8(response);
|
|
|
|
|
if (responseData.isEmpty()) {
|
2025-03-14 11:31:24 +00:00
|
|
|
qWarning() << "daemon ipc client failed to convert response to string";
|
2025-02-10 12:58:23 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (responseData != expectAck + "\n") {
|
2025-03-14 11:31:24 +00:00
|
|
|
qWarning() << "daemon ipc client got unexpected response: " << responseData;
|
2025-02-10 12:58:23 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-14 11:31:24 +00:00
|
|
|
qDebug() << "daemon ipc client sent message: " << messageData;
|
2025-02-10 12:58:23 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-07 17:02:20 +00:00
|
|
|
bool DaemonIpcClient::keepAlive()
|
|
|
|
|
{
|
|
|
|
|
if (!isConnected() && !connectToServer()) {
|
2025-03-14 11:31:24 +00:00
|
|
|
qWarning() << "daemon ipc client keep alive failed to connect";
|
2025-02-07 17:02:20 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!sendMessage("noop")) {
|
2025-04-18 12:56:37 +00:00
|
|
|
qWarning() << "daemon ipc client keep alive ping failed, reconnecting";
|
|
|
|
|
connectToServer();
|
2025-02-07 17:02:20 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DaemonIpcClient::sendLogLevel(const QString &logLevel)
|
|
|
|
|
{
|
|
|
|
|
if (!keepAlive())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
sendMessage("logLevel=" + logLevel);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-02 22:00:06 +00:00
|
|
|
bool DaemonIpcClient::sendStartProcess(const QString &command, bool elevate)
|
2025-02-06 13:31:18 +00:00
|
|
|
{
|
2025-02-07 17:02:20 +00:00
|
|
|
if (!keepAlive())
|
|
|
|
|
return false;
|
|
|
|
|
|
2025-04-02 22:00:06 +00:00
|
|
|
if (!sendMessage("elevate=" + (elevate ? QStringLiteral("yes") : QStringLiteral("no")))) {
|
2025-02-10 11:22:04 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!sendMessage("command=" + command)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return sendMessage("start");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DaemonIpcClient::sendStopProcess()
|
|
|
|
|
{
|
|
|
|
|
return sendMessage("stop");
|
2025-02-06 13:31:18 +00:00
|
|
|
}
|
|
|
|
|
|
2025-02-10 12:58:23 +00:00
|
|
|
QString DaemonIpcClient::requestLogPath()
|
2025-02-06 13:31:18 +00:00
|
|
|
{
|
2025-02-10 12:58:23 +00:00
|
|
|
if (!keepAlive())
|
|
|
|
|
return QString();
|
2025-02-06 12:03:51 +00:00
|
|
|
|
2025-02-10 12:58:23 +00:00
|
|
|
if (!sendMessage("logPath", QString())) {
|
|
|
|
|
return QString();
|
2025-02-06 13:31:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!m_socket->waitForReadyRead(kTimeout)) {
|
2025-03-14 11:31:24 +00:00
|
|
|
qWarning() << "daemon ipc client failed to read log path response";
|
2025-02-10 12:58:23 +00:00
|
|
|
return QString();
|
2025-02-06 12:03:51 +00:00
|
|
|
}
|
|
|
|
|
|
2025-02-06 13:31:18 +00:00
|
|
|
QByteArray response = m_socket->readAll();
|
2025-02-06 12:03:51 +00:00
|
|
|
if (response.isEmpty()) {
|
2025-03-14 11:31:24 +00:00
|
|
|
qWarning() << "daemon ipc client got empty log path response";
|
2025-02-10 12:58:23 +00:00
|
|
|
return QString();
|
2025-02-06 12:03:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString responseData = QString::fromUtf8(response);
|
|
|
|
|
if (responseData.isEmpty()) {
|
2025-03-14 11:31:24 +00:00
|
|
|
qWarning() << "daemon ipc client failed to convert log path response to string";
|
2025-02-10 12:58:23 +00:00
|
|
|
return QString();
|
2025-02-06 12:03:51 +00:00
|
|
|
}
|
|
|
|
|
|
2025-02-10 12:58:23 +00:00
|
|
|
// Trimming removes newline from end of message.
|
|
|
|
|
QStringList parts = responseData.trimmed().split("=");
|
|
|
|
|
if (parts.size() != 2) {
|
2025-03-14 11:31:24 +00:00
|
|
|
qWarning() << "daemon ipc client got invalid log path response: " << responseData;
|
2025-02-10 12:58:23 +00:00
|
|
|
return QString();
|
2025-02-06 12:03:51 +00:00
|
|
|
}
|
|
|
|
|
|
2025-02-10 12:58:23 +00:00
|
|
|
if (parts[0] != "logPath") {
|
2025-03-14 11:31:24 +00:00
|
|
|
qWarning() << "daemon ipc client got unexpected log path response: " << responseData;
|
2025-02-10 12:58:23 +00:00
|
|
|
return QString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return parts[1];
|
2025-02-06 12:03:51 +00:00
|
|
|
}
|
|
|
|
|
|
2025-03-03 18:01:32 +00:00
|
|
|
bool DaemonIpcClient::sendClearSettings()
|
|
|
|
|
{
|
|
|
|
|
return sendMessage("clearSettings");
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-06 12:03:51 +00:00
|
|
|
} // namespace deskflow::gui::ipc
|