1
0
Fork 0
mirror of https://gitlab.com/Syping/mayu synced 2024-11-22 03:50:22 +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 <QTextStream>
#include <QDataStream>
#include <QEventLoop>
#include <QHostInfo>
#include <QSaveFile>
@ -144,7 +146,7 @@ double mayu::ping(const QString &host, int tries, double timeout)
return -1;
}
#ifdef E_DEBUG
qDebug() << "IPv4 Address" << hostAddress.toString() << "found";
QTextStream(stderr) << "IPv4 Address " << hostAddress.toString() << " found" << endl;
#endif
}
else if (QAbstractSocket::IPv6Protocol == hostAddress.protocol()) {
@ -153,7 +155,7 @@ double mayu::ping(const QString &host, int tries, double timeout)
return -1;
}
#ifdef E_DEBUG
qDebug() << "IPv6 Address" << hostAddress.toString() << "found";
QTextStream(stderr) << "IPv6 Address " << hostAddress.toString() << " found" << endl;
#endif
}
else {
@ -165,12 +167,12 @@ double mayu::ping(const QString &host, int tries, double timeout)
return -1;
}
#ifdef E_DEBUG
qDebug() << "Hostname" << host << "found and resolved" << ipStr;
QTextStream(stderr) << "Hostname " << host << " found and resolved " << ipStr << endl;
#endif
}
else {
#ifdef E_DEBUG
qDebug() << "Hostname" << host << "not found";
QTextStream(stderr) << "Hostname " << host << " not found" << endl;
#endif
ping_destroy(pingObj);
return -1;
@ -202,7 +204,7 @@ double mayu::ping(const QString &host, int tries, double timeout)
else {
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
}
if (pingSuccess) {
@ -224,7 +226,7 @@ const QList<mayuResult> mayu::resolve(const QString &host, bool emptyWhenError)
if (hostAddresses.length() >= 1) {
for (const QHostAddress &hostAddress : hostAddresses) {
#ifdef E_DEBUG
qDebug() << "Hostname" << host << "found and resolved" << hostAddress.toString();
QTextStream(stderr) << "Hostname " << host << " found and resolved " << hostAddress.toString() << endl;
#endif
mayuResult m_result;
m_result.host = host;
@ -234,7 +236,7 @@ const QList<mayuResult> mayu::resolve(const QString &host, bool emptyWhenError)
}
else {
#ifdef E_DEBUG
qDebug() << "Hostname" << host << "not found";
QTextStream(stderr) << "Hostname " << host << " not found" << endl;
#endif
if (!emptyWhenError) {
mayuResult m_result;
@ -249,6 +251,7 @@ const QList<mayuResult> mayu::resolve(const QString &host, bool emptyWhenError)
void mayu::parse_hosts()
{
p_hostsList.clear();
if (p_hostsFile != "-") {
#ifdef PRIVILEGE_DROP_REQUIRED
if (!p_dropPrivileges()) {
p_return = 2;
@ -258,23 +261,8 @@ void mayu::parse_hosts()
QFile hostsFile(p_hostsFile);
if (hostsFile.open(QFile::ReadOnly)) {
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();
p_workHosts(hostsArray);
p_hostsParsed = true;
}
else
@ -288,12 +276,20 @@ void mayu::parse_hosts()
}
#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;
jsonDocument.setObject(jsonObject);
QByteArray jsonArray = jsonDocument.toJson();
if (p_jsonFile != "-") {
#ifdef PRIVILEGE_DROP_REQUIRED
if (!p_dropPrivileges()) {
p_return = 2;
@ -318,6 +314,10 @@ void mayu::p_saveWork(QJsonObject jsonObject)
return;
}
#endif
}
else {
QTextStream(stdout) << jsonArray;
}
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
void mayu::p_workPing()
{

3
mayu.h
View file

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