use correctly first-party url when blocking with adblock
This commit is contained in:
parent
ef1b32bb52
commit
d9c8246858
3 changed files with 34 additions and 7 deletions
|
@ -12,7 +12,8 @@
|
||||||
// How to use:
|
// How to use:
|
||||||
// curl -i -X POST --data '
|
// curl -i -X POST --data '
|
||||||
// {
|
// {
|
||||||
// "url": "http://gompoozu.net",
|
// "url": "http://gompoozu.net/cwqcwq/js.js",
|
||||||
|
// "fp_url": "http://bbc.com",
|
||||||
// "url_type": "main_frame",
|
// "url_type": "main_frame",
|
||||||
// "filter": true,
|
// "filter": true,
|
||||||
// "cosmetic": true
|
// "cosmetic": true
|
||||||
|
@ -60,6 +61,7 @@ else {
|
||||||
const jsonStruct = JSON.parse(jsonData.toString());
|
const jsonStruct = JSON.parse(jsonData.toString());
|
||||||
|
|
||||||
const askUrl = jsonStruct['url'];
|
const askUrl = jsonStruct['url'];
|
||||||
|
const askFpUrl = jsonStruct['fp_url'];
|
||||||
const askFilter = jsonStruct['filter'];
|
const askFilter = jsonStruct['filter'];
|
||||||
const askCosmetic = jsonStruct['cosmetic'];
|
const askCosmetic = jsonStruct['cosmetic'];
|
||||||
const askUrlType = jsonStruct['url_type'];
|
const askUrlType = jsonStruct['url_type'];
|
||||||
|
@ -71,6 +73,7 @@ else {
|
||||||
const adblockMatch = engine.match(adblock.Request.fromRawDetails({
|
const adblockMatch = engine.match(adblock.Request.fromRawDetails({
|
||||||
type: askUrlType,
|
type: askUrlType,
|
||||||
url: askUrl,
|
url: askUrl,
|
||||||
|
sourceUrl: askFpUrl
|
||||||
}));
|
}));
|
||||||
|
|
||||||
resultJson["filter"] = adblockMatch;
|
resultJson["filter"] = adblockMatch;
|
||||||
|
|
|
@ -26,7 +26,7 @@
|
||||||
|
|
||||||
AdBlockManager::AdBlockManager(QObject* parent)
|
AdBlockManager::AdBlockManager(QObject* parent)
|
||||||
: QObject(parent), m_loaded(false), m_enabled(false), m_interceptor(new AdBlockUrlInterceptor(this)),
|
: QObject(parent), m_loaded(false), m_enabled(false), m_interceptor(new AdBlockUrlInterceptor(this)),
|
||||||
m_serverProcess(nullptr) {
|
m_serverProcess(nullptr), m_cacheBlocks({}) {
|
||||||
m_adblockIcon = new AdBlockIcon(this);
|
m_adblockIcon = new AdBlockIcon(this);
|
||||||
m_adblockIcon->setObjectName(QSL("m_adblockIconAction"));
|
m_adblockIcon->setObjectName(QSL("m_adblockIconAction"));
|
||||||
m_unifiedFiltersFile = qApp->userDataFolder() + QDir::separator() + QSL("adblock-unified-filters.txt");
|
m_unifiedFiltersFile = qApp->userDataFolder() + QDir::separator() + QSL("adblock-unified-filters.txt");
|
||||||
|
@ -38,21 +38,37 @@ AdBlockManager::~AdBlockManager() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
BlockingResult AdBlockManager::block(const AdblockRequestInfo& request) const {
|
BlockingResult AdBlockManager::block(const AdblockRequestInfo& request) {
|
||||||
if (!isEnabled()) {
|
if (!isEnabled()) {
|
||||||
return { false };
|
return { false };
|
||||||
}
|
}
|
||||||
|
|
||||||
const QString url_string = request.requestUrl().toEncoded().toLower();
|
const QString url_string = request.requestUrl().toEncoded().toLower();
|
||||||
|
const QString firstparty_url_string = request.firstPartyUrl().toEncoded().toLower();
|
||||||
const QString url_scheme = request.requestUrl().scheme().toLower();
|
const QString url_scheme = request.requestUrl().scheme().toLower();
|
||||||
|
const QPair<QString, QString> url_pair = { firstparty_url_string, url_string };
|
||||||
|
|
||||||
if (!canRunOnScheme(url_scheme)) {
|
if (!canRunOnScheme(url_scheme)) {
|
||||||
return { false };
|
return { false };
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (m_serverProcess != nullptr && m_serverProcess->state() == QProcess::ProcessState::Running) {
|
if (m_serverProcess != nullptr && m_serverProcess->state() == QProcess::ProcessState::Running) {
|
||||||
|
if (m_cacheBlocks.contains(url_pair)) {
|
||||||
|
qDebugNN << LOGSEC_ADBLOCK
|
||||||
|
<< "Found blocking data in cache, URL:"
|
||||||
|
<< QUOTE_W_SPACE_DOT(url_pair);
|
||||||
|
|
||||||
|
return m_cacheBlocks.value(url_pair);
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
auto result = askServerIfBlocked(url_string);
|
auto result = askServerIfBlocked(firstparty_url_string, url_string);
|
||||||
|
|
||||||
|
m_cacheBlocks.insert(url_pair, result);
|
||||||
|
|
||||||
|
qDebugNN << LOGSEC_ADBLOCK
|
||||||
|
<< "Inserted blocking data to cache for:"
|
||||||
|
<< QUOTE_W_SPACE_DOT(url_pair);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -177,11 +193,12 @@ void AdBlockManager::showDialog() {
|
||||||
AdBlockDialog(qApp->mainFormWidget()).exec();
|
AdBlockDialog(qApp->mainFormWidget()).exec();
|
||||||
}
|
}
|
||||||
|
|
||||||
BlockingResult AdBlockManager::askServerIfBlocked(const QString& url) const {
|
BlockingResult AdBlockManager::askServerIfBlocked(const QString& fp_url, const QString& url) const {
|
||||||
QJsonObject req_obj;
|
QJsonObject req_obj;
|
||||||
QByteArray out;
|
QByteArray out;
|
||||||
QElapsedTimer tmr;
|
QElapsedTimer tmr;
|
||||||
|
|
||||||
|
req_obj["fp_url"] = fp_url;
|
||||||
req_obj["url"] = url;
|
req_obj["url"] = url;
|
||||||
req_obj["filter"] = true;
|
req_obj["filter"] = true;
|
||||||
|
|
||||||
|
@ -312,6 +329,8 @@ QProcess* AdBlockManager::restartServer(int port) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void AdBlockManager::updateUnifiedFiltersFile() {
|
void AdBlockManager::updateUnifiedFiltersFile() {
|
||||||
|
m_cacheBlocks.clear();
|
||||||
|
|
||||||
if (QFile::exists(m_unifiedFiltersFile)) {
|
if (QFile::exists(m_unifiedFiltersFile)) {
|
||||||
QFile::remove(m_unifiedFiltersFile);
|
QFile::remove(m_unifiedFiltersFile);
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,8 @@
|
||||||
|
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
|
|
||||||
|
#include <QHash>
|
||||||
|
|
||||||
class QUrl;
|
class QUrl;
|
||||||
class QProcess;
|
class QProcess;
|
||||||
class AdblockRequestInfo;
|
class AdblockRequestInfo;
|
||||||
|
@ -15,6 +17,8 @@ struct BlockingResult {
|
||||||
bool m_blocked;
|
bool m_blocked;
|
||||||
QString m_blockedByFilter;
|
QString m_blockedByFilter;
|
||||||
|
|
||||||
|
BlockingResult() : m_blocked(false), m_blockedByFilter(QString()) {}
|
||||||
|
|
||||||
BlockingResult(bool blocked, QString blocked_by_filter = {})
|
BlockingResult(bool blocked, QString blocked_by_filter = {})
|
||||||
: m_blocked(blocked), m_blockedByFilter(std::move(blocked_by_filter)) {}
|
: m_blocked(blocked), m_blockedByFilter(std::move(blocked_by_filter)) {}
|
||||||
|
|
||||||
|
@ -38,7 +42,7 @@ class AdBlockManager : public QObject {
|
||||||
AdBlockIcon* adBlockIcon() const;
|
AdBlockIcon* adBlockIcon() const;
|
||||||
|
|
||||||
// General methods for adblocking.
|
// General methods for adblocking.
|
||||||
BlockingResult block(const AdblockRequestInfo& request) const;
|
BlockingResult block(const AdblockRequestInfo& request);
|
||||||
QString elementHidingRulesForDomain(const QUrl& url) const;
|
QString elementHidingRulesForDomain(const QUrl& url) const;
|
||||||
|
|
||||||
QStringList filterLists() const;
|
QStringList filterLists() const;
|
||||||
|
@ -58,7 +62,7 @@ class AdBlockManager : public QObject {
|
||||||
void enabledChanged(bool enabled);
|
void enabledChanged(bool enabled);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
BlockingResult askServerIfBlocked(const QString& url) const;
|
BlockingResult askServerIfBlocked(const QString& fp_url, const QString& url) const;
|
||||||
QString askServerForCosmeticRules(const QString& url) const;
|
QString askServerForCosmeticRules(const QString& url) const;
|
||||||
QProcess* restartServer(int port);
|
QProcess* restartServer(int port);
|
||||||
|
|
||||||
|
@ -69,6 +73,7 @@ class AdBlockManager : public QObject {
|
||||||
AdBlockUrlInterceptor* m_interceptor;
|
AdBlockUrlInterceptor* m_interceptor;
|
||||||
QString m_unifiedFiltersFile;
|
QString m_unifiedFiltersFile;
|
||||||
QProcess* m_serverProcess;
|
QProcess* m_serverProcess;
|
||||||
|
QHash<QPair<QString, QString>, BlockingResult> m_cacheBlocks;
|
||||||
};
|
};
|
||||||
|
|
||||||
inline AdBlockIcon* AdBlockManager::adBlockIcon() const {
|
inline AdBlockIcon* AdBlockManager::adBlockIcon() const {
|
||||||
|
|
Loading…
Add table
Reference in a new issue