Skip to content

Commit

Permalink
Merge pull request #63 from synacker/feature/fix_pcap
Browse files Browse the repository at this point in the history
Added -x flag for pcap fix
  • Loading branch information
synacker authored Dec 10, 2023
2 parents 52bd3a1 + 4f59313 commit 10af81c
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 17 deletions.
5 changes: 4 additions & 1 deletion conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
62 changes: 53 additions & 9 deletions src/Daggy/CConsoleDaggy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,43 +36,45 @@ using namespace daggy;
CConsoleDaggy::CConsoleDaggy(QObject* parent)
: QObject(parent)
, daggy_core_(nullptr)
, console_aggreagator_(nullptr)
, need_hard_stop_(false)
{
qApp->setApplicationName("daggy");
qApp->setApplicationVersion(DAGGY_VERSION_STANDARD);
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();;
}
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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();
}
Expand All @@ -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<pcpp::IFileReaderDevice> 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<daggy::Core*>();
Expand Down
11 changes: 11 additions & 0 deletions src/Daggy/CConsoleDaggy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ SOFTWARE.
#include <DaggyCore/Types.hpp>


namespace daggy {
namespace aggregators {
class CConsole;
}
}

#include "ISystemSignalHandler.hpp"

class QCoreApplication;
Expand Down Expand Up @@ -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;
Expand All @@ -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_;
Expand Down
4 changes: 4 additions & 0 deletions src/Daggy/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions src/Daggy/Precompiled.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include <QTimer>
#include <QDir>
#include <QDirIterator>

#include <QProcessEnvironment>

Expand All @@ -24,8 +25,13 @@
#include <mustache.hpp>
#endif

#include <pcapplusplus/PcapFileDevice.h>
#include <pcapplusplus/Logger.h>

#ifdef Q_OS_WIN
#include <windows.h>
#else
#include <signal.h>
#endif


2 changes: 1 addition & 1 deletion src/DaggyCore/aggregators/CConsole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
9 changes: 4 additions & 5 deletions src/DaggyCore/aggregators/CConsole.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/DaggyCore/aggregators/CFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 10af81c

Please sign in to comment.