OpenSecurity/bin/opensecurity_tray.pyw
author Oliver Maurhart <oliver.maurhart@ait.ac.at>
Thu, 24 Jul 2014 11:57:00 +0200
changeset 215 9d777587cdab
parent 213 2e0b94e12bfc
child 223 a4fb6694e6fe
permissions -rwxr-xr-x
fix empty format menu entry in tray
om@13
     1
# -*- coding: utf-8 -*-
om@13
     2
om@13
     3
# ------------------------------------------------------------
om@13
     4
# opensecurity-dialog
om@13
     5
# 
om@13
     6
# an opensecurity dialog
om@13
     7
#
om@13
     8
# Autor: Oliver Maurhart, <oliver.maurhart@ait.ac.at>
om@13
     9
#
om@13
    10
# Copyright (C) 2013 AIT Austrian Institute of Technology
om@13
    11
# AIT Austrian Institute of Technology GmbH
om@13
    12
# Donau-City-Strasse 1 | 1220 Vienna | Austria
om@13
    13
# http://www.ait.ac.at
om@13
    14
#
om@13
    15
# This program is free software; you can redistribute it and/or
om@13
    16
# modify it under the terms of the GNU General Public License
om@13
    17
# as published by the Free Software Foundation version 2.
om@13
    18
# 
om@13
    19
# This program is distributed in the hope that it will be useful,
om@13
    20
# but WITHOUT ANY WARRANTY; without even the implied warranty of
om@13
    21
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
om@13
    22
# GNU General Public License for more details.
om@13
    23
# 
om@13
    24
# You should have received a copy of the GNU General Public License
om@13
    25
# along with this program; if not, write to the Free Software
om@13
    26
# Foundation, Inc., 51 Franklin Street, Fifth Floor, 
om@13
    27
# Boston, MA  02110-1301, USA.
om@13
    28
# ------------------------------------------------------------
om@13
    29
om@13
    30
om@13
    31
# ------------------------------------------------------------
om@13
    32
# imports
om@13
    33
om@13
    34
import argparse
oliver@165
    35
import json
om@13
    36
import os
om@13
    37
import subprocess
om@13
    38
import sys
oliver@145
    39
import urllib
om@42
    40
import urllib2
oliver@145
    41
import webbrowser
BarthaM@213
    42
import _winreg
BarthaM@213
    43
import re
om@13
    44
om@13
    45
from PyQt4 import QtCore
om@13
    46
from PyQt4 import QtGui
om@13
    47
om@13
    48
# local
oliver@144
    49
import __init__ as opensecurity
oliver@144
    50
oliver@104
    51
if sys.platform == 'win32' or sys.platform == 'cygwin':
oliver@104
    52
    from cygwin import Cygwin
oliver@104
    53
oliver@136
    54
import opensecurity_client_restful_server 
oliver@104
    55
from ui import AboutDialog
oliver@106
    56
from ui import ConfigureDialog
oliver@104
    57
from ui import opensecurity_rc
om@13
    58
oliver@106
    59
om@13
    60
# ------------------------------------------------------------
om@13
    61
# code
om@13
    62
om@13
    63
om@42
    64
class OpenSecurityWait(QtGui.QDialog):
om@42
    65
om@42
    66
    """OpenSecurity: please wait ..."""
om@42
    67
    
om@42
    68
    def __init__(self, parent = None, flags = QtCore.Qt.WindowFlags(0)):
om@42
    69
        super(OpenSecurityWait, self).__init__(parent, flags)
om@42
    70
        self.setWindowTitle('OpenSecurity')
om@42
    71
        self.setup_ui()
om@42
    72
        
om@42
    73
        
om@42
    74
    def setup_ui(self):
om@42
    75
        """Create the widgets."""
om@42
    76
        
om@42
    77
        lyMain = QtGui.QVBoxLayout(self)
om@42
    78
        lyMain.setContentsMargins(8, 8, 8, 8)
om@42
    79
        
om@42
    80
        # content area: left pixmap, right text
