Move whitelist host code to networkmanager
This means no :adblock-update after modifying the whitelist.
This commit is contained in:
parent
523e071a97
commit
fc806525a2
@ -24,7 +24,6 @@ import os.path
|
||||
import functools
|
||||
import posixpath
|
||||
import zipfile
|
||||
import fnmatch
|
||||
|
||||
from qutebrowser.config import config
|
||||
from qutebrowser.utils import objreg, standarddir, log, message
|
||||
@ -60,22 +59,6 @@ def get_fileobj(byte_io):
|
||||
return io.TextIOWrapper(byte_io, encoding='utf-8')
|
||||
|
||||
|
||||
def is_whitelisted_domain(host):
|
||||
"""Check if the given host is on the adblock whitelist.
|
||||
|
||||
Args:
|
||||
host: The host as given by the adblocker as string.
|
||||
"""
|
||||
whitelist = config.get('content', 'host-blocking-whitelist')
|
||||
if whitelist is None:
|
||||
return False
|
||||
|
||||
for pattern in whitelist:
|
||||
if fnmatch.fnmatch(host, pattern.lower()):
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
class FakeDownload:
|
||||
|
||||
"""A download stub to use on_download_finished with local files."""
|
||||
@ -205,8 +188,7 @@ class HostBlocker:
|
||||
else:
|
||||
error_count += 1
|
||||
continue
|
||||
if (host not in self.WHITELISTED
|
||||
and not is_whitelisted_domain(host)):
|
||||
if host not in self.WHITELISTED:
|
||||
self.blocked_hosts.add(host)
|
||||
log.misc.debug("{}: read {} lines".format(byte_io.name, line_count))
|
||||
if error_count > 0:
|
||||
|
@ -20,6 +20,7 @@
|
||||
"""Our own QNetworkAccessManager."""
|
||||
|
||||
import collections
|
||||
import fnmatch
|
||||
|
||||
from PyQt5.QtCore import (pyqtSlot, pyqtSignal, PYQT_VERSION, QCoreApplication,
|
||||
QUrl, QByteArray)
|
||||
@ -49,6 +50,22 @@ def init():
|
||||
QSslSocket.setDefaultCiphers(good_ciphers)
|
||||
|
||||
|
||||
def is_whitelisted_domain(host):
|
||||
"""Check if the given host is on the adblock whitelist.
|
||||
|
||||
Args:
|
||||
host: The host as given by the adblocker as string.
|
||||
"""
|
||||
whitelist = config.get('content', 'host-blocking-whitelist')
|
||||
if whitelist is None:
|
||||
return False
|
||||
|
||||
for pattern in whitelist:
|
||||
if fnmatch.fnmatch(host, pattern.lower()):
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
class SslError(QSslError):
|
||||
|
||||
"""A QSslError subclass which provides __hash__ on Qt < 5.4."""
|
||||
@ -347,7 +364,8 @@ class NetworkManager(QNetworkAccessManager):
|
||||
host_blocker = objreg.get('host-blocker')
|
||||
if (op == QNetworkAccessManager.GetOperation and
|
||||
req.url().host() in host_blocker.blocked_hosts and
|
||||
config.get('content', 'host-blocking-enabled')):
|
||||
config.get('content', 'host-blocking-enabled') and
|
||||
not is_whitelisted_domain(req.url().host())):
|
||||
log.webview.info("Request to {} blocked by host blocker.".format(
|
||||
req.url().host()))
|
||||
return networkreply.ErrorNetworkReply(
|
||||
|
Loading…
Reference in New Issue
Block a user