Merge branch 'Kingdread-whitelist-hosts'

This commit is contained in:
Florian Bruhin 2015-09-17 06:39:17 +02:00
commit accbf157e0
4 changed files with 39 additions and 2 deletions

View File

@ -145,11 +145,11 @@ Contributors, sorted by the number of commits in descending order:
* Lamar Pavel
* Austin Anderson
* Artur Shaik
* Daniel
* ZDarian
* Thorsten Wißmann
* Peter Vilim
* John ShaggyTwoDope Jenkins
* Daniel
* Jimmy
* Zach-Button
* rikn00

View File

@ -159,6 +159,7 @@
|<<content-cookies-store,cookies-store>>|Whether to store cookies.
|<<content-host-block-lists,host-block-lists>>|List of URLs of lists which contain hosts to block.
|<<content-host-blocking-enabled,host-blocking-enabled>>|Whether host blocking is enabled.
|<<content-host-blocking-whitelist,host-blocking-whitelist>>|List of domains that should always be loaded, despite being ad-blocked.
|==============
.Quick reference for section ``hints''
@ -1433,6 +1434,16 @@ Valid values:
Default: +pass:[true]+
[[content-host-blocking-whitelist]]
=== host-blocking-whitelist
List of domains that should always be loaded, despite being ad-blocked.
Domains may contain * and ? wildcards and are otherwise required to exactly match the requested domain.
Local domains are always exempt from hostblocking.
Default: empty
== hints
Hinting settings.

View File

@ -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 of the request 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(

View File

@ -735,6 +735,14 @@ def data(readonly=False):
SettingValue(typ.Bool(), 'true'),
"Whether host blocking is enabled."),
('host-blocking-whitelist',
SettingValue(typ.List(none_ok=True), 'piwik.org'),
"List of domains that should always be loaded, despite being "
"ad-blocked.\n\n"
"Domains may contain * and ? wildcards and are otherwise "
"required to exactly match the requested domain.\n\n"
"Local domains are always exempt from hostblocking."),
readonly=readonly
)),