Addded http proxy server support.
1 # -*- coding: utf-8 -*-
3 # ------------------------------------------------------------
6 # an opensecurity dialog
8 # Autor: Oliver Maurhart, <oliver.maurhart@ait.ac.at>
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
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.
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.
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 # ------------------------------------------------------------
31 # ------------------------------------------------------------
45 from PyQt4 import QtCore
46 from PyQt4 import QtGui
49 import __init__ as opensecurity
51 if sys.platform == 'win32' or sys.platform == 'cygwin':
52 from cygwin import Cygwin
54 import opensecurity_client_restful_server
55 from ui import AboutDialog
56 from ui import ConfigureDialog
57 from ui import opensecurity_rc
60 # ------------------------------------------------------------
64 class OpenSecurityWait(QtGui.QDialog):
66 """OpenSecurity: please wait ..."""
68 def __init__(self, parent = None, flags = QtCore.Qt.WindowFlags(0)):
69 super(OpenSecurityWait, self).__init__(parent, flags)
70 self.setWindowTitle('OpenSecurity')
75 """Create the widgets."""
77 lyMain = QtGui.QVBoxLayout(self)
78 lyMain.setContentsMargins(8, 8, 8, 8)
80 # content area: left pixmap, right text
81 lbTitle = QtGui.QLabel('Creating secure subsystem. Please stand by ...')
82 lyMain.addWidget(lbTitle)
84 self.setMinimumSize(400, 50)
85 self.resize(lyMain.minimumSize())
88 class OpenSecurityTrayIcon(QtGui.QSystemTrayIcon):
90 """This is the OpenSecuirty Tray Icon"""
92 def __init__(self, icon, parent=None):
94 super(OpenSecurityTrayIcon, self).__init__(icon, parent)
96 self.activated.connect(self.activated_)
99 def activated_(self, reason):
101 """the system tray icon was activated"""
102 self.refresh_format_menu()
105 def clicked_about(self):
110 def getProxySettings(self):
111 aReg = _winreg.ConnectRegistry(None,_winreg.HKEY_CURRENT_USER)
112 aKey = _winreg.OpenKey(aReg, r"Software\Microsoft\Windows\CurrentVersion\Internet Settings")
113 subCount, valueCount, lastModified = _winreg.QueryInfoKey(aKey)
115 for i in range(valueCount):
117 n,v,t = _winreg.EnumValue(aKey,i)
119 except EnvironmentError:
121 _winreg.CloseKey(aKey)
123 if 'ProxyEnable' in reg_entries.keys() and reg_entries['ProxyEnable'] == 1:
124 proxy_search = re.search(r"(?<=http=)(?P<ProxyServer>.*?)(?=;)", reg_entries['ProxyServer'])
126 proxies = proxy_search.groupdict()
127 if 'ProxyServer' in proxies.keys(): # found http proxy
128 return {'ProxyServer': proxies['ProxyServer']}
129 return {'ProxyServer': reg_entries['ProxyServer']}
132 def clicked_browser(self):
133 """wish for safe internet browsing"""
135 if not (sys.platform == 'win32' or sys.platform == 'cygwin'):
136 QtGui.QMessageBox.critical(self.parent(), 'OpenSecurity Error', 'This action is not supported on this platform.\nSorry.')
140 # get a proper browsing VM
143 # TODO: HARDCODED ADDRESS OF OPENSECURITYD
144 proxy_support = urllib2.ProxyHandler({})
145 opener = urllib2.build_opener(proxy_support)
146 urllib2.install_opener(opener)
149 proxy = self.getProxySettings()
151 req_data = '?' + urllib.urlencode(proxy)
152 req = 'http://127.0.0.1:8080/browsing'+ req_data
153 browsing_vm = urllib2.urlopen(req).readline()
154 print('Called '+ req + ' got: ' + str(browsing_vm))
156 QtGui.QApplication.instance().processEvents()
157 QtGui.QMessageBox.critical(None, 'Failed to invoke Safe Internet Browsing', 'OpenSecurity Error')
159 QtGui.QApplication.instance().processEvents()
162 def clicked_configure(self):
163 """clicked configure"""
164 d = ConfigureDialog()
168 def clicked_exit(self):
170 opensecurity_client_restful_server.stop()
174 def clicked_launch_application(self):
175 """clicked the launch an application"""
176 dlg_launch_image = os.path.join(sys.path[0], 'ui', 'launch_dialog.py')
177 process_command = [sys.executable, dlg_launch_image]
178 process = subprocess.Popen(process_command, shell = False, stdout = subprocess.PIPE)
180 stdout = process.communicate()[0]
181 j = json.loads(stdout)
187 # get a proper browsing VM
190 # TODO: HARDCODED ADDRESS OF OPENSECURITYD
191 url = 'http://127.0.0.1:8080/sdvms/' + j['vm'] + '/application' + j['application']
192 result = urllib2.urlopen(url).readline()
198 def clicked_mail(self):
201 address = 'feedback@opensecurity.at'
202 subject = 'Feedback zu OpenSecurity V' + opensecurity.__version__
204 if sys.platform == 'linux2':
205 subprocess.Popen(['xdg-email', '--subject', subject, address])
206 elif sys.platform == 'win32' or sys.platform == 'cygwin':
207 mail_url = 'mailto:' + urllib.quote(address, '@') + '?' + urllib.quote('subject=' + subject)
208 subprocess.Popen(['cmd', '/C', 'start', mail_url])
211 def format_drive(self):
213 """format drive clicked (the sender should a QAction created with refresh_format_menu)"""
216 # fiddle the IP of the VM controlling the VM
218 ip = str(s.text().split('\\\\')[1])
220 # invoke the format drive dialog
221 dlg_format_drive = os.path.join(sys.path[0], 'ui', 'format_drive_dialog.py')
222 process_command = [sys.executable, dlg_format_drive, ip]
223 process = subprocess.Popen(process_command, shell = False, stdout = subprocess.PIPE)
225 stdout = process.communicate()[0]
231 def refresh_format_menu(self):
233 """create a new list of format 'drives'"""
234 self._menu_format.clear()
235 a = self._menu_format.addAction('<No Drive given>')
241 machines = json.load(urllib2.urlopen('http://127.0.0.1:8080/sdvms'))
242 if len(machines) == 0:
245 self._icon_network = QtGui.QIcon()
246 self._icon_network.addPixmap(QtGui.QPixmap(":/opensecurity/gfx/network-workgroup.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
248 self._menu_format.clear()
250 if u'SecurityDVM0' in m:
252 a = self._menu_format.addAction(m + '\\\\' + machines[m])
253 a.setIcon(self._icon_network)
254 a.triggered.connect(self.format_drive)
261 """create the user interface
262 As for the system tray this is 'just' the context menu.
265 # define the tray icon menu
266 menu = QtGui.QMenu(self.parent())
267 self.setContextMenu(menu)
270 self.acBrowser = QtGui.QAction('Secure Browsing', self.parent())
272 icon.addPixmap(QtGui.QPixmap(QtCore.QString.fromUtf8(':/opensecurity/gfx/opensecurity_browsing_64.png')), QtGui.QIcon.Normal, QtGui.QIcon.Off)
273 self.acBrowser.setIcon(icon)
274 menu.addAction(self.acBrowser)
277 # add standard menu items
278 self.acLaunch = QtGui.QAction('Launch Application', self.parent())
280 icon.addPixmap(QtGui.QPixmap(QtCore.QString.fromUtf8(':/opensecurity/gfx/opensecurity_launch_64.png')), QtGui.QIcon.Normal, QtGui.QIcon.Off)
281 self.acLaunch.setIcon(icon)
282 menu.addAction(self.acLaunch)
283 self.acConfigure = QtGui.QAction('Configuration', self.parent())
285 icon.addPixmap(QtGui.QPixmap(QtCore.QString.fromUtf8(':/opensecurity/gfx/opensecurity_configure_64.png')), QtGui.QIcon.Normal, QtGui.QIcon.Off)
286 self.acConfigure.setIcon(icon)
287 menu.addAction(self.acConfigure)
290 self._menu_format = menu.addMenu('Format')
293 self.acMail = menu.addAction('Send feedback mail')
295 icon.addPixmap(QtGui.QPixmap(QtCore.QString.fromUtf8(':/opensecurity/gfx/opensecurity_mail_64.png')), QtGui.QIcon.Normal, QtGui.QIcon.Off)
296 self.acMail.setIcon(icon)
297 self.acAbout = menu.addAction('About')
298 self.acExit = menu.addAction('Exit')
300 self.acBrowser.triggered.connect(self.clicked_browser)
301 self.acLaunch.triggered.connect(self.clicked_launch_application)
302 self.acConfigure.triggered.connect(self.clicked_configure)
303 self.acAbout.triggered.connect(self.clicked_about)
304 self.acExit.triggered.connect(self.clicked_exit)
305 self.acMail.triggered.connect(self.clicked_mail)
311 parser = argparse.ArgumentParser(description = 'OpenSecurity Tray Icon.')
312 parser.add_argument('-p', '--port', type=int, default=8090, help='port number of the REST API this instance will listen on.')
313 args = parser.parse_args()
316 a = QtGui.QApplication(sys.argv)
318 # enforce singelton process
319 if opensecurity_client_restful_server.is_already_running(args.port):
320 QtGui.QMessageBox.critical(None, 'OpenSecurity Error', 'OpenSecurity Tray Instance already launched.\nClose previous instance first.')
324 opensecurity_client_restful_server.serve(args.port, True)
326 # init tray icon widget
329 icon.addPixmap(QtGui.QPixmap(QtCore.QString.fromUtf8(":/opensecurity/gfx/opensecurity_icon_64.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
330 trayIcon = OpenSecurityTrayIcon(icon)
331 opensecurity_client_restful_server.tray_icon = trayIcon
335 a.setQuitOnLastWindowClosed(False)
340 if __name__ == "__main__":