make the host_blocker aware of patterns

Add an additional argument to is_blocked for the current url, and enable
the host_blocking.enabled property to be set per domain.
This allows to bypass host blocking for specific domains
This commit is contained in:
Markus Ongyerth 2018-07-08 13:29:41 +02:00
parent ae32b79d54
commit 40b2d03688
4 changed files with 14 additions and 11 deletions

View File

@ -111,9 +111,10 @@ class HostBlocker:
config.instance.changed.connect(self._update_files)
def is_blocked(self, url):
def is_blocked(self, url, first_party_url=None):
"""Check if the given URL (as QUrl) is blocked."""
if not config.val.content.host_blocking.enabled:
if not config.instance.get('content.host_blocking.enabled',
url=first_party_url):
return False
host = url.host()
return ((host in self._blocked_hosts or

View File

@ -69,9 +69,10 @@ class RequestInterceptor(QWebEngineUrlRequestInterceptor):
resource_type, navigation_type))
url = info.requestUrl()
first_party = info.firstPartyUrl()
# FIXME:qtwebengine only block ads for NavigationTypeOther?
if self._host_blocker.is_blocked(url):
if self._host_blocker.is_blocked(url, first_party):
log.webview.info("Request to {} blocked by host blocker.".format(
url.host()))
info.block(True)

View File

@ -383,14 +383,6 @@ class NetworkManager(QNetworkAccessManager):
for header, value in shared.custom_headers(url=req.url()):
req.setRawHeader(header, value)
host_blocker = objreg.get('host-blocker')
if host_blocker.is_blocked(req.url()):
log.webview.info("Request to {} blocked by host blocker.".format(
req.url().host()))
return networkreply.ErrorNetworkReply(
req, HOSTBLOCK_ERROR_STRING, QNetworkReply.ContentAccessDenied,
self)
# There are some scenarios where we can't figure out current_url:
# - There's a generic NetworkManager, e.g. for downloads
# - The download was in a tab which is now closed.
@ -408,6 +400,14 @@ class NetworkManager(QNetworkAccessManager):
# the webpage shutdown here.
current_url = QUrl()
host_blocker = objreg.get('host-blocker')
if host_blocker.is_blocked(req.url(), current_url):
log.webview.info("Request to {} blocked by host blocker.".format(
req.url().host()))
return networkreply.ErrorNetworkReply(
req, HOSTBLOCK_ERROR_STRING, QNetworkReply.ContentAccessDenied,
self)
if 'log-requests' in self._args.debug_flags:
operation = debug.qenum_key(QNetworkAccessManager, op)
operation = operation.replace('Operation', '').upper()

View File

@ -467,6 +467,7 @@ content.headers.user_agent:
content.host_blocking.enabled:
default: true
supports_pattern: true
type: Bool
desc: Enable host blocking.