OpenSecurity/bin/opensecurity_tray.pyw
author BarthaM@N3SIM1218.D03.arc.local
Mon, 19 May 2014 17:19:47 +0100
changeset 163 e7fbdaabd0bc
parent 148 79ab65de75f0
child 165 a1b7a5a48a1e
permissions -rwxr-xr-x
OpenSecurity/bin/opensecurity_client_restful_server.py added join to wait for netmount /umount thread finish
     1 # -*- coding: utf-8 -*-
     2 
     3 # ------------------------------------------------------------
     4 # opensecurity-dialog
     5 # 
     6 # an opensecurity dialog
     7 #
     8 # Autor: Oliver Maurhart, <oliver.maurhart@ait.ac.at>
     9 #
    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
    14 #
    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.
    18 # 
    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.
    23 # 
    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 # ------------------------------------------------------------
    29 
    30 
    31 # ------------------------------------------------------------
    32 # imports
    33 
    34 import argparse
    35 import os
    36 import subprocess
    37 import sys
    38 import urllib
    39 import urllib2
    40 import webbrowser
    41 
    42 from PyQt4 import QtCore
    43 from PyQt4 import QtGui
    44 
    45 # local
    46 import __init__ as opensecurity
    47 
    48 if sys.platform == 'win32' or sys.platform == 'cygwin':
    49     from cygwin import Cygwin
    50 
    51 import opensecurity_client_restful_server 
    52 from ui import AboutDialog
    53 from ui import ConfigureDialog
    54 from ui import opensecurity_rc
    55 
    56 
    57 # ------------------------------------------------------------
    58 # code
    59 
    60 
    61 class OpenSecurityWait(QtGui.QDialog):
    62 
    63     """OpenSecurity: please wait ..."""
    64     
    65     def __init__(self, parent = None, flags = QtCore.Qt.WindowFlags(0)):
    66         super(OpenSecurityWait, self).__init__(parent, flags)
    67         self.setWindowTitle('OpenSecurity')
    68         self.setup_ui()
    69         
    70         
    71     def setup_ui(self):
    72         """Create the widgets."""
    73         
    74         lyMain = QtGui.QVBoxLayout(self)
    75         lyMain.setContentsMargins(8, 8, 8, 8)
    76         
    77         # content area: left pixmap, right text
    78         lbTitle = QtGui.QLabel('Creating secure subsystem. Please stand by ...')
    79         lyMain.addWidget(lbTitle)
    80         
    81         self.setMinimumSize(400, 50)
    82         self.resize(lyMain.minimumSize())
    83 
    84 
    85 class OpenSecurityTrayIcon(QtGui.QSystemTrayIcon):
    86     
    87     """This is the OpenSecuirty Tray Icon"""
    88 
    89     def __init__(self, icon, parent=None):
    90         
    91         super(OpenSecurityTrayIcon, self).__init__(icon, parent)
    92         self.setup_ui()
    93         
    94         
    95     def clicked_about(self):
    96         """clicked about"""
    97         d = AboutDialog()
    98         d.exec_()
    99     
   100 
   101     def clicked_browser(self):
   102         """wish for safe internet browsing"""
   103         
   104         if not (sys.platform == 'win32' or sys.platform == 'cygwin'):
   105             QtGui.QMessageBox.critical(self.parent(), 'OpenSecurity Error', 'This action is not supported on this platform.\nSorry.')
   106             return
   107 
   108         # tell the user to wait
   109         d = OpenSecurityWait()
   110         d.show()
   111         QtGui.QApplication.instance().processEvents()
   112         
   113         try:
   114         
   115             # get a proper browsing VM
   116             Cygwin.start_X11()
   117 
   118             # TODO: HARDCODED ADDRESS OF OPENSECURITYD
   119             browsing_vm = urllib2.urlopen('http://127.0.0.1:8080/browsing').readline()
   120             
   121         except:
   122             d.hide()
   123             QtGui.QApplication.instance().processEvents()
   124             QtGui.QMessageBox.critical(None, 'Failed to invoke Safe Internet Browsing', 'OpenSecurity Error')
   125             
   126         d.hide()
   127         QtGui.QApplication.instance().processEvents()
   128             
   129             
   130     def clicked_configure(self):
   131         """clicked configure"""
   132         d = ConfigureDialog()
   133         d.exec_()
   134     
   135 
   136     def clicked_exit(self):
   137         """clicked exit"""
   138         opensecurity_client_restful_server.stop()
   139         sys.exit(0)
   140     
   141 
   142     def clicked_launch_application(self):
   143         """clicked the launch an application"""
   144         dlg_launch_image = os.path.join(sys.path[0], 'ui', 'launch_dialog.py')
   145         process_command = [sys.executable, dlg_launch_image]
   146         print(process_command)
   147         process = subprocess.Popen(process_command, shell = False)
   148             
   149             
   150     def clicked_mail(self):
   151         
   152         """clicked mail"""
   153         address = 'feedback@opensecurity.at'
   154         subject = 'Feedback zu OpenSecurity V' + opensecurity.__version__
   155 
   156         if sys.platform == 'linux2':
   157             subprocess.Popen(['xdg-email', '--subject', subject, address])
   158         elif sys.platform == 'win32' or sys.platform == 'cygwin':
   159             mail_url = 'mailto:' + urllib.quote(address, '@') + '?' + urllib.quote('subject=' + subject)
   160             subprocess.Popen(['cmd', '/C', 'start', mail_url])
   161     
   162 
   163     def setup_ui(self):
   164         """create the user interface
   165         As for the system tray this is 'just' the context menu.
   166         """
   167     
   168         # define the tray icon menu
   169         menu = QtGui.QMenu(self.parent())
   170         self.setContextMenu(menu)
   171         
   172         # add known apps
   173         self.acBrowser = QtGui.QAction('Secure Browsing', self.parent())
   174         icon = QtGui.QIcon()
   175         icon.addPixmap(QtGui.QPixmap(QtCore.QString.fromUtf8(':/opensecurity/gfx/opensecurity_browsing_64.png')), QtGui.QIcon.Normal, QtGui.QIcon.Off)
   176         self.acBrowser.setIcon(icon)
   177         menu.addAction(self.acBrowser)
   178         menu.addSeparator()
   179         
   180         # add standard menu items
   181         self.acLaunch = QtGui.QAction('Launch Application', self.parent())
   182         icon = QtGui.QIcon()
   183         icon.addPixmap(QtGui.QPixmap(QtCore.QString.fromUtf8(':/opensecurity/gfx/opensecurity_launch_64.png')), QtGui.QIcon.Normal, QtGui.QIcon.Off)
   184         self.acLaunch.setIcon(icon)
   185         menu.addAction(self.acLaunch)
   186         self.acConfigure = QtGui.QAction('Configuration', self.parent())
   187         icon = QtGui.QIcon()
   188         icon.addPixmap(QtGui.QPixmap(QtCore.QString.fromUtf8(':/opensecurity/gfx/opensecurity_configure_64.png')), QtGui.QIcon.Normal, QtGui.QIcon.Off)
   189         self.acConfigure.setIcon(icon)
   190         menu.addAction(self.acConfigure)
   191         menu.addSeparator()
   192 
   193         self.acMail = menu.addAction('Send feedback mail')
   194         icon = QtGui.QIcon()
   195         icon.addPixmap(QtGui.QPixmap(QtCore.QString.fromUtf8(':/opensecurity/gfx/opensecurity_mail_64.png')), QtGui.QIcon.Normal, QtGui.QIcon.Off)
   196         self.acMail.setIcon(icon)
   197         self.acAbout = menu.addAction('About')
   198         self.acExit = menu.addAction('Exit')
   199        
   200         self.acBrowser.triggered.connect(self.clicked_browser)
   201         self.acLaunch.triggered.connect(self.clicked_launch_application)
   202         self.acConfigure.triggered.connect(self.clicked_configure)
   203         self.acAbout.triggered.connect(self.clicked_about)
   204         self.acExit.triggered.connect(self.clicked_exit)
   205         self.acMail.triggered.connect(self.clicked_mail)
   206         
   207 
   208 def main():
   209     
   210     # parse arguments
   211     parser = argparse.ArgumentParser(description = 'OpenSecurity Tray Icon.')
   212     parser.add_argument('-p', '--port', type=int, default=8090, help='port number of the REST API this instance will listen on.')
   213     args = parser.parse_args()
   214 
   215     # get up Qt
   216     a = QtGui.QApplication(sys.argv)
   217 
   218     # enforce singelton process
   219     if opensecurity_client_restful_server.is_already_running(args.port):
   220         QtGui.QMessageBox.critical(None, 'OpenSecurity Error', 'OpenSecurity Tray Instance already launched.\nClose previous instance first.')
   221         sys.exit(1)
   222 
   223     # start serving
   224     opensecurity_client_restful_server.serve(args.port, True)
   225 
   226     # init tray icon widget
   227     w = QtGui.QWidget()
   228     icon = QtGui.QIcon()
   229     icon.addPixmap(QtGui.QPixmap(QtCore.QString.fromUtf8(":/opensecurity/gfx/opensecurity_icon_64.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
   230     trayIcon = OpenSecurityTrayIcon(icon)
   231 
   232     # go!
   233     trayIcon.show()
   234     a.setQuitOnLastWindowClosed(False)
   235     sys.exit(a.exec_())
   236    
   237 
   238 # start
   239 if __name__ == "__main__":
   240     main()
   241