1
0
Fork 0
mirror of https://gitlab.com/Syping/mayu synced 2024-11-22 12:00:23 +01:00

add pipe support

This commit is contained in:
Syping 2018-05-15 09:46:21 +02:00
parent d5663e583b
commit 2f6507f7c5
2 changed files with 81 additions and 60 deletions

View file

@ -16,6 +16,8 @@
*****************************************************************************/ *****************************************************************************/
#include <QHostAddress> #include <QHostAddress>
#include <QTextStream>
#include <QDataStream>
#include <QEventLoop> #include <QEventLoop>
#include <QHostInfo> #include <QHostInfo>
#include <QSaveFile> #include <QSaveFile>
@ -144,7 +146,7 @@ double mayu::ping(const QString &host, int tries, double timeout)
return -1; return -1;
} }
#ifdef E_DEBUG #ifdef E_DEBUG
qDebug() << "IPv4 Address" << hostAddress.toString() << "found"; QTextStream(stderr) << "IPv4 Address " << hostAddress.toString() << " found" << endl;
#endif #endif
} }
else if (QAbstractSocket::IPv6Protocol == hostAddress.protocol()) { else if (QAbstractSocket::IPv6Protocol == hostAddress.protocol()) {
@ -153,7 +155,7 @@ double mayu::ping(const QString &host, int tries, double timeout)
return -1; return -1;
} }
#ifdef E_DEBUG #ifdef E_DEBUG
qDebug() << "IPv6 Address" << hostAddress.toString() << "found"; QTextStream(stderr) << "IPv6 Address " << hostAddress.toString() << " found" << endl;
#endif #endif
} }
else { else {
@ -165,12 +167,12 @@ double mayu::ping(const QString &host, int tries, double timeout)
return -1; return -1;
} }
#ifdef E_DEBUG #ifdef E_DEBUG
qDebug() << "Hostname" << host << "found and resolved" << ipStr; QTextStream(stderr) << "Hostname " << host << " found and resolved " << ipStr << endl;
#endif #endif
} }
else { else {
#ifdef E_DEBUG #ifdef E_DEBUG
qDebug() << "Hostname" << host << "not found"; QTextStream(stderr) << "Hostname " << host << " not found" << endl;
#endif #endif
ping_destroy(pingObj); ping_destroy(pingObj);
return -1; return -1;
@ -202,7 +204,7 @@ double mayu::ping(const QString &host, int tries, double timeout)
else { else {
latencyString = QString::number(latency); latencyString = QString::number(latency);
} }
QTextStream(stdout) << "Host: " << hostname << " Ping: " << latencyString << " Status: " << (pingSuccess ? "true" : "false") << endl; QTextStream(stderr) << "Host: " << hostname << " Ping: " << latencyString << " Status: " << (pingSuccess ? "true" : "false") << endl;
#endif #endif
} }
if (pingSuccess) { if (pingSuccess) {
@ -224,7 +226,7 @@ const QList<mayuResult> mayu::resolve(const QString &host, bool emptyWhenError)
if (hostAddresses.length() >= 1) { if (hostAddresses.length() >= 1) {
for (const QHostAddress &hostAddress : hostAddresses) { for (const QHostAddress &hostAddress : hostAddresses) {
#ifdef E_DEBUG #ifdef E_DEBUG
qDebug() << "Hostname" << host << "found and resolved" << hostAddress.toString(); QTextStream(stderr) << "Hostname " << host << " found and resolved " << hostAddress.toString() << endl;
#endif #endif
mayuResult m_result; mayuResult m_result;
m_result.host = host; m_result.host = host;
@ -234,7 +236,7 @@ const QList<mayuResult> mayu::resolve(const QString &host, bool emptyWhenError)
} }
else { else {
#ifdef E_DEBUG #ifdef E_DEBUG
qDebug() << "Hostname" << host << "not found"; QTextStream(stderr) << "Hostname " << host << " not found" << endl;
#endif #endif
if (!emptyWhenError) { if (!emptyWhenError) {
mayuResult m_result; mayuResult m_result;
@ -249,6 +251,7 @@ const QList<mayuResult> mayu::resolve(const QString &host, bool emptyWhenError)
void mayu::parse_hosts() void mayu::parse_hosts()
{ {
p_hostsList.clear(); p_hostsList.clear();
if (p_hostsFile != "-") {
#ifdef PRIVILEGE_DROP_REQUIRED #ifdef PRIVILEGE_DROP_REQUIRED
if (!p_dropPrivileges()) { if (!p_dropPrivileges()) {
p_return = 2; p_return = 2;
@ -258,23 +261,8 @@ void mayu::parse_hosts()
QFile hostsFile(p_hostsFile); QFile hostsFile(p_hostsFile);
if (hostsFile.open(QFile::ReadOnly)) { if (hostsFile.open(QFile::ReadOnly)) {
const QList<QByteArray> hostsArray = hostsFile.readAll().split('\n'); const QList<QByteArray> hostsArray = hostsFile.readAll().split('\n');
for (const QByteArray &lineArray : hostsArray) {
QString lineStr = QString::fromUtf8(lineArray).trimmed();
if (!lineStr.isEmpty()) {
QStringList lineStrList = lineStr.split(',');
QString hostStr;
QString alternativeStr;
if (lineStrList.length() >= 1) {
hostStr = lineStrList.at(0);
lineStrList.removeAt(0);
if (lineStrList.length() >= 1) {
alternativeStr = lineStrList.join(','); // Alternative Name in Future Version
}
p_hostsList += hostStr;
}
}
}
hostsFile.close(); hostsFile.close();
p_workHosts(hostsArray);
p_hostsParsed = true; p_hostsParsed = true;
} }
else else
@ -288,12 +276,20 @@ void mayu::parse_hosts()
} }
#endif #endif
} }
else {
QByteArray b_hostsArray = QTextStream(stdin).readAll().replace("\\n", "\n").toUtf8();
const QList<QByteArray> hostsArray = b_hostsArray.split('\n');
p_workHosts(hostsArray);
p_hostsParsed = true;
}
}
void mayu::p_saveWork(QJsonObject jsonObject) void mayu::p_saveWork(const QJsonObject &jsonObject)
{ {
QJsonDocument jsonDocument; QJsonDocument jsonDocument;
jsonDocument.setObject(jsonObject); jsonDocument.setObject(jsonObject);
QByteArray jsonArray = jsonDocument.toJson(); QByteArray jsonArray = jsonDocument.toJson();
if (p_jsonFile != "-") {
#ifdef PRIVILEGE_DROP_REQUIRED #ifdef PRIVILEGE_DROP_REQUIRED
if (!p_dropPrivileges()) { if (!p_dropPrivileges()) {
p_return = 2; p_return = 2;
@ -318,6 +314,10 @@ void mayu::p_saveWork(QJsonObject jsonObject)
return; return;
} }
#endif #endif
}
else {
QTextStream(stdout) << jsonArray;
}
p_return = 0; p_return = 0;
} }
@ -337,6 +337,26 @@ void mayu::work()
} }
} }
void mayu::p_workHosts(const QList<QByteArray> &hostsArray)
{
for (const QByteArray &lineArray : hostsArray) {
QString lineStr = QString::fromUtf8(lineArray).trimmed();
if (!lineStr.isEmpty()) {
QStringList lineStrList = lineStr.split(',');
QString hostStr;
QString alternativeStr;
if (lineStrList.length() >= 1) {
hostStr = lineStrList.at(0);
lineStrList.removeAt(0);
if (lineStrList.length() >= 1) {
alternativeStr = lineStrList.join(','); // Alternative Name in Future Version
}
p_hostsList += hostStr;
}
}
}
}
#ifdef MAYU_UNIX #ifdef MAYU_UNIX
void mayu::p_workPing() void mayu::p_workPing()
{ {

3
mayu.h
View file

@ -65,7 +65,8 @@ private:
bool p_dropPrivileges(); bool p_dropPrivileges();
bool p_regainPrivileges(); bool p_regainPrivileges();
#endif #endif
void p_saveWork(QJsonObject jsonObject); void p_saveWork(const QJsonObject &jsonObject);
void p_workHosts(const QList<QByteArray> &hostsArray);
#ifdef MAYU_UNIX #ifdef MAYU_UNIX
void p_workPing(); void p_workPing();
#endif #endif