proxy.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. from .ssl_ import create_urllib3_context, resolve_cert_reqs, resolve_ssl_version
  2. def connection_requires_http_tunnel(
  3. proxy_url=None, proxy_config=None, destination_scheme=None
  4. ):
  5. """
  6. Returns True if the connection requires an HTTP CONNECT through the proxy.
  7. :param URL proxy_url:
  8. URL of the proxy.
  9. :param ProxyConfig proxy_config:
  10. Proxy configuration from poolmanager.py
  11. :param str destination_scheme:
  12. The scheme of the destination. (i.e https, http, etc)
  13. """
  14. # If we're not using a proxy, no way to use a tunnel.
  15. if proxy_url is None:
  16. return False
  17. # HTTP destinations never require tunneling, we always forward.
  18. if destination_scheme == "http":
  19. return False
  20. # Support for forwarding with HTTPS proxies and HTTPS destinations.
  21. if (
  22. proxy_url.scheme == "https"
  23. and proxy_config
  24. and proxy_config.use_forwarding_for_https
  25. ):
  26. return False
  27. # Otherwise always use a tunnel.
  28. return True
  29. def create_proxy_ssl_context(
  30. ssl_version, cert_reqs, ca_certs=None, ca_cert_dir=None, ca_cert_data=None
  31. ):
  32. """
  33. Generates a default proxy ssl context if one hasn't been provided by the
  34. user.
  35. """
  36. ssl_context = create_urllib3_context(
  37. ssl_version=resolve_ssl_version(ssl_version),
  38. cert_reqs=resolve_cert_reqs(cert_reqs),
  39. )
  40. if (
  41. not ca_certs
  42. and not ca_cert_dir
  43. and not ca_cert_data
  44. and hasattr(ssl_context, "load_default_certs")
  45. ):
  46. ssl_context.load_default_certs()
  47. return ssl_context