Skip to content
Myr

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

3 files+106 added−5 removed

modified:requests/utils.py+64 added-5 removed
15 unmodified lines
16import os17import platform18import re19import sys20import socket21import struct2223from . import __version__24from . import certs25from .compat import parse_http_list as _parse_list_header
380 unmodified lines
406 # or '%')407 return quote(unquote_unreserved(uri), safe="!#$%&'()*+,/:;=?@[]~")408409410def 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)421422423def 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))430431432def is_ipv4_address(string_ip):433 try:434 socket.inet_aton(string_ip)435 except socket.error:436 return False437 return True438439440def 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 False447448 if mask < 1 or mask > 32:449 return False450451 try:452 socket.inet_aton(string_network.split('/')[0])453 except socket.error:454 return False455 else:456 return False457 return True458459460def get_environ_proxies(url):461 """Return a dict of environment proxies."""462463 get_proxy = lambda k: os.environ.get(k) or os.environ.get(k.upper())
7 unmodified lines
471 # 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(',')474475 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 {}487488 # If the system proxy settings indicate that this URL should be bypassed,489 # don't proxy.490 if proxy_bypass(netloc):
153 unmodified lines
modified:AUTHORS.rst+1 added
141 unmodified lines
142- 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 lines
962 pass963 else:964 assert super_len(cStringIO.StringIO('but some how, some way...')) == 25965966 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/') != {}976977 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/') != {}983984 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')989990 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')994995 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'10001001 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')100510061007if __name__ == '__main__':1008 unittest.main()