om@13: # -*- coding: utf-8 -*- om@13: om@13: # ------------------------------------------------------------ om@13: # opensecurity-dialog om@13: # om@13: # an opensecurity dialog om@13: # om@13: # Autor: Oliver Maurhart, om@13: # oliver@240: # Copyright 2013-2014 X-Net and AIT Austrian Institute of Technology om@13: # om@13: # oliver@240: # X-Net Services GmbH oliver@240: # Elisabethstrasse 1 oliver@240: # 4020 Linz oliver@240: # AUSTRIA oliver@240: # https://www.x-net.at oliver@240: # oliver@240: # AIT Austrian Institute of Technology oliver@240: # Donau City Strasse 1 oliver@240: # 1220 Wien oliver@240: # AUSTRIA oliver@240: # http://www.ait.ac.at oliver@240: # oliver@240: # oliver@240: # Licensed under the Apache License, Version 2.0 (the "License"); oliver@240: # you may not use this file except in compliance with the License. oliver@240: # You may obtain a copy of the License at oliver@240: # oliver@240: # http://www.apache.org/licenses/LICENSE-2.0 oliver@240: # oliver@240: # Unless required by applicable law or agreed to in writing, software oliver@240: # distributed under the License is distributed on an "AS IS" BASIS, oliver@240: # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. oliver@240: # See the License for the specific language governing permissions and oliver@240: # limitations under the License. om@13: # ------------------------------------------------------------ om@13: om@13: om@13: # ------------------------------------------------------------ om@13: # imports om@13: om@13: import argparse oliver@165: import json om@13: import os om@13: import subprocess om@13: import sys oliver@145: import urllib om@42: import urllib2 oliver@145: import webbrowser om@13: om@13: from PyQt4 import QtCore om@13: from PyQt4 import QtGui om@13: om@13: # local oliver@144: import __init__ as opensecurity oliver@144: oliver@104: if sys.platform == 'win32' or sys.platform == 'cygwin': oliver@104: from cygwin import Cygwin oliver@104: oliver@136: import opensecurity_client_restful_server BarthaM@234: import proxy_getter oliver@104: from ui import AboutDialog oliver@106: from ui import ConfigureDialog oliver@104: from ui import opensecurity_rc om@13: om@13: # ------------------------------------------------------------ om@13: # code om@13: om@13: om@42: class OpenSecurityWait(QtGui.QDialog): om@42: om@42: """OpenSecurity: please wait ...""" om@42: om@42: def __init__(self, parent = None, flags = QtCore.Qt.WindowFlags(0)): om@42: super(OpenSecurityWait, self).__init__(parent, flags) om@42: self.setWindowTitle('OpenSecurity') om@42: self.setup_ui() om@42: om@42: om@42: def setup_ui(self): om@42: """Create the widgets.""" om@42: om@42: lyMain = QtGui.QVBoxLayout(self) om@42: lyMain.setContentsMargins(8, 8, 8, 8) om@42: om@42: # content area: left pixmap, right text om@42: lbTitle = QtGui.QLabel('Creating secure subsystem. Please stand by ...') om@42: lyMain.addWidget(lbTitle) om@42: om@42: self.setMinimumSize(400, 50) om@42: self.resize(lyMain.minimumSize()) om@42: om@42: om@13: class OpenSecurityTrayIcon(QtGui.QSystemTrayIcon): om@13: om@13: """This is the OpenSecuirty Tray Icon""" om@13: om@13: def __init__(self, icon, parent=None): om@13: om@13: super(OpenSecurityTrayIcon, self).__init__(icon, parent) om@13: self.setup_ui() oliver@207: self.activated.connect(self.activated_) oliver@207: oliver@207: oliver@207: def activated_(self, reason): oliver@207: oliver@207: """the system tray icon was activated""" oliver@207: self.refresh_format_menu() oliver@207: om@13: om@13: def clicked_about(self): om@13: """clicked about""" oliver@104: d = AboutDialog() oliver@104: d.exec_() om@13: om@37: def clicked_browser(self): om@37: """wish for safe internet browsing""" om@42: oliver@104: if not (sys.platform == 'win32' or sys.platform == 'cygwin'): oliver@104: QtGui.QMessageBox.critical(self.parent(), 'OpenSecurity Error', 'This action is not supported on this platform.\nSorry.') oliver@104: return oliver@193: om@42: try: om@42: # get a proper browsing VM mb@110: Cygwin.start_X11() oliver@144: oliver@144: # TODO: HARDCODED ADDRESS OF OPENSECURITYD BarthaM@213: proxy_support = urllib2.ProxyHandler({}) BarthaM@213: opener = urllib2.build_opener(proxy_support) BarthaM@213: urllib2.install_opener(opener) BarthaM@213: BarthaM@213: req_data = "" BarthaM@234: proxy = proxy_getter.getProxySettings() BarthaM@213: if proxy: BarthaM@213: req_data = '?' + urllib.urlencode(proxy) BarthaM@213: req = 'http://127.0.0.1:8080/browsing'+ req_data BarthaM@213: browsing_vm = urllib2.urlopen(req).readline() BarthaM@213: print('Called '+ req + ' got: ' + str(browsing_vm)) om@42: except: om@42: QtGui.QApplication.instance().processEvents() om@42: QtGui.QMessageBox.critical(None, 'Failed to invoke Safe Internet Browsing', 'OpenSecurity Error') om@42: om@42: QtGui.QApplication.instance().processEvents() om@37: om@37: oliver@106: def clicked_configure(self): oliver@106: """clicked configure""" oliver@106: d = ConfigureDialog() oliver@106: d.exec_() oliver@106: oliver@106: om@13: def clicked_exit(self): om@13: """clicked exit""" mihai@238: #opensecurity_client_restful_server.stop() om@13: sys.exit(0) om@13: om@13: om@13: def clicked_launch_application(self): om@13: """clicked the launch an application""" oliver@104: dlg_launch_image = os.path.join(sys.path[0], 'ui', 'launch_dialog.py') om@13: process_command = [sys.executable, dlg_launch_image] oliver@165: process = subprocess.Popen(process_command, shell = False, stdout = subprocess.PIPE) oliver@165: try: oliver@165: stdout = process.communicate()[0] oliver@165: j = json.loads(stdout) oliver@165: except: oliver@165: return oliver@165: oliver@165: try: oliver@165: # get a proper browsing VM oliver@165: Cygwin.start_X11() oliver@165: # TODO: HARDCODED ADDRESS OF OPENSECURITYD oliver@165: url = 'http://127.0.0.1:8080/sdvms/' + j['vm'] + '/application' + j['application'] oliver@165: result = urllib2.urlopen(url).readline() oliver@165: except: oliver@165: pass om@13: om@13: oliver@144: def clicked_mail(self): oliver@145: oliver@144: """clicked mail""" oliver@145: address = 'feedback@opensecurity.at' oliver@145: subject = 'Feedback zu OpenSecurity V' + opensecurity.__version__ oliver@145: oliver@145: if sys.platform == 'linux2': oliver@145: subprocess.Popen(['xdg-email', '--subject', subject, address]) oliver@145: elif sys.platform == 'win32' or sys.platform == 'cygwin': oliver@145: mail_url = 'mailto:' + urllib.quote(address, '@') + '?' + urllib.quote('subject=' + subject) oliver@148: subprocess.Popen(['cmd', '/C', 'start', mail_url]) oliver@207: oliver@207: oliver@207: def format_drive(self): oliver@207: oliver@207: """format drive clicked (the sender should a QAction created with refresh_format_menu)""" oliver@207: try: oliver@207: oliver@207: # fiddle the IP of the VM controlling the VM oliver@207: s = self.sender() oliver@207: ip = str(s.text().split('\\\\')[1]) oliver@207: oliver@207: # invoke the format drive dialog oliver@207: dlg_format_drive = os.path.join(sys.path[0], 'ui', 'format_drive_dialog.py') oliver@207: process_command = [sys.executable, dlg_format_drive, ip] oliver@207: process = subprocess.Popen(process_command, shell = False, stdout = subprocess.PIPE) oliver@207: oliver@207: stdout = process.communicate()[0] oliver@207: oliver@207: except: oliver@207: pass oliver@207: oliver@207: oliver@207: def refresh_format_menu(self): oliver@207: oliver@207: """create a new list of format 'drives'""" oliver@207: self._menu_format.clear() oliver@207: a = self._menu_format.addAction('') oliver@207: a.setEnabled(False) oliver@207: oliver@207: try: oliver@207: oliver@207: # get machines oliver@207: machines = json.load(urllib2.urlopen('http://127.0.0.1:8080/sdvms')) oliver@207: if len(machines) == 0: oliver@207: return oliver@207: oliver@207: self._icon_network = QtGui.QIcon() oliver@207: self._icon_network.addPixmap(QtGui.QPixmap(":/opensecurity/gfx/network-workgroup.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off) oliver@207: mihai@244: self._menu_format.clear() mihai@244: oliver@207: for m in machines: oliver@215: mihai@244: properties = json.load(urllib2.urlopen('http://127.0.0.1:8080/sdvms/' + m)) mihai@244: if 'USBAttachedUUID1' in properties and properties['USBAttachedUUID1'] != None: mihai@244: a = self._menu_format.addAction(m + '\\\\' + machines[m]) mihai@244: a.setIcon(self._icon_network) mihai@244: a.triggered.connect(self.format_drive) oliver@207: oliver@207: except: oliver@207: pass oliver@207: oliver@144: om@13: def setup_ui(self): om@13: """create the user interface om@13: As for the system tray this is 'just' the context menu. om@13: """ om@13: om@13: # define the tray icon menu om@13: menu = QtGui.QMenu(self.parent()) om@13: self.setContextMenu(menu) om@13: om@13: # add known apps oliver@104: self.acBrowser = QtGui.QAction('Secure Browsing', self.parent()) oliver@104: icon = QtGui.QIcon() oliver@106: icon.addPixmap(QtGui.QPixmap(QtCore.QString.fromUtf8(':/opensecurity/gfx/opensecurity_browsing_64.png')), QtGui.QIcon.Normal, QtGui.QIcon.Off) oliver@104: self.acBrowser.setIcon(icon) oliver@104: menu.addAction(self.acBrowser) om@37: menu.addSeparator() om@13: om@13: # add standard menu items oliver@104: self.acLaunch = QtGui.QAction('Launch Application', self.parent()) oliver@104: icon = QtGui.QIcon() oliver@106: icon.addPixmap(QtGui.QPixmap(QtCore.QString.fromUtf8(':/opensecurity/gfx/opensecurity_launch_64.png')), QtGui.QIcon.Normal, QtGui.QIcon.Off) oliver@104: self.acLaunch.setIcon(icon) oliver@104: menu.addAction(self.acLaunch) oliver@106: self.acConfigure = QtGui.QAction('Configuration', self.parent()) oliver@106: icon = QtGui.QIcon() oliver@106: icon.addPixmap(QtGui.QPixmap(QtCore.QString.fromUtf8(':/opensecurity/gfx/opensecurity_configure_64.png')), QtGui.QIcon.Normal, QtGui.QIcon.Off) oliver@106: self.acConfigure.setIcon(icon) oliver@106: menu.addAction(self.acConfigure) om@13: menu.addSeparator() oliver@104: oliver@207: self._menu_format = menu.addMenu('Format') oliver@207: menu.addSeparator() oliver@207: oliver@144: self.acMail = menu.addAction('Send feedback mail') oliver@144: icon = QtGui.QIcon() oliver@144: icon.addPixmap(QtGui.QPixmap(QtCore.QString.fromUtf8(':/opensecurity/gfx/opensecurity_mail_64.png')), QtGui.QIcon.Normal, QtGui.QIcon.Off) oliver@144: self.acMail.setIcon(icon) oliver@104: self.acAbout = menu.addAction('About') oliver@215: oliver@215: # diabled as of TICKET #11 oliver@215: # self.acExit = menu.addAction('Exit') oliver@144: oliver@104: self.acBrowser.triggered.connect(self.clicked_browser) oliver@104: self.acLaunch.triggered.connect(self.clicked_launch_application) oliver@106: self.acConfigure.triggered.connect(self.clicked_configure) oliver@104: self.acAbout.triggered.connect(self.clicked_about) oliver@215: oliver@215: # disabled as for TICKET #11 oliver@215: # self.acExit.triggered.connect(self.clicked_exit) oliver@144: self.acMail.triggered.connect(self.clicked_mail) om@13: oliver@136: om@13: def main(): om@13: oliver@136: # parse arguments oliver@136: parser = argparse.ArgumentParser(description = 'OpenSecurity Tray Icon.') oliver@136: parser.add_argument('-p', '--port', type=int, default=8090, help='port number of the REST API this instance will listen on.') oliver@136: args = parser.parse_args() oliver@136: oliver@136: # get up Qt oliver@104: a = QtGui.QApplication(sys.argv) om@13: oliver@136: # enforce singelton process oliver@136: if opensecurity_client_restful_server.is_already_running(args.port): oliver@136: QtGui.QMessageBox.critical(None, 'OpenSecurity Error', 'OpenSecurity Tray Instance already launched.\nClose previous instance first.') oliver@136: sys.exit(1) oliver@136: oliver@136: # start serving oliver@136: opensecurity_client_restful_server.serve(args.port, True) oliver@136: oliver@136: # init tray icon widget om@13: w = QtGui.QWidget() oliver@104: icon = QtGui.QIcon() oliver@104: icon.addPixmap(QtGui.QPixmap(QtCore.QString.fromUtf8(":/opensecurity/gfx/opensecurity_icon_64.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off) oliver@104: trayIcon = OpenSecurityTrayIcon(icon) oliver@193: opensecurity_client_restful_server.tray_icon = trayIcon om@13: oliver@136: # go! om@13: trayIcon.show() oliver@104: a.setQuitOnLastWindowClosed(False) oliver@104: sys.exit(a.exec_()) om@13: om@13: om@13: # start om@13: if __name__ == "__main__": om@13: main() om@13: