Register host blocker as request filter
This commit is contained in:
parent
58d179302e
commit
a146ce865b
@ -26,7 +26,8 @@ import posixpath
|
|||||||
import zipfile
|
import zipfile
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from qutebrowser.api import cmdutils, hook, config, message, downloads
|
from qutebrowser.api import (cmdutils, hook, config, message, downloads,
|
||||||
|
requests)
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger('misc')
|
logger = logging.getLogger('misc')
|
||||||
@ -108,24 +109,28 @@ class HostBlocker:
|
|||||||
|
|
||||||
self._config_hosts_file = str(config_dir / 'blocked-hosts')
|
self._config_hosts_file = str(config_dir / 'blocked-hosts')
|
||||||
|
|
||||||
def is_blocked(self, url, first_party_url=None):
|
def filter_request(self, info: requests.Request) -> None:
|
||||||
"""Check if the given URL (as QUrl) is blocked."""
|
"""Block the given request if necessary."""
|
||||||
if first_party_url is not None and not first_party_url.isValid():
|
if info.first_party_url is None:
|
||||||
first_party_url = None
|
first_party_url = None
|
||||||
|
elif not info.first_party_url.isValid():
|
||||||
|
first_party_url = None
|
||||||
|
else:
|
||||||
|
first_party_url = info.first_party_url
|
||||||
|
|
||||||
if not config.get('content.host_blocking.enabled',
|
if not config.get('content.host_blocking.enabled',
|
||||||
url=first_party_url):
|
url=first_party_url):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
host = url.host()
|
host = info.request_url.host()
|
||||||
blocked = ((host in self._blocked_hosts or
|
blocked = ((host in self._blocked_hosts or
|
||||||
host in self._config_blocked_hosts) and
|
host in self._config_blocked_hosts) and
|
||||||
not _is_whitelisted_url(url))
|
not _is_whitelisted_url(info.request_url))
|
||||||
|
|
||||||
if blocked:
|
if blocked:
|
||||||
logger.info("Request to {} blocked by host blocker."
|
logger.info("Request to {} blocked by host blocker."
|
||||||
.format(url.host()))
|
.format(info.request_url.host()))
|
||||||
|
info.block()
|
||||||
return blocked
|
|
||||||
|
|
||||||
def _read_hosts_file(self, filename, target):
|
def _read_hosts_file(self, filename, target):
|
||||||
"""Read hosts from the given filename.
|
"""Read hosts from the given filename.
|
||||||
@ -329,5 +334,7 @@ def init(context):
|
|||||||
host_blocker = HostBlocker(data_dir=context.data_dir,
|
host_blocker = HostBlocker(data_dir=context.data_dir,
|
||||||
config_dir=context.config_dir,
|
config_dir=context.config_dir,
|
||||||
args=context.args)
|
args=context.args)
|
||||||
context.signals.config_changed.connect(host_blocker.update_files)
|
|
||||||
host_blocker.read_hosts()
|
host_blocker.read_hosts()
|
||||||
|
|
||||||
|
context.signals.config_changed.connect(host_blocker.update_files)
|
||||||
|
requests.register_filter(host_blocker.filter_request)
|
||||||
|
Loading…
Reference in New Issue
Block a user