Addded http proxy server support.
authorBarthaM@N3SIM1218.D03.arc.local
Fri, 18 Jul 2014 13:45:09 +0100
changeset 2132e0b94e12bfc
parent 212 59ebaa44c12c
child 214 2e2261ce334b
Addded http proxy server support.
OpenSecurity/bin/opensecurity_tray.pyw
OpenSecurity/bin/opensecurityd.pyw
OpenSecurity/bin/test_vmmanager.pyw
OpenSecurity/bin/ui/format_drive_dialog.py
OpenSecurity/bin/vmmanager.pyw
     1.1 --- a/OpenSecurity/bin/opensecurity_tray.pyw	Thu Jul 17 10:20:10 2014 +0100
     1.2 +++ b/OpenSecurity/bin/opensecurity_tray.pyw	Fri Jul 18 13:45:09 2014 +0100
     1.3 @@ -39,6 +39,8 @@
     1.4  import urllib
     1.5  import urllib2
     1.6  import webbrowser
     1.7 +import _winreg
     1.8 +import re
     1.9  
    1.10  from PyQt4 import QtCore
    1.11  from PyQt4 import QtGui
    1.12 @@ -105,6 +107,27 @@
    1.13          d = AboutDialog()
    1.14          d.exec_()
    1.15      
    1.16 +    def getProxySettings(self):        
    1.17 +        aReg = _winreg.ConnectRegistry(None,_winreg.HKEY_CURRENT_USER)
    1.18 +        aKey = _winreg.OpenKey(aReg, r"Software\Microsoft\Windows\CurrentVersion\Internet Settings")
    1.19 +        subCount, valueCount, lastModified = _winreg.QueryInfoKey(aKey)
    1.20 +        reg_entries = dict()
    1.21 +        for i in range(valueCount):                                           
    1.22 +            try:
    1.23 +                n,v,t = _winreg.EnumValue(aKey,i)
    1.24 +                reg_entries[n] = v
    1.25 +            except EnvironmentError:                                               
    1.26 +                break
    1.27 +        _winreg.CloseKey(aKey)
    1.28 +
    1.29 +        if 'ProxyEnable' in reg_entries.keys() and reg_entries['ProxyEnable'] == 1:
    1.30 +            proxy_search = re.search(r"(?<=http=)(?P<ProxyServer>.*?)(?=;)", reg_entries['ProxyServer'])
    1.31 +            if proxy_search:
    1.32 +                proxies = proxy_search.groupdict()
    1.33 +                if 'ProxyServer' in proxies.keys(): # found http proxy
    1.34 +                    return {'ProxyServer': proxies['ProxyServer']}  
    1.35 +            return {'ProxyServer': reg_entries['ProxyServer']}
    1.36 +        return None
    1.37  
    1.38      def clicked_browser(self):
    1.39          """wish for safe internet browsing"""
    1.40 @@ -114,16 +137,22 @@
    1.41              return
    1.42         
    1.43          try:
    1.44 -        
    1.45              # get a proper browsing VM
    1.46              Cygwin.start_X11()
    1.47  
    1.48              # TODO: HARDCODED ADDRESS OF OPENSECURITYD
    1.49 -            browsing_vm = urllib2.urlopen('http://127.0.0.1:8080/browsing').readline()
    1.50 -            print('Called http://127.0.0.1:8080/browsing got: ' + str(browsing_vm))
    1.51 -            
    1.52 +            proxy_support = urllib2.ProxyHandler({})
    1.53 +            opener = urllib2.build_opener(proxy_support)
    1.54 +            urllib2.install_opener(opener)
    1.55 +
    1.56 +            req_data = ""
    1.57 +            proxy = self.getProxySettings()
    1.58 +            if proxy:
    1.59 +                req_data = '?' + urllib.urlencode(proxy) 
    1.60 +            req = 'http://127.0.0.1:8080/browsing'+ req_data
    1.61 +            browsing_vm = urllib2.urlopen(req).readline()
    1.62 +            print('Called '+ req + ' got: ' + str(browsing_vm))
    1.63          except:
    1.64 -            
    1.65              QtGui.QApplication.instance().processEvents()
    1.66              QtGui.QMessageBox.critical(None, 'Failed to invoke Safe Internet Browsing', 'OpenSecurity Error')
    1.67              
    1.68 @@ -218,6 +247,8 @@
    1.69  
    1.70              self._menu_format.clear()
    1.71              for m in machines:
    1.72 +                if u'SecurityDVM0' in m:
    1.73 +                    continue
    1.74                  a = self._menu_format.addAction(m + '\\\\' + machines[m])
    1.75                  a.setIcon(self._icon_network)
    1.76                  a.triggered.connect(self.format_drive)
     2.1 --- a/OpenSecurity/bin/opensecurityd.pyw	Thu Jul 17 10:20:10 2014 +0100
     2.2 +++ b/OpenSecurity/bin/opensecurityd.pyw	Fri Jul 18 13:45:09 2014 +0100
     2.3 @@ -97,10 +97,14 @@
     2.4      """
     2.5      
     2.6      def GET(self):
     2.7 +        args = web.input()
     2.8          log_call(web.ctx.environ)
     2.9          global gvm_mgr
    2.10          try:
    2.11 -            result = gvm_mgr.handleBrowsingRequest()
    2.12 +            proxy = None
    2.13 +            if 'ProxyServer' in args:
    2.14 +                proxy = args['ProxyServer']
    2.15 +            result = gvm_mgr.handleBrowsingRequest(proxy)
    2.16              return result
    2.17          except:
    2.18              raise web.internalerror()
     3.1 --- a/OpenSecurity/bin/test_vmmanager.pyw	Thu Jul 17 10:20:10 2014 +0100
     3.2 +++ b/OpenSecurity/bin/test_vmmanager.pyw	Fri Jul 18 13:45:09 2014 +0100
     3.3 @@ -35,10 +35,12 @@
     3.4  import unittest
     3.5  
     3.6  import os
     3.7 +import re
     3.8  import os.path
     3.9  import sys
    3.10  import cygwin
    3.11  import vmmanager
    3.12 +import _winreg
    3.13  gvm_mgr = None
    3.14  
    3.15  class TestVMManager(unittest.TestCase):
    3.16 @@ -57,11 +59,43 @@
    3.17          template = vmmanager.VMManager.getTemplateUUID()
    3.18          self.assertIsNotNone(template,  "returned no UUID for template")
    3.19      
    3.20 -    #@unittest.skip("skipping (requires running vmmanager)")    
    3.21 +    @unittest.skip("skipping")    
    3.22      def testUpdateTemplate(self):
    3.23          gvm_mgr.updateTemplate()
    3.24          pass
    3.25      
    3.26 +    def setKey(self, key, name, value):
    3.27 +        _, reg_type = _winreg.QueryValueEx(key, name)
    3.28 +        _winreg.SetValueEx(key, name, 0, reg_type, value)
    3.29 +    
    3.30 +    @unittest.skip("skipping")    
    3.31 +    def testGetProxySettings(self):
    3.32 +        #sudo echo "http_proxy=http://80.122.169.38:8080/" >> /etc/environment
    3.33 +        aReg = _winreg.ConnectRegistry(None,_winreg.HKEY_CURRENT_USER)
    3.34 +        aKey = _winreg.OpenKey(aReg, r"Software\Microsoft\Windows\CurrentVersion\Internet Settings")
    3.35 +        subCount, valueCount, lastModified = _winreg.QueryInfoKey(aKey)
    3.36 +        proxy = dict()
    3.37 +        for i in range(valueCount):                                           
    3.38 +            try:
    3.39 +                n,v,t = _winreg.EnumValue(aKey,i)
    3.40 +                proxy[n] = v
    3.41 +            except EnvironmentError:                                               
    3.42 +                break
    3.43 +        _winreg.CloseKey(aKey)
    3.44 +        print proxy
    3.45 +        if 'ProxyEnable' in proxy.keys() and proxy['ProxyEnable'] == 1:
    3.46 +            print proxy['ProxyServer']
    3.47 +            return proxy['ProxyServer']
    3.48 +        else: 
    3.49 +            return ""
    3.50 +        
    3.51 +    def testMatchProxy(self):
    3.52 +        #http=212.17.86.109:8080;https=212.17.86.109:8080;ftp=212.17.86.109:8080
    3.53 +        #212.17.86.109:8080
    3.54 +        text = 'http=212.17.86.109:8080;https=212.17.86.109:8080;ftp=212.17.86.109:8080'
    3.55 +        print re.search(r"(?<=http=)(?P<HttpProxy>.*?)(?=;)", text).groupdict()
    3.56 +        print re.search(r"(?<=http=)(.*?)(?=;)", text)
    3.57 +        
    3.58      #@classmethod
    3.59      #def tearOffClass(self):
    3.60      #    gvm_mgr.stop()
     4.1 --- a/OpenSecurity/bin/ui/format_drive_dialog.py	Thu Jul 17 10:20:10 2014 +0100
     4.2 +++ b/OpenSecurity/bin/ui/format_drive_dialog.py	Fri Jul 18 13:45:09 2014 +0100
     4.3 @@ -35,13 +35,15 @@
     4.4  import base64
     4.5  import sys
     4.6  
     4.7 +import urllib
     4.8 +import urllib2
     4.9 +
    4.10  from PyQt4 import QtCore
    4.11  from PyQt4 import QtGui
    4.12  
    4.13  from ui_FormatDriveDialog import Ui_FormatDriveDialog 
    4.14  from about_dialog import AboutDialog
    4.15  
    4.16 -
    4.17  # ------------------------------------------------------------
    4.18  # code
    4.19  
    4.20 @@ -61,7 +63,7 @@
    4.21  <br/>
    4.22  <b>This is irreversible.</b><br/>
    4.23  <br/>
    4.24 -Please provide an approbitate password and keyfile to proceed:
    4.25 +Please provide an appropriate password or keyfile to proceed:
    4.26  """ % ip
    4.27  
    4.28          # setup the user interface
    4.29 @@ -104,10 +106,10 @@
    4.30          
    4.31          """Ok button has been clicked."""
    4.32  
    4.33 -        init_data = {}
    4.34 +        init_data = dict()
    4.35          
    4.36          # pick the password
    4.37 -        init_data['password'] = self.ui.edtPassword.text()
    4.38 +        init_data['password'] = str(self.ui.edtPassword.text())
    4.39          if len(init_data['password']) == 0:
    4.40              QtGui.QMessageBox.critical(self, 'Format error', 'Please specify a password.')
    4.41              return
    4.42 @@ -128,17 +130,19 @@
    4.43              keyfile_content_base64 = base64.b64encode(keyfile_content)
    4.44              init_data['keyfile'] = keyfile_content_base64
    4.45  
    4.46 +        res = ""
    4.47          try:
    4.48 -            req = urllib2.Request('http://' + ip + ':58081/init', urllib.urlencode(init_data))
    4.49 +            req_data = urllib.urlencode(init_data)
    4.50 +            req = urllib2.Request('http://' + ip + ':58081/init', req_data)
    4.51              res = urllib2.urlopen(req)
    4.52          except:
    4.53 -            print('EXCEPTION')
    4.54 +            print('EXCEPTION ' + res)
    4.55              pass
    4.56  
    4.57          self.accept()
    4.58  
    4.59  
    4.60 -    def set_user_text(user_text):
    4.61 +    def set_user_text(self, user_text):
    4.62  
    4.63          """Set a text to explain which password we need."""
    4.64          self.ui.lblText.setText(user_text)
     5.1 --- a/OpenSecurity/bin/vmmanager.pyw	Thu Jul 17 10:20:10 2014 +0100
     5.2 +++ b/OpenSecurity/bin/vmmanager.pyw	Fri Jul 18 13:45:09 2014 +0100
     5.3 @@ -674,9 +674,9 @@
     5.4          return network_drives
     5.5      
     5.6      # handles browsing request    
     5.7 -    def handleBrowsingRequest(self):
     5.8 +    def handleBrowsingRequest(self, proxy):
     5.9          showTrayMessage('Starting Secure Browsing...', 7000)
    5.10 -        handler = BrowsingHandler(self)
    5.11 +        handler = BrowsingHandler(self, proxy)
    5.12          handler.start()
    5.13          return 'ok'
    5.14      
    5.15 @@ -723,14 +723,21 @@
    5.16  #handles browsing session creation 
    5.17  class BrowsingHandler(threading.Thread):
    5.18      vmm = None
    5.19 -    def __init__(self, vmmanager):
    5.20 -         threading.Thread.__init__(self)
    5.21 -         self.vmm = vmmanager
    5.22 +    proxy = None
    5.23 +    def __init__(self, vmmanager, proxy):
    5.24 +        threading.Thread.__init__(self)
    5.25 +        self.vmm = vmmanager
    5.26 +        self.proxy = proxy
    5.27          
    5.28      def run(self):
    5.29          #browser = '\\\"/usr/bin/chromium; pidof dbus-launch | xargs kill\\\"'
    5.30 -        browser = '\\\"/usr/bin/chromium\\\"'
    5.31 +        #browser = '\\\"/usr/bin/chromium\\\"'
    5.32 +        
    5.33          try:
    5.34 +            if self.proxy:
    5.35 +                browser = '\\\"export http_proxy='+self.proxy+'; /usr/bin/chromium\\\"'
    5.36 +            else:
    5.37 +                browser = '\\\"/usr/bin/chromium\\\"'
    5.38              self.vmm.browsingManager.started.wait() 
    5.39              result = Cygwin.checkResult(Cygwin.sshExecuteX11(browser, self.vmm.browsingManager.ip_addr, 'osecuser', Cygwin.cygPath(self.vmm.getMachineFolder()) + '/' + self.vmm.browsingManager.vm_name + '/dvm_key'))
    5.40              self.vmm.backupFile('/home/osecuser/.config/chromium', self.vmm.browsingManager.appDataDir + '/OpenSecurity/')
    5.41 @@ -899,7 +906,7 @@
    5.42                      new_sdvm = self.vmm.newSDVM()
    5.43                      self.vmm.storageAttach(new_sdvm)
    5.44                      self.vmm.startVM(new_sdvm)
    5.45 -                    new_ip = self.vmm.waitStartup(new_sdvm)
    5.46 +                    new_ip = self.vmm.waitStartup(new_sdvm, timeout_ms=30000)
    5.47                      if new_ip == None:
    5.48                          logger.error("Error getting IP address of SDVM. Cleaning up.")
    5.49                          self.vmm.poweroffVM(new_sdvm)