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:
parent
ae32b79d54
commit
40b2d03688
@ -111,9 +111,10 @@ class HostBlocker:
|
|||||||
|
|
||||||
config.instance.changed.connect(self._update_files)
|
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."""
|
"""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
|
return False
|
||||||
host = url.host()
|
host = url.host()
|
||||||
return ((host in self._blocked_hosts or
|
return ((host in self._blocked_hosts or
|
||||||
|
@ -69,9 +69,10 @@ class RequestInterceptor(QWebEngineUrlRequestInterceptor):
|
|||||||
resource_type, navigation_type))
|
resource_type, navigation_type))
|
||||||
|
|
||||||
url = info.requestUrl()
|
url = info.requestUrl()
|
||||||
|
first_party = info.firstPartyUrl()
|
||||||
|
|
||||||
# FIXME:qtwebengine only block ads for NavigationTypeOther?
|
# 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(
|
log.webview.info("Request to {} blocked by host blocker.".format(
|
||||||
url.host()))
|
url.host()))
|
||||||
info.block(True)
|
info.block(True)
|
||||||
|
@ -383,14 +383,6 @@ class NetworkManager(QNetworkAccessManager):
|
|||||||
for header, value in shared.custom_headers(url=req.url()):
|
for header, value in shared.custom_headers(url=req.url()):
|
||||||
req.setRawHeader(header, value)
|
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 are some scenarios where we can't figure out current_url:
|
||||||
# - There's a generic NetworkManager, e.g. for downloads
|
# - There's a generic NetworkManager, e.g. for downloads
|
||||||
# - The download was in a tab which is now closed.
|
# - The download was in a tab which is now closed.
|
||||||
@ -408,6 +400,14 @@ class NetworkManager(QNetworkAccessManager):
|
|||||||
# the webpage shutdown here.
|
# the webpage shutdown here.
|
||||||
current_url = QUrl()
|
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:
|
if 'log-requests' in self._args.debug_flags:
|
||||||
operation = debug.qenum_key(QNetworkAccessManager, op)
|
operation = debug.qenum_key(QNetworkAccessManager, op)
|
||||||
operation = operation.replace('Operation', '').upper()
|
operation = operation.replace('Operation', '').upper()
|
||||||
|
@ -467,6 +467,7 @@ content.headers.user_agent:
|
|||||||
|
|
||||||
content.host_blocking.enabled:
|
content.host_blocking.enabled:
|
||||||
default: true
|
default: true
|
||||||
|
supports_pattern: true
|
||||||
type: Bool
|
type: Bool
|
||||||
desc: Enable host blocking.
|
desc: Enable host blocking.
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user