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
|
oliver@104
|
53 |
from ui import AboutDialog
|
oliver@106
|
54 |
from ui import ConfigureDialog
|
oliver@104
|
55 |
from ui import opensecurity_rc
|
om@13
|
56 |
|
oliver@106
|
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()
|
om@13
|
94 |
|
om@13
|
95 |
|
om@13
|
96 |
def clicked_about(self):
|
om@13
|
97 |
"""clicked about"""
|
oliver@104
|
98 |
d = AboutDialog()
|
oliver@104
|
99 |
d.exec_()
|
om@13
|
100 |
|
om@13
|
101 |
|
om@37
|
102 |
def clicked_browser(self):
|
om@37
|
103 |
"""wish for safe internet browsing"""
|
om@42
|
104 |
|
oliver@104
|
105 |
if not (sys.platform == 'win32' or sys.platform == 'cygwin'):
|
oliver@104
|
106 |
QtGui.QMessageBox.critical(self.parent(), 'OpenSecurity Error', 'This action is not supported on this platform.\nSorry.')
|
oliver@104
|
107 |
return
|
oliver@193
|
108 |
|
om@42
|
109 |
try:
|
om@42
|
110 |
|
om@42
|
111 |
# get a proper browsing VM
|
mb@110
|
112 |
Cygwin.start_X11()
|
oliver@144
|
113 |
|
oliver@144
|
114 |
# TODO: HARDCODED ADDRESS OF OPENSECURITYD
|
om@42
|
115 |
browsing_vm = urllib2.urlopen('http://127.0.0.1:8080/browsing').readline()
|
oliver@165
|
116 |
print('Called http://127.0.0.1:8080/browsing got: ' + str(browsing_vm))
|
om@42
|
117 |
|
om@42
|
118 |
except:
|
oliver@165
|
119 |
|
om@42
|
120 |
QtGui.QApplication.instance().processEvents()
|
om@42
|
121 |
QtGui.QMessageBox.critical(None, 'Failed to invoke Safe Internet Browsing', 'OpenSecurity Error')
|
om@42
|
122 |
|
om@42
|
123 |
QtGui.QApplication.instance().processEvents()
|
om@37
|
124 |
|
om@37
|
125 |
|
oliver@106
|
126 |
def clicked_configure(self):
|
oliver@106
|
127 |
"""clicked configure"""
|
oliver@106
|
128 |
d = ConfigureDialog()
|
oliver@106
|
129 |
d.exec_()
|
oliver@106
|
130 |
|
oliver@106
|
131 |
|
om@13
|
132 |
def clicked_exit(self):
|
om@13
|
133 |
"""clicked exit"""
|
oliver@136
|
134 |
opensecurity_client_restful_server.stop()
|
om@13
|
135 |
sys.exit(0)
|
om@13
|
136 |
|
om@13
|
137 |
|
om@13
|
138 |
def clicked_launch_application(self):
|
om@13
|
139 |
"""clicked the launch an application"""
|
oliver@104
|
140 |
dlg_launch_image = os.path.join(sys.path[0], 'ui', 'launch_dialog.py')
|
om@13
|
141 |
process_command = [sys.executable, dlg_launch_image]
|
oliver@165
|
142 |
process = subprocess.Popen(process_command, shell = False, stdout = subprocess.PIPE)
|
oliver@165
|
143 |
try:
|
oliver@165
|
144 |
stdout = process.communicate()[0]
|
oliver@165
|
145 |
j = json.loads(stdout)
|
oliver@165
|
146 |
except:
|
oliver@165
|
147 |
return
|
oliver@165
|
148 |
|
oliver@165
|
149 |
try:
|
oliver@165
|
150 |
|
oliver@165
|
151 |
# get a proper browsing VM
|
oliver@165
|
152 |
Cygwin.start_X11()
|
oliver@165
|
153 |
|
oliver@165
|
154 |
# TODO: HARDCODED ADDRESS OF OPENSECURITYD
|
oliver@165
|
155 |
url = 'http://127.0.0.1:8080/sdvms/' + j['vm'] + '/application' + j['application']
|
oliver@165
|
156 |
result = urllib2.urlopen(url).readline()
|
oliver@165
|
157 |
|
oliver@165
|
158 |
except:
|
oliver@165
|
159 |
pass
|
om@13
|
160 |
|
om@13
|
161 |
|
oliver@144
|
162 |
def clicked_mail(self):
|
oliver@145
|
163 |
|
oliver@144
|
164 |
"""clicked mail"""
|
oliver@145
|
165 |
address = 'feedback@opensecurity.at'
|
oliver@145
|
166 |
subject = 'Feedback zu OpenSecurity V' + opensecurity.__version__
|
oliver@145
|
167 |
|
oliver@145
|
168 |
if sys.platform == 'linux2':
|
oliver@145
|
169 |
subprocess.Popen(['xdg-email', '--subject', subject, address])
|
oliver@145
|
170 |
elif sys.platform == 'win32' or sys.platform == 'cygwin':
|
oliver@145
|
171 |
mail_url = 'mailto:' + urllib.quote(address, '@') + '?' + urllib.quote('subject=' + subject)
|
oliver@148
|
172 |
subprocess.Popen(['cmd', '/C', 'start', mail_url])
|
oliver@144
|
173 |
|
oliver@144
|
174 |
|
om@13
|
175 |
def setup_ui(self):
|
om@13
|
176 |
"""create the user interface
|
om@13
|
177 |
As for the system tray this is 'just' the context menu.
|
om@13
|
178 |
"""
|
om@13
|
179 |
|
om@13
|
180 |
# define the tray icon menu
|
om@13
|
181 |
menu = QtGui.QMenu(self.parent())
|
om@13
|
182 |
self.setContextMenu(menu)
|
om@13
|
183 |
|
om@13
|
184 |
# add known apps
|
oliver@104
|
185 |
self.acBrowser = QtGui.QAction('Secure Browsing', self.parent())
|
oliver@104
|
186 |
icon = QtGui.QIcon()
|
oliver@106
|
187 |
icon.addPixmap(QtGui.QPixmap(QtCore.QString.fromUtf8(':/opensecurity/gfx/opensecurity_browsing_64.png')), QtGui.QIcon.Normal, QtGui.QIcon.Off)
|
oliver@104
|
188 |
self.acBrowser.setIcon(icon)
|
oliver@104
|
189 |
menu.addAction(self.acBrowser)
|
om@37
|
190 |
menu.addSeparator()
|
om@13
|
191 |
|
om@13
|
192 |
# add standard menu items
|
oliver@104
|
193 |
self.acLaunch = QtGui.QAction('Launch Application', self.parent())
|
oliver@104
|
194 |
icon = QtGui.QIcon()
|
oliver@106
|
195 |
icon.addPixmap(QtGui.QPixmap(QtCore.QString.fromUtf8(':/opensecurity/gfx/opensecurity_launch_64.png')), QtGui.QIcon.Normal, QtGui.QIcon.Off)
|
oliver@104
|
196 |
self.acLaunch.setIcon(icon)
|
oliver@104
|
197 |
menu.addAction(self.acLaunch)
|
oliver@106
|
198 |
self.acConfigure = QtGui.QAction('Configuration', self.parent())
|
oliver@106
|
199 |
icon = QtGui.QIcon()
|
oliver@106
|
200 |
icon.addPixmap(QtGui.QPixmap(QtCore.QString.fromUtf8(':/opensecurity/gfx/opensecurity_configure_64.png')), QtGui.QIcon.Normal, QtGui.QIcon.Off)
|
oliver@106
|
201 |
self.acConfigure.setIcon(icon)
|
oliver@106
|
202 |
menu.addAction(self.acConfigure)
|
om@13
|
203 |
menu.addSeparator()
|
oliver@104
|
204 |
|
oliver@144
|
205 |
self.acMail = menu.addAction('Send feedback mail')
|
oliver@144
|
206 |
icon = QtGui.QIcon()
|
oliver@144
|
207 |
icon.addPixmap(QtGui.QPixmap(QtCore.QString.fromUtf8(':/opensecurity/gfx/opensecurity_mail_64.png')), QtGui.QIcon.Normal, QtGui.QIcon.Off)
|
oliver@144
|
208 |
self.acMail.setIcon(icon)
|
oliver@104
|
209 |
self.acAbout = menu.addAction('About')
|
oliver@104
|
210 |
self.acExit = menu.addAction('Exit')
|
oliver@144
|
211 |
|
oliver@104
|
212 |
self.acBrowser.triggered.connect(self.clicked_browser)
|
oliver@104
|
213 |
self.acLaunch.triggered.connect(self.clicked_launch_application)
|
oliver@106
|
214 |
self.acConfigure.triggered.connect(self.clicked_configure)
|
oliver@104
|
215 |
self.acAbout.triggered.connect(self.clicked_about)
|
oliver@104
|
216 |
self.acExit.triggered.connect(self.clicked_exit)
|
oliver@144
|
217 |
self.acMail.triggered.connect(self.clicked_mail)
|
om@13
|
218 |
|
oliver@136
|
219 |
|
om@13
|
220 |
def main():
|
om@13
|
221 |
|
oliver@136
|
222 |
# parse arguments
|
oliver@136
|
223 |
parser = argparse.ArgumentParser(description = 'OpenSecurity Tray Icon.')
|
oliver@136
|
224 |
parser.add_argument('-p', '--port', type=int, default=8090, help='port number of the REST API this instance will listen on.')
|
oliver@136
|
225 |
args = parser.parse_args()
|
oliver@136
|
226 |
|
oliver@136
|
227 |
# get up Qt
|
oliver@104
|
228 |
a = QtGui.QApplication(sys.argv)
|
om@13
|
229 |
|
oliver@136
|
230 |
# enforce singelton process
|
oliver@136
|
231 |
if opensecurity_client_restful_server.is_already_running(args.port):
|
oliver@136
|
232 |
QtGui.QMessageBox.critical(None, 'OpenSecurity Error', 'OpenSecurity Tray Instance already launched.\nClose previous instance first.')
|
oliver@136
|
233 |
sys.exit(1)
|
oliver@136
|
234 |
|
oliver@136
|
235 |
# start serving
|
oliver@136
|
236 |
opensecurity_client_restful_server.serve(args.port, True)
|
oliver@136
|
237 |
|
oliver@136
|
238 |
# init tray icon widget
|
om@13
|
239 |
w = QtGui.QWidget()
|
oliver@104
|
240 |
icon = QtGui.QIcon()
|
oliver@104
|
241 |
icon.addPixmap(QtGui.QPixmap(QtCore.QString.fromUtf8(":/opensecurity/gfx/opensecurity_icon_64.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
|
oliver@104
|
242 |
trayIcon = OpenSecurityTrayIcon(icon)
|
oliver@193
|
243 |
opensecurity_client_restful_server.tray_icon = trayIcon
|
om@13
|
244 |
|
oliver@136
|
245 |
# go!
|
om@13
|
246 |
trayIcon.show()
|
oliver@104
|
247 |
a.setQuitOnLastWindowClosed(False)
|
oliver@104
|
248 |
sys.exit(a.exec_())
|
om@13
|
249 |
|
om@13
|
250 |
|
om@13
|
251 |
# start
|
om@13
|
252 |
if __name__ == "__main__":
|
om@13
|
253 |
main()
|
om@13
|
254 |
|