aboutsummaryrefslogtreecommitdiff
path: root/src/filter/filtercollection.cpp
blob: 7b07606266d14b9cef205ce6fb37bfa1332dfd6c (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*******************************************************************************
 **
 ** smolbote: yet another qute browser
 ** Copyright (C) 2017  Xian Nox
 **
 ** This program is free software: you can redistribute it and/or modify
 ** it under the terms of the GNU General Public License as published by
 ** the Free Software Foundation, either version 3 of the License, or
 ** (at your option) any later version.
 **
 ** This program is distributed in the hope that it will be useful,
 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 ** GNU General Public License for more details.
 **
 ** You should have received a copy of the GNU General Public License
 ** along with this program.  If not, see <http://www.gnu.org/licenses/>.
 **
 ******************************************************************************/

#include "filtercollection.h"
#include "ui_subscriptionform.h"

#include "browser.h"
#include <QNetworkRequest>
#include <QNetworkReply>

#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>

#include <QListWidget>
#include <QLabel>

#include <QTreeView>
#include "filtertree.h"
#include "filter.h"

FilterCollection::FilterCollection(const QString path, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SubscriptionForm)
{
    ui->setupUi(this);

    ui->homepage->setText(path);

    qDebug("Adding subscription [%s]", qUtf8Printable(path));

    m_filters = new FilterTree(this);
    ui->treeView->setModel(m_filters);

    QFile filterFile(path);
    if(!filterFile.open(QIODevice::ReadOnly)) {
        qWarning("Could not open filter!");
        return;
    }

    QJsonDocument filters(QJsonDocument::fromJson(filterFile.readAll()));
    qDebug("Added %i rules", load(filters.object()));
}

FilterCollection::~FilterCollection()
{
    delete ui;
}

QString FilterCollection::name() const
{
    return ui->title->text();
}

/**
 * Check if a URL request should be blocked or not
 * @param info
 * @return true if it should be blocked; false otherwise
 */
FilterCollection::MatchResult FilterCollection::match(QWebEngineUrlRequestInfo &info)
{
    MatchResult result;

    for(Filter *rule : m_filters->filters(info.firstPartyUrl().toString())) {
        if(rule->isValid()) {
            result.match = rule->hasMatch(info);
            if(result.match) {
                result.block = rule->shouldBlock(info);
            } else {
                result.block = false;
            }
            result.pattern = rule->domain() + " | " + rule->request();
            return result;
        }
    }

    // request matches neither whitelist nor blacklist
    result.match = false;
    result.block = false;
    return result;
}

/**
 * Load rules from JSON object
 * @param json
 */
int FilterCollection::load(const QJsonObject &json)
{
    int number = 0;

    ui->title->setText(json["name"].toString());
    ui->homepage->setText(json["url"].toString());

    for(QJsonValue v : json["rules"].toArray()) {
        ++number;

        QJsonObject obj = v.toObject();

        Filter::ResourceRules r;
        r.allowed = parseJsonRules(obj["allowTypes"]);
        r.blocked = parseJsonRules(obj["blockTypes"]);

        m_filters->addFilter(obj["firstPartyUrl"].toString(), obj["requestUrl"].toString(), r, obj["shouldBlock"].toBool());
    }

    return number;
}

Filter::Resources FilterCollection::parseJsonRules(const QJsonValue &obj)
{
    Filter::Resources res;
    for(QJsonValue v : obj.toArray()) {
        QString t = v.toString();
        if(t == "MainFrame") {
            res.setFlag(Filter::ResourceType::MainFrame);
        } else if(t == "SubFrame") {
            res.setFlag(Filter::ResourceType::SubFrame);
        } else if(t == "Stylesheet") {
            res.setFlag(Filter::ResourceType::Stylesheet);
        } else if(t == "Script") {
            res.setFlag(Filter::ResourceType::Script);
        } else if(t == "Image") {
            res.setFlag(Filter::ResourceType::Image);
        } else if(t == "FontResource") {
            res.setFlag(Filter::ResourceType::Font);
        } else if(t == "SubResource") {
            res.setFlag(Filter::ResourceType::SubResource);
        } else if(t == "Object") {
            res.setFlag(Filter::ResourceType::Object);
        } else if(t == "Media") {
            res.setFlag(Filter::ResourceType::Media);
        } else if(t == "Worker") {
            res.setFlag(Filter::ResourceType::Worker);
        } else if(t == "SharedWorker") {
            res.setFlag(Filter::ResourceType::SharedWorker);
        } else if(t == "Prefetch") {
            res.setFlag(Filter::ResourceType::Prefetch);
        } else if(t == "Favicon") {
            res.setFlag(Filter::ResourceType::Favicon);
        } else if(t == "Xhr") {
            res.setFlag(Filter::ResourceType::Xhr);
        } else if(t == "Ping") {
            res.setFlag(Filter::ResourceType::Ping);
        } else if(t == "ServiceWorker") {
            res.setFlag(Filter::ResourceType::ServiceWorker);
        } else if(t == "CspWorker") {
            res.setFlag(Filter::ResourceType::CspReport);
        } else if(t == "PluginResource") {
            res.setFlag(Filter::ResourceType::PluginResource);
        }
    }

    return res;
}