Redesigned no_proxy ip range imlementation to use only stdlib functions
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Kamil Madac · committed by Kamil Madac · parent 2c91412 · Browse files · 3 files·+52 added−17 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+52 added−17 removed
- other:+52 added−17 removed
Provenance
- Commit
8aff6f5- Tree
58968ed- Author
Kamil Madac <kamil.madac@gmail.com>- Committer
Kamil Madac <kamil.madac@gmail.com>- Parent
2c91412
3 files changed+52 added−17 removed
modified:requests/utils.py+40 added-15 removed
15 unmodified lines16 import os17 import platform18 import re19 import sys20-import netaddr20+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+ '''This function allows you to check if on IP belogs to a Network'''412+ ipaddr = struct.unpack('=L', socket.inet_aton(ip))[0]413+ netaddr, bits = net.split('/')414+ netmask = struct.unpack('=L', socket.inet_aton(dotted_netmask(int(bits))))[0]415+ network = struct.unpack('=L', socket.inet_aton(netaddr))[0] & netmask416+ return (ipaddr & netmask) == (network & netmask)417+418+419+def dotted_netmask(mask):420+ bits = 0xffffffff ^ (1 << 32 - mask) - 1421+ return socket.inet_ntoa(struct.pack('>I', bits))422+423+424+def is_ipv4_address(string_ip):425+ try:426+ socket.inet_aton(string_ip)427+ except BaseException:428+ return False429+ return True430+431+432+def is_ipv4_network(string_network):433+ if '/' in string_network:434+ try:435+ socket.inet_aton(string_network.split('/')[0])436+ except OSError:437+ return False438+ else:439+ return False440+ return True441+442+443 def get_environ_proxies(url):444 """Return a dict of environment proxies."""445 446 get_proxy = lambda k: os.environ.get(k) or os.environ.get(k.upper())7 unmodified lines454 # We need to check whether we match here. We need to see if we match455 # the end of the netloc, both with and without the port.456 no_proxy = no_proxy.replace(' ', '').split(',')457 424- ip = None425- try:426- ip = netaddr.IPAddress(netloc.split(':')[0])427- except netaddr.AddrFormatError:428- pass429-430- if ip:458+ ip = netloc.split(':')[0]459+ if is_ipv4_address(ip):460 for proxy_ip in no_proxy:432- proxy_ipaddress = None433- try:434- proxy_ipaddress = netaddr.IPNetwork(proxy_ip)435- except (netaddr.AddrFormatError, ValueError):436- continue437- if proxy_ipaddress and ip in proxy_ipaddress:438- return {}461+ if is_ipv4_network(proxy_ip):462+ if address_in_network(ip, proxy_ip):463+ return {}464 else:465 for host in no_proxy:466 if netloc.endswith(host) or netloc.split(':')[0].endswith(host):467 # The URL does match something in no_proxy, so we don't want158 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+11 added-2 removed
911 unmodified lines912 913 def test_get_environ_proxies_ip_ranges(self):914 """ Ensures that IP addresses are correctly matches with ranges in no_proxy variable """915 from requests.utils import get_environ_proxies916- os.environ['no_proxy'] = "127.0.0.1,localhost.localdomain,192.168.0.0/24,172.16.1.1"916+ os.environ['no_proxy'] = "192.168.0.0/24,127.0.0.1,localhost.localdomain,172.16.1.1"917 assert get_environ_proxies('http://192.168.0.1:5000/') == {}918 assert get_environ_proxies('http://192.168.0.1/') == {}919 assert get_environ_proxies('http://172.16.1.1/') == {}920- assert get_environ_proxies('http://192.168.1.1:5000/') == {'no': os.environ['no_proxy']}920+ assert get_environ_proxies('http://172.16.1.1:5000/') == {}921+ assert get_environ_proxies('http://192.168.1.1:5000/') != {}922+ assert get_environ_proxies('http://192.168.1.1/') != {}923 924+ def test_get_environ_proxies(self):925+ """ Ensures that IP addresses are correctly matches with ranges in no_proxy variable """926+ from requests.utils import get_environ_proxies927+ os.environ['no_proxy'] = "127.0.0.1,localhost.localdomain,192.168.0.0/24,172.16.1.1"928+ assert get_environ_proxies('http://localhost.localdomain:5000/v1.0/') == {}929+ assert get_environ_proxies('http://www.requests.com/') != {}930+931 if __name__ == '__main__':932 unittest.main()