smsub/SMSubServer.cpp

167 lines
6.3 KiB
C++
Raw Normal View History

2020-07-16 16:59:05 +02:00
/*****************************************************************************
* smsub Server Manager Subprocess
* Copyright (C) 2020 Syping
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* This software is provided as-is, no warranties are given to you, we are not
* responsible for anything with use of the software, you are self responsible.
*****************************************************************************/
#include <QCoreApplication>
#include <QTimer>
#include <QUuid>
2020-07-16 16:59:05 +02:00
#include "SMSubServer.h"
#include "smsub.h"
SMSubServer::SMSubServer(SMSubServerSettings *serverSettings, const QString &socket) : serverSettings(serverSettings)
2020-07-16 16:59:05 +02:00
{
2020-07-16 17:05:22 +02:00
setSocketOptions(QLocalServer::UserAccessOption | QLocalServer::GroupAccessOption);
2020-07-16 16:59:05 +02:00
listen(socket);
}
void SMSubServer::incomingConnection(quintptr socketDescriptor)
{
QLocalSocket *socket = new QLocalSocket();
socket->setSocketDescriptor(socketDescriptor);
QObject::connect(socket, &QLocalSocket::readyRead, this, &SMSubServer::readyRead);
QObject::connect(socket, &QLocalSocket::disconnected, this, &SMSubServer::deleteSocket);
// Set authentication state
if (serverSettings->isLocal) {
socket->setProperty("Authenticated", true);
sockets << socket;
}
else {
socket->setProperty("Authenticated", false);
}
// Initial open writing
socket->write(QString("SMSub Version %1\n").arg(QCoreApplication::applicationVersion()).toUtf8());
2020-07-16 16:59:05 +02:00
}
void SMSubServer::readyRead()
{
// Manage client input
2020-07-16 16:59:05 +02:00
QLocalSocket *socket = (QLocalSocket*)sender();
bool isAuthenticated = socket->property("Authenticated").toBool();
2020-07-16 16:59:05 +02:00
while (socket->canReadLine()) {
const QByteArray readData = socket->readLine().trimmed();
if (likely(isAuthenticated)) {
if (readData.startsWith("+dbg")) {
socket->setProperty("ReceiveDbgMsg", true);
socket->write("Debug messages enabled!\n");
}
else if (readData.startsWith("-dbg")) {
socket->setProperty("ReceiveDbgMsg", false);
socket->write("Debug messages disabled!\n");
}
else if (readData.startsWith("+log")) {
socket->setProperty("ReceiveLog", true);
debugOutput(socket, "Log output enabled!");
}
else if (readData.startsWith("-log")) {
socket->setProperty("ReceiveLog", false);
debugOutput(socket, "Log output disabled!");
}
else if (readData.startsWith("+reg")) {
if (likely(serverSettings->canRegister)) {
QByteArray authUuid = QUuid::createUuid().toByteArray(QUuid::Id128);
authUuid = QByteArray::fromHex(authUuid).toBase64(QByteArray::OmitTrailingEquals);
emit tokenRegistered(QString::fromUtf8(authUuid));
socket->write("Token: " + authUuid + '\n');
}
else {
socket->write("Permission denied!\n");
}
}
else if (readData.startsWith("kill")) {
emit killRequested();
debugOutput(socket, "Killing server!");
}
else if (readData.startsWith("stop")) {
emit stopRequested();
debugOutput(socket, "Stopping server!");
}
else if (readData.startsWith("wl")) {
const QByteArray writeData = readData.mid(3);
emit inputWritten(writeData + '\n');
debugOutput(socket, "Write line \"" + writeData + "\"!");
}
else if (readData.startsWith("w")) {
const QByteArray writeData = readData.mid(2);
emit inputWritten(writeData);
debugOutput(socket, "Write \"" + writeData + "\"!");
}
2020-07-16 16:59:05 +02:00
}
else {
if (unlikely(tokens.contains(QString::fromUtf8(readData)))) {
socket->setProperty("Authenticated", true);
socket->write("Login successful!\n");
isAuthenticated = true;
sockets << socket;
}
else {
// Stop receiving data and disconnect socket
QObject::disconnect(socket, &QLocalSocket::readyRead, this, &SMSubServer::readyRead);
socket->write("Incorrect token!\n");
socket->disconnectFromServer();
return;
}
2020-07-16 16:59:05 +02:00
}
}
}
void SMSubServer::deleteSocket()
{
// Delete socket and remove from index
2020-07-16 16:59:05 +02:00
QLocalSocket *socket = (QLocalSocket*)sender();
sockets.removeAll(socket);
socket->deleteLater();
}
void SMSubServer::debugOutput(QLocalSocket *socket, const QByteArray &message)
{
// Only send debug messages when the client opted-in
const QVariant variant = socket->property("ReceiveDbgMsg");
if (unlikely(variant.type() == QVariant::Bool)) {
bool receiveDbgMsg = variant.toBool();
if (likely(receiveDbgMsg)) {
socket->write(message + '\n');
}
}
}
2020-07-16 16:59:05 +02:00
void SMSubServer::writeOutput(const QByteArray &output)
{
// Read process output when client opted-in for log
2020-07-16 16:59:05 +02:00
QVector<QLocalSocket*>::const_iterator it = sockets.constBegin();
QVector<QLocalSocket*>::const_iterator end = sockets.constEnd();
while (it != end) {
const QVariant variant = (*it)->property("ReceiveLog");
if (unlikely(variant.type() == QVariant::Bool)) {
bool receiveLog = variant.toBool();
if (likely(receiveLog)) {
(*it)->write(output);
}
}
it++;
}
}
void SMSubServer::registerToken(const QString &token)
{
tokens << token;
QTimer::singleShot(30000, [this, token]() {
tokens.removeAll(token);
});
}