fix single ip ignored in no proxy setting
[tools/mic.git] / mic / utils / proxy.py
1 #!/usr/bin/python -tt
2 #
3 # Copyright (c) 2010, 2011 Intel, Inc.
4 #
5 # This program is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the Free
7 # Software Foundation; version 2 of the License
8 #
9 # This program is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 # or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 # for more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with this program; if not, write to the Free Software Foundation, Inc., 59
16 # Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18 import os
19 import re
20 import urlparse
21
22 _my_proxies = {}
23 _my_noproxy = None
24 _my_noproxy_list = []
25
26 def set_proxy_environ():
27     global _my_noproxy, _my_proxies
28     if not _my_proxies:
29         return
30     for key in _my_proxies.keys():
31         os.environ[key + "_proxy"] = _my_proxies[key]
32     if not _my_noproxy:
33         return
34     os.environ["no_proxy"] = _my_noproxy
35
36 def unset_proxy_environ():
37     for env in ('http_proxy',
38                 'https_proxy',
39                 'ftp_proxy',
40                 'all_proxy'):
41         if env in os.environ:
42             del os.environ[env]
43
44         ENV=env.upper()
45         if ENV in os.environ:
46             del os.environ[ENV]
47
48 def _set_proxies(proxy = None, no_proxy = None):
49     """Return a dictionary of scheme -> proxy server URL mappings.
50     """
51
52     global _my_noproxy, _my_proxies
53     _my_proxies = {}
54     _my_noproxy = None
55     proxies = []
56     if proxy:
57         proxies.append(("http_proxy", proxy))
58     if no_proxy:
59         proxies.append(("no_proxy", no_proxy))
60
61     # Get proxy settings from environment if not provided
62     if not proxy and not no_proxy:
63         proxies = os.environ.items()
64
65         # Remove proxy env variables, urllib2 can't handle them correctly
66         unset_proxy_environ()
67
68     for name, value in proxies:
69         name = name.lower()
70         if value and name[-6:] == '_proxy':
71             if name[0:2] != "no":
72                 _my_proxies[name[:-6]] = value
73             else:
74                 _my_noproxy = value
75
76 def _ip_to_int(ip):
77     ipint=0
78     shift=24
79     for dec in ip.split("."):
80         ipint |= int(dec) << shift
81         shift -= 8
82     return ipint
83
84 def _int_to_ip(val):
85     ipaddr=""
86     shift=0
87     for i in range(4):
88         dec = val >> shift
89         dec &= 0xff
90         ipaddr = ".%d%s" % (dec, ipaddr)
91         shift += 8
92     return ipaddr[1:]
93
94 def _isip(host):
95     if host.replace(".", "").isdigit():
96         return True
97     return False
98
99 def _set_noproxy_list():
100     global _my_noproxy, _my_noproxy_list
101     _my_noproxy_list = []
102     if not _my_noproxy:
103         return
104     for item in _my_noproxy.split(","):
105         item = item.strip()
106         if not item:
107             continue
108
109         if item[0] != '.' and item.find("/") == -1:
110             # Need to match it
111             _my_noproxy_list.append({"match":0,"needle":item})
112
113         elif item[0] == '.':
114             # Need to match at tail
115             _my_noproxy_list.append({"match":1,"needle":item})
116
117         elif item.find("/") > 3:
118             # IP/MASK, need to match at head
119             needle = item[0:item.find("/")].strip()
120             ip = _ip_to_int(needle)
121             netmask = 0
122             mask = item[item.find("/")+1:].strip()
123
124             if mask.isdigit():
125                 netmask = int(mask)
126                 netmask = ~((1<<(32-netmask)) - 1)
127                 ip &= netmask
128             else:
129                 shift=24
130                 netmask=0
131                 for dec in mask.split("."):
132                     netmask |= int(dec) << shift
133                     shift -= 8
134                 ip &= netmask
135
136             _my_noproxy_list.append({"match":2,"needle":ip,"netmask":netmask})
137
138 def _isnoproxy(url):
139     (scheme, host, path, parm, query, frag) = urlparse.urlparse(url)
140
141     if '@' in host:
142         user_pass, host = host.split('@', 1)
143
144     if ':' in host:
145         host, port = host.split(':', 1)
146
147     hostisip = _isip(host)
148     for item in _my_noproxy_list:
149         if hostisip and item["match"] == 1:
150             continue
151
152         if item["match"] == 2 and hostisip:
153             if (_ip_to_int(host) & item["netmask"]) == item["needle"]:
154                 return True
155
156         if item["match"] == 0:
157             if host == item["needle"]:
158                 return True
159
160         if item["match"] == 1:
161             if re.match(r".*%s$" % item["needle"], host):
162                 return True
163
164     return False
165
166 def set_proxies(proxy = None, no_proxy = None):
167     _set_proxies(proxy, no_proxy)
168     _set_noproxy_list()
169     set_proxy_environ()
170
171 def get_proxy_for(url):
172     if url.startswith('file:') or _isnoproxy(url):
173         return None
174
175     type = url[0:url.index(":")]
176     proxy = None
177     if _my_proxies.has_key(type):
178         proxy = _my_proxies[type]
179     elif _my_proxies.has_key("http"):
180         proxy = _my_proxies["http"]
181     else:
182         proxy = None
183
184     return proxy