OpenSecurity/bin/proxy_getter.py
author BarthaM@N3SIM1218.D03.arc.local
Thu, 02 Oct 2014 13:08:09 +0100
changeset 234 216da9017f8f
child 240 d7ef04254e9c
permissions -rw-r--r--
- changed opensecurity to always hold at least 2 SDVM sessions to be ready when needed
- added automatic wpad proxy detection
     1 import socket
     2 import os
     3 import httplib
     4 import sys
     5 import _winreg
     6 import re
     7 
     8 DNS_WPAD_FILENAME = "wpad.dat"
     9 
    10 def check_for_wpad_file(server, path):
    11 
    12     wpad_url = "http://%s/%s"%(server, path)
    13     print "checking", wpad_url
    14     try:
    15         conn = httplib.HTTPConnection(server)
    16         conn.request("HEAD", "/%s"%path)
    17         r = conn.getresponse()
    18         if r.status == 200:
    19             return wpad_url
    20     except Exception, e:
    21         return None
    22 
    23     return None
    24 
    25 def get_wpad_server_searchlist():
    26     #get fully-qualified hostname
    27     fqhn = socket.getfqdn().split(" ")[0]
    28     
    29     #do we really have a fully-qualified name?
    30     #if not, linux offers a second possibility
    31     if fqhn.count(".") == 0 and os.name == 'posix':
    32         #weird method to get own ip address and fqhn 
    33         s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    34         s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
    35         s.connect(('<broadcast>', 0))
    36         my_ip = s.getsockname()[0]
    37         s.close()
    38         fqhn = socket.gethostbyaddr(my_ip)[0]
    39 
    40     parts = fqhn.split(".")
    41     if len(parts) < 3:
    42         return []
    43 
    44     servers = []
    45     parts[0] = "wpad"
    46     servers.append(".".join(parts))
    47     parts = parts[0:1]+parts[2:]
    48     #not correct for some suffixes, but everyone does it that way, see wikipedia for details
    49     while len(parts) >= 3:
    50         servers.append(".".join(parts))
    51         parts = parts[0:1]+parts[2:]
    52 
    53     return servers
    54 
    55 def search_for_wpad_url():
    56     #TODO: According to RFC we should check DHCP first, but it isn't used often
    57     #check most common case first
    58     wpad_server = "wpad"
    59     wpad_url = check_for_wpad_file(wpad_server, DNS_WPAD_FILENAME)
    60     if wpad_url:
    61         return wpad_url
    62 
    63     #wpad DNS search procedure
    64     possible_wpad_servers = get_wpad_server_searchlist()
    65     for server in possible_wpad_servers:
    66         wpad_url = check_for_wpad_file(server, DNS_WPAD_FILENAME)
    67         if wpad_url:
    68             return wpad_url
    69         
    70     #no wpad url found
    71     return None
    72     
    73 def getProxySettings():
    74     # try to autodetect domain wpad file
    75     wpad_url = search_for_wpad_url()
    76     if wpad_url:
    77         return {'ProxyAutoConfigURL': wpad_url}
    78     
    79     # get Proxy settings from registry      
    80     aReg = _winreg.ConnectRegistry(None,_winreg.HKEY_CURRENT_USER)
    81     aKey = _winreg.OpenKey(aReg, r"Software\Microsoft\Windows\CurrentVersion\Internet Settings")
    82     _, valueCount, _ = _winreg.QueryInfoKey(aKey)
    83     reg_entries = dict()
    84     for i in range(valueCount):                                           
    85         try:
    86             n,v,_ = _winreg.EnumValue(aKey,i)
    87             reg_entries[n] = v
    88         except EnvironmentError:                                               
    89             break
    90     _winreg.CloseKey(aKey)
    91     
    92     # return configured WPAD url
    93     if 'AutoConfigURL' in reg_entries.keys():
    94         return {'ProxyAutoConfigURL': reg_entries['AutoConfigURL']}
    95 
    96     # return manually configured proxy
    97     if 'ProxyEnable' in reg_entries.keys() and reg_entries['ProxyEnable'] == 1:
    98         proxy_search = re.search(r"(?<=http=)(?P<ProxyServer>.*?)(?=;)", reg_entries['ProxyServer'])
    99         if proxy_search:
   100             proxies = proxy_search.groupdict()
   101             if 'ProxyServer' in proxies.keys(): # found http proxy
   102                 return {'ProxyServer': proxies['ProxyServer']}  
   103         return {'ProxyServer': reg_entries['ProxyServer']}
   104         
   105     return None