Merge pull request #1770 from kmadac/master
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Implemetation of IP address ranges for no_proxy environment variable
authored by Kenneth Reitz · committed by Kenneth Reitz · parents 1cac72b, 1d42d9d · Browse files · 3 files·+106 added−5 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+106 added−5 removed
- other:+106 added−5 removed
Provenance
- Commit
ba3dab5- Tree
7e1840a- Author
Kenneth Reitz <me@kennethreitz.org>- Committer
Kenneth Reitz <me@kennethreitz.org>- Parent 1
1cac72b- Parent 2
1d42d9d
3 files changed+106 added−5 removed
modified:requests/utils.py+64 added-5 removed
15 unmodified lines16 import os17 import platform18 import re19 import sys20+import socket21+import struct22 23 from . import __version__24 from . import certs25 from .compat import parse_http_list as _parse_list_header380 unmodified lines406 # or '%')407 return quote(unquote_unreserved(uri), safe="!#$%&'()*+,/:;=?@[]~")408 409 410+def address_in_network(ip, net):411+ """412+ This function allows you to check if on IP belongs to a network subnet413+ Example: returns True if ip = 192.168.1.1 and net = 192.168.1.0/24414+ returns False if ip = 192.168.1.1 and net = 192.168.100.0/24415+ """416+ ipaddr = struct.unpack('=L', socket.inet_aton(ip))[0]417+ netaddr, bits = net.split('/')418+ netmask = struct.unpack('=L', socket.inet_aton(dotted_netmask(int(bits))))[0]419+ network = struct.unpack('=L', socket.inet_aton(netaddr))[0] & netmask420+ return (ipaddr & netmask) == (network & netmask)421+422+423+def dotted_netmask(mask):424+ """425+ Converts mask from /xx format to xxx.xxx.xxx.xxx426+ Example: if mask is 24 function returns 255.255.255.0427+ """428+ bits = 0xffffffff ^ (1 << 32 - mask) - 1429+ return socket.inet_ntoa(struct.pack('>I', bits))430+431+432+def is_ipv4_address(string_ip):433+ try:434+ socket.inet_aton(string_ip)435+ except socket.error:436+ return False437+ return True438+439+440+def is_valid_cidr(string_network):441+ """Very simple check of the cidr format in no_proxy variable"""442+ if string_network.count('/') == 1:443+ try:444+ mask = int(string_network.split('/')[1])445+ except ValueError:446+ return False447+448+ if mask < 1 or mask > 32:449+ return False450+451+ try:452+ socket.inet_aton(string_network.split('/')[0])453+ except socket.error:454+ return False455+ else:456+ return False457+ return True458+459+460 def get_environ_proxies(url):461 """Return a dict of environment proxies."""462 463 get_proxy = lambda k: os.environ.get(k) or os.environ.get(k.upper())7 unmodified lines471 # We need to check whether we match here. We need to see if we match472 # the end of the netloc, both with and without the port.473 no_proxy = no_proxy.replace(' ', '').split(',')474 475+ ip = netloc.split(':')[0]476+ if is_ipv4_address(ip):477+ for proxy_ip in no_proxy:478+ if is_valid_cidr(proxy_ip):479+ if address_in_network(ip, proxy_ip):480+ return {}481+ else:423- for host in no_proxy:424- if netloc.endswith(host) or netloc.split(':')[0].endswith(host):425- # The URL does match something in no_proxy, so we don't want426- # to apply the proxies on this URL.427- return {}482+ for host in no_proxy:483+ if netloc.endswith(host) or netloc.split(':')[0].endswith(host):484+ # The URL does match something in no_proxy, so we don't want485+ # to apply the proxies on this URL.486+ return {}487 488 # If the system proxy settings indicate that this URL should be bypassed,489 # don't proxy.490 if proxy_bypass(netloc):153 unmodified linesmodified:AUTHORS.rst+1 added
141 unmodified lines142 - Can Ibanoglu <can.ibanoglu@gmail.com> @canibanoglu143 - Thomas Weißschuh <thomas@t-8ch.de> @t-8ch144 - Jayson Vantuyl <jayson@aggressive.ly> @kagato145 - Pengfei.X <pengphy@gmail.com>146+- Kamil Madac <kamil.madac@gmail.com>modified:test_requests.py+41 added
961 unmodified lines962 pass963 else:964 assert super_len(cStringIO.StringIO('but some how, some way...')) == 25965 966+ def test_get_environ_proxies_ip_ranges(self):967+ """ Ensures that IP addresses are correctly matches with ranges in no_proxy variable """968+ from requests.utils import get_environ_proxies969+ os.environ['no_proxy'] = "192.168.0.0/24,127.0.0.1,localhost.localdomain,172.16.1.1"970+ assert get_environ_proxies('http://192.168.0.1:5000/') == {}971+ assert get_environ_proxies('http://192.168.0.1/') == {}972+ assert get_environ_proxies('http://172.16.1.1/') == {}973+ assert get_environ_proxies('http://172.16.1.1:5000/') == {}974+ assert get_environ_proxies('http://192.168.1.1:5000/') != {}975+ assert get_environ_proxies('http://192.168.1.1/') != {}976+977+ def test_get_environ_proxies(self):978+ """ Ensures that IP addresses are correctly matches with ranges in no_proxy variable """979+ from requests.utils import get_environ_proxies980+ os.environ['no_proxy'] = "127.0.0.1,localhost.localdomain,192.168.0.0/24,172.16.1.1"981+ assert get_environ_proxies('http://localhost.localdomain:5000/v1.0/') == {}982+ assert get_environ_proxies('http://www.requests.com/') != {}983+984+ def test_is_ipv4_address(self):985+ from requests.utils import is_ipv4_address986+ assert is_ipv4_address('8.8.8.8')987+ assert not is_ipv4_address('8.8.8.8.8')988+ assert not is_ipv4_address('localhost.localdomain')989+990+ def test_is_valid_cidr(self):991+ from requests.utils import is_valid_cidr992+ assert not is_valid_cidr('8.8.8.8')993+ assert is_valid_cidr('192.168.1.0/24')994+995+ def test_dotted_netmask(self):996+ from requests.utils import dotted_netmask997+ assert dotted_netmask(8) == '255.0.0.0'998+ assert dotted_netmask(24) == '255.255.255.0'999+ assert dotted_netmask(25) == '255.255.255.128'1000+1001+ def test_address_in_network(self):1002+ from requests.utils import address_in_network1003+ assert address_in_network('192.168.1.1', '192.168.1.0/24')1004+ assert not address_in_network('172.16.0.1', '192.168.1.0/24')1005+1006+1007 if __name__ == '__main__':1008 unittest.main()