3 # Copyright (c) 2010, 2011 Intel, Inc.
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
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
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.
27 def set_proxy_environ():
30 for key in list(_my_proxies.keys()):
31 os.environ[key + "_proxy"] = _my_proxies[key]
34 os.environ["no_proxy"] = _my_noproxy
36 def unset_proxy_environ():
37 for env in ('http_proxy',
44 env_upper = env.upper()
45 if env_upper in os.environ:
46 del os.environ[env_upper]
48 def _set_proxies(proxy = None, no_proxy = None):
49 """Return a dictionary of scheme -> proxy server URL mappings.
52 global _my_noproxy, _my_proxies
57 proxies.append(("http_proxy", proxy))
59 proxies.append(("no_proxy", no_proxy))
61 # Get proxy settings from environment if not provided
62 if not proxy and not no_proxy:
63 proxies = list(os.environ.items())
65 # Remove proxy env variables, urllib2 can't handle them correctly
68 for name, value in proxies:
70 if value and name[-6:] == '_proxy':
72 _my_proxies[name[:-6]] = value
79 for dec in ip.split("."):
82 ipint |= int(dec) << shift
92 ipaddr = ".%d%s" % (dec, ipaddr)
97 if host.replace(".", "").isdigit():
101 def _set_noproxy_list():
102 global _my_noproxy, _my_noproxy_list
103 _my_noproxy_list = []
107 #solve in /etc/enviroment contains command like `echo 165.xxx.xxx.{1..255} | sed 's/ /,/g'``
108 _my_noproxy_bak = _my_noproxy
109 start = _my_noproxy.find("`")
110 while(start < len(_my_noproxy) and start != -1):
111 start = _my_noproxy.find("`",start)
112 end = _my_noproxy.find("`",start+1)
113 cmd = _my_noproxy[start+1:end]
114 pstr = _my_noproxy[start:end+1]
117 _my_noproxy=_my_noproxy.replace(pstr,len(pstr)*" ")
119 c_result = os.popen(cmd).readlines()
120 if len(c_result) == 0:
122 except Exception as e:
123 msger.warning(str(e))
125 to_list = c_result[0].strip("\n").split(",")
126 _my_noproxy_list.extend(to_list)
128 for item in _my_noproxy.split(","):
133 if item[0] != '.' and item.find("/") == -1:
135 _my_noproxy_list.append({"match":0, "needle":item})
138 # Need to match at tail
139 _my_noproxy_list.append({"match":1, "needle":item})
141 elif item.find("/") > 3:
142 # IP/MASK, need to match at head
143 needle = item[0:item.find("/")].strip()
144 ip = _ip_to_int(needle)
146 mask = item[item.find("/")+1:].strip()
150 netmask = ~((1<<(32-netmask)) - 1)
155 for dec in mask.split("."):
156 if not dec.isdigit():
158 netmask |= int(dec) << shift
162 _my_noproxy_list.append({"match":2, "needle":ip, "netmask":netmask})
163 _my_noproxy = _my_noproxy_bak
166 host = urllib.parse.urlparse(url)[1]
167 # urlparse.urlparse(url) returns (scheme, host, path, parm, query, frag)
170 user_pass, host = host.split('@', 1)
173 host, port = host.split(':', 1)
175 hostisip = _isip(host)
176 for item in _my_noproxy_list:
177 if hostisip and item["match"] == 1:
180 if item["match"] == 2 and hostisip:
181 if (_ip_to_int(host) & item["netmask"]) == item["needle"]:
184 if item["match"] == 0:
185 if host == item["needle"]:
188 if item["match"] == 1:
189 if re.match(r".*%s$" % item["needle"], host):
194 def set_proxies(proxy = None, no_proxy = None):
195 _set_proxies(proxy, no_proxy)
199 def get_proxy_for(url):
200 if url.startswith('file:') or _isnoproxy(url):
203 type = url[0:url.index(":")]
205 if type in _my_proxies:
206 proxy = _my_proxies[type]
207 elif "http" in _my_proxies:
208 proxy = _my_proxies["http"]