From 6518be00897937b025f866fbc64d89cf1830e995 Mon Sep 17 00:00:00 2001 From: synacker Date: Sat, 9 Dec 2023 22:25:34 +0300 Subject: [PATCH 1/2] Added -x flag for pcap fix --- conanfile.py | 5 ++- src/Daggy/CConsoleDaggy.cpp | 62 ++++++++++++++++++++++---- src/Daggy/CConsoleDaggy.hpp | 11 +++++ src/Daggy/CMakeLists.txt | 4 ++ src/Daggy/Precompiled.hpp | 4 ++ src/DaggyCore/aggregators/CConsole.cpp | 2 +- src/DaggyCore/aggregators/CConsole.hpp | 9 ++-- src/DaggyCore/aggregators/CFile.cpp | 2 +- 8 files changed, 82 insertions(+), 17 deletions(-) diff --git a/conanfile.py b/conanfile.py index 09d8d1e0..49aa20ea 100644 --- a/conanfile.py +++ b/conanfile.py @@ -86,7 +86,10 @@ def requirements(self): self.requires("yaml-cpp/0.8.0") if self.options.with_ssh2: - self.requires("libssh2/1.11.0") + self.requires("libssh2/1.11.0") + + self.requires("pcapplusplus/23.09") + def layout(self): self.folders.source = "src" diff --git a/src/Daggy/CConsoleDaggy.cpp b/src/Daggy/CConsoleDaggy.cpp index 99e87e35..9c57069d 100644 --- a/src/Daggy/CConsoleDaggy.cpp +++ b/src/Daggy/CConsoleDaggy.cpp @@ -36,6 +36,7 @@ using namespace daggy; CConsoleDaggy::CConsoleDaggy(QObject* parent) : QObject(parent) , daggy_core_(nullptr) + , console_aggreagator_(nullptr) , need_hard_stop_(false) { qApp->setApplicationName("daggy"); @@ -43,36 +44,37 @@ CConsoleDaggy::CConsoleDaggy(QObject* parent) qApp->setOrganizationName(DAGGY_VENDOR); connect(this, &CConsoleDaggy::interrupt, this, &CConsoleDaggy::stop, Qt::QueuedConnection); + connect(qApp, &QCoreApplication::aboutToQuit, this, &CConsoleDaggy::fixPcaps); } std::error_code CConsoleDaggy::prepare() { if (daggy_core_) return errors::success; - const auto settings = parse(); + settings_ = parse(); Sources sources; - switch (settings.data_source_text_type) { + switch (settings_.data_source_text_type) { case Json: - sources = std::move(*sources::convertors::json(settings.data_source_text)); + sources = std::move(*sources::convertors::json(settings_.data_source_text)); break; case Yaml: - sources = std::move(*sources::convertors::yaml(settings.data_source_text)); + sources = std::move(*sources::convertors::yaml(settings_.data_source_text)); break; } - const QString& session = QDateTime::currentDateTime().toString("dd-MM-yy_hh-mm-ss-zzz") + "_" + settings.data_sources_name; + session_ = QDateTime::currentDateTime().toString("dd-MM-yy_hh-mm-ss-zzz") + "_" + settings_.data_sources_name; - daggy_core_ = new Core(session, std::move(sources), this); + daggy_core_ = new Core(session_, std::move(sources), this); connect(daggy_core_, &Core::stateChanged, this, &CConsoleDaggy::onDaggyCoreStateChanged); - auto file_aggregator = new aggregators::CFile(settings.output_folder); + auto file_aggregator = new aggregators::CFile(settings_.output_folder); file_aggregator->moveToThread(&file_thread_); connect(this, &CConsoleDaggy::destroyed, file_aggregator, &aggregators::CFile::deleteLater); - auto console_aggregator = new aggregators::CConsole(session, daggy_core_); + console_aggreagator_ = new aggregators::CConsole(session_, this); daggy_core_->connectAggregator(file_aggregator); - daggy_core_->connectAggregator(console_aggregator); + daggy_core_->connectAggregator(console_aggreagator_); return daggy_core_->prepare();; } @@ -155,11 +157,15 @@ CConsoleDaggy::Settings CConsoleDaggy::parse() const const QCommandLineOption input_from_stdin_option({"i", "stdin"}, "Read data aggregation sources from stdin"); + const QCommandLineOption fix_pcap_option({"x", "fix-pcap"}, + "Fix and convert pcap files to pcapng"); + QCommandLineParser command_line_parser; command_line_parser.addOption(output_folder_option); command_line_parser.addOption(input_format_option); command_line_parser.addOption(input_from_stdin_option); command_line_parser.addOption(auto_complete_timeout); + command_line_parser.addOption(fix_pcap_option); command_line_parser.addHelpOption(); command_line_parser.addVersionOption(); command_line_parser.addPositionalArgument("file", "data aggregation sources file", "*.yaml|*.yml|*.json"); @@ -200,6 +206,10 @@ CConsoleDaggy::Settings CConsoleDaggy::parse() const result.data_source_text_type = textFormatType(source_file_name); } + if (command_line_parser.isSet(fix_pcap_option)) { + result.fix_pcap = true; + } + if (command_line_parser.isSet(auto_complete_timeout)) { result.timeout = command_line_parser.value(auto_complete_timeout).toUInt(); } @@ -211,6 +221,40 @@ CConsoleDaggy::Settings CConsoleDaggy::parse() const return result; } +void CConsoleDaggy::fixPcaps() const +{ + if (!settings_.fix_pcap) + return; + + auto output_folder = QDir(QDir::cleanPath(settings_.output_folder + QDir::separator() + session_)); + QDirIterator pcap_files(output_folder.absolutePath(), {"*.pcap"}); + while (pcap_files.hasNext()) + { + const auto& pcap_file = pcap_files.next(); + const QString& pcap_name = QFileInfo(pcap_file).baseName(); + const auto& pcapng_file = QDir::cleanPath(output_folder.absolutePath() + QDir::separator() + pcap_name + ".pcapng"); + + std::unique_ptr reader(pcpp::IFileReaderDevice::getReader(qPrintable(pcap_file))); + + pcpp::PcapNgFileWriterDevice pcapNgWriter(qPrintable(pcapng_file)); + + if (!reader || !reader->open() || !pcapNgWriter.open()) + { + continue; + } + + pcpp::RawPacket rawPacket; + while (reader->getNextPacket(rawPacket)) + { + pcapNgWriter.writePacket(rawPacket); + } + reader->close(); + pcapNgWriter.close(); + output_folder.remove(pcap_file); + console_aggreagator_->printAppMessage(QString("fix pcap %1").arg(pcap_name)); + } +} + daggy::Core* CConsoleDaggy::daggyCore() const { return findChild(); diff --git a/src/Daggy/CConsoleDaggy.hpp b/src/Daggy/CConsoleDaggy.hpp index 1aa8eb96..e7acc4be 100644 --- a/src/Daggy/CConsoleDaggy.hpp +++ b/src/Daggy/CConsoleDaggy.hpp @@ -29,6 +29,12 @@ SOFTWARE. #include +namespace daggy { +namespace aggregators { +class CConsole; +} +} + #include "ISystemSignalHandler.hpp" class QCoreApplication; @@ -69,8 +75,10 @@ private slots: QString output_folder; QString data_sources_name; unsigned int timeout = 0; + bool fix_pcap = false; }; Settings parse() const; + void fixPcaps() const; daggy::Core* daggyCore() const; QCoreApplication* application() const; @@ -80,9 +88,12 @@ private slots: QString mustache(const QString& text, const QString& output_folder) const; + Settings settings_; + QString session_; QThread file_thread_; daggy::Core* daggy_core_; + daggy::aggregators::CConsole* console_aggreagator_; bool need_hard_stop_; QString error_message_; diff --git a/src/Daggy/CMakeLists.txt b/src/Daggy/CMakeLists.txt index b86bd5b4..bb02f212 100644 --- a/src/Daggy/CMakeLists.txt +++ b/src/Daggy/CMakeLists.txt @@ -27,6 +27,10 @@ endif() target_link_libraries(${TARGET} PRIVATE DaggyCore) + +find_package(PcapPlusPlus REQUIRED) +target_link_libraries(${TARGET} PRIVATE PcapPlusPlus::PcapPlusPlus) + if (CONAN_BUILD) find_package(kainjow_mustache REQUIRED) target_link_libraries(${TARGET} PRIVATE kainjow_mustache::kainjow_mustache) diff --git a/src/Daggy/Precompiled.hpp b/src/Daggy/Precompiled.hpp index d611e66b..ef65fc0e 100644 --- a/src/Daggy/Precompiled.hpp +++ b/src/Daggy/Precompiled.hpp @@ -11,6 +11,7 @@ #include #include +#include #include @@ -29,3 +30,6 @@ #else #include #endif + +#include +#include diff --git a/src/DaggyCore/aggregators/CConsole.cpp b/src/DaggyCore/aggregators/CConsole.cpp index d770c604..a91d4e3d 100644 --- a/src/DaggyCore/aggregators/CConsole.cpp +++ b/src/DaggyCore/aggregators/CConsole.cpp @@ -179,7 +179,7 @@ void daggy::aggregators::CConsole::printMessage(const ConsoleMessageType& messag const char* message_type_string = console_message_type_.valueToKey(message_type); printf ( - "%12s | %-9s | %-15s | %-15s | %s\n", + "%12s | %-9s | %-20s | %-30s | %s\n", qPrintable(currentConsoleTime()), message_type_string, qPrintable(provider_id), diff --git a/src/DaggyCore/aggregators/CConsole.hpp b/src/DaggyCore/aggregators/CConsole.hpp index f2dd41ff..11e0cf44 100644 --- a/src/DaggyCore/aggregators/CConsole.hpp +++ b/src/DaggyCore/aggregators/CConsole.hpp @@ -39,6 +39,10 @@ class DAGGYCORE_EXPORT CConsole : public IAggregator QObject* parent = nullptr); bool isReady() const override; + void printAppMessage + ( + const QString& message + ); public slots: void onDataProviderStateChanged(QString provider_id, DaggyProviderStates state) override; @@ -52,11 +56,6 @@ public slots: QString stateName(DaggyProviderStates state) const; QString stateName(DaggyCommandStates state) const; - - void printAppMessage - ( - const QString& message - ); void printProviderMessage ( const ConsoleMessageType& message_type, diff --git a/src/DaggyCore/aggregators/CFile.cpp b/src/DaggyCore/aggregators/CFile.cpp index d92348d6..0b8e5d06 100644 --- a/src/DaggyCore/aggregators/CFile.cpp +++ b/src/DaggyCore/aggregators/CFile.cpp @@ -135,7 +135,7 @@ QString daggy::aggregators::CFile::name(const QString& session, const QString& p result = QString("%1/%2/%3-%4.%5").arg(output_folder_, session, provider_id, command_id, extension); break; case DaggyStreamError: - result = QString("%1/%2/%3-%4.%5.%6").arg(output_folder_, session, provider_id, command_id, "err", extension); + result = QString("%1/%2/%3-%4.%5.%6").arg(output_folder_, session, provider_id, command_id, extension, "err"); break; } return result; From 4f593138bcaae64c718a47363d7535a628133d36 Mon Sep 17 00:00:00 2001 From: synacker Date: Sun, 10 Dec 2023 13:40:41 +0300 Subject: [PATCH 2/2] Tried to fix windows compilation --- src/Daggy/Precompiled.hpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Daggy/Precompiled.hpp b/src/Daggy/Precompiled.hpp index ef65fc0e..3586580f 100644 --- a/src/Daggy/Precompiled.hpp +++ b/src/Daggy/Precompiled.hpp @@ -25,11 +25,13 @@ #include #endif +#include +#include + #ifdef Q_OS_WIN #include #else #include #endif -#include -#include +