为通过HTTPS隧道的请求设置CA捆绑包

问题描述 投票:1回答:1

我正在尝试通过HTTPS隧道发送HTTPS请求。也就是说,我的代理期望CONNECT使用HTTPS。它还需要客户端证书。

我正在使用Requests' proxy features

import requests

url = "https://some.external.com/endpoint"
with requests.Session() as session:
    response = session.get(
        url,
        proxies={"https": "https://proxy.host:4443"},
        # client certificates expected by proxy
        cert=(cert_path, key_path),
        verify="/home/savior/proxy-ca-bundle.pem",
    )
    with response:
        ...

此方法有效,但有一些限制:

  1. 我只能为与代理的TLS连接设置客户端证书,而不能为外部端点设置客户端证书。
  2. proxy-ca-bundle.pem仅在与代理的TLS连接中验证服务器证书。似乎忽略了来自外部端点的服务器证书。

有没有办法使用requests解决这两个问题?我想为外部端点设置一组不同的CA。

[我也尝试过使用http.clientHTTPSConnection.set_tunnel,但据我所知,它的隧道是通过HTTP完成的,我需要HTTPS。

python ssl https python-requests http-tunneling
1个回答
0
投票

查看源代码,看来HTTPSConnection.set_tunnel当前不支持此“ TLS in TLS”,即。提供两组用于代理请求的客户端/ CA捆绑包。

我们可以使用只包装libcurl的requests

PycURL

PycURL将使用from io import BytesIO import pycurl url = "https://some.external.com/endpoint" buffer = BytesIO() curl = pycurl.Curl() curl.setopt(curl.URL, url) curl.setopt(curl.WRITEDATA, buffer) # proxy settings curl.setopt(curl.HTTPPROXYTUNNEL, 1) curl.setopt(curl.PROXY, "https://proxy.host") curl.setopt(curl.PROXYPORT, 4443) curl.setopt(curl.PROXY_SSLCERT, cert_path) curl.setopt(curl.PROXY_SSLKEY, key_path) curl.setopt(curl.PROXY_CAINFO, "/home/savior/proxy-ca-bundle.pem") # endpoint verification curl.setopt(curl.CAINFO, "/home/savior/external-ca-bundle.pem") try: curl.perform() except pycurl.error: pass # log or re-raise else: status_code = curl.getinfo(curl.RESPONSE_CODE) 设置与代理建立TLS连接,并向其发送HTTP CONNECT请求。然后它将通过与外部端点的代理连接建立一个新的TLS会话,并使用PROXY_捆绑包来验证那些服务器证书。

© www.soinside.com 2019 - 2024. All rights reserved.