WebBrowser/SafeBrowsing/SafeBrowsingManager.py

Sun, 27 Aug 2017 14:06:10 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sun, 27 Aug 2017 14:06:10 +0200
changeset 5853
e45a570528a4
parent 5842
c3f41b959a65
child 6048
82ad8ec9548c
permissions
-rw-r--r--

Changed the safe browsing code to be less interruptive.

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()
5853
e45a570528a4 Changed the safe browsing code to be less interruptive.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5842
diff changeset
243 QCoreApplication.processEvents()
5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
244
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
245 # step 2: update threat lists
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
246 threatListsForRemove = {}
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
247 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
248 threatListsForRemove[repr(threatList)] = threatList
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
249 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
250 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
251 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
252 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
253 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
254 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
255 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
256 QCoreApplication.processEvents()
5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
257 threatList = ThreatList.fromApiEntry(entry)
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
258 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
259 threatList.platformType in self.__platforms:
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
260 self.__cache.addThreatList(threatList)
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
261 key = repr(threatList)
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
262 if key in threatListsForRemove:
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
263 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
264 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
265 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
266 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
267 maximum)
5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
268 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
269 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
270 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
271 QCoreApplication.processEvents()
5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
272 self.__cache.deleteHashPrefixList(threatList)
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
273 self.__cache.deleteThreatList(threatList)
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
274 del threatListsForRemove
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
275
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
276 # step 3: update threats
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
277 threatLists = self.__cache.getThreatLists()
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
278 clientStates = {}
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
279 for threatList, clientState in threatLists:
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
280 clientStates[threatList.asTuple()] = clientState
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
281 threatsUpdateResponses = \
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
282 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
283 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
284 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
285 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
286 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
287 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
288 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
289 QCoreApplication.processEvents()
5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
290 responseThreatList = ThreatList.fromApiEntry(response)
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
291 if response["responseType"] == "FULL_UPDATE":
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
292 self.__cache.deleteHashPrefixList(responseThreatList)
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
293 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
294 self.__cache.removeHashPrefixIndices(
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
295 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
296 QCoreApplication.processEvents()
5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
297 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
298 hashPrefixList = HashPrefixList(
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
299 addition["rawHashes"]["prefixSize"],
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
300 base64.b64decode(addition["rawHashes"]["rawHashes"]))
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
301 self.__cache.populateHashPrefixList(responseThreatList,
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
302 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
303 QCoreApplication.processEvents()
5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
304 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
305 if self.__verifyThreatListChecksum(responseThreatList,
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
306 expectedChecksum):
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
307 self.__cache.updateThreatListClientState(
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
308 responseThreatList, response["newClientState"])
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
309 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
310 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
311 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
312 "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
313 " 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
314
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
315 self.__updatingThreatLists = False
5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
316
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
317 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
318
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 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
320 """
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 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
322 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
323
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 @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
325 @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
326 """
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
327 return self.__updatingThreatLists
5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
328
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
329 def __verifyThreatListChecksum(self, threatList, remoteChecksum):
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
330 """
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
331 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
332 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
333
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
334 @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
335 @type ThreatList
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
336 @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
337 @type bytes
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
338 @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
339 @rtype bool
5820
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
340 """
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
341 localChecksum = self.__cache.hashPrefixListChecksum(threatList)
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
342 return remoteChecksum == localChecksum
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
343
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
344 def fullCacheCleanup(self):
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
345 """
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
346 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
347 """
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
348 self.__cache.prepareCacheDb()
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
349
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
350 def showSafeBrowsingDialog(self):
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
351 """
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
352 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
353 """
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
354 if self.__gsbDialog is None:
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
355 from WebBrowser.WebBrowserWindow import WebBrowserWindow
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
356 from .SafeBrowsingDialog import SafeBrowsingDialog
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
357 self.__gsbDialog = SafeBrowsingDialog(
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
358 self, parent=WebBrowserWindow.mainWindow())
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
359
b610cb5b501a Started implementing the safe browsing manager and management dialog.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
360 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
361
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
362 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
363 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
364 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
365
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
366 @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
367 @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
368 @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
369 @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
370 @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
371 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
372 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
373 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
374 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
375 else:
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
376 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
377
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
378 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
379 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
380
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
381 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
382 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
383 if listNames:
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
384 return listNames
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
385
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
386 return None
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
387
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
388 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
389 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
390 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
391
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
392 @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
393 @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
394 @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
395 @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
396 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
397 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
398 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
399 result = []
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
400
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
401 matchingPrefixes = {}
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
402 matchingFullHashes = set()
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
403 isPotentialThreat = False
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
404 # 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
405 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
406 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
407 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
408 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
409 isPotentialThreat = True
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
410 # 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
411 # 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
412 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
413 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
414 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
415
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
416 # 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
417 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
418 return []
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
419
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
420 # 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
421 matchingExpiredThreatLists = set()
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
422 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
423 matchingFullHashes):
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
424 if hasExpired:
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
425 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
426 else:
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
427 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
428 if result:
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
429 return result
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
430
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
431 # 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
432 # 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
433 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
434 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
435 return []
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
436
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
437 # 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
438 # 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
439 # 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
440 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
441
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
442 # 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
443 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
444 matchingFullHashes):
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
445 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
446 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
447
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
448 return result
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
449
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
450 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
451 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
452 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
453
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
454 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
455
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
456 @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
457 @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
458 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
459 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
460 clientStates = {}
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
461 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
462 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
463
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
464 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
465 hashPrefixes, clientStates)
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
466
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
467 # 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
468 # 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
469 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
470 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
471 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
472 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
473 malwareThreatType = None
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
474 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
475 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
476 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
477 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
478 malwareThreatType = value
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
479 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
480 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
481 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
482 malwareThreatType)
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
483
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
484 negativeCacheDuration = int(
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
485 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
486 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
487 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
488 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
489 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
490
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
491 @classmethod
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
492 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
493 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
494 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
495
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
496 @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
497 @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
498 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
499 return [
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
500 "about",
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
501 "eric",
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
502 "qrc",
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
503 "qthelp",
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
504 "chrome",
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
505 "abp",
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
506 "file",
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
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
509 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
510 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
511 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
512
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
513 @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
514 @type str
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
515 @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
516 @rtype str
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
517 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
518 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
519 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
520 else:
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
521 msg = ""
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
522
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
523 return msg
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
524
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
525 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
526 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
527 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
528
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
529 @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
530 @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
531 @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
532 @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
533 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
534 threatTypes = set()
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
535 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
536 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
537
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
538 messages = []
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
539 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
540 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
541 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
542 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
543
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
544 return messages
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
545
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
546 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
547 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
548 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
549
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
550 @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
551 @type str
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
552 @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
553 @rtype str
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
554 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
555 displayString = ""
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
556 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
557 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
558 threatList.threatType)
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
559 return displayString
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
560
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
561 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
562 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
563 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
564
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
565 @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
566 @type str
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
567 @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
568 @rtype str
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
569 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
570 if self.__apiClient:
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
571 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
572 else:
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
573 return ""
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
574
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
575 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
576 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
577 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
578
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
579 @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
580 @type str
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
581 @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
582 @rtype str
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
583 """
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
584 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
585 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
586 else:
d3448873ced3 Finished coding the safe browsing module of the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5821
diff changeset
587 return ""

eric ide

mercurial