improve security
This commit is contained in:
parent
d4a25b8346
commit
2cef92e61f
6 changed files with 827 additions and 824 deletions
212
SMSubProcess.cpp
212
SMSubProcess.cpp
|
@ -1,106 +1,106 @@
|
||||||
/*****************************************************************************
|
/*****************************************************************************
|
||||||
* smsub Server Manager Subprocess
|
* smsub Server Manager Subprocess
|
||||||
* Copyright (C) 2020 Syping
|
* Copyright (C) 2020 Syping
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without modification,
|
* Redistribution and use in source and binary forms, with or without modification,
|
||||||
* are permitted provided that the following conditions are met:
|
* are permitted provided that the following conditions are met:
|
||||||
*
|
*
|
||||||
* 1. Redistributions of source code must retain the above copyright notice,
|
* 1. Redistributions of source code must retain the above copyright notice,
|
||||||
* this list of conditions and the following disclaimer.
|
* this list of conditions and the following disclaimer.
|
||||||
*
|
*
|
||||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||||
* this list of conditions and the following disclaimer in the documentation
|
* this list of conditions and the following disclaimer in the documentation
|
||||||
* and/or other materials provided with the distribution.
|
* and/or other materials provided with the distribution.
|
||||||
*
|
*
|
||||||
* This software is provided as-is, no warranties are given to you, we are not
|
* 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.
|
* responsible for anything with use of the software, you are self responsible.
|
||||||
*****************************************************************************/
|
*****************************************************************************/
|
||||||
|
|
||||||
#include <QCoreApplication>
|
#include <QCoreApplication>
|
||||||
#include <QTextStream>
|
#include <QTextStream>
|
||||||
#include "SMSubProcess.h"
|
#include "SMSubProcess.h"
|
||||||
#include "smsub.h"
|
#include "smsub.h"
|
||||||
|
|
||||||
SMSubProcess::SMSubProcess(const QString &executable, const QStringList &arguments, const QString &workingDirectory, const int &termTimeout) :
|
SMSubProcess::SMSubProcess(const QString &executable, const QStringList &arguments, const QString &workingDirectory, const int &termTimeout) :
|
||||||
termTimeout(termTimeout)
|
termTimeout(termTimeout)
|
||||||
{
|
{
|
||||||
// Set process executable, arguments and working directory
|
// Set process executable, arguments and working directory
|
||||||
process.setProgram(executable);
|
process.setProgram(executable);
|
||||||
process.setArguments(arguments);
|
process.setArguments(arguments);
|
||||||
process.setWorkingDirectory(workingDirectory);
|
process.setWorkingDirectory(workingDirectory);
|
||||||
|
|
||||||
// stdout and stderr in same IO stream
|
// stdout and stderr in same IO stream
|
||||||
process.setProcessChannelMode(QProcess::MergedChannels);
|
process.setProcessChannelMode(QProcess::MergedChannels);
|
||||||
|
|
||||||
// Connect process signal handlers
|
// Connect process signal handlers
|
||||||
QObject::connect(&process, &QProcess::readyRead, this, &SMSubProcess::readyRead);
|
QObject::connect(&process, &QProcess::readyRead, this, &SMSubProcess::readyRead);
|
||||||
QObject::connect(&process, &QProcess::errorOccurred, this, &SMSubProcess::processError);
|
QObject::connect(&process, &QProcess::errorOccurred, this, &SMSubProcess::processError);
|
||||||
QObject::connect(&process, QOverload<int>::of(&QProcess::finished), this, &SMSubProcess::processExit);
|
QObject::connect(&process, QOverload<int>::of(&QProcess::finished), this, &SMSubProcess::processExit);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SMSubProcess::start()
|
void SMSubProcess::start()
|
||||||
{
|
{
|
||||||
process.start(QIODevice::ReadWrite);
|
process.start(QIODevice::ReadWrite);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SMSubProcess::readyRead()
|
void SMSubProcess::readyRead()
|
||||||
{
|
{
|
||||||
// Read process output and emit event
|
// Read process output and emit event
|
||||||
while (process.canReadLine()) {
|
while (process.canReadLine()) {
|
||||||
const QByteArray readData = process.readLine().trimmed();
|
const QByteArray readData = process.readLine().trimmed();
|
||||||
emit outputWritten(readData + '\n');
|
emit outputWritten(readData + '\n');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SMSubProcess::processExit(int exitCode)
|
void SMSubProcess::processExit(int exitCode)
|
||||||
{
|
{
|
||||||
// Exit with the same exit code as the process
|
// Exit with the same exit code as the process
|
||||||
emit processStopped();
|
emit processStopped();
|
||||||
QCoreApplication::exit(exitCode);
|
QCoreApplication::exit(exitCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SMSubProcess::processError(QProcess::ProcessError error)
|
void SMSubProcess::processError(QProcess::ProcessError error)
|
||||||
{
|
{
|
||||||
// Handle process errors
|
// Handle process errors
|
||||||
if (likely(error == QProcess::FailedToStart)) {
|
if (likely(error == QProcess::FailedToStart)) {
|
||||||
QTextStream(stderr) << "Process failed to start!" << endl;
|
QTextStream(stderr) << "Process failed to start!" << endl;
|
||||||
QCoreApplication::exit(1);
|
QCoreApplication::exit(1);
|
||||||
}
|
}
|
||||||
else if (error == QProcess::UnknownError) {
|
else if (error == QProcess::UnknownError) {
|
||||||
QTextStream(stderr) << "Unknown error occurred!" << endl;
|
QTextStream(stderr) << "Unknown error occurred!" << endl;
|
||||||
QCoreApplication::exit(1);
|
QCoreApplication::exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SMSubProcess::aboutToQuit()
|
void SMSubProcess::aboutToQuit()
|
||||||
{
|
{
|
||||||
// Main process terminated, terminate subprocess with set timeout
|
// Main process terminated, terminate subprocess with set timeout
|
||||||
if (process.state() == QProcess::Running) {
|
if (process.state() == QProcess::Running) {
|
||||||
process.terminate();
|
process.terminate();
|
||||||
if (!process.waitForFinished(termTimeout)) {
|
if (!process.waitForFinished(termTimeout)) {
|
||||||
QTextStream(stderr) << "Failed to terminate process!" << endl;
|
QTextStream(stderr) << "Failed to terminate process!" << endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SMSubProcess::killProcess()
|
void SMSubProcess::killProcess()
|
||||||
{
|
{
|
||||||
// Kill process as requested
|
// Kill process as requested
|
||||||
if (process.state() == QProcess::Running) {
|
if (process.state() == QProcess::Running) {
|
||||||
process.kill();
|
process.kill();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SMSubProcess::stopProcess()
|
void SMSubProcess::stopProcess()
|
||||||
{
|
{
|
||||||
// Terminate process as requested
|
// Terminate process as requested
|
||||||
if (process.state() == QProcess::Running) {
|
if (process.state() == QProcess::Running) {
|
||||||
process.terminate();
|
process.terminate();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SMSubProcess::writeInput(const QByteArray &input)
|
void SMSubProcess::writeInput(const QByteArray &input)
|
||||||
{
|
{
|
||||||
// Write input from Unix/IPC socket to process
|
// Write input from Unix/IPC socket to process
|
||||||
process.write(input);
|
process.write(input);
|
||||||
}
|
}
|
||||||
|
|
106
SMSubProcess.h
106
SMSubProcess.h
|
@ -1,53 +1,53 @@
|
||||||
/*****************************************************************************
|
/*****************************************************************************
|
||||||
* smsub Server Manager Subprocess
|
* smsub Server Manager Subprocess
|
||||||
* Copyright (C) 2020 Syping
|
* Copyright (C) 2020 Syping
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without modification,
|
* Redistribution and use in source and binary forms, with or without modification,
|
||||||
* are permitted provided that the following conditions are met:
|
* are permitted provided that the following conditions are met:
|
||||||
*
|
*
|
||||||
* 1. Redistributions of source code must retain the above copyright notice,
|
* 1. Redistributions of source code must retain the above copyright notice,
|
||||||
* this list of conditions and the following disclaimer.
|
* this list of conditions and the following disclaimer.
|
||||||
*
|
*
|
||||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||||
* this list of conditions and the following disclaimer in the documentation
|
* this list of conditions and the following disclaimer in the documentation
|
||||||
* and/or other materials provided with the distribution.
|
* and/or other materials provided with the distribution.
|
||||||
*
|
*
|
||||||
* This software is provided as-is, no warranties are given to you, we are not
|
* 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.
|
* responsible for anything with use of the software, you are self responsible.
|
||||||
*****************************************************************************/
|
*****************************************************************************/
|
||||||
|
|
||||||
#ifndef SMSUBPROCESS_H
|
#ifndef SMSUBPROCESS_H
|
||||||
#define SMSUBPROCESS_H
|
#define SMSUBPROCESS_H
|
||||||
|
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
#include <QProcess>
|
#include <QProcess>
|
||||||
|
|
||||||
class SMSubProcess : public QObject
|
class SMSubProcess : public QObject
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
SMSubProcess(const QString &executable, const QStringList &arguments, const QString& workingDirectory, const int &termTimeout = 60000);
|
SMSubProcess(const QString &executable, const QStringList &arguments, const QString& workingDirectory, const int &termTimeout = 60000);
|
||||||
void start();
|
void start();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QProcess process;
|
QProcess process;
|
||||||
int termTimeout;
|
int termTimeout;
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void aboutToQuit();
|
void aboutToQuit();
|
||||||
void killProcess();
|
void killProcess();
|
||||||
void stopProcess();
|
void stopProcess();
|
||||||
void writeInput(const QByteArray &input);
|
void writeInput(const QByteArray &input);
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void readyRead();
|
void readyRead();
|
||||||
void processExit(int exitCode);
|
void processExit(int exitCode);
|
||||||
void processError(QProcess::ProcessError error);
|
void processError(QProcess::ProcessError error);
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void outputWritten(const QByteArray &output);
|
void outputWritten(const QByteArray &output);
|
||||||
void processStopped();
|
void processStopped();
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // SMSUBPROCESS_H
|
#endif // SMSUBPROCESS_H
|
||||||
|
|
485
SMSubServer.cpp
485
SMSubServer.cpp
|
@ -1,241 +1,244 @@
|
||||||
/*****************************************************************************
|
/*****************************************************************************
|
||||||
* smsub Server Manager Subprocess
|
* smsub Server Manager Subprocess
|
||||||
* Copyright (C) 2020 Syping
|
* Copyright (C) 2020 Syping
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without modification,
|
* Redistribution and use in source and binary forms, with or without modification,
|
||||||
* are permitted provided that the following conditions are met:
|
* are permitted provided that the following conditions are met:
|
||||||
*
|
*
|
||||||
* 1. Redistributions of source code must retain the above copyright notice,
|
* 1. Redistributions of source code must retain the above copyright notice,
|
||||||
* this list of conditions and the following disclaimer.
|
* this list of conditions and the following disclaimer.
|
||||||
*
|
*
|
||||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||||
* this list of conditions and the following disclaimer in the documentation
|
* this list of conditions and the following disclaimer in the documentation
|
||||||
* and/or other materials provided with the distribution.
|
* and/or other materials provided with the distribution.
|
||||||
*
|
*
|
||||||
* This software is provided as-is, no warranties are given to you, we are not
|
* 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.
|
* responsible for anything with use of the software, you are self responsible.
|
||||||
*****************************************************************************/
|
*****************************************************************************/
|
||||||
|
|
||||||
#include <QCoreApplication>
|
#include <QCoreApplication>
|
||||||
#include <QTimer>
|
#include <QTimer>
|
||||||
#include <QUuid>
|
#include <QUuid>
|
||||||
#include "SMSubServer.h"
|
#include "SMSubServer.h"
|
||||||
#include "smsub.h"
|
#include "smsub.h"
|
||||||
|
|
||||||
SMSubServer::SMSubServer(SMSubServerSettings *serverSettings, const QString &socket) : serverSettings(serverSettings)
|
SMSubServer::SMSubServer(SMSubServerSettings *serverSettings, const QString &socket) : serverSettings(serverSettings)
|
||||||
{
|
{
|
||||||
QLocalServer *localServer = new QLocalServer(this);
|
QLocalServer *localServer = new QLocalServer(this);
|
||||||
localServer->setSocketOptions(QLocalServer::UserAccessOption | QLocalServer::GroupAccessOption);
|
localServer->setSocketOptions(QLocalServer::UserAccessOption | QLocalServer::GroupAccessOption);
|
||||||
localServer->listen(socket);
|
localServer->listen(socket);
|
||||||
|
|
||||||
QObject::connect(localServer, &QLocalServer::newConnection, this, &SMSubServer::newConnection);
|
QObject::connect(localServer, &QLocalServer::newConnection, this, &SMSubServer::newConnection);
|
||||||
|
|
||||||
type = ServerType::Local;
|
type = ServerType::Local;
|
||||||
server = localServer;
|
server = localServer;
|
||||||
}
|
}
|
||||||
|
|
||||||
SMSubServer::SMSubServer(SMSubServerSettings *serverSettings, const QString &serverName, const quint16 &port) : serverSettings(serverSettings)
|
SMSubServer::SMSubServer(SMSubServerSettings *serverSettings, const QString &serverName, const quint16 &port) : serverSettings(serverSettings)
|
||||||
{
|
{
|
||||||
QWebSocketServer *webSocketServer = new QWebSocketServer(serverName, QWebSocketServer::NonSecureMode, this);
|
QWebSocketServer *webSocketServer = new QWebSocketServer(serverName, QWebSocketServer::NonSecureMode, this);
|
||||||
webSocketServer->listen(QHostAddress::LocalHost, port);
|
webSocketServer->listen(QHostAddress::LocalHost, port);
|
||||||
|
|
||||||
QObject::connect(webSocketServer, &QWebSocketServer::newConnection, this, &SMSubServer::newConnection);
|
QObject::connect(webSocketServer, &QWebSocketServer::newConnection, this, &SMSubServer::newConnection);
|
||||||
|
|
||||||
type = ServerType::WebSocket;
|
type = ServerType::WebSocket;
|
||||||
server = webSocketServer;
|
server = webSocketServer;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SMSubServer::isListening()
|
bool SMSubServer::isListening()
|
||||||
{
|
{
|
||||||
if (likely(type == ServerType::Local)) {
|
if (likely(type == ServerType::Local)) {
|
||||||
return static_cast<QLocalServer*>(server)->isListening();
|
return static_cast<QLocalServer*>(server)->isListening();
|
||||||
}
|
}
|
||||||
else if (type == ServerType::WebSocket) {
|
else if (type == ServerType::WebSocket) {
|
||||||
return static_cast<QWebSocketServer*>(server)->isListening();
|
return static_cast<QWebSocketServer*>(server)->isListening();
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SMSubServer::newConnection()
|
void SMSubServer::newConnection()
|
||||||
{
|
{
|
||||||
QObject *socket;
|
QObject *socket;
|
||||||
if (likely(type == ServerType::Local)) {
|
if (likely(type == ServerType::Local)) {
|
||||||
QLocalSocket *localSocket = static_cast<QLocalServer*>(server)->nextPendingConnection();
|
QLocalSocket *localSocket = static_cast<QLocalServer*>(server)->nextPendingConnection();
|
||||||
QObject::connect(localSocket, &QLocalSocket::readyRead, this, &SMSubServer::lsReadyRead);
|
QObject::connect(localSocket, &QLocalSocket::readyRead, this, &SMSubServer::lsReadyRead);
|
||||||
QObject::connect(localSocket, &QLocalSocket::disconnected, this, &SMSubServer::deleteSocket);
|
QObject::connect(localSocket, &QLocalSocket::disconnected, this, &SMSubServer::deleteSocket);
|
||||||
localSocket->write(QString("SMSub Version %1\n").arg(QCoreApplication::applicationVersion()).toUtf8());
|
localSocket->write(QString("SMSub Version %1\n").arg(QCoreApplication::applicationVersion()).toUtf8());
|
||||||
socket = localSocket;
|
socket = localSocket;
|
||||||
}
|
}
|
||||||
else if (type == ServerType::WebSocket) {
|
else if (type == ServerType::WebSocket) {
|
||||||
QWebSocket *webSocket = static_cast<QWebSocketServer*>(server)->nextPendingConnection();
|
QWebSocket *webSocket = static_cast<QWebSocketServer*>(server)->nextPendingConnection();
|
||||||
QObject::connect(webSocket, &QWebSocket::binaryMessageReceived, this, &SMSubServer::wsMessageReceived);
|
QObject::connect(webSocket, &QWebSocket::binaryMessageReceived, this, &SMSubServer::wsMessageReceived);
|
||||||
QObject::connect(webSocket, &QWebSocket::disconnected, this, &SMSubServer::deleteSocket);
|
QObject::connect(webSocket, &QWebSocket::disconnected, this, &SMSubServer::deleteSocket);
|
||||||
webSocket->sendBinaryMessage(QString("SMSub Version %1\n").arg(QCoreApplication::applicationVersion()).toUtf8());
|
webSocket->sendBinaryMessage(QString("SMSub Version %1\n").arg(QCoreApplication::applicationVersion()).toUtf8());
|
||||||
socket = webSocket;
|
socket = webSocket;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// Just for being sure
|
// Just for being sure
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set authentication state
|
// Set authentication state
|
||||||
if (serverSettings->isLocal) {
|
if (serverSettings->isLocal) {
|
||||||
socket->setProperty("Authenticated", true);
|
socket->setProperty("Authenticated", true);
|
||||||
sockets << socket;
|
sockets << socket;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
socket->setProperty("Authenticated", false);
|
socket->setProperty("Authenticated", false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SMSubServer::messageReceived(QObject *socket, const QByteArray &message)
|
bool SMSubServer::messageReceived(QObject *socket, const QByteArray &message)
|
||||||
{
|
{
|
||||||
// Only allow commands being sent if authenticated
|
// Only allow commands being sent if authenticated
|
||||||
bool isAuthenticated = socket->property("Authenticated").toBool();
|
bool isAuthenticated = socket->property("Authenticated").toBool();
|
||||||
if (likely(isAuthenticated)) {
|
if (likely(isAuthenticated)) {
|
||||||
if (message.startsWith("+dbg")) {
|
if (message.startsWith("+dbg")) {
|
||||||
socket->setProperty("ReceiveDbgMsg", true);
|
socket->setProperty("ReceiveDbgMsg", true);
|
||||||
sendMessage(socket, "Debug messages enabled!\n");
|
sendMessage(socket, "Debug messages enabled!\n");
|
||||||
}
|
}
|
||||||
else if (message.startsWith("-dbg")) {
|
else if (message.startsWith("-dbg")) {
|
||||||
socket->setProperty("ReceiveDbgMsg", false);
|
socket->setProperty("ReceiveDbgMsg", false);
|
||||||
sendMessage(socket, "Debug messages disabled!\n");
|
sendMessage(socket, "Debug messages disabled!\n");
|
||||||
}
|
}
|
||||||
else if (message.startsWith("+log")) {
|
else if (message.startsWith("+log")) {
|
||||||
socket->setProperty("ReceiveLog", true);
|
socket->setProperty("ReceiveLog", true);
|
||||||
debugOutput(socket, "Log output enabled!");
|
debugOutput(socket, "Log output enabled!");
|
||||||
}
|
}
|
||||||
else if (message.startsWith("-log")) {
|
else if (message.startsWith("-log")) {
|
||||||
socket->setProperty("ReceiveLog", false);
|
socket->setProperty("ReceiveLog", false);
|
||||||
debugOutput(socket, "Log output disabled!");
|
debugOutput(socket, "Log output disabled!");
|
||||||
}
|
}
|
||||||
else if (message.startsWith("+reg")) {
|
else if (message.startsWith("+reg")) {
|
||||||
if (likely(serverSettings->canRegister)) {
|
if (likely(serverSettings->canRegister)) {
|
||||||
QByteArray authUuid = QUuid::createUuid().toByteArray(QUuid::Id128);
|
QByteArray authUuid = QUuid::createUuid().toByteArray(QUuid::Id128);
|
||||||
authUuid = QByteArray::fromHex(authUuid).toBase64(QByteArray::OmitTrailingEquals);
|
authUuid = QByteArray::fromHex(authUuid).toBase64(QByteArray::OmitTrailingEquals);
|
||||||
emit tokenRegistered(QString::fromUtf8(authUuid));
|
emit tokenRegistered(QString::fromUtf8(authUuid));
|
||||||
sendMessage(socket, "Token: " + authUuid + '\n');
|
sendMessage(socket, "Token: " + authUuid + '\n');
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
sendMessage(socket, "Permission denied!\n");
|
sendMessage(socket, "Permission denied!\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (message.startsWith("kill")) {
|
else if (message.startsWith("kill")) {
|
||||||
emit killRequested();
|
emit killRequested();
|
||||||
debugOutput(socket, "Killing server!");
|
debugOutput(socket, "Killing server!");
|
||||||
}
|
}
|
||||||
else if (message.startsWith("stop")) {
|
else if (message.startsWith("stop")) {
|
||||||
emit stopRequested();
|
emit stopRequested();
|
||||||
debugOutput(socket, "Stopping server!");
|
debugOutput(socket, "Stopping server!");
|
||||||
}
|
}
|
||||||
else if (message.startsWith("wl")) {
|
else if (message.startsWith("wl")) {
|
||||||
const QByteArray writeData = message.mid(3);
|
const QByteArray writeData = message.mid(3);
|
||||||
emit inputWritten(writeData + '\n');
|
emit inputWritten(writeData + '\n');
|
||||||
debugOutput(socket, "Write line \"" + writeData + "\"!");
|
debugOutput(socket, "Write line \"" + writeData + "\"!");
|
||||||
}
|
}
|
||||||
else if (message.startsWith("w")) {
|
else if (message.startsWith("w")) {
|
||||||
const QByteArray writeData = message.mid(2);
|
const QByteArray writeData = message.mid(2);
|
||||||
emit inputWritten(writeData);
|
emit inputWritten(writeData);
|
||||||
debugOutput(socket, "Write \"" + writeData + "\"!");
|
debugOutput(socket, "Write \"" + writeData + "\"!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// Authenticate when token is valid, otherwise disconnect
|
// Authenticate when token is valid, otherwise disconnect
|
||||||
if (unlikely(tokens.contains(QString::fromUtf8(message)))) {
|
if (unlikely(tokens.contains(QString::fromUtf8(message)))) {
|
||||||
// Set client as authenticated and add it to vector
|
// Set client as authenticated and add it to vector
|
||||||
socket->setProperty("Authenticated", true);
|
socket->setProperty("Authenticated", true);
|
||||||
sendMessage(socket, "Login successful!\n");
|
sendMessage(socket, "Login successful!\n");
|
||||||
isAuthenticated = true;
|
isAuthenticated = true;
|
||||||
sockets << socket;
|
sockets << socket;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// Stop receiving data and disconnect socket
|
// Stop receiving data and disconnect socket
|
||||||
if (likely(type == ServerType::Local)) {
|
if (likely(type == ServerType::Local)) {
|
||||||
QLocalSocket *localSocket = static_cast<QLocalSocket*>(socket);
|
QLocalSocket *localSocket = static_cast<QLocalSocket*>(socket);
|
||||||
QObject::disconnect(localSocket, &QLocalSocket::readyRead, this, &SMSubServer::lsReadyRead);
|
QObject::disconnect(localSocket, &QLocalSocket::readyRead, this, &SMSubServer::lsReadyRead);
|
||||||
localSocket->write("Incorrect token!\n");
|
localSocket->write("Incorrect token!\n");
|
||||||
localSocket->disconnectFromServer();
|
localSocket->disconnectFromServer();
|
||||||
}
|
return false;
|
||||||
else if (type == ServerType::WebSocket) {
|
}
|
||||||
QWebSocket *webSocket = static_cast<QWebSocket*>(socket);
|
else if (type == ServerType::WebSocket) {
|
||||||
QObject::disconnect(webSocket, &QWebSocket::binaryMessageReceived, this, &SMSubServer::wsMessageReceived);
|
QWebSocket *webSocket = static_cast<QWebSocket*>(socket);
|
||||||
webSocket->sendBinaryMessage("Incorrect token!\n");
|
QObject::disconnect(webSocket, &QWebSocket::binaryMessageReceived, this, &SMSubServer::wsMessageReceived);
|
||||||
webSocket->close(QWebSocketProtocol::CloseCodeNormal);
|
webSocket->sendBinaryMessage("Incorrect token!\n");
|
||||||
}
|
webSocket->close(QWebSocketProtocol::CloseCodeNormal);
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
void SMSubServer::wsMessageReceived(const QByteArray &message)
|
}
|
||||||
{
|
|
||||||
QWebSocket *socket = static_cast<QWebSocket*>(sender());
|
void SMSubServer::wsMessageReceived(const QByteArray &message)
|
||||||
messageReceived(socket, message.trimmed());
|
{
|
||||||
}
|
QWebSocket *socket = static_cast<QWebSocket*>(sender());
|
||||||
|
messageReceived(socket, message.trimmed());
|
||||||
void SMSubServer::lsReadyRead()
|
}
|
||||||
{
|
|
||||||
QLocalSocket *socket = static_cast<QLocalSocket*>(sender());
|
void SMSubServer::lsReadyRead()
|
||||||
while (socket->canReadLine()) {
|
{
|
||||||
const QByteArray message = socket->readLine().trimmed();
|
QLocalSocket *socket = static_cast<QLocalSocket*>(sender());
|
||||||
messageReceived(socket, message);
|
while (socket->canReadLine()) {
|
||||||
}
|
const QByteArray message = socket->readLine().trimmed();
|
||||||
}
|
if (!messageReceived(socket, message))
|
||||||
|
return;
|
||||||
void SMSubServer::deleteSocket()
|
}
|
||||||
{
|
}
|
||||||
// Delete socket and remove from index
|
|
||||||
QObject *socket = sender();
|
void SMSubServer::deleteSocket()
|
||||||
sockets.removeAll(socket);
|
{
|
||||||
socket->deleteLater();
|
// Delete socket and remove from index
|
||||||
}
|
QObject *socket = sender();
|
||||||
|
sockets.removeAll(socket);
|
||||||
void SMSubServer::debugOutput(QObject *socket, const QByteArray &message)
|
socket->deleteLater();
|
||||||
{
|
}
|
||||||
// Only send debug messages when the client opted-in
|
|
||||||
const QVariant variant = socket->property("ReceiveDbgMsg");
|
void SMSubServer::debugOutput(QObject *socket, const QByteArray &message)
|
||||||
if (unlikely(variant.type() == QVariant::Bool)) {
|
{
|
||||||
bool receiveDbgMsg = variant.toBool();
|
// Only send debug messages when the client opted-in
|
||||||
if (likely(receiveDbgMsg)) {
|
const QVariant variant = socket->property("ReceiveDbgMsg");
|
||||||
sendMessage(socket, message + '\n');
|
if (unlikely(variant.type() == QVariant::Bool)) {
|
||||||
}
|
bool receiveDbgMsg = variant.toBool();
|
||||||
}
|
if (likely(receiveDbgMsg)) {
|
||||||
}
|
sendMessage(socket, message + '\n');
|
||||||
|
}
|
||||||
void SMSubServer::writeOutput(const QByteArray &output)
|
}
|
||||||
{
|
}
|
||||||
// Read process output when client opted-in for log
|
|
||||||
QVector<QObject*>::const_iterator it = sockets.constBegin();
|
void SMSubServer::writeOutput(const QByteArray &output)
|
||||||
QVector<QObject*>::const_iterator end = sockets.constEnd();
|
{
|
||||||
while (it != end) {
|
// Read process output when client opted-in for log
|
||||||
const QVariant variant = (*it)->property("ReceiveLog");
|
QVector<QObject*>::const_iterator it = sockets.constBegin();
|
||||||
if (unlikely(variant.type() == QVariant::Bool)) {
|
QVector<QObject*>::const_iterator end = sockets.constEnd();
|
||||||
bool receiveLog = variant.toBool();
|
while (it != end) {
|
||||||
if (likely(receiveLog)) {
|
const QVariant variant = (*it)->property("ReceiveLog");
|
||||||
sendMessage(*it, output);
|
if (unlikely(variant.type() == QVariant::Bool)) {
|
||||||
}
|
bool receiveLog = variant.toBool();
|
||||||
}
|
if (likely(receiveLog)) {
|
||||||
it++;
|
sendMessage(*it, output);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
it++;
|
||||||
void SMSubServer::sendMessage(QObject *socket, const QByteArray &message)
|
}
|
||||||
{
|
}
|
||||||
if (likely(type == ServerType::Local)) {
|
|
||||||
QLocalSocket *localSocket = static_cast<QLocalSocket*>(socket);
|
void SMSubServer::sendMessage(QObject *socket, const QByteArray &message)
|
||||||
localSocket->write(message);
|
{
|
||||||
}
|
if (likely(type == ServerType::Local)) {
|
||||||
else if (type == ServerType::WebSocket) {
|
QLocalSocket *localSocket = static_cast<QLocalSocket*>(socket);
|
||||||
QWebSocket *webSocket = static_cast<QWebSocket*>(socket);
|
localSocket->write(message);
|
||||||
webSocket->sendBinaryMessage(message);
|
}
|
||||||
}
|
else if (type == ServerType::WebSocket) {
|
||||||
}
|
QWebSocket *webSocket = static_cast<QWebSocket*>(socket);
|
||||||
|
webSocket->sendBinaryMessage(message);
|
||||||
void SMSubServer::registerToken(const QString &token)
|
}
|
||||||
{
|
}
|
||||||
// Register temporary token for a secure remote connection
|
|
||||||
tokens << token;
|
void SMSubServer::registerToken(const QString &token)
|
||||||
QTimer::singleShot(30000, [this, token]() {
|
{
|
||||||
tokens.removeAll(token);
|
// Register temporary token for a secure remote connection
|
||||||
});
|
tokens << token;
|
||||||
}
|
QTimer::singleShot(30000, [this, token]() {
|
||||||
|
tokens.removeAll(token);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
144
SMSubServer.h
144
SMSubServer.h
|
@ -1,72 +1,72 @@
|
||||||
/*****************************************************************************
|
/*****************************************************************************
|
||||||
* smsub Server Manager Subprocess
|
* smsub Server Manager Subprocess
|
||||||
* Copyright (C) 2020 Syping
|
* Copyright (C) 2020 Syping
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without modification,
|
* Redistribution and use in source and binary forms, with or without modification,
|
||||||
* are permitted provided that the following conditions are met:
|
* are permitted provided that the following conditions are met:
|
||||||
*
|
*
|
||||||
* 1. Redistributions of source code must retain the above copyright notice,
|
* 1. Redistributions of source code must retain the above copyright notice,
|
||||||
* this list of conditions and the following disclaimer.
|
* this list of conditions and the following disclaimer.
|
||||||
*
|
*
|
||||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||||
* this list of conditions and the following disclaimer in the documentation
|
* this list of conditions and the following disclaimer in the documentation
|
||||||
* and/or other materials provided with the distribution.
|
* and/or other materials provided with the distribution.
|
||||||
*
|
*
|
||||||
* This software is provided as-is, no warranties are given to you, we are not
|
* 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.
|
* responsible for anything with use of the software, you are self responsible.
|
||||||
*****************************************************************************/
|
*****************************************************************************/
|
||||||
|
|
||||||
#ifndef SMSUBSERVER_H
|
#ifndef SMSUBSERVER_H
|
||||||
#define SMSUBSERVER_H
|
#define SMSUBSERVER_H
|
||||||
|
|
||||||
#include <QWebSocketServer>
|
#include <QWebSocketServer>
|
||||||
#include <QLocalServer>
|
#include <QLocalServer>
|
||||||
#include <QLocalSocket>
|
#include <QLocalSocket>
|
||||||
#include <QWebSocket>
|
#include <QWebSocket>
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
|
|
||||||
struct SMSubServerSettings
|
struct SMSubServerSettings
|
||||||
{
|
{
|
||||||
bool canRegister;
|
bool canRegister;
|
||||||
bool isLocal;
|
bool isLocal;
|
||||||
};
|
};
|
||||||
|
|
||||||
class SMSubServer : public QObject
|
class SMSubServer : public QObject
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
SMSubServer(SMSubServerSettings *serverSettings, const QString &socket);
|
SMSubServer(SMSubServerSettings *serverSettings, const QString &socket);
|
||||||
SMSubServer(SMSubServerSettings *serverSettings, const QString &serverName, const quint16 &port);
|
SMSubServer(SMSubServerSettings *serverSettings, const QString &serverName, const quint16 &port);
|
||||||
bool isListening();
|
bool isListening();
|
||||||
|
|
||||||
enum ServerType { Local, WebSocket };
|
enum ServerType { Local, WebSocket };
|
||||||
Q_ENUM(ServerType)
|
Q_ENUM(ServerType)
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void writeOutput(const QByteArray &output);
|
void writeOutput(const QByteArray &output);
|
||||||
void registerToken(const QString &token);
|
void registerToken(const QString &token);
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void wsMessageReceived(const QByteArray &message);
|
void wsMessageReceived(const QByteArray &message);
|
||||||
void lsReadyRead();
|
void lsReadyRead();
|
||||||
void newConnection();
|
void newConnection();
|
||||||
void deleteSocket();
|
void deleteSocket();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
inline void debugOutput(QObject *socket, const QByteArray &message);
|
inline void debugOutput(QObject *socket, const QByteArray &message);
|
||||||
inline void sendMessage(QObject *socket, const QByteArray &message);
|
inline void sendMessage(QObject *socket, const QByteArray &message);
|
||||||
void messageReceived(QObject *socket, const QByteArray &message);
|
bool messageReceived(QObject *socket, const QByteArray &message);
|
||||||
SMSubServerSettings *serverSettings;
|
SMSubServerSettings *serverSettings;
|
||||||
QVector<QObject*> sockets;
|
QVector<QObject*> sockets;
|
||||||
QVector<QString> tokens;
|
QVector<QString> tokens;
|
||||||
ServerType type;
|
ServerType type;
|
||||||
QObject *server;
|
QObject *server;
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void tokenRegistered(const QString &password);
|
void tokenRegistered(const QString &password);
|
||||||
void inputWritten(const QByteArray &input);
|
void inputWritten(const QByteArray &input);
|
||||||
void killRequested();
|
void killRequested();
|
||||||
void stopRequested();
|
void stopRequested();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // SMSUBSERVER_H
|
#endif // SMSUBSERVER_H
|
||||||
|
|
634
main.cpp
634
main.cpp
|
@ -1,317 +1,317 @@
|
||||||
/*****************************************************************************
|
/*****************************************************************************
|
||||||
* smsub Server Manager Subprocess
|
* smsub Server Manager Subprocess
|
||||||
* Copyright (C) 2020 Syping
|
* Copyright (C) 2020 Syping
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without modification,
|
* Redistribution and use in source and binary forms, with or without modification,
|
||||||
* are permitted provided that the following conditions are met:
|
* are permitted provided that the following conditions are met:
|
||||||
*
|
*
|
||||||
* 1. Redistributions of source code must retain the above copyright notice,
|
* 1. Redistributions of source code must retain the above copyright notice,
|
||||||
* this list of conditions and the following disclaimer.
|
* this list of conditions and the following disclaimer.
|
||||||
*
|
*
|
||||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||||
* this list of conditions and the following disclaimer in the documentation
|
* this list of conditions and the following disclaimer in the documentation
|
||||||
* and/or other materials provided with the distribution.
|
* and/or other materials provided with the distribution.
|
||||||
*
|
*
|
||||||
* This software is provided as-is, no warranties are given to you, we are not
|
* 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.
|
* responsible for anything with use of the software, you are self responsible.
|
||||||
*****************************************************************************/
|
*****************************************************************************/
|
||||||
|
|
||||||
#include <QCommandLineParser>
|
#include <QCommandLineParser>
|
||||||
#include <QCommandLineOption>
|
#include <QCommandLineOption>
|
||||||
#include <QCoreApplication>
|
#include <QCoreApplication>
|
||||||
#include <QJsonDocument>
|
#include <QJsonDocument>
|
||||||
#include <QJsonObject>
|
#include <QJsonObject>
|
||||||
#include <QTextStream>
|
#include <QTextStream>
|
||||||
#include <QJsonValue>
|
#include <QJsonValue>
|
||||||
#include <QJsonArray>
|
#include <QJsonArray>
|
||||||
#include <QFileInfo>
|
#include <QFileInfo>
|
||||||
#include <QFile>
|
#include <QFile>
|
||||||
#include "SMSubProcess.h"
|
#include "SMSubProcess.h"
|
||||||
#include "SMSubServer.h"
|
#include "SMSubServer.h"
|
||||||
#include "smsub.h"
|
#include "smsub.h"
|
||||||
|
|
||||||
#ifdef Q_OS_UNIX
|
#ifdef Q_OS_UNIX
|
||||||
#include <initializer_list>
|
#include <initializer_list>
|
||||||
#include "signal.h"
|
#include "signal.h"
|
||||||
#include "unistd.h"
|
#include "unistd.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef Q_OS_UNIX
|
#ifdef Q_OS_UNIX
|
||||||
void catchUnixSignals(std::initializer_list<int> quitSignals) {
|
void catchUnixSignals(std::initializer_list<int> quitSignals) {
|
||||||
auto handler = [](int sig) -> void {
|
auto handler = [](int sig) -> void {
|
||||||
QString unixSignal;
|
QString unixSignal;
|
||||||
switch (sig) {
|
switch (sig) {
|
||||||
case SIGINT:
|
case SIGINT:
|
||||||
unixSignal = QLatin1String("SIGINT");
|
unixSignal = QLatin1String("SIGINT");
|
||||||
break;
|
break;
|
||||||
case SIGHUP:
|
case SIGHUP:
|
||||||
unixSignal = QLatin1String("SIGHUP");
|
unixSignal = QLatin1String("SIGHUP");
|
||||||
break;
|
break;
|
||||||
case SIGQUIT:
|
case SIGQUIT:
|
||||||
unixSignal = QLatin1String("SIGQUIT");
|
unixSignal = QLatin1String("SIGQUIT");
|
||||||
break;
|
break;
|
||||||
case SIGTERM:
|
case SIGTERM:
|
||||||
unixSignal = QLatin1String("SIGTERM");
|
unixSignal = QLatin1String("SIGTERM");
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
unixSignal = QString::number(sig);
|
unixSignal = QString::number(sig);
|
||||||
}
|
}
|
||||||
QTextStream(stderr) << "Received Unix signal: " << unixSignal << endl;
|
QTextStream(stderr) << "Received Unix signal: " << unixSignal << endl;
|
||||||
QCoreApplication::quit();
|
QCoreApplication::quit();
|
||||||
};
|
};
|
||||||
|
|
||||||
sigset_t blocking_mask;
|
sigset_t blocking_mask;
|
||||||
sigemptyset(&blocking_mask);
|
sigemptyset(&blocking_mask);
|
||||||
for (int sig : quitSignals)
|
for (int sig : quitSignals)
|
||||||
sigaddset(&blocking_mask, sig);
|
sigaddset(&blocking_mask, sig);
|
||||||
|
|
||||||
struct sigaction sa;
|
struct sigaction sa;
|
||||||
sa.sa_handler = handler;
|
sa.sa_handler = handler;
|
||||||
sa.sa_mask = blocking_mask;
|
sa.sa_mask = blocking_mask;
|
||||||
sa.sa_flags = 0;
|
sa.sa_flags = 0;
|
||||||
|
|
||||||
for (int sig : quitSignals)
|
for (int sig : quitSignals)
|
||||||
sigaction(sig, &sa, nullptr);
|
sigaction(sig, &sa, nullptr);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
QCoreApplication a(argc, argv);
|
QCoreApplication a(argc, argv);
|
||||||
a.setApplicationName("Server Manager Subprocess");
|
a.setApplicationName("Server Manager Subprocess");
|
||||||
a.setApplicationVersion("0.3");
|
a.setApplicationVersion("0.3.1");
|
||||||
|
|
||||||
#ifdef Q_OS_UNIX
|
#ifdef Q_OS_UNIX
|
||||||
catchUnixSignals({SIGINT, SIGHUP, SIGQUIT, SIGTERM});
|
catchUnixSignals({SIGINT, SIGHUP, SIGQUIT, SIGTERM});
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
QCommandLineParser commandLineParser;
|
QCommandLineParser commandLineParser;
|
||||||
commandLineParser.addHelpOption();
|
commandLineParser.addHelpOption();
|
||||||
commandLineParser.addVersionOption();
|
commandLineParser.addVersionOption();
|
||||||
|
|
||||||
QCommandLineOption processManifest("json", "JSON process manifest.", "json");
|
QCommandLineOption processManifest("json", "JSON process manifest.", "json");
|
||||||
commandLineParser.addOption(processManifest);
|
commandLineParser.addOption(processManifest);
|
||||||
|
|
||||||
QCommandLineOption processExecutable(QStringList() << "exec" << "executable", "Process executable to run.", "exec");
|
QCommandLineOption processExecutable(QStringList() << "exec" << "executable", "Process executable to run.", "exec");
|
||||||
commandLineParser.addOption(processExecutable);
|
commandLineParser.addOption(processExecutable);
|
||||||
|
|
||||||
QCommandLineOption processArguments(QStringList() << "args" << "arguments", "Arguments given to process.", "args");
|
QCommandLineOption processArguments(QStringList() << "args" << "arguments", "Arguments given to process.", "args");
|
||||||
commandLineParser.addOption(processArguments);
|
commandLineParser.addOption(processArguments);
|
||||||
|
|
||||||
#ifdef Q_OS_WIN
|
#ifdef Q_OS_WIN
|
||||||
QCommandLineOption subprocessSocket(QStringList() << "sock" << "socket", "IPC socket used for local communication.", "sock");
|
QCommandLineOption subprocessSocket(QStringList() << "sock" << "socket", "IPC socket used for local communication.", "sock");
|
||||||
#else
|
#else
|
||||||
QCommandLineOption subprocessSocket(QStringList() << "sock" << "socket", "Unix socket used for local communication.", "sock");
|
QCommandLineOption subprocessSocket(QStringList() << "sock" << "socket", "Unix socket used for local communication.", "sock");
|
||||||
#endif
|
#endif
|
||||||
commandLineParser.addOption(subprocessSocket);
|
commandLineParser.addOption(subprocessSocket);
|
||||||
|
|
||||||
QCommandLineOption subprocessRemotePort("rport", "WebSockets port used for remote communication.", "rport");
|
QCommandLineOption subprocessRemotePort("rport", "WebSockets port used for remote communication.", "rport");
|
||||||
commandLineParser.addOption(subprocessRemotePort);
|
commandLineParser.addOption(subprocessRemotePort);
|
||||||
|
|
||||||
#ifdef Q_OS_WIN
|
#ifdef Q_OS_WIN
|
||||||
QCommandLineOption subprocessRemoteSocket(QStringList() << "rsock" << "rsocket", "IPC socket used for remote communication.", "rsock");
|
QCommandLineOption subprocessRemoteSocket(QStringList() << "rsock" << "rsocket", "IPC socket used for remote communication.", "rsock");
|
||||||
#else
|
#else
|
||||||
QCommandLineOption subprocessRemoteSocket(QStringList() << "rsock" << "rsocket", "Unix socket used for remote communication.", "rsock");
|
QCommandLineOption subprocessRemoteSocket(QStringList() << "rsock" << "rsocket", "Unix socket used for remote communication.", "rsock");
|
||||||
#endif
|
#endif
|
||||||
commandLineParser.addOption(subprocessRemoteSocket);
|
commandLineParser.addOption(subprocessRemoteSocket);
|
||||||
|
|
||||||
QCommandLineOption processTimeout("timeout", "SMSub termination timeout.", "timeout");
|
QCommandLineOption processTimeout("timeout", "SMSub termination timeout.", "timeout");
|
||||||
commandLineParser.addOption(processTimeout);
|
commandLineParser.addOption(processTimeout);
|
||||||
|
|
||||||
commandLineParser.process(a);
|
commandLineParser.process(a);
|
||||||
|
|
||||||
if (unlikely(commandLineParser.isSet(processManifest) && commandLineParser.isSet(processExecutable))) {
|
if (unlikely(commandLineParser.isSet(processManifest) && commandLineParser.isSet(processExecutable))) {
|
||||||
QTextStream(stderr) << "You can't define a Process executable and a JSON process manifest at the same time!" << endl;
|
QTextStream(stderr) << "You can't define a Process executable and a JSON process manifest at the same time!" << endl;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (unlikely(commandLineParser.isSet(subprocessRemotePort) && commandLineParser.isSet(subprocessRemoteSocket))) {
|
if (unlikely(commandLineParser.isSet(subprocessRemotePort) && commandLineParser.isSet(subprocessRemoteSocket))) {
|
||||||
#ifdef Q_OS_WIN
|
#ifdef Q_OS_WIN
|
||||||
QTextStream(stderr) << "You can't define a WebSockets port and a IPC socket at same time!" << endl;
|
QTextStream(stderr) << "You can't define a WebSockets port and a IPC socket at same time!" << endl;
|
||||||
#else
|
#else
|
||||||
QTextStream(stderr) << "You can't define a WebSockets port and a Unix socket at same time!" << endl;
|
QTextStream(stderr) << "You can't define a WebSockets port and a Unix socket at same time!" << endl;
|
||||||
#endif
|
#endif
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool timeoutSet = false;
|
bool timeoutSet = false;
|
||||||
int termTimeout = 60000;
|
int termTimeout = 60000;
|
||||||
if (unlikely(commandLineParser.isSet(processTimeout))) {
|
if (unlikely(commandLineParser.isSet(processTimeout))) {
|
||||||
bool ok;
|
bool ok;
|
||||||
const int _termTimeout = commandLineParser.value(processTimeout).toInt(&ok);
|
const int _termTimeout = commandLineParser.value(processTimeout).toInt(&ok);
|
||||||
if (ok) {
|
if (ok) {
|
||||||
termTimeout = _termTimeout;
|
termTimeout = _termTimeout;
|
||||||
timeoutSet = true;
|
timeoutSet = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
QString executable;
|
QString executable;
|
||||||
QString workingDirectory;
|
QString workingDirectory;
|
||||||
QStringList argumentList;
|
QStringList argumentList;
|
||||||
if (likely(commandLineParser.isSet(processManifest))) {
|
if (likely(commandLineParser.isSet(processManifest))) {
|
||||||
QFile manifestFile(commandLineParser.value(processManifest));
|
QFile manifestFile(commandLineParser.value(processManifest));
|
||||||
if (likely(manifestFile.open(QIODevice::ReadOnly))) {
|
if (likely(manifestFile.open(QIODevice::ReadOnly))) {
|
||||||
const QByteArray jsonData = manifestFile.readAll();
|
const QByteArray jsonData = manifestFile.readAll();
|
||||||
QJsonDocument jsonDocument = QJsonDocument::fromJson(jsonData);
|
QJsonDocument jsonDocument = QJsonDocument::fromJson(jsonData);
|
||||||
QJsonObject jsonObject = jsonDocument.object();
|
QJsonObject jsonObject = jsonDocument.object();
|
||||||
|
|
||||||
if (likely(jsonObject.contains("Executable"))) {
|
if (likely(jsonObject.contains("Executable"))) {
|
||||||
const QJsonValue jsonExecutable = jsonObject.value("Executable");
|
const QJsonValue jsonExecutable = jsonObject.value("Executable");
|
||||||
if (unlikely(!jsonExecutable.isString())) {
|
if (unlikely(!jsonExecutable.isString())) {
|
||||||
QTextStream(stderr) << "Executable is not a string in manifest, aborting!" << endl;
|
QTextStream(stderr) << "Executable is not a string in manifest, aborting!" << endl;
|
||||||
manifestFile.close();
|
manifestFile.close();
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
executable = jsonExecutable.toString();
|
executable = jsonExecutable.toString();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
QTextStream(stderr) << "Executable is not defined in manifest, aborting!" << endl;
|
QTextStream(stderr) << "Executable is not defined in manifest, aborting!" << endl;
|
||||||
manifestFile.close();
|
manifestFile.close();
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (likely(jsonObject.contains("WorkingDirectory"))) {
|
if (likely(jsonObject.contains("WorkingDirectory"))) {
|
||||||
const QJsonValue jsonWorkingDirectory = jsonObject.value("WorkingDirectory");
|
const QJsonValue jsonWorkingDirectory = jsonObject.value("WorkingDirectory");
|
||||||
if (unlikely(!jsonWorkingDirectory.isString())) {
|
if (unlikely(!jsonWorkingDirectory.isString())) {
|
||||||
QTextStream(stderr) << "Working Directory is not a string in manifest, aborting!" << endl;
|
QTextStream(stderr) << "Working Directory is not a string in manifest, aborting!" << endl;
|
||||||
manifestFile.close();
|
manifestFile.close();
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
workingDirectory = jsonWorkingDirectory.toString();
|
workingDirectory = jsonWorkingDirectory.toString();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
workingDirectory = QFileInfo(executable).absolutePath();
|
workingDirectory = QFileInfo(executable).absolutePath();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (likely(jsonObject.contains("Arguments"))) {
|
if (likely(jsonObject.contains("Arguments"))) {
|
||||||
const QJsonValue jsonArguments = jsonObject.value("Arguments");
|
const QJsonValue jsonArguments = jsonObject.value("Arguments");
|
||||||
if (likely(jsonArguments.isArray())) {
|
if (likely(jsonArguments.isArray())) {
|
||||||
const QJsonArray jsonArray = jsonArguments.toArray();
|
const QJsonArray jsonArray = jsonArguments.toArray();
|
||||||
QJsonArray::const_iterator it = jsonArray.constBegin();
|
QJsonArray::const_iterator it = jsonArray.constBegin();
|
||||||
QJsonArray::const_iterator end = jsonArray.constEnd();
|
QJsonArray::const_iterator end = jsonArray.constEnd();
|
||||||
while (it != end) {
|
while (it != end) {
|
||||||
argumentList << it->toString();
|
argumentList << it->toString();
|
||||||
it++;
|
it++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
QTextStream(stderr) << "Arguments is not a array in manifest, aborting!" << endl;
|
QTextStream(stderr) << "Arguments is not a array in manifest, aborting!" << endl;
|
||||||
manifestFile.close();
|
manifestFile.close();
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (unlikely(!timeoutSet && jsonObject.contains("TerminationTimeout"))) {
|
if (unlikely(!timeoutSet && jsonObject.contains("TerminationTimeout"))) {
|
||||||
const QJsonValue jsonTimeout = jsonObject.value("TerminationTimeout");
|
const QJsonValue jsonTimeout = jsonObject.value("TerminationTimeout");
|
||||||
if (unlikely(!jsonTimeout.isDouble())) {
|
if (unlikely(!jsonTimeout.isDouble())) {
|
||||||
termTimeout = qRound(jsonTimeout.toDouble());
|
termTimeout = qRound(jsonTimeout.toDouble());
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
QTextStream(stderr) << "Termination Timeout is not a number in manifest, aborting!" << endl;
|
QTextStream(stderr) << "Termination Timeout is not a number in manifest, aborting!" << endl;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
manifestFile.close();
|
manifestFile.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (unlikely(commandLineParser.isSet(processArguments))) {
|
else if (unlikely(commandLineParser.isSet(processArguments))) {
|
||||||
QTextStream(stderr) << "Arguments over command line are not supported yet!" << endl;
|
QTextStream(stderr) << "Arguments over command line are not supported yet!" << endl;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
QString socket;
|
QString socket;
|
||||||
if (likely(commandLineParser.isSet(subprocessSocket))) {
|
if (likely(commandLineParser.isSet(subprocessSocket))) {
|
||||||
socket = commandLineParser.value(subprocessSocket);
|
socket = commandLineParser.value(subprocessSocket);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
#ifdef Q_OS_WIN
|
#ifdef Q_OS_WIN
|
||||||
QTextStream(stderr) << "You must define at least a local IPC socket!" << endl;
|
QTextStream(stderr) << "You must define at least a local IPC socket!" << endl;
|
||||||
#else
|
#else
|
||||||
QTextStream(stderr) << "You must define at least a local Unix socket!" << endl;
|
QTextStream(stderr) << "You must define at least a local Unix socket!" << endl;
|
||||||
#endif
|
#endif
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
SMSubServerSettings localSettings;
|
SMSubServerSettings localSettings;
|
||||||
localSettings.isLocal = true;
|
localSettings.isLocal = true;
|
||||||
|
|
||||||
SMSubServer subLocal(&localSettings, socket);
|
SMSubServer subLocal(&localSettings, socket);
|
||||||
if (unlikely(!subLocal.isListening())) {
|
if (unlikely(!subLocal.isListening())) {
|
||||||
#ifdef Q_OS_WIN
|
#ifdef Q_OS_WIN
|
||||||
QTextStream(stderr) << "Failed to start local IPC socket!" << endl;
|
QTextStream(stderr) << "Failed to start local IPC socket!" << endl;
|
||||||
#else
|
#else
|
||||||
QTextStream(stderr) << "Failed to start local Unix socket!" << endl;
|
QTextStream(stderr) << "Failed to start local Unix socket!" << endl;
|
||||||
#endif
|
#endif
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
QString rsocket;
|
QString rsocket;
|
||||||
if (unlikely(commandLineParser.isSet(subprocessRemoteSocket))) {
|
if (unlikely(commandLineParser.isSet(subprocessRemoteSocket))) {
|
||||||
rsocket = commandLineParser.value(subprocessRemoteSocket);
|
rsocket = commandLineParser.value(subprocessRemoteSocket);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool rportSet = false;
|
bool rportSet = false;
|
||||||
quint16 rport;
|
quint16 rport;
|
||||||
if (unlikely(commandLineParser.isSet(subprocessRemotePort))) {
|
if (unlikely(commandLineParser.isSet(subprocessRemotePort))) {
|
||||||
bool ok;
|
bool ok;
|
||||||
rport = commandLineParser.value(subprocessRemotePort).toUShort(&ok);
|
rport = commandLineParser.value(subprocessRemotePort).toUShort(&ok);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
QTextStream(stderr) << "WebSockets port is not valid!" << endl;
|
QTextStream(stderr) << "WebSockets port is not valid!" << endl;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
rportSet = true;
|
rportSet = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SMSubProcess subProcess(executable, argumentList, workingDirectory, termTimeout);
|
SMSubProcess subProcess(executable, argumentList, workingDirectory, termTimeout);
|
||||||
|
|
||||||
SMSubServerSettings remoteSettings;
|
SMSubServerSettings remoteSettings;
|
||||||
remoteSettings.canRegister = false;
|
remoteSettings.canRegister = false;
|
||||||
remoteSettings.isLocal = false;
|
remoteSettings.isLocal = false;
|
||||||
|
|
||||||
if (unlikely(!rsocket.isEmpty())) {
|
if (unlikely(!rsocket.isEmpty())) {
|
||||||
SMSubServer *subRemote = new SMSubServer(&remoteSettings, rsocket);
|
SMSubServer *subRemote = new SMSubServer(&remoteSettings, rsocket);
|
||||||
if (unlikely(!subRemote->isListening())) {
|
if (unlikely(!subRemote->isListening())) {
|
||||||
#ifdef Q_OS_WIN
|
#ifdef Q_OS_WIN
|
||||||
QTextStream(stderr) << "Failed to start remote IPC socket!" << endl;
|
QTextStream(stderr) << "Failed to start remote IPC socket!" << endl;
|
||||||
#else
|
#else
|
||||||
QTextStream(stderr) << "Failed to start remote Unix socket!" << endl;
|
QTextStream(stderr) << "Failed to start remote Unix socket!" << endl;
|
||||||
#endif
|
#endif
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
localSettings.canRegister = true;
|
localSettings.canRegister = true;
|
||||||
QObject::connect(&subLocal, &SMSubServer::tokenRegistered, subRemote, &SMSubServer::registerToken);
|
QObject::connect(&subLocal, &SMSubServer::tokenRegistered, subRemote, &SMSubServer::registerToken);
|
||||||
QObject::connect(&subProcess, &SMSubProcess::outputWritten, subRemote, &SMSubServer::writeOutput);
|
QObject::connect(&subProcess, &SMSubProcess::outputWritten, subRemote, &SMSubServer::writeOutput);
|
||||||
QObject::connect(subRemote, &SMSubServer::inputWritten, &subProcess, &SMSubProcess::writeInput);
|
QObject::connect(subRemote, &SMSubServer::inputWritten, &subProcess, &SMSubProcess::writeInput);
|
||||||
QObject::connect(subRemote, &SMSubServer::killRequested, &subProcess, &SMSubProcess::killProcess);
|
QObject::connect(subRemote, &SMSubServer::killRequested, &subProcess, &SMSubProcess::killProcess);
|
||||||
QObject::connect(subRemote, &SMSubServer::stopRequested, &subProcess, &SMSubProcess::stopProcess);
|
QObject::connect(subRemote, &SMSubServer::stopRequested, &subProcess, &SMSubProcess::stopProcess);
|
||||||
}
|
}
|
||||||
else if (unlikely(rportSet)) {
|
else if (unlikely(rportSet)) {
|
||||||
SMSubServer *subRemote = new SMSubServer(&remoteSettings, QString(), rport);
|
SMSubServer *subRemote = new SMSubServer(&remoteSettings, QString(), rport);
|
||||||
if (unlikely(!subRemote->isListening())) {
|
if (unlikely(!subRemote->isListening())) {
|
||||||
QTextStream(stderr) << "Failed to start remote WebSockets server!" << endl;
|
QTextStream(stderr) << "Failed to start remote WebSockets server!" << endl;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
localSettings.canRegister = true;
|
localSettings.canRegister = true;
|
||||||
QObject::connect(&subLocal, &SMSubServer::tokenRegistered, subRemote, &SMSubServer::registerToken);
|
QObject::connect(&subLocal, &SMSubServer::tokenRegistered, subRemote, &SMSubServer::registerToken);
|
||||||
QObject::connect(&subProcess, &SMSubProcess::outputWritten, subRemote, &SMSubServer::writeOutput);
|
QObject::connect(&subProcess, &SMSubProcess::outputWritten, subRemote, &SMSubServer::writeOutput);
|
||||||
QObject::connect(subRemote, &SMSubServer::inputWritten, &subProcess, &SMSubProcess::writeInput);
|
QObject::connect(subRemote, &SMSubServer::inputWritten, &subProcess, &SMSubProcess::writeInput);
|
||||||
QObject::connect(subRemote, &SMSubServer::killRequested, &subProcess, &SMSubProcess::killProcess);
|
QObject::connect(subRemote, &SMSubServer::killRequested, &subProcess, &SMSubProcess::killProcess);
|
||||||
QObject::connect(subRemote, &SMSubServer::stopRequested, &subProcess, &SMSubProcess::stopProcess);
|
QObject::connect(subRemote, &SMSubServer::stopRequested, &subProcess, &SMSubProcess::stopProcess);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
localSettings.canRegister = false;
|
localSettings.canRegister = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
QObject::connect(&subProcess, &SMSubProcess::outputWritten, &subLocal, &SMSubServer::writeOutput);
|
QObject::connect(&subProcess, &SMSubProcess::outputWritten, &subLocal, &SMSubServer::writeOutput);
|
||||||
QObject::connect(&subLocal, &SMSubServer::inputWritten, &subProcess, &SMSubProcess::writeInput);
|
QObject::connect(&subLocal, &SMSubServer::inputWritten, &subProcess, &SMSubProcess::writeInput);
|
||||||
QObject::connect(&subLocal, &SMSubServer::killRequested, &subProcess, &SMSubProcess::killProcess);
|
QObject::connect(&subLocal, &SMSubServer::killRequested, &subProcess, &SMSubProcess::killProcess);
|
||||||
QObject::connect(&subLocal, &SMSubServer::stopRequested, &subProcess, &SMSubProcess::stopProcess);
|
QObject::connect(&subLocal, &SMSubServer::stopRequested, &subProcess, &SMSubProcess::stopProcess);
|
||||||
QObject::connect(&a, &QCoreApplication::aboutToQuit, &subProcess, &SMSubProcess::aboutToQuit);
|
QObject::connect(&a, &QCoreApplication::aboutToQuit, &subProcess, &SMSubProcess::aboutToQuit);
|
||||||
|
|
||||||
subProcess.start();
|
subProcess.start();
|
||||||
|
|
||||||
return a.exec();
|
return a.exec();
|
||||||
}
|
}
|
||||||
|
|
70
smsub.pro
70
smsub.pro
|
@ -1,35 +1,35 @@
|
||||||
###############################################################################
|
###############################################################################
|
||||||
# smsub Server Manager Subprocess
|
# smsub Server Manager Subprocess
|
||||||
# Copyright (C) 2020 Syping
|
# Copyright (C) 2020 Syping
|
||||||
#
|
#
|
||||||
# Redistribution and use in source and binary forms, with or without modification,
|
# Redistribution and use in source and binary forms, with or without modification,
|
||||||
# are permitted provided that the following conditions are met:
|
# are permitted provided that the following conditions are met:
|
||||||
#
|
#
|
||||||
# 1. Redistributions of source code must retain the above copyright notice,
|
# 1. Redistributions of source code must retain the above copyright notice,
|
||||||
# this list of conditions and the following disclaimer.
|
# this list of conditions and the following disclaimer.
|
||||||
#
|
#
|
||||||
# 2. Redistributions in binary form must reproduce the above copyright notice,
|
# 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||||
# this list of conditions and the following disclaimer in the documentation
|
# this list of conditions and the following disclaimer in the documentation
|
||||||
# and/or other materials provided with the distribution.
|
# and/or other materials provided with the distribution.
|
||||||
#
|
#
|
||||||
# This software is provided as-is, no warranties are given to you, we are not
|
# 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.
|
# responsible for anything with use of the software, you are self responsible.
|
||||||
###############################################################################
|
###############################################################################
|
||||||
|
|
||||||
QT -= gui
|
QT -= gui
|
||||||
QT += network websockets
|
QT += network websockets
|
||||||
|
|
||||||
CONFIG += c++11 console
|
CONFIG += c++11 console
|
||||||
CONFIG -= app_bundle
|
CONFIG -= app_bundle
|
||||||
|
|
||||||
SOURCES += main.cpp \
|
SOURCES += main.cpp \
|
||||||
SMSubProcess.cpp \
|
SMSubProcess.cpp \
|
||||||
SMSubServer.cpp
|
SMSubServer.cpp
|
||||||
|
|
||||||
HEADERS += smsub.h \
|
HEADERS += smsub.h \
|
||||||
SMSubProcess.h \
|
SMSubProcess.h \
|
||||||
SMSubServer.h
|
SMSubServer.h
|
||||||
|
|
||||||
qnx: target.path = /tmp/$${TARGET}/bin
|
qnx: target.path = /tmp/$${TARGET}/bin
|
||||||
else: unix:!android: target.path = /opt/$${TARGET}/bin
|
else: unix:!android: target.path = /opt/$${TARGET}/bin
|
||||||
!isEmpty(target.path): INSTALLS += target
|
!isEmpty(target.path): INSTALLS += target
|
||||||
|
|
Loading…
Reference in a new issue