om@42
    81
        lbTitle = QtGui.QLabel('Creating secure subsystem. Please stand by ...')
om@42
    82
        lyMain.addWidget(lbTitle)
om@42
    83
        
om@42
    84
        self.setMinimumSize(400, 50)
om@42
    85
        self.resize(lyMain.minimumSize())
om@42
    86
om@42
    87
om@13
    88
class OpenSecurityTrayIcon(QtGui.QSystemTrayIcon):
om@13
    89
    
om@13
    90
    """This is the OpenSecuirty Tray Icon"""
om@13
    91
om@13
    92
    def __init__(self, icon, parent=None):
om@13
    93
        
om@13
    94
        super(OpenSecurityTrayIcon, self).__init__(icon, parent)
om@13
    95
        self.setup_ui()
oliver@207
    96
        self.activated.connect(self.activated_)
oliver@207
    97
       
oliver@207
    98
oliver@207
    99
    def activated_(self, reason):
oliver@207
   100
oliver@207
   101
        """the system tray icon was activated"""
oliver@207
   102
        self.refresh_format_menu()
oliver@207
   103
om@13
   104
        
om@13
   105
    def clicked_about(self):
om@13
   106
        """clicked about"""
oliver@104
   107
        d = AboutDialog()
oliver@104
   108
        d.exec_()
om@13
   109
    
BarthaM@213
   110
    def getProxySettings(self):        
BarthaM@213
   111
        aReg = _winreg.ConnectRegistry(None,_winreg.HKEY_CURRENT_USER)
BarthaM@213
   112
        aKey = _winreg.OpenKey(aReg, r"Software\Microsoft\Windows\CurrentVersion\Internet Settings")
BarthaM@213
   113
        subCount, valueCount, lastModified = _winreg.QueryInfoKey(aKey)
BarthaM@213
   114
        reg_entries = dict()
BarthaM@213
   115
        for i in range(valueCount):                                           
BarthaM@213
   116
            try:
BarthaM@213
   117
                n,v,t = _winreg.EnumValue(aKey,i)
BarthaM@213
   118
                reg_entries[n] = v
BarthaM@213
   119
            except EnvironmentError:                                               
BarthaM@213
   120
                break
BarthaM@213
   121
        _winreg.CloseKey(aKey)
BarthaM@213
   122
BarthaM@213
   123
        if 'ProxyEnable' in reg_entries.keys() and reg_entries['ProxyEnable'] == 1:
BarthaM@213
   124
            proxy_search = re.search(r"(?<=http=)(?P<ProxyServer>.*?)(?=;)", reg_entries['ProxyServer'])
BarthaM@213
   125
            if proxy_search:
BarthaM@213
   126
                proxies = proxy_search.groupdict()
BarthaM@213
   127
                if 'ProxyServer' in proxies.keys(): # found http proxy
BarthaM@213
   128
                    return {'ProxyServer': proxies['ProxyServer']}  
BarthaM@213
   129
            return {'ProxyServer': reg_entries['ProxyServer']}
BarthaM@213
   130
        return None
om@13
   131
om@37
   132
    def clicked_browser(self):
om@37
   133
        """wish for safe internet browsing"""
om@42
   134
        
oliver@104
   135
        if not (sys.platform == 'win32' or sys.platform == 'cygwin'):
oliver@104
   136
            QtGui.QMessageBox.critical(self.parent(), 'OpenSecurity Error', 'This action is not supported on this platform.\nSorry.')
oliver@104
   137
            return
oliver@193
   138
       
om@42
   139
        try:
om@42
   140
            # get a proper browsing VM
mb@110
   141
            Cygwin.start_X11()
oliver@144
   142
oliver@144
   143
            # TODO: HARDCODED ADDRESS OF OPENSECURITYD
BarthaM@213
   144
            proxy_support = urllib2.ProxyHandler({})
BarthaM@213
   145
            opener = urllib2.build_opener(proxy_support)
BarthaM@213
   146
            urllib2.install_opener(opener)
BarthaM@213
   147
BarthaM@213
   148
            req_data = ""
BarthaM@213
   149
            proxy = self.getProxySettings()
BarthaM@213
   150
            if proxy:
BarthaM@213
   151
                req_data = '?' + urllib.urlencode(proxy) 
BarthaM@213
   152
            req = 'http://127.0.0.1:8080/browsing'+ req_data
BarthaM@213
   153
            browsing_vm = urllib2.urlopen(req).readline()
BarthaM@213
   154
            print('Called '+ req + ' got: ' + str(browsing_vm))
om@42
   155
        except:
om@42
   156
            QtGui.QApplication.instance().processEvents()
om@42
   157
            QtGui.QMessageBox.critical(None, 'Failed to invoke Safe Internet Browsing', 'OpenSecurity Error')
om@42
   158
            
om@42
   159
        QtGui.QApplication.instance().processEvents()
om@37
   160
            
om@37
   161
            
oliver@106
   162
    def clicked_configure(self):
oliver@106
   163
        """clicked configure"""
oliver@106
   164
        d = ConfigureDialog()
oliver@106
   165
        d.exec_()
oliver@106
   166
    
oliver@106
   167
om@13
   168
    def clicked_exit(self):
om@13
   169
        """clicked exit"""
oliver@136
   170
        opensecurity_client_restful_server.stop()
om@13
   171
        sys.exit(0)
om@13
   172
    
om@13
   173
om@13
   174
    def clicked_launch_application(self):
om@13
   175
        """clicked the launch an application"""
oliver@104
   176
        dlg_launch_image = os.path.join(sys.path[0], 'ui', 'launch_dialog.py')
om@13
   177
        process_command = [sys.executable, dlg_launch_image]
oliver@165
   178
        process = subprocess.Popen(process_command, shell = False, stdout = subprocess.PIPE)        
oliver@165
   179
        try:
oliver@165
   180
            stdout = process.communicate()[0]
oliver@165
   181
            j = json.loads(stdout)
oliver@165
   182
        except:
oliver@165
   183
            return
oliver@165
   184
oliver@165
   185
        try:
oliver@165
   186
        
oliver@165
   187
            # get a proper browsing VM
oliver@165
   188
            Cygwin.start_X11()
oliver@165
   189
oliver@165
   190
            # TODO: HARDCODED ADDRESS OF OPENSECURITYD
oliver@165
   191
            url = 'http://127.0.0.1:8080/sdvms/' + j['vm'] + '/application' + j['application']
oliver@165
   192
            result = urllib2.urlopen(url).readline()
oliver@165
   193
            
oliver@165
   194
        except:
oliver@165
   195
            pass 
om@13
   196
            
om@13
   197
            
oliver@144
   198
    def clicked_mail(self):
oliver@145
   199
        
oliver@144
   200
        """clicked mail"""
oliver@145
   201
        address = 'feedback@opensecurity.at'
oliver@145
   202
        subject = 'Feedback zu OpenSecurity V' + opensecurity.__version__
oliver@145
   203
oliver@145
   204
        if sys.platform == 'linux2':
oliver@145
   205
            subprocess.Popen(['xdg-email', '--subject', subject, address])
oliver@145
   206
        elif sys.platform == 'win32' or sys.platform == 'cygwin':
oliver@145
   207
            mail_url = 'mailto:' + urllib.quote(address, '@') + '?' + urllib.quote('subject=' + subject)
oliver@148
   208
            subprocess.Popen(['cmd', '/C', 'start', mail_url])
oliver@207
   209
   
oliver@207
   210
oliver@207
   211
    def format_drive(self):
oliver@207
   212
oliver@207
   213
        """format drive clicked (the sender should a QAction created with refresh_format_menu)"""
oliver@207
   214
        try:
oliver@207
   215
oliver@207
   216
            # fiddle the IP of the VM controlling the VM
oliver@207
   217
            s = self.sender()
oliver@207
   218
            ip = str(s.text().split('\\\\')[1])
oliver@207
   219
