eric6/WebBrowser/SafeBrowsing/SafeBrowsingManager.py

Sat, 31 Aug 2019 12:58:11 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 31 Aug 2019 12:58:11 +0200
branch
without_py2_and_pyqt4
changeset 7192
a22eee00b052
parent 6942
2602857055c5
child 7229
53054eb5b15a
permissions
-rw-r--r--

Started removing runtime support for Python2 and PyQt4.

5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
6645
ad476851d7e0 Updated copyright for 2019.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6234
diff changeset
3 # Copyright (c) 2017 - 2019 Detlev Offenbach <detlev@die-offenbachs.de>
5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 #
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6 """
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
7 Module implementing the interface for Google Safe Browsing.
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 """
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
10 #
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
11 # Some part of this code were ported from gglsbl.client and adapted
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
12 # to Qt.
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
13 #
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
14 # https://github.com/afilipovich/gglsbl
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
15 #
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
16
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17 from __future__ import unicode_literals
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
18
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
19 import os
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
20 import base64
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
21
5839
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
22 from PyQt5.QtCore import pyqtSignal, pyqtSlot, QObject, QCoreApplication, \
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
23 QUrl, QDateTime, QTimer
5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
24
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25 import Preferences
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26 import Utilities
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27
5842
c3f41b959a65 Fine tuned the safe browsing update notifications.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5839
diff changeset
28 import UI.PixmapCache
c3f41b959a65 Fine tuned the safe browsing update notifications.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5839
diff changeset
29
5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
30 from .SafeBrowsingAPIClient import SafeBrowsingAPIClient
6229
72b40274a3b6 Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6187
diff changeset
31 from .SafeBrowsingCache import SafeBrowsingCache
72b40274a3b6 Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6187
diff changeset
32 from .SafeBrowsingThreatList import ThreatList, HashPrefixList
5829
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
33 from .SafeBrowsingUrl import SafeBrowsingUrl
5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
34
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
35
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
36 class SafeBrowsingManager(QObject):
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
37 """
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
38 Class implementing the interface for Google Safe Browsing.
5821
6c7766cde4c1 Added a progress bar to the management part and fine tuned the hash prefix update process.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
39
6c7766cde4c1 Added a progress bar to the management part and fine tuned the hash prefix update process.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
40 @signal progressMessage(message,maximum) emitted to give a message for the
6c7766cde4c1 Added a progress bar to the management part and fine tuned the hash prefix update process.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
41 action about to be performed and the maximum value
6c7766cde4c1 Added a progress bar to the management part and fine tuned the hash prefix update process.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
42 @signal progress(current) emitted to signal the current progress
5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
43 """
5821
6c7766cde4c1 Added a progress bar to the management part and fine tuned the hash prefix update process.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
44 progressMessage = pyqtSignal(str, int)
6c7766cde4c1 Added a progress bar to the management part and fine tuned the hash prefix update process.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
45 progress = pyqtSignal(int)
6c7766cde4c1 Added a progress bar to the management part and fine tuned the hash prefix update process.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
46
6093
5ed91fe54bc9 Implemented some optimizations with respect to Google Safe Browsing and browser startup with Qt Help support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
47 enabled = (
5ed91fe54bc9 Implemented some optimizations with respect to Google Safe Browsing and browser startup with Qt Help support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
48 Preferences.getWebBrowser("SafeBrowsingEnabled") and
5ed91fe54bc9 Implemented some optimizations with respect to Google Safe Browsing and browser startup with Qt Help support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
49 bool(Preferences.getWebBrowser("SafeBrowsingApiKey"))
5ed91fe54bc9 Implemented some optimizations with respect to Google Safe Browsing and browser startup with Qt Help support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
50 )
5ed91fe54bc9 Implemented some optimizations with respect to Google Safe Browsing and browser startup with Qt Help support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
51
5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
52 def __init__(self):
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
53 """
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
54 Constructor
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
55 """
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
56 super(SafeBrowsingManager, self).__init__()
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
57
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
58 self.__apiKey = Preferences.getWebBrowser("SafeBrowsingApiKey")
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
59 if self.__apiKey:
5829
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
60 self.__apiClient = SafeBrowsingAPIClient(self.__apiKey,
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
61 parent=self)
5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
62 else:
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
63 self.__apiClient = None
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
64
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
65 gsbCachePath = os.path.join(
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
66 Utilities.getConfigDir(), "web_browser", "safe_browsing")
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
67 self.__cache = SafeBrowsingCache(gsbCachePath, self)
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
68
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
69 self.__gsbDialog = None
5829
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
70 self.__setPlatforms()
6233
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
71 self.__setLookupMethod()
5839
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
72
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
73 self.__updatingThreatLists = False
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
74 self.__threatListsUpdateTimer = QTimer(self)
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
75 self.__threatListsUpdateTimer.setSingleShot(True)
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
76 self.__threatListsUpdateTimer.timeout.connect(
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
77 self.__threatListsUpdateTimerTimeout)
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
78 self.__setAutoUpdateThreatLists()
5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
79
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
80 def configurationChanged(self):
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
81 """
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
82 Public method to handle changes of the settings.
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
83 """
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
84 apiKey = Preferences.getWebBrowser("SafeBrowsingApiKey")
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
85 if apiKey != self.__apiKey:
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
86 self.__apiKey = apiKey
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
87 if self.__apiKey:
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
88 if self.__apiClient:
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
89 self.__apiClient.setApiKey(self.__apiKey)
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
90 else:
5829
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
91 self.__apiClient = SafeBrowsingAPIClient(self.__apiKey,
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
92 parent=self)
5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
93
6093
5ed91fe54bc9 Implemented some optimizations with respect to Google Safe Browsing and browser startup with Qt Help support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
94 SafeBrowsingManager.enabled = (
5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
95 Preferences.getWebBrowser("SafeBrowsingEnabled") and
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
96 bool(self.__apiKey))
5829
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
97
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
98 self.__setPlatforms()
6233
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
99 self.__setLookupMethod()
5839
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
100 self.__setAutoUpdateThreatLists()
5829
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
101
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
102 def __setPlatforms(self):
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
103 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
104 Private method to set the platforms to be checked against.
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
105 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
106 self.__platforms = None
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
107 if Preferences.getWebBrowser("SafeBrowsingFilterPlatform"):
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
108 if Utilities.isWindowsPlatform():
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
109 platform = "windows"
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
110 elif Utilities.isMacPlatform():
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
111 platform = "macos"
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
112 else:
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
113 # treat all other platforms like linux
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
114 platform = "linux"
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
115 self.__platforms = SafeBrowsingAPIClient.getPlatformTypes(platform)
5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
116
6233
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
117 def __setLookupMethod(self):
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
118 """
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
119 Private method to set the lookup method (Update API or Lookup API).
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
120 """
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
121 self.__useLookupApi = Preferences.getWebBrowser(
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
122 "SafeBrowsingUseLookupApi")
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
123
6093
5ed91fe54bc9 Implemented some optimizations with respect to Google Safe Browsing and browser startup with Qt Help support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
124 @classmethod
5ed91fe54bc9 Implemented some optimizations with respect to Google Safe Browsing and browser startup with Qt Help support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
125 def isEnabled(cls):
5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
126 """
6093
5ed91fe54bc9 Implemented some optimizations with respect to Google Safe Browsing and browser startup with Qt Help support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
127 Class method to check, if safe browsing is enabled.
5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
128
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
129 @return flag indicating the enabled state
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
130 @rtype bool
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
131 """
6093
5ed91fe54bc9 Implemented some optimizations with respect to Google Safe Browsing and browser startup with Qt Help support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
132 return cls.enabled
5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
133
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
134 def close(self):
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
135 """
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
136 Public method to close the safe browsing interface.
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
137 """
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
138 self.__cache.close()
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
139
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
140 def fairUseDelayExpired(self):
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
141 """
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
142 Public method to check, if the fair use wait period has expired.
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
143
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
144 @return flag indicating expiration
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
145 @rtype bool
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
146 """
6093
5ed91fe54bc9 Implemented some optimizations with respect to Google Safe Browsing and browser startup with Qt Help support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
147 return self.isEnabled() and self.__apiClient.fairUseDelayExpired()
5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
148
5842
c3f41b959a65 Fine tuned the safe browsing update notifications.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5839
diff changeset
149 def __showNotificationMessage(self, message, timeout=5):
5839
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
150 """
5842
c3f41b959a65 Fine tuned the safe browsing update notifications.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5839
diff changeset
151 Private method to show some message in a notification widget.
c3f41b959a65 Fine tuned the safe browsing update notifications.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5839
diff changeset
152
c3f41b959a65 Fine tuned the safe browsing update notifications.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5839
diff changeset
153 If desktop notifications have been disabled, the message will
c3f41b959a65 Fine tuned the safe browsing update notifications.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5839
diff changeset
154 be shown in the status bar of the main window (either the main
c3f41b959a65 Fine tuned the safe browsing update notifications.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5839
diff changeset
155 web browser window or the eric main window)
5839
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
156
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
157 @param message message to be shown
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
158 @type str
5842
c3f41b959a65 Fine tuned the safe browsing update notifications.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5839
diff changeset
159 @param timeout amount of time in seconds the message should be shown
c3f41b959a65 Fine tuned the safe browsing update notifications.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5839
diff changeset
160 (0 = indefinitely)
c3f41b959a65 Fine tuned the safe browsing update notifications.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5839
diff changeset
161 @type int
5839
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
162 """
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
163 from WebBrowser.WebBrowserWindow import WebBrowserWindow
5842
c3f41b959a65 Fine tuned the safe browsing update notifications.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5839
diff changeset
164
c3f41b959a65 Fine tuned the safe browsing update notifications.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5839
diff changeset
165 if WebBrowserWindow.notificationsEnabled():
c3f41b959a65 Fine tuned the safe browsing update notifications.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5839
diff changeset
166 WebBrowserWindow.showNotification(
c3f41b959a65 Fine tuned the safe browsing update notifications.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5839
diff changeset
167 UI.PixmapCache.getPixmap("safeBrowsing48.png"),
c3f41b959a65 Fine tuned the safe browsing update notifications.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5839
diff changeset
168 self.tr("Google Safe Browsing"),
c3f41b959a65 Fine tuned the safe browsing update notifications.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5839
diff changeset
169 message,
c3f41b959a65 Fine tuned the safe browsing update notifications.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5839
diff changeset
170 timeout=timeout,
c3f41b959a65 Fine tuned the safe browsing update notifications.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5839
diff changeset
171 )
c3f41b959a65 Fine tuned the safe browsing update notifications.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5839
diff changeset
172 else:
c3f41b959a65 Fine tuned the safe browsing update notifications.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5839
diff changeset
173 statusBar = WebBrowserWindow.globalStatusBar()
c3f41b959a65 Fine tuned the safe browsing update notifications.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5839
diff changeset
174 if statusBar is not None:
c3f41b959a65 Fine tuned the safe browsing update notifications.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5839
diff changeset
175 statusBar.showMessage(message, timeout * 1000)
5839
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
176
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
177 def __setAutoUpdateThreatLists(self):
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
178 """
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
179 Private method to set auto update for the threat lists.
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
180 """
6233
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
181 autoUpdateEnabled = \
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
182 Preferences.getWebBrowser("SafeBrowsingAutoUpdate") and \
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
183 not Preferences.getWebBrowser("SafeBrowsingUseLookupApi")
6093
5ed91fe54bc9 Implemented some optimizations with respect to Google Safe Browsing and browser startup with Qt Help support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
184 if autoUpdateEnabled and self.isEnabled():
5839
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
185 nextUpdateDateTime = Preferences.getWebBrowser(
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
186 "SafeBrowsingUpdateDateTime")
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
187 if nextUpdateDateTime.isValid():
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
188 interval = \
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
189 QDateTime.currentDateTime().secsTo(nextUpdateDateTime) + 2
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
190 # 2 seconds extra wait time; interval in milliseconds
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
191
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
192 if interval < 5:
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
193 interval = 5
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
194 # minimum 5 seconds interval
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
195 else:
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
196 interval = 5
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
197 # just wait 5 seconds
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
198 self.__threatListsUpdateTimer.start(interval * 1000)
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
199 else:
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
200 if self.__threatListsUpdateTimer.isActive():
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
201 self.__threatListsUpdateTimer.stop()
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
202
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
203 @pyqtSlot()
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
204 def __threatListsUpdateTimerTimeout(self):
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
205 """
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
206 Private slot to perform the auto update of the threat lists.
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
207 """
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
208 ok = False
6093
5ed91fe54bc9 Implemented some optimizations with respect to Google Safe Browsing and browser startup with Qt Help support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
209 if self.isEnabled():
5842
c3f41b959a65 Fine tuned the safe browsing update notifications.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5839
diff changeset
210 self.__showNotificationMessage(
c3f41b959a65 Fine tuned the safe browsing update notifications.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5839
diff changeset
211 self.tr("Updating threat lists..."), 0)
5839
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
212 ok = self.updateHashPrefixCache()[0]
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
213 if ok:
5842
c3f41b959a65 Fine tuned the safe browsing update notifications.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5839
diff changeset
214 self.__showNotificationMessage(
5839
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
215 self.tr("Updating threat lists done"))
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
216 else:
5842
c3f41b959a65 Fine tuned the safe browsing update notifications.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5839
diff changeset
217 self.__showNotificationMessage(
5839
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
218 self.tr("Updating threat lists failed"))
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
219
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
220 if ok:
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
221 nextUpdateDateTime = \
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
222 self.__apiClient.getFairUseDelayExpirationDateTime()
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
223 Preferences.setWebBrowser("SafeBrowsingUpdateDateTime",
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
224 nextUpdateDateTime)
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
225 self.__threatListsUpdateTimer.start(
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
226 (QDateTime.currentDateTime().secsTo(nextUpdateDateTime) + 2) *
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
227 1000)
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
228 # 2 seconds extra wait time; interval in milliseconds
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
229 else:
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
230 Preferences.setWebBrowser("SafeBrowsingUpdateDateTime",
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
231 QDateTime())
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
232
5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
233 def updateHashPrefixCache(self):
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
234 """
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
235 Public method to load or update the locally cached threat lists.
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
236
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
237 @return flag indicating success and an error message
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
238 @rtype tuple of (bool, str)
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
239 """
6093
5ed91fe54bc9 Implemented some optimizations with respect to Google Safe Browsing and browser startup with Qt Help support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
240 if not self.isEnabled():
5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
241 return False, self.tr("Safe Browsing is disabled.")
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
242
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
243 if not self.__apiClient.fairUseDelayExpired():
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
244 return False, \
5829
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
245 self.tr("The fair use wait period has not expired yet."
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
246 "Expiration will be at {0}.").format(
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
247 self.__apiClient.getFairUseDelayExpirationDateTime()
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
248 .toString("yyyy-MM-dd, HH:mm:ss"))
5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
249
5839
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
250 self.__updatingThreatLists = True
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
251 ok = True
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
252 errorMessage = ""
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
253
5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
254 # step 1: remove expired hashes
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
255 self.__cache.cleanupFullHashes()
5853
e45a570528a4 Changed the safe browsing code to be less interruptive.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5842
diff changeset
256 QCoreApplication.processEvents()
5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
257
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
258 # step 2: update threat lists
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
259 threatListsForRemove = {}
6187
2cc7e3629784 Started to fix some unused loop variable warnings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6093
diff changeset
260 for threatList, _clientState in self.__cache.getThreatLists():
5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
261 threatListsForRemove[repr(threatList)] = threatList
6234
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
262 threatLists, error = self.__apiClient.getThreatLists()
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
263 if error:
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
264 return False, error
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
265
5821
6c7766cde4c1 Added a progress bar to the management part and fine tuned the hash prefix update process.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
266 maximum = len(threatLists)
6c7766cde4c1 Added a progress bar to the management part and fine tuned the hash prefix update process.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
267 current = 0
6c7766cde4c1 Added a progress bar to the management part and fine tuned the hash prefix update process.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
268 self.progressMessage.emit(self.tr("Updating threat lists"), maximum)
5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
269 for entry in threatLists:
5821
6c7766cde4c1 Added a progress bar to the management part and fine tuned the hash prefix update process.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
270 current += 1
6c7766cde4c1 Added a progress bar to the management part and fine tuned the hash prefix update process.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
271 self.progress.emit(current)
6c7766cde4c1 Added a progress bar to the management part and fine tuned the hash prefix update process.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
272 QCoreApplication.processEvents()
5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
273 threatList = ThreatList.fromApiEntry(entry)
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
274 if self.__platforms is None or \
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
275 threatList.platformType in self.__platforms:
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
276 self.__cache.addThreatList(threatList)
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
277 key = repr(threatList)
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
278 if key in threatListsForRemove:
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
279 del threatListsForRemove[key]
5821
6c7766cde4c1 Added a progress bar to the management part and fine tuned the hash prefix update process.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
280 maximum = len(threatListsForRemove.values())
6c7766cde4c1 Added a progress bar to the management part and fine tuned the hash prefix update process.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
281 current = 0
6c7766cde4c1 Added a progress bar to the management part and fine tuned the hash prefix update process.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
282 self.progressMessage.emit(self.tr("Deleting obsolete threat lists"),
6c7766cde4c1 Added a progress bar to the management part and fine tuned the hash prefix update process.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
283 maximum)
5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
284 for threatList in threatListsForRemove.values():
5821
6c7766cde4c1 Added a progress bar to the management part and fine tuned the hash prefix update process.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
285 current += 1
6c7766cde4c1 Added a progress bar to the management part and fine tuned the hash prefix update process.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
286 self.progress.emit(current)
6c7766cde4c1 Added a progress bar to the management part and fine tuned the hash prefix update process.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
287 QCoreApplication.processEvents()
5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
288 self.__cache.deleteHashPrefixList(threatList)
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
289 self.__cache.deleteThreatList(threatList)
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
290 del threatListsForRemove
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
291
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
292 # step 3: update threats
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
293 threatLists = self.__cache.getThreatLists()
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
294 clientStates = {}
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
295 for threatList, clientState in threatLists:
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
296 clientStates[threatList.asTuple()] = clientState
6234
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
297 threatsUpdateResponses, error = \
5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
298 self.__apiClient.getThreatsUpdate(clientStates)
6234
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
299 if error:
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
300 return False, error
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
301
5821
6c7766cde4c1 Added a progress bar to the management part and fine tuned the hash prefix update process.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
302 maximum = len(threatsUpdateResponses)
6c7766cde4c1 Added a progress bar to the management part and fine tuned the hash prefix update process.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
303 current = 0
6c7766cde4c1 Added a progress bar to the management part and fine tuned the hash prefix update process.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
304 self.progressMessage.emit(self.tr("Updating hash prefixes"), maximum)
5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
305 for response in threatsUpdateResponses:
5821
6c7766cde4c1 Added a progress bar to the management part and fine tuned the hash prefix update process.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
306 current += 1
6c7766cde4c1 Added a progress bar to the management part and fine tuned the hash prefix update process.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
307 self.progress.emit(current)
6c7766cde4c1 Added a progress bar to the management part and fine tuned the hash prefix update process.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
308 QCoreApplication.processEvents()
5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
309 responseThreatList = ThreatList.fromApiEntry(response)
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
310 if response["responseType"] == "FULL_UPDATE":
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
311 self.__cache.deleteHashPrefixList(responseThreatList)
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
312 for removal in response.get("removals", []):
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
313 self.__cache.removeHashPrefixIndices(
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
314 responseThreatList, removal["rawIndices"]["indices"])
5821
6c7766cde4c1 Added a progress bar to the management part and fine tuned the hash prefix update process.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
315 QCoreApplication.processEvents()
5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
316 for addition in response.get("additions", []):
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
317 hashPrefixList = HashPrefixList(
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
318 addition["rawHashes"]["prefixSize"],
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
319 base64.b64decode(addition["rawHashes"]["rawHashes"]))
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
320 self.__cache.populateHashPrefixList(responseThreatList,
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
321 hashPrefixList)
5821
6c7766cde4c1 Added a progress bar to the management part and fine tuned the hash prefix update process.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5820
diff changeset
322 QCoreApplication.processEvents()
5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
323 expectedChecksum = base64.b64decode(response["checksum"]["sha256"])
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
324 if self.__verifyThreatListChecksum(responseThreatList,
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
325 expectedChecksum):
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
326 self.__cache.updateThreatListClientState(
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
327 responseThreatList, response["newClientState"])
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
328 else:
5839
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
329 ok = False
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
330 errorMessage = self.tr(
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
331 "Local cache checksum does not match the server. Consider"
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
332 " cleaning the cache. Threat update has been aborted.")
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
333
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
334 self.__updatingThreatLists = False
5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
335
5839
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
336 return ok, errorMessage
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
337
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
338 def isUpdatingThreatLists(self):
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
339 """
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
340 Public method to check, if we are in the process of updating the
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
341 threat lists.
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
342
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
343 @return flag indicating an update process is active
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
344 @rtype bool
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
345 """
fe4d62e23908 Added an auto-update feature to the safe browsing manager of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5831
diff changeset
346 return self.__updatingThreatLists
5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
347
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
348 def __verifyThreatListChecksum(self, threatList, remoteChecksum):
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
349 """
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
350 Private method to verify the local checksum of a threat list with the
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
351 checksum of the safe browsing server.
5829
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
352
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
353 @param threatList threat list to calculate checksum for
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
354 @type ThreatList
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
355 @param remoteChecksum SHA256 checksum as reported by the Google server
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
356 @type bytes
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
357 @return flag indicating equality
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
358 @rtype bool
5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
359 """
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
360 localChecksum = self.__cache.hashPrefixListChecksum(threatList)
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
361 return remoteChecksum == localChecksum
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
362
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
363 def fullCacheCleanup(self):
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
364 """
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
365 Public method to clean up the cache completely.
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
366 """
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
367 self.__cache.prepareCacheDb()
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
368
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
369 def showSafeBrowsingDialog(self):
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
370 """
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
371 Public slot to show the safe browsing management dialog.
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
372 """
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
373 if self.__gsbDialog is None:
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
374 from WebBrowser.WebBrowserWindow import WebBrowserWindow
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
375 from .SafeBrowsingDialog import SafeBrowsingDialog
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
376 self.__gsbDialog = SafeBrowsingDialog(
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
377 self, parent=WebBrowserWindow.mainWindow())
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
378
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
379 self.__gsbDialog.show()
5829
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
380
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
381 def lookupUrl(self, url):
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
382 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
383 Public method to lookup an URL.
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
384
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
385 @param url URL to be checked
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
386 @type str or QUrl
6234
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
387 @return tuple containing the list of threat lists the URL was found in
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
388 and an error message
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
389 @rtype tuple of (list of ThreatList, str)
5829
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
390 @exception ValueError raised for an invalid URL
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
391 """
6093
5ed91fe54bc9 Implemented some optimizations with respect to Google Safe Browsing and browser startup with Qt Help support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
392 if self.isEnabled():
6233
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
393 if self.__useLookupApi:
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
394 if isinstance(url, str):
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
395 url = QUrl(url.strip())
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
396
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
397 if url.isEmpty():
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
398 raise ValueError("Empty URL given.")
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
399
6234
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
400 listNames, error = \
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
401 self.__apiClient.lookupUrl(url, self.__platforms)
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
402 return listNames, error
5829
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
403 else:
6233
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
404 if isinstance(url, QUrl):
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
405 urlStr = url.toString().strip()
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
406 else:
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
407 urlStr = url.strip()
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
408
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
409 if not urlStr:
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
410 raise ValueError("Empty URL given.")
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
411
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
412 urlHashes = SafeBrowsingUrl(urlStr).hashes()
a64b986abb54 Web Browser (NG), Safe Browsing: added support for the Google Safe Browsing Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6230
diff changeset
413 listNames = self.__lookupHashes(urlHashes)
6234
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
414
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
415 return listNames, ""
5829
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
416
6234
fb1f9e681848 Web Browser NG, Safe Browsing: improved error handling
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6233
diff changeset
417 return None, ""
5829
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
418
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
419 def __lookupHashes(self, fullHashes):
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
420 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
421 Private method to lookup the given hashes.
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
422
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
423 @param fullHashes list of hashes to lookup
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
424 @type list of bytes
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
425 @return names of threat lists hashes were found in
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
426 @rtype list of ThreatList
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
427 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
428 fullHashes = list(fullHashes)
7192
a22eee00b052 Started removing runtime support for Python2 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6942
diff changeset
429 cues = [fh[:4].hex() for fh in fullHashes]
5829
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
430 result = []
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
431
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
432 matchingPrefixes = {}
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
433 matchingFullHashes = set()
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
434 isPotentialThreat = False
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
435 # Lookup hash prefixes which match full URL hash
6187
2cc7e3629784 Started to fix some unused loop variable warnings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6093
diff changeset
436 for _threatList, hashPrefix, negativeCacheExpired in \
5829
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
437 self.__cache.lookupHashPrefix(cues):
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
438 for fullHash in fullHashes:
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
439 if fullHash.startswith(hashPrefix):
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
440 isPotentialThreat = True
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
441 # consider hash prefix negative cache as expired if it
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
442 # is expired in at least one threat list
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
443 matchingPrefixes[hashPrefix] = matchingPrefixes.get(
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
444 hashPrefix, False) or negativeCacheExpired
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
445 matchingFullHashes.add(fullHash)
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
446
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
447 # if none matches, url hash is clear
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
448 if not isPotentialThreat:
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
449 return []
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
450
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
451 # if there is non-expired full hash, URL is blacklisted
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
452 matchingExpiredThreatLists = set()
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
453 for threatList, hasExpired in self.__cache.lookupFullHashes(
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
454 matchingFullHashes):
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
455 if hasExpired:
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
456 matchingExpiredThreatLists.add(threatList)
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
457 else:
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
458 result.append(threatList)
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
459 if result:
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
460 return result
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
461
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
462 # If there are no matching expired full hash entries and negative
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
463 # cache is still current for all prefixes, consider it safe.
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
464 if len(matchingExpiredThreatLists) == 0 and \
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
465 sum(map(int, matchingPrefixes.values())) == 0:
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
466 return []
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
467
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
468 # Now it can be assumed that there are expired matching full hash
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
469 # entries and/or cache prefix entries with expired negative cache.
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
470 # Both require full hash synchronization.
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
471 self.__syncFullHashes(matchingPrefixes.keys())
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
472
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
473 # Now repeat full hash lookup
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
474 for threatList, hasExpired in self.__cache.lookupFullHashes(
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
475 matchingFullHashes):
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
476 if not hasExpired:
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
477 result.append(threatList)
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
478
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
479 return result
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
480
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
481 def __syncFullHashes(self, hashPrefixes):
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
482 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
483 Private method to download full hashes matching given prefixes.
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
484
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
485 This also updates the cache expiration timestamps.
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
486
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
487 @param hashPrefixes list of hash prefixes to get full hashes for
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
488 @type list of bytes
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
489 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
490 threatLists = self.__cache.getThreatLists()
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
491 clientStates = {}
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
492 for threatList, clientState in threatLists:
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
493 clientStates[threatList.asTuple()] = clientState
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
494
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
495 fullHashResponses = self.__apiClient.getFullHashes(
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
496 hashPrefixes, clientStates)
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
497
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
498 # update negative cache for each hash prefix
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
499 # store full hash with positive cache bumped up
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
500 for match in fullHashResponses["matches"]:
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
501 threatList = ThreatList.fromApiEntry(match)
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
502 hashValue = base64.b64decode(match["threat"]["hash"])
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
503 cacheDuration = int(match["cacheDuration"].rstrip("s"))
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
504 malwareThreatType = None
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
505 for metadata in match["threatEntryMetadata"].get("entries", []):
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
506 key = base64.b64decode(metadata["key"])
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
507 value = base64.b64decode(metadata["value"])
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
508 if key == b"malware_threat_type":
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
509 malwareThreatType = value
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
510 if not isinstance(malwareThreatType, str):
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
511 malwareThreatType = malwareThreatType.decode()
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
512 self.__cache.storeFullHash(threatList, hashValue, cacheDuration,
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
513 malwareThreatType)
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
514
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
515 negativeCacheDuration = int(
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
516 fullHashResponses["negativeCacheDuration"].rstrip("s"))
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
517 for prefixValue in hashPrefixes:
6187
2cc7e3629784 Started to fix some unused loop variable warnings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6093
diff changeset
518 for threatList, _clientState in threatLists:
5829
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
519 self.__cache.updateHashPrefixExpiration(
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
520 threatList, prefixValue, negativeCacheDuration)
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
521
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
522 @classmethod
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
523 def getIgnoreSchemes(cls):
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
524 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
525 Class method to get the schemes not to be checked.
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
526
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
527 @return list of schemes to be ignored
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
528 @rtype list of str
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
529 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
530 return [
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
531 "about",
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
532 "eric",
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
533 "qrc",
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
534 "qthelp",
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
535 "chrome",
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
536 "abp",
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
537 "file",
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
538 ]
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
539
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
540 def getThreatMessage(self, threatType):
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
541 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
542 Public method to get a warning message for the given threat type.
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
543
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
544 @param threatType threat type to get the message for
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
545 @type str
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
546 @return threat message
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
547 @rtype str
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
548 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
549 if self.__apiClient:
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
550 msg = self.__apiClient.getThreatMessage(threatType)
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
551 else:
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
552 msg = ""
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
553
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
554 return msg
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
555
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
556 def getThreatMessages(self, threatLists):
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
557 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
558 Public method to get threat messages for the given threats.
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
559
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
560 @param threatLists list of threat lists to get a message for
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
561 @type list of ThreatList
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
562 @return list of threat messages, one per unique threat type
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
563 @rtype list of str
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
564 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
565 threatTypes = set()
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
566 for threatList in threatLists:
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
567 threatTypes.add(threatList.threatType)
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
568
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
569 messages = []
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
570 if self.__apiClient:
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
571 for threatType in sorted(threatTypes):
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
572 msg = self.__apiClient.getThreatMessage(threatType)
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
573 messages.append(msg)
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
574
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
575 return messages
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
576
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
577 def getThreatType(self, threatList):
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
578 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
579 Public method to get a display string for a given threat type.
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
580
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
581 @param threatList threat list to get display string for
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
582 @type str
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
583 @return display string
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
584 @rtype str
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
585 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
586 displayString = ""
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
587 if self.__apiClient:
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
588 displayString = self.__apiClient.getThreatType(
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
589 threatList.threatType)
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
590 return displayString
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
591
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
592 def getPlatformString(self, platformType):
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
593 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
594 Public method to get the platform string for a given platform type.
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
595
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
596 @param platformType platform type as defined in the v4 API
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
597 @type str
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
598 @return platform string
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
599 @rtype str
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
600 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
601 if self.__apiClient:
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
602 return self.__apiClient.getPlatformString(platformType)
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
603 else:
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
604 return ""
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
605
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
606 def getThreatEntryString(self, threatEntry):
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
607 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
608 Public method to get the threat entry string.
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
609
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
610 @param threatEntry threat entry type as defined in the v4 API
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
611 @type str
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
612 @return threat entry string
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
613 @rtype str
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
614 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
615 if self.__apiClient:
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
616 return self.__apiClient.getThreatEntryString(threatEntry)
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
617 else:
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
618 return ""

eric ide

mercurial