Return True iff this host shouldn't be accessed using a proxy This function uses the MacOSX framework SystemConfiguration to fetch the proxy information. proxy_settings come from _scproxy._get_proxy_settings or get mocked ie: { 'exclude_simple': bool, 'exceptions': ['foo
(host, proxy_settings)
| 1946 | # This code tests an OSX specific data structure but is testable on all |
| 1947 | # platforms |
| 1948 | def _proxy_bypass_macosx_sysconf(host, proxy_settings): |
| 1949 | """ |
| 1950 | Return True iff this host shouldn't be accessed using a proxy |
| 1951 | |
| 1952 | This function uses the MacOSX framework SystemConfiguration |
| 1953 | to fetch the proxy information. |
| 1954 | |
| 1955 | proxy_settings come from _scproxy._get_proxy_settings or get mocked ie: |
| 1956 | { 'exclude_simple': bool, |
| 1957 | 'exceptions': ['foo.bar', '*.bar.com', '127.0.0.1', '10.1', '10.0/16'] |
| 1958 | } |
| 1959 | """ |
| 1960 | from fnmatch import fnmatch |
| 1961 | from ipaddress import AddressValueError, IPv4Address |
| 1962 | |
| 1963 | hostonly, port = _splitport(host) |
| 1964 | |
| 1965 | def ip2num(ipAddr): |
| 1966 | parts = ipAddr.split('.') |
| 1967 | parts = list(map(int, parts)) |
| 1968 | if len(parts) != 4: |
| 1969 | parts = (parts + [0, 0, 0, 0])[:4] |
| 1970 | return (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8) | parts[3] |
| 1971 | |
| 1972 | # Check for simple host names: |
| 1973 | if '.' not in host: |
| 1974 | if proxy_settings['exclude_simple']: |
| 1975 | return True |
| 1976 | |
| 1977 | hostIP = None |
| 1978 | try: |
| 1979 | hostIP = int(IPv4Address(hostonly)) |
| 1980 | except AddressValueError: |
| 1981 | pass |
| 1982 | |
| 1983 | for value in proxy_settings.get('exceptions', ()): |
| 1984 | # Items in the list are strings like these: *.local, 169.254/16 |
| 1985 | if not value: continue |
| 1986 | |
| 1987 | m = re.match(r"(\d+(?:\.\d+)*)(/\d+)?", value) |
| 1988 | if m is not None and hostIP is not None: |
| 1989 | base = ip2num(m.group(1)) |
| 1990 | mask = m.group(2) |
| 1991 | if mask is None: |
| 1992 | mask = 8 * (m.group(1).count('.') + 1) |
| 1993 | else: |
| 1994 | mask = int(mask[1:]) |
| 1995 | |
| 1996 | if mask < 0 or mask > 32: |
| 1997 | # System libraries ignore invalid prefix lengths |
| 1998 | continue |
| 1999 | |
| 2000 | mask = 32 - mask |
| 2001 | |
| 2002 | if (hostIP >> mask) == (base >> mask): |
| 2003 | return True |
| 2004 | |
| 2005 | elif fnmatch(host, value): |
searching dependent graphs…