oliver@207
   220
            # invoke the format drive dialog
oliver@207
   221
            dlg_format_drive = os.path.join(sys.path[0], 'ui', 'format_drive_dialog.py')
oliver@207
   222
            process_command = [sys.executable, dlg_format_drive, ip]
oliver@207
   223
            process = subprocess.Popen(process_command, shell = False, stdout = subprocess.PIPE)        
oliver@207
   224
oliver@207
   225
            stdout = process.communicate()[0]
oliver@207
   226
oliver@207
   227
        except:
oliver@207
   228
            pass
oliver@207
   229
oliver@207
   230
oliver@207
   231
    def refresh_format_menu(self):
oliver@207
   232
oliver@207
   233
        """create a new list of format 'drives'"""
oliver@207
   234
        self._menu_format.clear()
oliver@207
   235
        a = self._menu_format.addAction('<No Drive given>')
oliver@207
   236
        a.setEnabled(False)
oliver@207
   237
oliver@207
   238
        try:
oliver@207
   239
oliver@207
   240
            # get machines
oliver@207
   241
            machines = json.load(urllib2.urlopen('http://127.0.0.1:8080/sdvms'))
oliver@207
   242
            if len(machines) == 0:
oliver@207
   243
                return
oliver@207
   244
oliver@207
   245
            self._icon_network = QtGui.QIcon()
