OpenSecurity/bin/opensecurity_tray.pyw
author BarthaM@N3SIM1218.D03.arc.local
Thu, 02 Oct 2014 13:08:09 +0100
changeset 234 216da9017f8f
parent 223 a4fb6694e6fe
child 238 d33edf5c2717
permissions -rwxr-xr-x
- changed opensecurity to always hold at least 2 SDVM sessions to be ready when needed
- added automatic wpad proxy detection
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
om@13
    42
om@13
    43
from PyQt4 import QtCore
om@13
    44
from PyQt4 import QtGui
om@13
    45
om@13
    46
# local
oliver@144
    47
import __init__ as opensecurity
oliver@144
    48
oliver@104
    49
if sys.platform == 'win32' or sys.platform == 'cygwin':
oliver@104
    50
    from cygwin import Cygwin
oliver@104
    51
oliver@136
    52
import opensecurity_client_restful_server 
BarthaM@234
    53
import proxy_getter
oliver@104
    54
from ui import AboutDialog
oliver@106
    55
from ui import ConfigureDialog
oliver@104
    56
from ui import opensecurity_rc
om@13
    57
om@13
    58
# ------------------------------------------------------------
om@13
    59
# code
om@13
    60
om@13
    61
om@42
    62
class OpenSecurityWait(QtGui.QDialog):
om@42
    63
om@42
    64
    """OpenSecurity: please wait ..."""
om@42
    65
    
om@42
    66
    def __init__(self, parent = None, flags = QtCore.Qt.WindowFlags(0)):
om@42
    67
        super(OpenSecurityWait, self).__init__(parent, flags)
om@42
    68
        self.setWindowTitle('OpenSecurity')
om@42
    69
        self.setup_ui()
om@42
    70
        
om@42
    71
        
om@42
    72
    def setup_ui(self):
om@42
    73
        """Create the widgets."""
om@42
    74
        
om@42
    75
        lyMain = QtGui.QVBoxLayout(self)
om@42
    76
        lyMain.setContentsMargins(8, 8, 8, 8)
om@42
    77
        
om@42
    78
        # content area: left pixmap, right text
om@42
    79
        lbTitle = QtGui.QLabel('Creating secure subsystem. Please stand by ...')
om@42
    80
        lyMain.addWidget(lbTitle)
om@42
    81
        
om@42
    82
        self.setMinimumSize(400, 50)
om@42
    83
        self.resize(lyMain.minimumSize())
om@42
    84
om@42
    85
om@13
    86
class OpenSecurityTrayIcon(QtGui.QSystemTrayIcon):
om@13
    87
    
om@13
    88
    """This is the OpenSecuirty Tray Icon"""
om@13
    89
om@13
    90
    def __init__(self, icon, parent=None):
om@13
    91
        
om@13
    92
        super(OpenSecurityTrayIcon, self).__init__(icon, parent)
om@13
    93
        self.setup_ui()
oliver@207
    94
        self.activated.connect(self.activated_)
oliver@207
    95
       
oliver@207
    96
oliver@207
    97
    def activated_(self, reason):
oliver@207
    98
oliver@207
    99
        """the system tray icon was activated"""
oliver@207
   100
        self.refresh_format_menu()
oliver@207
   101
om@13
   102
        
om@13
   103
    def clicked_about(self):
om@13
   104
        """clicked about"""
oliver@104
   105
        d = AboutDialog()
oliver@104
   106
        d.exec_()
om@13
   107
om@37
   108
    def clicked_browser(self):
om@37
   109
        """wish for safe internet browsing"""
om@42
   110
        
oliver@104
   111
        if not (sys.platform == 'win32' or sys.platform == 'cygwin'):
oliver@104
   112
            QtGui.QMessageBox.critical(self.parent(), 'OpenSecurity Error', 'This action is not supported on this platform.\nSorry.')
oliver@104
   113
            return
oliver@193
   114
       
om@42
   115
        try:
om@42
   116
            # get a proper browsing VM
mb@110
   117
            Cygwin.start_X11()
oliver@144
   118
