
php小编苹果在这里为大家带来解决"proxyconnect tcp: tls: 第一条记录看起来不像 TLS 握手"问题的方法。这种错误通常出现在使用代理服务器时,可能会导致网络连接问题。在解决此问题之前,我们首先需要了解问题的根源。通过以下简明的步骤,我们将向您展示如何修复这个问题,以确保您的网络连接正常运行。
在如何在 Go 中使用 REST API 中,提供了一个完全有效的示例代码来调用公共 REST API。但如果我尝试示例,则会出现此错误:
error getting cat fact:
Get "https://catfact.ninja/fact":
proxyconnect tcp: tls: first record does not look like a TLS handshake有关http状态的文档
<code> For control over proxies, TLS configuration, keep-alives, compression, and other settings, create a Transport: </code>
以及传输文档:
<code> // DialContext specifies the dial function for creating unencrypted TCP connections.
// If DialContext is nil (and the deprecated Dial below is also nil),
// then the transport dials using package net.
//
// DialContext runs concurrently with calls to RoundTrip.
// A RoundTrip call that initiates a dial may end up using
// a connection dialed previously when the earlier connection
// becomes idle before the later DialContext completes.
DialContext func(ctx context.Context, network, addr string) (net.Conn, error)
</code>因此,我假设我必须配置 Dialcontext 才能启用从客户端到代理的不安全连接 without TLS。但我不知道该怎么做。阅读这些:
也没有帮助。有些有同样的错误 proxyconnect tcp: tls:first record does not Look like a TLS handshake 并解释原因:
<code> 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). </code>
但是Steffen的回复没有示例代码如何设置DialContext func(ctx context.Context, network, addr string),Bogdan和cyberdelia都建议设置tls.Config{InsecureSkipVerify: true},例如这样
<code> tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
</code>但是上面的没有效果。我仍然遇到同样的错误。并且连接仍然调用 https://* 而不是 http://*
这是示例代码,我尝试包含上述建议并对其进行调整:
<code>var tr = &http.Transport{ TLSClientConfig:
&tls.Config{InsecureSkipVerify: true}, }
// lacks DialContext config
var client* http.Client = &http.Client{Transport: tr} // modified * added
// var client *http.Client // code from tutorial
type CatFact struct {
Fact string `json:"fact"`
Length int `json:"length"`
}
func GetCatFact() {
url := "http://catfact.ninja/fact" // changed from https to http
var catFact CatFact
err := GetJson(url, &catFact)
if err != nil {
fmt.Printf("error getting cat fact: %s\n", err.Error())
} else {
fmt.Printf("A super interesting Cat Fact: %s\n", catFact.Fact)
}
}
func main() {
client = &http.Client{Timeout: 10 * time.Second}
GetCatFact()
// same error
// proxyconnect tcp: tls: first record does
// not look like a TLS handshake
// still uses https
// for GET catfact.ninja
}
</code>如何将连接配置为使用从 myClient 通过代理到服务器的未加密连接?设置 DialContext func(ctx context.Context, network, addr string) 有助于做到这一点吗?怎么办?
我刚刚尝试过:
package main
import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
"net"
"net/http"
"time"
)
type CatFact struct {
Fact string `json:"fact"`
Length int `json:"length"`
}
// Custom dialing function to handle connections
func customDialContext(ctx context.Context, network, addr string) (net.Conn, error) {
conn, err := net.Dial(network, addr)
return conn, err
}
// Function to get a cat fact
func GetCatFact(client *http.Client) {
url := "https://catfact.ninja/fact" // Reverted back to https
var catFact CatFact
err := GetJson(url, &catFact, client)
if err != nil {
fmt.Printf("error getting cat fact: %s\n", err.Error())
} else {
fmt.Printf("A super interesting Cat Fact: %s\n", catFact.Fact)
}
}
// Function to send a GET request and decode the JSON response
func GetJson(url string, target interface{}, client *http.Client) error {
resp, err := client.Get(url)
if err != nil {
return fmt.Errorf("error sending GET request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("received non-OK HTTP status: %d", resp.StatusCode)
}
err = json.NewDecoder(resp.Body).Decode(target)
if err != nil {
return fmt.Errorf("error decoding JSON response: %w", err)
}
return nil
}
func main() {
// Create a custom Transport with the desired settings
tr := &http.Transport{
Proxy: http.ProxyFromEnvironment, // Use the proxy settings from the environment
DialContext: customDialContext, // Use the custom dialing function
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true, // Skip certificate verification (not recommended in production)
},
}
// Create a new HTTP client using the custom Transport
client := &http.Client{
Transport: tr,
Timeout: 10 * time.Second,
}
// Call the function to get a cat fact
GetCatFact(client)
}
它包括:
自定义拨号函数customDialContext:
该函数目前是 net.Dial 的简单包装,但它提供了一个可以在必要时引入自定义拨号逻辑的位置。它用作自定义拨号功能,用于创建网络连接。
传输配置:
http.Transport,包括自定义拨号功能、环境中的代理设置以及跳过证书验证的 TLS 配置(用于测试)。http.Transport,但仅包含跳过证书验证的TLS配置,并没有设置自定义拨号功能或代理设置。客户端配置:
http.Transport 创建新的 http.Client,并设置超时为 10 秒。http.Transport 创建新的 http.Client ,但后来在 main 函数中,它使用新的 http.Client 覆盖了 client 变量,其中包含默认的 Transport 和超时10秒的,有效丢弃自定义的 Transport。函数签名:
GetCatFact 和 GetJson 函数以接受 *http.Client 参数,允许它们使用在 main 中创建的自定义 http.Client。http.Client 传递给这些函数,因此它们将使用 net/http 包提供的默认 http.Client 。网址:
GetCatFact 函数中的 URL 恢复为“https://catfact.ninja/fact”,因为服务器无论如何都会将 HTTP 请求重定向到 HTTPS。上面提供的代码中的 customDialContext 函数不包含任何专门忽略 TLS 握手错误或将 TLS 握手更改为非 TLS 连接的逻辑。它只提供了自定义拨号功能,在提供的形式中,直接调用net.Dial,无需任何特殊处理。
忽略TLS证书验证错误的机制实际上是由http.Transport结构体的TLSClientConfig字段提供的,具体是将InsecureSkipVerify字段设置为true:
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
该配置告诉 Go 跳过验证服务器的证书链和主机名,这是 TLS 握手过程的一部分。但是,它不会忽略其他类型的 TLS 握手错误或切换到非 TLS 连接。通常不建议在生产环境中使用 InsecureSkipVerify: true,因为它会禁用重要的安全检查。
如果您想强制使用非 TLS(纯 HTTP)连接,通常只需使用 http:// URL,而不是 https:// URL。但是,如果服务器或代理服务器将 HTTP 重定向到 HTTPS(例如 http://catfact.ninja/fact 的情况),则客户端将遵循重定向并切换到 TLS 连接。
以上就是如何修复 proxyconnect tcp: tls: 第一条记录看起来不像 TLS 握手的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号