OpenSecurity/bin/proxy_getter.py
author Oliver Maurhart <oliver.maurhart@ait.ac.at>
Wed, 29 Oct 2014 15:18:22 +0100
changeset 240 d7ef04254e9c
parent 234 216da9017f8f
permissions -rw-r--r--
lizenz fixed in all files
     1 # ------------------------------------------------------------
     2 # proxy_getter
     3 #   
     4 # Aquire the correct proxy settings of the current machine
     5 #
     6 # Autor: Mihai Bartha, <mihai.bartha@ait.ac.at>       
     7 #
     8 # Copyright 2013-2014 X-Net and AIT Austrian Institute of Technology
     9 # 
    10 # 
    11 #     X-Net Services GmbH
    12 #     Elisabethstrasse 1
    13 #     4020 Linz
    14 #     AUSTRIA
    15 #     https://www.x-net.at
    16 # 
    17 #     AIT Austrian Institute of Technology
    18 #     Donau City Strasse 1
    19 #     1220 Wien
    20 #     AUSTRIA
    21 #     http://www.ait.ac.at
    22 # 
    23 # 
    24 # Licensed under the Apache License, Version 2.0 (the "License");
    25 # you may not use this file except in compliance with the License.
    26 # You may obtain a copy of the License at
    27 # 
    28 #    http://www.apache.org/licenses/LICENSE-2.0
    29 # 
    30 # Unless required by applicable law or agreed to in writing, software
    31 # distributed under the License is distributed on an "AS IS" BASIS,
    32 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    33 # See the License for the specific language governing permissions and
    34 # limitations under the License.
    35 # ------------------------------------------------------------
    36 
    37 import socket
    38 import os
    39 import httplib
    40 import sys
    41 import _winreg
    42 import re
    43 
    44 DNS_WPAD_FILENAME = "wpad.dat"
    45 
    46 def check_for_wpad_file(server, path):
    47 
    48     wpad_url = "http://%s/%s"%(server, path)
    49     print "checking", wpad_url
    50     try:
    51         conn = httplib.HTTPConnection(server)
    52         conn.request("HEAD", "/%s"%path)
    53         r = conn.getresponse()
    54         if r.status == 200:
    55             return wpad_url
    56     except Exception, e:
    57         return None
    58 
    59     return None
    60 
    61 def get_wpad_server_searchlist():
    62     #get fully-qualified hostname
    63     fqhn = socket.getfqdn().split(" ")[0]
    64     
    65     #do we really have a fully-qualified name?
    66     #if not, linux offers a second possibility
    67     if fqhn.count(".") == 0 and os.name == 'posix':
    68         #weird method to get own ip address and fqhn 
    69         s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    70         s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
    71         s.connect(('<broadcast>', 0))
    72         my_ip = s.getsockname()[0]
    73         s.close()
    74         fqhn = socket.gethostbyaddr(my_ip)[0]
    75 
    76     parts = fqhn.split(".")
    77     if len(parts) < 3:
    78         return []
    79 
    80     servers = []
    81     parts[0] = "wpad"
    82     servers.append(".".join(parts))
    83     parts = parts[0:1]+parts[2:]
    84     #not correct for some suffixes, but everyone does it that way, see wikipedia for details
    85     while len(parts) >= 3:
    86         servers.append(".".join(parts))
    87         parts = parts[0:1]+parts[2:]
    88 
    89     return servers
    90 
    91 def search_for_wpad_url():
    92     #TODO: According to RFC we should check DHCP first, but it isn't used often
    93     #check most common case first
    94     wpad_server = "wpad"
    95     wpad_url = check_for_wpad_file(wpad_server, DNS_WPAD_FILENAME)
    96     if wpad_url:
    97         return wpad_url
    98 
    99     #wpad DNS search procedure
   100     possible_wpad_servers = get_wpad_server_searchlist()
   101     for server in possible_wpad_servers:
   102         wpad_url = check_for_wpad_file(server, DNS_WPAD_FILENAME)
   103         if wpad_url:
   104             return wpad_url
   105         
   106     #no wpad url found
   107     return None
   108     
   109 def getProxySettings():
   110     # try to autodetect domain wpad file
   111     wpad_url = search_for_wpad_url()
   112     if wpad_url:
   113         return {'ProxyAutoConfigURL': wpad_url}
   114     
   115     # get Proxy settings from registry      
   116     aReg = _winreg.ConnectRegistry(None,_winreg.HKEY_CURRENT_USER)
   117     aKey = _winreg.OpenKey(aReg, r"Software\Microsoft\Windows\CurrentVersion\Internet Settings")
   118     _, valueCount, _ = _winreg.QueryInfoKey(aKey)
   119     reg_entries = dict()
   120     for i in range(valueCount):                                           
   121         try:
   122             n,v,_ = _winreg.EnumValue(aKey,i)
   123             reg_entries[n] = v
   124         except EnvironmentError:                                               
   125             break
   126     _winreg.CloseKey(aKey)
   127     
   128     # return configured WPAD url
   129     if 'AutoConfigURL' in reg_entries.keys():
   130         return {'ProxyAutoConfigURL': reg_entries['AutoConfigURL']}
   131 
   132     # return manually configured proxy
   133     if 'ProxyEnable' in reg_entries.keys() and reg_entries['ProxyEnable'] == 1:
   134         proxy_search = re.search(r"(?<=http=)(?P<ProxyServer>.*?)(?=;)", reg_entries['ProxyServer'])
   135         if proxy_search:
   136             proxies = proxy_search.groupdict()
   137             if 'ProxyServer' in proxies.keys(): # found http proxy
   138                 return {'ProxyServer': proxies['ProxyServer']}  
   139         return {'ProxyServer': reg_entries['ProxyServer']}
   140         
   141     return None