oliver@144
   119
            # TODO: HARDCODED ADDRESS OF OPENSECURITYD
BarthaM@213
   120
            proxy_support = urllib2.ProxyHandler({})
BarthaM@213
   121
            opener = urllib2.build_opener(proxy_support)
BarthaM@213
   122
            urllib2.install_opener(opener)
BarthaM@213
   123
BarthaM@213
   124
            req_data = ""
BarthaM@234
   125
            proxy = proxy_getter.getProxySettings()
BarthaM@213
   126
            if proxy:
BarthaM@213
   127
                req_data = '?' + urllib.urlencode(proxy) 
BarthaM@213
   128
            req = 'http://127.0.0.1:8080/browsing'+ req_data
BarthaM@213
   129
            browsing_vm = urllib2.urlopen(req).readline()
BarthaM@213
   130
            print('Called '+ req + ' got: ' + str(browsing_vm))
om@42
   131
        except:
om@42
   132
            QtGui.QApplication.instance().processEvents()
om@42
   133
            QtGui.QMessageBox.critical(None, 'Failed to invoke Safe Internet Browsing', 'OpenSecurity Error')
om@42
   134
            
om@42
   135
        QtGui.QApplication.instance().processEvents()
om@37
   136
            
om@37
   137
            
oliver@106
   138
    def clicked_configure(self):
oliver@106
   139
        """clicked configure"""
oliver@106
   140
        d = ConfigureDialog()
oliver@106
   141
        d.exec_()
oliver@106
   142
    
oliver@106
   143
om@13
   144
    def clicked_exit(self):
om@13
   145
        """clicked exit"""
oliver@136
   146
        opensecurity_client_restful_server.stop()
om@13
   147
        sys.exit(0)
om@13
   148
    
om@13
   149
om@13
   150
    def clicked_launch_application(self):
om@13
   151
        """clicked the launch an application"""
oliver@104
   152
        dlg_launch_image = os.path.join(sys.path[0], 'ui', 'launch_dialog.py')
om@13
   153
        process_command = [sys.executable, dlg_launch_image]
oliver@165
   154
        process = subprocess.Popen(process_command, shell = False, stdout = subprocess.PIPE)        
oliver@165
   155
        try:
oliver@165
   156
            stdout = process.communicate()[0]
oliver@165
   157
            j = json.loads(stdout)
oliver@165
   158
        except:
oliver@165
   159
            return
oliver@165
   160
oliver@165
   161
        try:
oliver@165
   162
            # get a proper browsing VM
oliver@165
   163
            Cygwin.start_X11()
oliver@165
   164
            # TODO: HARDCODED ADDRESS OF OPENSECURITYD
oliver@165
   165
            url = 'http://127.0.0.1:8080/sdvms/' + j['vm'] + '/application' + j['application']
oliver@165
   166
            result = urllib2.urlopen(url).readline()
oliver@165
   167
        except:
oliver@165
   168
            pass 
om@13
   169
            
om@13
   170
            
oliver@144
   171
    def clicked_mail(self):
oliver@145
   172
        
oliver@144
   173
        """clicked mail"""
oliver@145
   174
        address = 'feedback@opensecurity.at'
oliver@145
   175
        subject = 'Feedback zu OpenSecurity V' + opensecurity.__version__
oliver@145
   176
oliver@145
   177
        if sys.platform == 'linux2':
oliver@145
   178
            subprocess.Popen(['xdg-email', '--subject', subject, address])
oliver@145
   179
        elif sys.platform == 'win32' or sys.platform == 'cygwin':
oliver@145
   180
            mail_url = 'mailto:' + urllib.quote(address, '@') + '?' + urllib.quote('subject=' + subject)
oliver@148
   181
            subprocess.Popen(['cmd', '/C', 'start', mail_url])
oliver@207
   182
   
oliver@207
   183
oliver@207
   184
    def format_drive(self):
oliver@207
   185
oliver@207
   186
        """format drive clicked (the sender should a QAction created with refresh_format_menu)"""
oliver@207
   187
        try:
oliver@207
   188
oliver@207
   189
            # fiddle the IP of the VM controlling the VM