oliver@207
   246
            self._icon_network.addPixmap(QtGui.QPixmap(":/opensecurity/gfx/network-workgroup.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
oliver@207
   247
oliver@215
   248
            cleared = False
oliver@207
   249
            for m in machines:
oliver@215
   250
oliver@215
   251
                # do not insert Browsing VM
BarthaM@213
   252
                if u'SecurityDVM0' in m:
BarthaM@213
   253
                    continue
oliver@215
   254
oliver@215
   255
                if not cleared:
oliver@215
   256
                    self._menu_format.clear()
oliver@215
   257
                    cleared = True
oliver@215
   258
oliver@207
   259
                a = self._menu_format.addAction(m + '\\\\' + machines[m])
oliver@207
   260
                a.setIcon(self._icon_network)
oliver@207
   261
                a.triggered.connect(self.format_drive)
oliver@207
   262
oliver@207
   263
        except:
oliver@207
   264
            pass
oliver@207
   265
oliver@144
   266
om@13
   267
    def setup_ui(self):
om@13
   268
        """create the user interface
om@13
   269
        As for the system tray this is 'just' the context menu.
om@13
   270
        """
om@13
   271
    
om@13
   272
        # define the tray icon menu
om@13
   273
        menu = QtGui.QMenu(self.parent())
om@13
   274
        self.setContextMenu(menu)
om@13
   275
        
om@13
   276
        # add known apps
oliver@104
   277
        self.acBrowser = QtGui.QAction('Secure Browsing', self.parent())
oliver@104
   278
        icon = QtGui.QIcon()
oliver@106
   279
        icon.addPixmap(QtGui.QPixmap(QtCore.QString.fromUtf8(':/opensecurity/gfx/opensecurity_browsing_64.png')), QtGui.QIcon.Normal, QtGui.QIcon.Off)
oliver@104
   280
        self.acBrowser.setIcon(icon)
oliver@104
   281
        menu.addAction(self.acBrowser)
om@37
   282
        menu.addSeparator()
om@13
   283
        
om@13
   284
        # add standard menu items
oliver@104
   285
        self.acLaunch = QtGui.QAction('Launch Application', self.parent())
oliver@104
   286
        icon = QtGui.QIcon()
oliver@106
   287
        icon.addPixmap(QtGui.QPixmap(QtCore.QString.fromUtf8(':/opensecurity/gfx/opensecurity_launch_64.png')), QtGui.QIcon.Normal, QtGui.QIcon.Off)
oliver@104
   288
        self.acLaunch.setIcon(icon)
oliver@104
   289
        menu.addAction(self.acLaunch)
oliver@106
   290
        self.acConfigure = QtGui.QAction('Configuration', self.parent())
oliver@106
   291
        icon = QtGui.QIcon()
oliver@106
   292
        icon.addPixmap(QtGui.QPixmap(QtCore.QString.fromUtf8(':/opensecurity/gfx/opensecurity_configure_64.png')), QtGui.QIcon.Normal, QtGui.QIcon.Off)
oliver@106
   293
        self.acConfigure.setIcon(icon)
oliver@106
   294
        menu.addAction(self.acConfigure)
om@13
   295
        menu.addSeparator()
oliver@104
   296
oliver@207
   297
        self._menu_format = menu.addMenu('Format')
oliver@207
   298
        menu.addSeparator()
oliver@207
   299
oliver@144
   300
        self.acMail = menu.addAction('Send feedback mail')
oliver@144
   301
        icon = QtGui.QIcon()
oliver@144
   302
        icon.addPixmap(QtGui.QPixmap(QtCore.QString.fromUtf8(':/opensecurity/gfx/opensecurity_mail_64.png')), QtGui.QIcon.Normal, QtGui.QIcon.Off)
oliver@144
   303
        self.acMail.setIcon(icon)
oliver@104
   304
        self.acAbout = menu.addAction('About')
oliver@215
   305
        
oliver@215
   306
        # diabled as of TICKET #11
oliver@215
   307
        # self.acExit = menu.addAction('Exit')
oliver@144
   308
       
oliver@104
   309
        self.acBrowser.triggered.connect(self.clicked_browser)
oliver@104
   310
        self.acLaunch.triggered.connect(self.clicked_launch_application)
oliver@106
   311
        self.acConfigure.triggered.connect(self.clicked_configure)
oliver@104
   312
        self.acAbout.triggered.connect(self.clicked_about)
oliver@215
   313
oliver@215
   314
        # disabled as for TICKET #11
oliver@215
   315
        # self.acExit.triggered.connect(self.clicked_exit)
oliver@144
   316
        self.acMail.triggered.connect(self.clicked_mail)
om@13
   317
        
oliver@136
   318
om@13
   319
def main():
om@13
   320
    
oliver@136
   321
    # parse arguments
oliver@136
   322
    parser = argparse.ArgumentParser(description = 'OpenSecurity Tray Icon.')
oliver@136
   323
    parser.add_argument('-p', '--port', type=int, default=8090, help='port number of the REST API this instance will listen on.')
oliver@136
   324
    args = parser.parse_args()
oliver@136
   325
oliver@136
   326
    # get up Qt
oliver@104
   327
    a = QtGui.QApplication(sys.argv)
om@13
   328
oliver@136
   329
    # enforce singelton process
oliver@136
   330
    if opensecurity_client_restful_server.is_already_running(args.port):
oliver@136
   331
        QtGui.QMessageBox.critical(None, 'OpenSecurity Error', 'OpenSecurity Tray Instance already launched.\nClose previous instance first.')
oliver@136
   332
        sys.exit(1)
oliver@136
   333
oliver@136
   334
    # start serving
oliver@136
   335
    opensecurity_client_restful_server.serve(args.port, True)
oliver@136
   336
oliver@136
   337
    # init tray icon widget
om@13
   338
    w = QtGui.QWidget()
oliver@104
   339
    icon = QtGui.QIcon()
oliver@104
   340
    icon.addPixmap(QtGui.QPixmap(QtCore.QString.fromUtf8(":/opensecurity/gfx/opensecurity_icon_64.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
oliver@104
   341
    trayIcon = OpenSecurityTrayIcon(icon)
oliver@193
   342
    opensecurity_client_restful_server.tray_icon = trayIcon
om@13
   343
oliver@136
   344
    # go!
om@13
   345
    trayIcon.show()
oliver@104
   346
    a.setQuitOnLastWindowClosed(False)
oliver@104
   347
    sys.exit(a.exec_())
om@13
   348
   
om@13
   349
om@13
   350
# start
om@13
   351
if __name__ == "__main__":
om@13
   352
    main()
om@13
   353