Beispiel #1
0
bool AdBlockManager::block(QWebEngineUrlRequestInfo &request)
{
    QMutexLocker locker(&m_mutex);

    if (!isEnabled()) {
        return false;
    }

#ifdef ADBLOCK_DEBUG
    QElapsedTimer timer;
    timer.start();
#endif
    const QString urlString = request.requestUrl().toEncoded().toLower();
    const QString urlDomain = request.requestUrl().host().toLower();
    const QString urlScheme = request.requestUrl().scheme().toLower();

    if (!canRunOnScheme(urlScheme) || !canBeBlocked(request.firstPartyUrl())) {
        return false;
    }

    bool res = false;
    const AdBlockRule* blockedRule = m_matcher->match(request, urlDomain, urlString);

    if (blockedRule) {
        res = true;

        if (request.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeMainFrame) {
            QUrl url(QSL("qupzilla:adblock"));
            QUrlQuery query;
            query.addQueryItem(QSL("rule"), blockedRule->filter());
            query.addQueryItem(QSL("subscription"), blockedRule->subscription()->title());
            url.setQuery(query);
            request.redirect(url);
        }
        else {
            request.block(true);
        }

#ifdef ADBLOCK_DEBUG
        qDebug() << "BLOCKED: " << timer.elapsed() << blockedRule->filter() << request.requestUrl();
#endif
    }

#ifdef ADBLOCK_DEBUG
    qDebug() << timer.elapsed() << request.requestUrl();
#endif

    return res;
}
void QtWebEngineUrlRequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo &request)
{
	if (!m_areImagesEnabled && request.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeImage)
	{
		request.block(true);

		return;
	}

	if (!m_contentBlockingProfiles.contains(request.firstPartyUrl().host()))
	{
		if (SettingsManager::getOption(SettingsManager::ContentBlocking_EnableContentBlockingOption, request.firstPartyUrl()).toBool())
		{
			m_contentBlockingProfiles[request.firstPartyUrl().host()] = ContentBlockingManager::getProfileList(SettingsManager::getOption(SettingsManager::ContentBlocking_ProfilesOption, request.firstPartyUrl()).toStringList());
		}
		else
		{
			m_contentBlockingProfiles[request.firstPartyUrl().host()] = QVector<int>();
		}
	}

	const QVector<int> contentBlockingProfiles(m_contentBlockingProfiles.value(request.firstPartyUrl().host()));

	if (contentBlockingProfiles.isEmpty())
	{
		const NetworkManagerFactory::DoNotTrackPolicy doNotTrackPolicy(NetworkManagerFactory::getDoNotTrackPolicy());

		if (doNotTrackPolicy != NetworkManagerFactory::SkipTrackPolicy)
		{
			request.setHttpHeader(QStringLiteral("DNT").toLatin1(), ((doNotTrackPolicy == NetworkManagerFactory::DoNotAllowToTrackPolicy) ? QStringLiteral("1") : QStringLiteral("0")).toLatin1());
		}

		return;
	}

	NetworkManager::ResourceType resourceType(NetworkManager::OtherType);
	bool storeBlockedUrl(true);

	switch (request.resourceType())
	{
		case QWebEngineUrlRequestInfo::ResourceTypeMainFrame:
			resourceType = NetworkManager::MainFrameType;

			break;
		case QWebEngineUrlRequestInfo::ResourceTypeSubFrame:
			resourceType = NetworkManager::SubFrameType;

			break;
		case QWebEngineUrlRequestInfo::ResourceTypeStylesheet:
			resourceType = NetworkManager::StyleSheetType;
			storeBlockedUrl = false;

			break;
		case QWebEngineUrlRequestInfo::ResourceTypeScript:
			resourceType = NetworkManager::ScriptType;
			storeBlockedUrl = false;

			break;
		case QWebEngineUrlRequestInfo::ResourceTypeImage:
			resourceType = NetworkManager::ImageType;

			break;
		case QWebEngineUrlRequestInfo::ResourceTypeObject:
		case QWebEngineUrlRequestInfo::ResourceTypeMedia:
			resourceType = NetworkManager::ObjectType;

			break;
#if QT_VERSION >= 0x050700
		case QWebEngineUrlRequestInfo::ResourceTypePluginResource:
			resourceType = NetworkManager::ObjectSubrequestType;
			storeBlockedUrl = false;

			break;
#endif
		case QWebEngineUrlRequestInfo::ResourceTypeXhr:
			resourceType = NetworkManager::XmlHttpRequestType;

			break;
		default:
			break;
	}

	const ContentBlockingManager::CheckResult result(ContentBlockingManager::checkUrl(contentBlockingProfiles, request.firstPartyUrl(), request.requestUrl(), resourceType));

	if (result.isBlocked)
	{
		if (storeBlockedUrl && !m_blockedElements.value(request.firstPartyUrl().host()).contains(request.requestUrl().url()))
		{
			m_blockedElements[request.firstPartyUrl().host()].append(request.requestUrl().url());
		}

		Console::addMessage(QCoreApplication::translate("main", "Request blocked with rule: %1").arg(result.rule), Console::NetworkCategory, Console::LogLevel, request.requestUrl().toString(), -1);

		request.block(true);
	}
}
void QtWebEngineUrlRequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo &request)
{
	if (!m_contentBlockingProfiles.contains(request.firstPartyUrl().host()))
	{
		m_contentBlockingProfiles[request.firstPartyUrl().host()] = ContentBlockingManager::getProfileList(SettingsManager::getValue(QLatin1String("Content/BlockingProfiles"), request.firstPartyUrl()).toStringList());
	}

	const QVector<int> contentBlockingProfiles(m_contentBlockingProfiles.value(request.firstPartyUrl().host()));

	if (contentBlockingProfiles.isEmpty())
	{
		const NetworkManagerFactory::DoNotTrackPolicy doNotTrackPolicy(NetworkManagerFactory::getDoNotTrackPolicy());

		if (doNotTrackPolicy != NetworkManagerFactory::SkipTrackPolicy)
		{
			request.setHttpHeader(QStringLiteral("DNT").toLatin1(), ((doNotTrackPolicy == NetworkManagerFactory::DoNotAllowToTrackPolicy) ? QStringLiteral("1") : QStringLiteral("0")).toLatin1());
		}

		return;
	}

	ContentBlockingManager::ResourceType resourceType(ContentBlockingManager::OtherType);

	switch (request.resourceType())
	{
		case QWebEngineUrlRequestInfo::ResourceTypeMainFrame:
			resourceType = ContentBlockingManager::MainFrameType;

			break;
		case QWebEngineUrlRequestInfo::ResourceTypeSubFrame:
			resourceType = ContentBlockingManager::SubFrameType;

			break;
		case QWebEngineUrlRequestInfo::ResourceTypeStylesheet:
			resourceType = ContentBlockingManager::StyleSheetType;

			break;
		case QWebEngineUrlRequestInfo::ResourceTypeScript:
			resourceType = ContentBlockingManager::ScriptType;

			break;
		case QWebEngineUrlRequestInfo::ResourceTypeImage:
			resourceType = ContentBlockingManager::ImageType;

			break;
		case QWebEngineUrlRequestInfo::ResourceTypeObject:
		case QWebEngineUrlRequestInfo::ResourceTypeMedia:
			resourceType = ContentBlockingManager::ObjectType;

			break;
		case QWebEngineUrlRequestInfo::ResourceTypeXhr:
			resourceType = ContentBlockingManager::XmlHttpRequestType;

			break;
		default:
			break;
	}

	const ContentBlockingManager::CheckResult result(ContentBlockingManager::checkUrl(contentBlockingProfiles, request.firstPartyUrl(), request.requestUrl(), resourceType));

	if (result.isBlocked)
	{
		Console::addMessage(QCoreApplication::translate("main", "Blocked request"), Otter::NetworkMessageCategory, LogMessageLevel, request.requestUrl().toString(), -1);

		request.block(true);
	}
}