oliver@207
   190
            s = self.sender()
oliver@207
   191
            ip = str(s.text().split('\\\\')[1])
oliver@207
   192
oliver@207
   193
            # invoke the format drive dialog
oliver@207
   194
            dlg_format_drive = os.path.join(sys.path[0], 'ui', 'format_drive_dialog.py')
oliver@207
   195
            process_command = [sys.executable, dlg_format_drive, ip]
oliver@207
   196
            process = subprocess.Popen(process_command, shell = False, stdout = subprocess.PIPE)        
oliver@207
   197
oliver@207
   198
            stdout = process.communicate()[0]
oliver@207
   199
oliver@207
   200
        except:
oliver@207
   201
            pass
oliver@207
   202
oliver@207
   203
oliver@207
   204
    def refresh_format_menu(self):
oliver@207
   205
oliver@207
   206
        """create a new list of format 'drives'"""
oliver@207
   207
        self._menu_format.clear()
oliver@207
   208
        a = self._menu_format.addAction('<No Drive given>')
oliver@207
   209
        a.setEnabled(False)
oliver@207
   210
oliver@207
   211
        try:
oliver@207
   212
oliver@207
   213
            # get machines
oliver@207
   214
            machines = json.load(urllib2.urlopen('http://127.0.0.1:8080/sdvms'))
oliver@207
   215
            if len(machines) == 0:
oliver@207
   216
                return
oliver@207
   217
oliver@207
   218
            self._icon_network = QtGui.QIcon()
