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