问题描述
我正在尝试通过代理路由我的请求,并且还在TLS配置中发送cert.pem.下面的代码引发了此错误- proxyconnect tcp:tls:第一条记录看起来不像TLS握手.当我将代理URL从https更改为HTTP时,相同的代码有效.但是,带有https的代理URL在python中有效.下面是到目前为止的代码
I am trying to route my requests through a proxy and also sending cert.pem in TLS config. Below code is throwing this error - proxyconnect tcp: tls: first record does not look like a TLS handshake. When I change the proxy URL from https to HTTP, the same code works. However proxy URL with https works in python. Below is my code so far
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))
推荐答案
当我将代理URL从https更改为HTTP时,相同的代码有效.
When I change the proxy URL from https to HTTP, the same code works.
https://.
https://..
proxyconnect tcp:tls:第一条记录看起来不像TLS握手.
proxyconnect tcp: tls: first record does not look like a TLS handshake.
这是因为代理使用简单的HTTP错误来响应奇怪的HTTP请求(这实际上是TLS握手的开始).
This is because the proxy answers with an plain HTTP error to the strange HTTP request (which is actually the start of the TLS handshake).
带https的代理URL在python中有效.
However proxy URL with https works in python.
https://
https://
openssl s_client
openssl s_client
这篇关于如何在Golang中执行代理和TLS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!