File indexing completed on 2025-01-19 05:03:58

0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 // SPDX-FileCopyrightText: 2020 Tomaz Canabrava <tcanabrava@kde.org>
0003 
0004 #include "ufwlogmodel.h"
0005 #include <QDebug>
0006 
0007 UfwLogModel::UfwLogModel(QObject *parent)
0008     : LogListModel(parent)
0009 {
0010 }
0011 
0012 // Regexp failed me, parsing it is.
0013 std::map<QString, QString> parseString(const QString &line)
0014 {
0015     // We can find a line we are not interested.:
0016     // "-- Journal begins at Sun 2020-09-20 11:37:15 BST, ends at Wed 2020-12-09 18:45:16 GMT. --"
0017     if (line.startsWith(QLatin1String("-- Journal begins at "))) {
0018         return {};
0019     }
0020 
0021     // indices
0022     // 0    1   2        3          4       5    6    7
0023     // Dec 06 17:42:45 tomatoland kernel: [UFW BLOCK] IN=wlan0 OUT= MAC= SRC=192.168.50.181
0024     // DST=224.0.0.252 LEN=56 TOS=0x00 PREC=0x00 TTL=255 ID=52151 PROTO=UDP SPT=5355 DPT=5355 LEN=36
0025     //
0026     // We are interested in the dates, (0, 1, 2), and then starting on 7.
0027     std::map<QString, QString> results;
0028     QStringList splited = line.split(' ');
0029     if (splited.size() < 7) {
0030         return {};
0031     }
0032 
0033     results[QStringLiteral("date")] = splited[0] + " " + splited[1];
0034     results[QStringLiteral("time")] = splited[3];
0035 
0036     // We can drop now everything up to 7.
0037     splited.erase(std::begin(splited), std::begin(splited) + 7);
0038     for (const QString &element : std::as_const(splited)) {
0039         for (const QString &key :
0040              {QStringLiteral("IN"), QStringLiteral("SRC"), QStringLiteral("DST"), QStringLiteral("PROTO"), QStringLiteral("STP"), QStringLiteral("DPT")}) {
0041             if (element.startsWith(key)) {
0042                 results[key] = element.mid(element.indexOf('=') + 1);
0043             }
0044         }
0045     }
0046 
0047     return results;
0048 }
0049 
0050 void UfwLogModel::addRawLogs(const QStringList &rawLogsList)
0051 {
0052     QList<LogData> newLogs;
0053     newLogs.reserve(rawLogsList.count());
0054     for (const QString &log : rawLogsList) {
0055         auto map = parseString(log);
0056 
0057         LogData logDetails{.sourceAddress = map["SRC"],
0058                            .sourcePort = map["SPT"],
0059                            .destinationAddress = map["DST"],
0060                            .destinationPort = map["DPT"],
0061                            .protocol = map["PROTO"],
0062                            .interface = map["IN"],
0063                            .action = "",
0064                            .time = map["time"],
0065                            .date = map["date"]};
0066         newLogs.append(logDetails);
0067     }
0068     qDebug() << "Number of logs" << newLogs.count();
0069     appendLogData(newLogs);
0070 }