1 # ------------------------------------------------------------
4 # Aquire the correct proxy settings of the current machine
6 # Autor: Mihai Bartha, <mihai.bartha@ait.ac.at>
8 # Copyright 2013-2014 X-Net and AIT Austrian Institute of Technology
15 # https://www.x-net.at
17 # AIT Austrian Institute of Technology
18 # Donau City Strasse 1
21 # http://www.ait.ac.at
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
28 # http://www.apache.org/licenses/LICENSE-2.0
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 # ------------------------------------------------------------
44 DNS_WPAD_FILENAME = "wpad.dat"
46 def check_for_wpad_file(server, path):
48 wpad_url = "http://%s/%s"%(server, path)
49 print "checking", wpad_url
51 conn = httplib.HTTPConnection(server)
52 conn.request("HEAD", "/%s"%path)
53 r = conn.getresponse()
61 def get_wpad_server_searchlist():
62 #get fully-qualified hostname
63 fqhn = socket.getfqdn().split(" ")[0]
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]
74 fqhn = socket.gethostbyaddr(my_ip)[0]
76 parts = fqhn.split(".")
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:]
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
95 wpad_url = check_for_wpad_file(wpad_server, DNS_WPAD_FILENAME)
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)
109 def getProxySettings():
110 # try to autodetect domain wpad file
111 wpad_url = search_for_wpad_url()
113 return {'ProxyAutoConfigURL': wpad_url}
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)
120 for i in range(valueCount):
122 n,v,_ = _winreg.EnumValue(aKey,i)
124 except EnvironmentError:
126 _winreg.CloseKey(aKey)
128 # return configured WPAD url
129 if 'AutoConfigURL' in reg_entries.keys():
130 return {'ProxyAutoConfigURL': reg_entries['AutoConfigURL']}
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'])
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']}