1 # -*- coding: utf-8 -*-
3 # ------------------------------------------------------------
6 # an opensecurity dialog
8 # Autor: Oliver Maurhart, <oliver.maurhart@ait.ac.at>
10 # Copyright (C) 2013 AIT Austrian Institute of Technology
11 # AIT Austrian Institute of Technology GmbH
12 # Donau-City-Strasse 1 | 1220 Vienna | Austria
13 # http://www.ait.ac.at
15 # This program is free software; you can redistribute it and/or
16 # modify it under the terms of the GNU General Public License
17 # as published by the Free Software Foundation version 2.
19 # This program is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 # GNU General Public License for more details.
24 # You should have received a copy of the GNU General Public License
25 # along with this program; if not, write to the Free Software
26 # Foundation, Inc., 51 Franklin Street, Fifth Floor,
27 # Boston, MA 02110-1301, USA.
28 # ------------------------------------------------------------
31 # ------------------------------------------------------------
43 from PyQt4 import QtCore
44 from PyQt4 import QtGui
47 import __init__ as opensecurity
49 if sys.platform == 'win32' or sys.platform == 'cygwin':
50 from cygwin import Cygwin
52 import opensecurity_client_restful_server
53 from ui import AboutDialog
54 from ui import ConfigureDialog
55 from ui import opensecurity_rc
58 # ------------------------------------------------------------
62 class OpenSecurityWait(QtGui.QDialog):
64 """OpenSecurity: please wait ..."""
66 def __init__(self, parent = None, flags = QtCore.Qt.WindowFlags(0)):
67 super(OpenSecurityWait, self).__init__(parent, flags)
68 self.setWindowTitle('OpenSecurity')
73 """Create the widgets."""
75 lyMain = QtGui.QVBoxLayout(self)
76 lyMain.setContentsMargins(8, 8, 8, 8)
78 # content area: left pixmap, right text
79 lbTitle = QtGui.QLabel('Creating secure subsystem. Please stand by ...')
80 lyMain.addWidget(lbTitle)
82 self.setMinimumSize(400, 50)
83 self.resize(lyMain.minimumSize())
86 class OpenSecurityTrayIcon(QtGui.QSystemTrayIcon):
88 """This is the OpenSecuirty Tray Icon"""
90 def __init__(self, icon, parent=None):
92 super(OpenSecurityTrayIcon, self).__init__(icon, parent)
94 self.activated.connect(self.activated_)
97 def activated_(self, reason):
99 """the system tray icon was activated"""
100 self.refresh_format_menu()
103 def clicked_about(self):
109 def clicked_browser(self):
110 """wish for safe internet browsing"""
112 if not (sys.platform == 'win32' or sys.platform == 'cygwin'):
113 QtGui.QMessageBox.critical(self.parent(), 'OpenSecurity Error', 'This action is not supported on this platform.\nSorry.')
118 # get a proper browsing VM
121 # TODO: HARDCODED ADDRESS OF OPENSECURITYD
122 browsing_vm = urllib2.urlopen('http://127.0.0.1:8080/browsing').readline()
123 print('Called http://127.0.0.1:8080/browsing got: ' + str(browsing_vm))
127 QtGui.QApplication.instance().processEvents()
128 QtGui.QMessageBox.critical(None, 'Failed to invoke Safe Internet Browsing', 'OpenSecurity Error')
130 QtGui.QApplication.instance().processEvents()
133 def clicked_configure(self):
134 """clicked configure"""
135 d = ConfigureDialog()
139 def clicked_exit(self):
141 opensecurity_client_restful_server.stop()
145 def clicked_launch_application(self):
146 """clicked the launch an application"""
147 dlg_launch_image = os.path.join(sys.path[0], 'ui', 'launch_dialog.py')
148 process_command = [sys.executable, dlg_launch_image]
149 process = subprocess.Popen(process_command, shell = False, stdout = subprocess.PIPE)
151 stdout = process.communicate()[0]
152 j = json.loads(stdout)
158 # get a proper browsing VM
161 # TODO: HARDCODED ADDRESS OF OPENSECURITYD
162 url = 'http://127.0.0.1:8080/sdvms/' + j['vm'] + '/application' + j['application']
163 result = urllib2.urlopen(url).readline()
169 def clicked_mail(self):
172 address = 'feedback@opensecurity.at'
173 subject = 'Feedback zu OpenSecurity V' + opensecurity.__version__
175 if sys.platform == 'linux2':
176 subprocess.Popen(['xdg-email', '--subject', subject, address])
177 elif sys.platform == 'win32' or sys.platform == 'cygwin':
178 mail_url = 'mailto:' + urllib.quote(address, '@') + '?' + urllib.quote('subject=' + subject)
179 subprocess.Popen(['cmd', '/C', 'start', mail_url])
182 def format_drive(self):
184 """format drive clicked (the sender should a QAction created with refresh_format_menu)"""
187 # fiddle the IP of the VM controlling the VM
189 ip = str(s.text().split('\\\\')[1])
191 # invoke the format drive dialog
192 dlg_format_drive = os.path.join(sys.path[0], 'ui', 'format_drive_dialog.py')
193 process_command = [sys.executable, dlg_format_drive, ip]
194 process = subprocess.Popen(process_command, shell = False, stdout = subprocess.PIPE)
196 stdout = process.communicate()[0]
202 def refresh_format_menu(self):
204 """create a new list of format 'drives'"""
205 self._menu_format.clear()
206 a = self._menu_format.addAction('<No Drive given>')
212 machines = json.load(urllib2.urlopen('http://127.0.0.1:8080/sdvms'))
213 if len(machines) == 0:
216 self._icon_network = QtGui.QIcon()
217 self._icon_network.addPixmap(QtGui.QPixmap(":/opensecurity/gfx/network-workgroup.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
219 self._menu_format.clear()
221 a = self._menu_format.addAction(m + '\\\\' + machines[m])
222 a.setIcon(self._icon_network)
223 a.triggered.connect(self.format_drive)
230 """create the user interface
231 As for the system tray this is 'just' the context menu.
234 # define the tray icon menu
235 menu = QtGui.QMenu(self.parent())
236 self.setContextMenu(menu)
239 self.acBrowser = QtGui.QAction('Secure Browsing', self.parent())
241 icon.addPixmap(QtGui.QPixmap(QtCore.QString.fromUtf8(':/opensecurity/gfx/opensecurity_browsing_64.png')), QtGui.QIcon.Normal, QtGui.QIcon.Off)
242 self.acBrowser.setIcon(icon)
243 menu.addAction(self.acBrowser)
246 # add standard menu items
247 self.acLaunch = QtGui.QAction('Launch Application', self.parent())
249 icon.addPixmap(QtGui.QPixmap(QtCore.QString.fromUtf8(':/opensecurity/gfx/opensecurity_launch_64.png')), QtGui.QIcon.Normal, QtGui.QIcon.Off)
250 self.acLaunch.setIcon(icon)
251 menu.addAction(self.acLaunch)
252 self.acConfigure = QtGui.QAction('Configuration', self.parent())
254 icon.addPixmap(QtGui.QPixmap(QtCore.QString.fromUtf8(':/opensecurity/gfx/opensecurity_configure_64.png')), QtGui.QIcon.Normal, QtGui.QIcon.Off)
255 self.acConfigure.setIcon(icon)
256 menu.addAction(self.acConfigure)
259 self._menu_format = menu.addMenu('Format')
262 self.acMail = menu.addAction('Send feedback mail')
264 icon.addPixmap(QtGui.QPixmap(QtCore.QString.fromUtf8(':/opensecurity/gfx/opensecurity_mail_64.png')), QtGui.QIcon.Normal, QtGui.QIcon.Off)
265 self.acMail.setIcon(icon)
266 self.acAbout = menu.addAction('About')
267 self.acExit = menu.addAction('Exit')
269 self.acBrowser.triggered.connect(self.clicked_browser)
270 self.acLaunch.triggered.connect(self.clicked_launch_application)
271 self.acConfigure.triggered.connect(self.clicked_configure)
272 self.acAbout.triggered.connect(self.clicked_about)
273 self.acExit.triggered.connect(self.clicked_exit)
274 self.acMail.triggered.connect(self.clicked_mail)
280 parser = argparse.ArgumentParser(description = 'OpenSecurity Tray Icon.')
281 parser.add_argument('-p', '--port', type=int, default=8090, help='port number of the REST API this instance will listen on.')
282 args = parser.parse_args()
285 a = QtGui.QApplication(sys.argv)
287 # enforce singelton process
288 if opensecurity_client_restful_server.is_already_running(args.port):
289 QtGui.QMessageBox.critical(None, 'OpenSecurity Error', 'OpenSecurity Tray Instance already launched.\nClose previous instance first.')
293 opensecurity_client_restful_server.serve(args.port, True)
295 # init tray icon widget
298 icon.addPixmap(QtGui.QPixmap(QtCore.QString.fromUtf8(":/opensecurity/gfx/opensecurity_icon_64.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
299 trayIcon = OpenSecurityTrayIcon(icon)
300 opensecurity_client_restful_server.tray_icon = trayIcon
304 a.setQuitOnLastWindowClosed(False)
309 if __name__ == "__main__":