WebBrowser/SafeBrowsing/SafeBrowsingManager.py

Thu, 10 Aug 2017 13:58:50 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Thu, 10 Aug 2017 13:58:50 +0200
changeset 5842
c3f41b959a65
parent 5839
fe4d62e23908
child 5853
e45a570528a4
permissions
-rw-r--r--

Fine tuned the safe browsing update notifications.

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
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
3 # Copyright (c) 2017 Detlev Offenbach <detlev@die-offenbachs.de>
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
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
31 from .SafeBrowsingCache import SafeBrowsingCache, 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
32 from .SafeBrowsingUrl import SafeBrowsingUrl
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
33 from .SafeBrowsingUtilities import toHex
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
5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
47 def __init__(self):
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
48 """
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
49 Constructor
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
50 """
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
51 super(SafeBrowsingManager, self).__init__()
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
52
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
53 self.__apiKey = Preferences.getWebBrowser("SafeBrowsingApiKey")
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
54 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
55 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
56 parent=self)
5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
57 else:
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
58 self.__apiClient = None
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
59
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
60 self.__enabled = (
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
61 Preferences.getWebBrowser("SafeBrowsingEnabled") and
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
62 bool(self.__apiKey))
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
63
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
64 gsbCachePath = os.path.join(
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
65 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
66 self.__cache = SafeBrowsingCache(gsbCachePath, self)
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
67
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
68 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
69 self.__setPlatforms()
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
70
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
71 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
72 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
73 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
74 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
75 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
76 self.__setAutoUpdateThreatLists()
5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
77
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
78 def configurationChanged(self):
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 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
81 """
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
82 apiKey = Preferences.getWebBrowser("SafeBrowsingApiKey")
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
83 if apiKey != self.__apiKey:
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
84 self.__apiKey = apiKey
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
85 if self.__apiKey:
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
86 if self.__apiClient:
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
87 self.__apiClient.setApiKey(self.__apiKey)
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
88 else:
5829
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
89 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
90 parent=self)
5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
91
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
92 self.__enabled = (
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
93 Preferences.getWebBrowser("SafeBrowsingEnabled") and
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
94 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
95
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
96 self.__setPlatforms()
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
97 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
98
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
99 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
100 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
101 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
102 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
103 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
104 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
105 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
106 platform = "windows"
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
107 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
108 platform = "macos"
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
109 else:
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
110 # 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
111 platform = "linux"
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
112 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
113
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
114 def isEnabled(self):
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
115 """
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
116 Public method to check, if safe browsing is enabled.
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
117
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
118 @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
119 @rtype bool
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
120 """
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
121 return self.__enabled
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
122
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
123 def close(self):
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
124 """
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
125 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
126 """
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
127 self.__cache.close()
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 def fairUseDelayExpired(self):
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
130 """
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
131 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
132
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
133 @return flag indicating expiration
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
134 @rtype bool
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 return self.__enabled and self.__apiClient.fairUseDelayExpired()
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
137
5842
c3f41b959a65 Fine tuned the safe browsing update notifications.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5839
diff changeset
138 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
139 """
5842
c3f41b959a65 Fine tuned the safe browsing update notifications.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5839
diff changeset
140 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
141
c3f41b959a65 Fine tuned the safe browsing update notifications.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5839
diff changeset
142 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
143 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
144 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
145
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
146 @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
147 @type str
5842
c3f41b959a65 Fine tuned the safe browsing update notifications.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5839
diff changeset
148 @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
149 (0 = indefinitely)
c3f41b959a65 Fine tuned the safe browsing update notifications.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5839
diff changeset
150 @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
151 """
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
152 from WebBrowser.WebBrowserWindow import WebBrowserWindow
5842
c3f41b959a65 Fine tuned the safe browsing update notifications.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5839
diff changeset
153
c3f41b959a65 Fine tuned the safe browsing update notifications.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5839
diff changeset
154 if WebBrowserWindow.notificationsEnabled():
c3f41b959a65 Fine tuned the safe browsing update notifications.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5839
diff changeset
155 WebBrowserWindow.showNotification(
c3f41b959a65 Fine tuned the safe browsing update notifications.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5839
diff changeset
156 UI.PixmapCache.getPixmap("safeBrowsing48.png"),
c3f41b959a65 Fine tuned the safe browsing update notifications.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5839
diff changeset
157 self.tr("Google Safe Browsing"),
c3f41b959a65 Fine tuned the safe browsing update notifications.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5839
diff changeset
158 message,
c3f41b959a65 Fine tuned the safe browsing update notifications.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5839
diff changeset
159 timeout=timeout,
c3f41b959a65 Fine tuned the safe browsing update notifications.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5839
diff changeset
160 )
c3f41b959a65 Fine tuned the safe browsing update notifications.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5839
diff changeset
161 else:
c3f41b959a65 Fine tuned the safe browsing update notifications.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5839
diff changeset
162 statusBar = WebBrowserWindow.globalStatusBar()
c3f41b959a65 Fine tuned the safe browsing update notifications.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5839
diff changeset
163 if statusBar is not None:
c3f41b959a65 Fine tuned the safe browsing update notifications.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5839
diff changeset
164 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
165
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
166 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
167 """
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
168 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
169 """
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
170 autoUpdateEnabled = Preferences.getWebBrowser("SafeBrowsingAutoUpdate")
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
171 if autoUpdateEnabled and self.__enabled:
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
172 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
173 "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
174 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
175 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
176 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
177 # 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
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 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
180 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
181 # 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
182 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
183 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
184 # 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
185 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
186 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
187 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
188 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
189
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 @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
191 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
192 """
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 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
194 """
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 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
196 if self.__enabled:
5842
c3f41b959a65 Fine tuned the safe browsing update notifications.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5839
diff changeset
197 self.__showNotificationMessage(
c3f41b959a65 Fine tuned the safe browsing update notifications.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5839
diff changeset
198 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
199 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
200 if ok:
5842
c3f41b959a65 Fine tuned the safe browsing update notifications.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5839
diff changeset
201 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
202 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
203 else:
5842
c3f41b959a65 Fine tuned the safe browsing update notifications.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5839
diff changeset
204 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
205 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
206
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 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
208 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
209 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
210 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
211 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
212 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
213 (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
214 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
215 # 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
216 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
217 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
218 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
219
5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
220 def updateHashPrefixCache(self):
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
221 """
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
222 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
223
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
224 @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
225 @rtype tuple of (bool, str)
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
226 """
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
227 if not self.__enabled:
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
228 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
229
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
230 if not self.__apiClient.fairUseDelayExpired():
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
231 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
232 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
233 "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
234 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
235 .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
236
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
237 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
238 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
239 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
240
5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
241 # step 1: remove expired hashes
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
242 self.__cache.cleanupFullHashes()
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
243
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
244 # step 2: update threat lists
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
245 threatListsForRemove = {}
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
246 for threatList, clientState in self.__cache.getThreatLists():
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
247 threatListsForRemove[repr(threatList)] = threatList
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
248 threatLists = self.__apiClient.getThreatLists()
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
249 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
250 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
251 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
252 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
253 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
254 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
255 QCoreApplication.processEvents()
5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
256 threatList = ThreatList.fromApiEntry(entry)
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
257 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
258 threatList.platformType in self.__platforms:
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
259 self.__cache.addThreatList(threatList)
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
260 key = repr(threatList)
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
261 if key in threatListsForRemove:
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
262 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
263 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
264 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
265 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
266 maximum)
5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
267 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
268 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
269 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
270 QCoreApplication.processEvents()
5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
271 self.__cache.deleteHashPrefixList(threatList)
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
272 self.__cache.deleteThreatList(threatList)
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
273 del threatListsForRemove
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
274
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
275 # step 3: update threats
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
276 threatLists = self.__cache.getThreatLists()
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
277 clientStates = {}
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
278 for threatList, clientState in threatLists:
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
279 clientStates[threatList.asTuple()] = clientState
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
280 threatsUpdateResponses = \
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
281 self.__apiClient.getThreatsUpdate(clientStates)
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
282 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
283 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
284 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
285 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
286 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
287 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
288 QCoreApplication.processEvents()
5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
289 responseThreatList = ThreatList.fromApiEntry(response)
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
290 if response["responseType"] == "FULL_UPDATE":
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
291 self.__cache.deleteHashPrefixList(responseThreatList)
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
292 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
293 self.__cache.removeHashPrefixIndices(
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
294 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
295 QCoreApplication.processEvents()
5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
296 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
297 hashPrefixList = HashPrefixList(
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
298 addition["rawHashes"]["prefixSize"],
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
299 base64.b64decode(addition["rawHashes"]["rawHashes"]))
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
300 self.__cache.populateHashPrefixList(responseThreatList,
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
301 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
302 QCoreApplication.processEvents()
5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
303 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
304 if self.__verifyThreatListChecksum(responseThreatList,
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
305 expectedChecksum):
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
306 self.__cache.updateThreatListClientState(
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
307 responseThreatList, response["newClientState"])
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
308 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
309 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
310 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
311 "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
312 " 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
313
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
314 self.__updatingThreatLists = False
5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
315
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
316 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
317
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
318 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
319 """
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
320 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
321 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
322
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
323 @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
324 @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
325 """
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
326 return self.__updatingThreatLists
5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
327
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
328 def __verifyThreatListChecksum(self, threatList, remoteChecksum):
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
329 """
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
330 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
331 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
332
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
333 @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
334 @type ThreatList
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
335 @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
336 @type bytes
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
337 @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
338 @rtype bool
5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
339 """
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
340 localChecksum = self.__cache.hashPrefixListChecksum(threatList)
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
341 return remoteChecksum == localChecksum
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
342
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
343 def fullCacheCleanup(self):
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
344 """
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
345 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
346 """
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
347 self.__cache.prepareCacheDb()
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
348
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
349 def showSafeBrowsingDialog(self):
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
350 """
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
351 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
352 """
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
353 if self.__gsbDialog is None:
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
354 from WebBrowser.WebBrowserWindow import WebBrowserWindow
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
355 from .SafeBrowsingDialog import SafeBrowsingDialog
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
356 self.__gsbDialog = SafeBrowsingDialog(
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
357 self, parent=WebBrowserWindow.mainWindow())
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
358
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
359 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
360
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
361 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
362 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
363 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
364
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
365 @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
366 @type str or QUrl
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
367 @return list of threat lists the URL was found in
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
368 @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
369 @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
370 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
371 if self.__enabled:
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
372 if isinstance(url, QUrl):
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
373 urlStr = url.toString().strip()
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
374 else:
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
375 urlStr = url.strip()
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
376
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
377 if not urlStr:
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
378 raise ValueError("Empty URL given.")
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
379
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
380 urlHashes = SafeBrowsingUrl(urlStr).hashes()
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
381 listNames = self.__lookupHashes(urlHashes)
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
382 if listNames:
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
383 return listNames
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 return None
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
386
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
387 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
388 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
389 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
390
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
391 @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
392 @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
393 @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
394 @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
395 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
396 fullHashes = list(fullHashes)
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
397 cues = [toHex(fh[:4]) for fh in fullHashes]
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
398 result = []
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
399
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
400 matchingPrefixes = {}
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
401 matchingFullHashes = set()
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
402 isPotentialThreat = False
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
403 # Lookup hash prefixes which match full URL hash
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
404 for threatList, hashPrefix, negativeCacheExpired in \
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
405 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
406 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
407 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
408 isPotentialThreat = True
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
409 # 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
410 # 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
411 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
412 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
413 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
414
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
415 # 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
416 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
417 return []
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 # 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
420 matchingExpiredThreatLists = set()
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
421 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
422 matchingFullHashes):
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
423 if hasExpired:
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
424 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
425 else:
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
426 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
427 if result:
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
428 return result
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
429
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
430 # 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
431 # 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
432 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
433 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
434 return []
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
435
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
436 # 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
437 # 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
438 # 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
439 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
440
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
441 # 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
442 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
443 matchingFullHashes):
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
444 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
445 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
446
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
447 return result
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
448
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
449 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
450 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
451 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
452
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
453 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
454
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
455 @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
456 @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
457 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
458 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
459 clientStates = {}
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
460 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
461 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
462
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
463 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
464 hashPrefixes, clientStates)
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
465
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
466 # 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
467 # 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
468 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
469 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
470 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
471 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
472 malwareThreatType = None
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
473 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
474 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
475 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
476 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
477 malwareThreatType = value
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
478 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
479 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
480 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
481 malwareThreatType)
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 negativeCacheDuration = int(
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
484 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
485 for prefixValue in hashPrefixes:
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
486 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
487 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
488 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
489
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
490 @classmethod
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
491 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
492 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
493 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
494
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
495 @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
496 @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
497 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
498 return [
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
499 "about",
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
500 "eric",
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
501 "qrc",
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
502 "qthelp",
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
503 "chrome",
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
504 "abp",
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
505 "file",
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
506 ]
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
507
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
508 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
509 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
510 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
511
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
512 @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
513 @type str
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
514 @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
515 @rtype str
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
516 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
517 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
518 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
519 else:
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
520 msg = ""
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 return msg
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
523
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
524 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
525 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
526 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
527
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
528 @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
529 @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
530 @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
531 @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
532 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
533 threatTypes = set()
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
534 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
535 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
536
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
537 messages = []
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
538 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
539 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
540 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
541 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
542
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
543 return messages
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
544
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
545 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
546 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
547 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
548
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
549 @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
550 @type str
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
551 @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
552 @rtype str
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 displayString = ""
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
555 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
556 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
557 threatList.threatType)
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
558 return displayString
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 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
561 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
562 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
563
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
564 @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
565 @type str
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
566 @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
567 @rtype str
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 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
570 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
571 else:
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
572 return ""
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
573
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
574 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
575 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
576 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
577
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
578 @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
579 @type str
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
580 @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
581 @rtype str
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
582 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
583 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
584 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
585 else:
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
586 return ""

eric ide

mercurial