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