77 lines
2.4 KiB
C++
77 lines
2.4 KiB
C++
#include <QCommandLineParser>
|
|
#include <QCoreApplication>
|
|
#include <QJsonDocument>
|
|
#include <QTextStream>
|
|
#include <QJsonObject>
|
|
#include <QJsonArray>
|
|
#include <QFile>
|
|
#include "Runner.h"
|
|
#include "Socket.h"
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
QCoreApplication::setSetuidAllowed(true);
|
|
QCoreApplication a(argc, argv);
|
|
|
|
QCommandLineParser parser;
|
|
parser.addPositionalArgument("config", "Ping configuration");
|
|
parser.addPositionalArgument("socket", "Unix socket");
|
|
parser.process(a);
|
|
|
|
const QStringList args = parser.positionalArguments();
|
|
if (args.length() != 2) {
|
|
QTextStream(stderr) << "Not enough or too many arguments was given!" << endl;
|
|
return 1;
|
|
}
|
|
const QString pingConfig = args.at(0);
|
|
const QString unixSocket = args.at(1);
|
|
|
|
QByteArray jsonContent;
|
|
QFile jsonFile(pingConfig);
|
|
if (jsonFile.open(QFile::ReadOnly)) {
|
|
jsonContent = jsonFile.readAll();
|
|
jsonFile.close();
|
|
}
|
|
else {
|
|
QTextStream(stderr) << "Failed to read Ping configuration!" << endl;
|
|
return 1;
|
|
}
|
|
|
|
QList<QString> pingHosts;
|
|
QJsonDocument jsonDocument = QJsonDocument::fromJson(jsonContent);
|
|
QJsonObject jsonObject = jsonDocument.object();
|
|
QJsonObject::const_iterator it = jsonObject.constBegin();
|
|
QJsonObject::const_iterator end = jsonObject.constEnd();
|
|
while (it != end) {
|
|
if (it.value().isArray()) {
|
|
QJsonArray array = it.value().toArray();
|
|
QJsonArray::const_iterator it = array.constBegin();
|
|
QJsonArray::const_iterator end = array.constEnd();
|
|
while (it != end) {
|
|
const QString host = array.at(it.i).toString();
|
|
if (!host.isEmpty() && host != "none" && !pingHosts.contains(host))
|
|
pingHosts << host;
|
|
it++;
|
|
}
|
|
}
|
|
else if (it.value().isString()) {
|
|
const QString host = it.value().toString();
|
|
if (!host.isEmpty() && host != "none" && !pingHosts.contains(host))
|
|
pingHosts << host;
|
|
}
|
|
it++;
|
|
}
|
|
|
|
Runner runner(pingHosts);
|
|
Socket socket(&runner);
|
|
socket.setSocketOptions(QLocalServer::WorldAccessOption);
|
|
if (!socket.listen(unixSocket)) {
|
|
QTextStream(stderr) << "Failed to initialise Unix socket!" << endl;
|
|
return 1;
|
|
}
|
|
|
|
runner.start();
|
|
|
|
return a.exec();
|
|
}
|