oliver@207
   219
            self._icon_network.addPixmap(QtGui.QPixmap(":/opensecurity/gfx/network-workgroup.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
oliver@207
   220
oliver@215
   221
            cleared = False
oliver@207
   222
            for m in machines:
oliver@215
   223
oliver@215
   224
                # do not insert Browsing VM
BarthaM@213
   225
                if u'SecurityDVM0' in m:
BarthaM@213
   226
                    continue
oliver@215
   227
oliver@215
   228
                if not cleared:
oliver@215
   229
                    self._menu_format.clear()
oliver@215
   230
                    cleared = True
oliver@215
   231
oliver@207
   232
                a = self._menu_format.addAction(m + '\\\\' + machines[m])
oliver@207
   233
                a.setIcon(self._icon_network)
oliver@207
   234
                a.triggered.connect(self.format_drive)
oliver@207
   235
oliver@207
   236
        except:
oliver@207
   237
            pass
oliver@207
   238
oliver@144
   239
om@13
   240
    def setup_ui(self):
om@13
   241
        """create the user interface
om@13
   242
        As for the system tray this is 'just' the context menu.
om@13
   243
        """
om@13
   244
    
om@13
   245
        # define the tray icon menu
om@13
   246
        menu = QtGui.QMenu(self.parent())
om@13
   247
        self.setContextMenu(menu)
om@13
   248
        
om@13
   249
        # add known apps
oliver@104
   250
        self.acBrowser = QtGui.QAction('Secure Browsing', self.parent())
oliver@104
   251
        icon = QtGui.QIcon()
oliver@106
   252
        icon.addPixmap(QtGui.QPixmap(QtCore.QString.fromUtf8(':/opensecurity/gfx/opensecurity_browsing_64.png')), QtGui.QIcon.Normal, QtGui.QIcon.Off)
oliver@104
   253
        self.acBrowser.setIcon(icon)
oliver@104
   254
        menu.addAction(self.acBrowser)
om@37
   255
        menu.addSeparator()
om@13
   256
        
om@13
   257
        # add standard menu items
oliver@104
   258
        self.acLaunch = QtGui.QAction('Launch Application', self.parent())
oliver@104
   259
        icon = QtGui.QIcon()
oliver@106
   260
        icon.addPixmap(QtGui.QPixmap(QtCore.QString.fromUtf8(':/opensecurity/gfx/opensecurity_launch_64.png')), QtGui.QIcon.Normal, QtGui.QIcon.Off)
oliver@104
   261
        self.acLaunch.setIcon(icon)
oliver@104
   262
        menu.addAction(self.acLaunch)
oliver@106
   263
        self.acConfigure = QtGui.QAction('Configuration', self.parent())
oliver@106
   264
        icon = QtGui.QIcon()
oliver@106
   265
        icon.addPixmap(QtGui.QPixmap(QtCore.QString.fromUtf8(':/opensecurity/gfx/opensecurity_configure_64.png')), QtGui.QIcon.Normal, QtGui.QIcon.Off)
oliver@106
   266
        self.acConfigure.setIcon(icon)
oliver@106
   267
        menu.addAction(self.acConfigure)
om@13
   268
        menu.addSeparator()
oliver@104
   269
oliver@207
   270
        self._menu_format = menu.addMenu('Format')
oliver@207
   271
        menu.addSeparator()
oliver@207
   272
oliver@144
   273
        self.acMail = menu.addAction('Send feedback mail')
oliver@144
   274
        icon = QtGui.QIcon()
oliver@144
   275
        icon.addPixmap(QtGui.QPixmap(QtCore.QString.fromUtf8(':/opensecurity/gfx/opensecurity_mail_64.png')), QtGui.QIcon.Normal, QtGui.QIcon.Off)
oliver@144
   276
        self.acMail.setIcon(icon)
oliver@104
   277
        self.acAbout = menu.addAction('About')
oliver@215
   278
        
oliver@215
   279
        # diabled as of TICKET #11
oliver@215
   280
        # self.acExit = menu.addAction('Exit')
oliver@144
   281
       
oliver@104
   282
        self.acBrowser.triggered.connect(self.clicked_browser)
oliver@104
   283
        self.acLaunch.triggered.connect(self.clicked_launch_application)
oliver@106
   284
        self.acConfigure.triggered.connect(self.clicked_configure)
oliver@104
   285
        self.acAbout.triggered.connect(self.clicked_about)
oliver@215
   286
oliver@215
   287
        # disabled as for TICKET #11
oliver@215
   288
        # self.acExit.triggered.connect(self.clicked_exit)
oliver@144
   289
        self.acMail.triggered.connect(self.clicked_mail)
om@13
   290
        
oliver@136
   291
om@13
   292
def main():
om@13
   293
    
oliver@136
   294
    # parse arguments
oliver@136
   295
    parser = argparse.ArgumentParser(description = 'OpenSecurity Tray Icon.')
oliver@136
   296
    parser.add_argument('-p', '--port', type=int, default=8090, help='port number of the REST API this instance will listen on.')
oliver@136
   297
    args = parser.parse_args()
oliver@136
   298
oliver@136
   299
    # get up Qt
oliver@104
   300
    a = QtGui.QApplication(sys.argv)
om@13
   301
oliver@136
   302
    # enforce singelton process
oliver@136
   303
    if opensecurity_client_restful_server.is_already_running(args.port):
oliver@136
   304
        QtGui.QMessageBox.critical(None, 'OpenSecurity Error', 'OpenSecurity Tray Instance already launched.\nClose previous instance first.')
oliver@136
   305
        sys.exit(1)
oliver@136
   306
oliver@136
   307
    # start serving
oliver@136
   308
    opensecurity_client_restful_server.serve(args.port, True)
oliver@136
   309
oliver@136
   310
    # init tray icon widget
om@13
   311
    w = QtGui.QWidget()
oliver@104
   312
    icon = QtGui.QIcon()
oliver@104
   313
    icon.addPixmap(QtGui.QPixmap(QtCore.QString.fromUtf8(":/opensecurity/gfx/opensecurity_icon_64.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
oliver@104
   314
    trayIcon = OpenSecurityTrayIcon(icon)
oliver@193
   315
    opensecurity_client_restful_server.tray_icon = trayIcon
om@13
   316
oliver@136
   317
    # go!
om@13
   318
    trayIcon.show()
oliver@104
   319
    a.setQuitOnLastWindowClosed(False)
oliver@104
   320
    sys.exit(a.exec_())
om@13
   321
   
om@13
   322
om@13
   323
# start
om@13
   324
if __name__ == "__main__":
om@13
   325
    main()
om@13
   326