【发布时间】:2020-08-07 14:22:41
【问题描述】:
我正在尝试通过代理路由我的请求,并在 TLS 配置中发送 cert.pem。下面的代码抛出了这个错误 - proxyconnect tcp: tls: first record doesn't look like a TLS handshake。当我将代理 URL 从 https 更改为 HTTP 时,相同的代码可以工作。但是带有 https 的代理 URL 在 python 中有效。以下是我目前的代码
certs := x509.NewCertPool()
pemFile, err := ioutil.ReadFile("cert.pem")
if err != nil {
return
}
certs.AppendCertsFromPEM(pemFile)
tlsConfig := &tls.Config{
RootCAs: certs,
}
proxyUrl, err := url.Parse("https://someproxyurlhere.com:8080")
if err != nil {
return
}
t := &http.Transport{
TLSClientConfig: tlsConfig,
Proxy: http.ProxyURL(proxyUrl),
}
client := http.Client{
Transport: t,
}
reqBody := "some JSON body here"
buff, err := json.Marshal(reqBody)
if err != nil {
return
}
req, err := http.NewRequest(http.MethodPost, "https://someurlhere.com", bytes.NewBuffer(buff))
if err != nil {
return
}
res, err := client.Do(req)
if err != nil {
// Error here - proxyconnect tcp: tls: first record does not look like a TLS handshake
return
}
defer res.Body.Close()
Python 代码
import requests
os.environ['HTTPS_PROXY'] = 'https://someproxyurlhere.com:8080'
response = requests.post("https://someurlhere.com",
json={'key': 'value'},
verify='cert.pem')
print(str(response.content))
【问题讨论】:
-
“但是带有 https 的代理 URL 在 python 中有效。” - 请也提供有效的 Python 代码来说明你的意思。此外,输出
openssl s_client -connect someproxyurlhere.com:8080将有助于证明代理本身实际上使用 HTTPS - 常见的情况是即使对于 HTTPS URL,代理本身也可以通过 HTTP 访问(代理只是在执行 CONNECT 时通过隧道连接到最终服务器命令,因此这保留了端到端的安全性)。如果同一个代理和端口都可以通过 HTTP 和 HTTPS 访问,那就特别奇怪了。 -
@SteffenUllrich 使用 python 代码更新
-
os.environ['HTTPS_PROXY'] = 'https://someproxyurlhere.com:8080'- requests 并不真正关心此处给出的协议(https 与 http)。它将仅使用带有纯 HTTP 的 IP 和端口连接到代理。