aboutsummaryrefslogtreecommitdiff
path: root/src/webengine/filter.cpp
blob: 6941ac477a3e17e027d4cb535884cebf9fab88b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
 * This file is part of smolbote. It's copyrighted by the contributors recorded
 * in the version control history of the file, available from its original
 * location: https://neueland.iserlohn-fortress.net/gitea/aqua/smolbote
 *
 * SPDX-License-Identifier: GPL-3.0
 */

#include "filter.h"
#include "configuration.h"
#include "urlinterceptor.h"
#include "util.h"
#include <QDir>
#include <QJsonArray>
#include <QJsonDocument>
#include <QTextStream>

/*
inline std::vector<FilterRule> parseAdBlockList(const QString &filename)
{
    std::vector<FilterRule> rules;
    QFile list(filename);

    if(list.open(QIODevice::ReadOnly | QIODevice::Text), true) {
        QTextStream l(&list);
        QString line;
        while(l.readLineInto(&line)) {
            AdBlockRule rule(line);
            if(rule.isEnabled()) {
                rules.emplace_back(std::move(rule));
            }
        }
        list.close();
    }

    return rules;
}*/

Filter::Filter::Filter(const std::unique_ptr<Configuration> &config, QObject *parent)
    : QObject(parent)
{
    // parse headers
    if(const auto headers = config->value<QStringList>("filter.header"); headers) {
        for(const QString &header : headers.value()) {
            const auto list = header.split(QLatin1Literal(":"));
            if(list.length() == 2)
                m_headers.insert(list.at(0).toLatin1(), list.at(1).toLatin1());
        }
    }

    const QStringList hostfiles = Util::files(config->value<QString>("filter.hosts").value());
    //qDebug("filter.path=[%s]", qUtf8Printable(config->value<QString>("filter.hosts").value()));
    for(const QString &hostfile : hostfiles) {
        QFile f(hostfile);
        if(f.open(QIODevice::ReadOnly | QIODevice::Text)) {
#ifdef QT_DEBUG
            qDebug("Loading hostlist filters [%s]", qUtf8Printable(hostfile));
#endif
            loadHostlist(f, &filters);
            f.close();
        }
    }

    /*
    auto filtersPath = config->value<QString>("filter.adblock");
    if(filtersPath)
        filters = parseAdBlockList(filtersPath.value());
    */
}

void Filter::filterRequest(QWebEngineUrlRequestInfo &info) const
{
    auto matches = filters.match(info.firstPartyUrl().toString(), info.requestUrl().toString());
    for(const auto &rule : matches) {
        switch(rule->action().first) {
        case FilterLeaf::NotMatched:
#ifdef QT_DEBUG
            qDebug("Paradoxical match: request matched, but not matched.");
            qDebug(" - %s", qUtf8Printable(info.requestUrl().toString()));
#endif
            break;
        case FilterLeaf::Block:
            //qDebug("block %s", qUtf8Printable(info.requestUrl().toString()));
            info.block(true);
            break;
        case FilterLeaf::Allow:
            info.block(false);
            break;
        //case FilterLeaf::Redirect:
        //    break;
        }
    }
}