如何修复 proxyconnect tcp: tls: 第一条记录看起来不像 TLS 握手
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
。但我不知道该怎么做。阅读这些:
- 如何在golang中进行代理和TLS;
- 如何通过代理进行 HTTP/HTTPS GET;和
- 如何使用错误的证书执行 https 请求?
也没有帮助。有些有同样的错误 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}
,例如这样< /p>
<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
创建新的http.Client
,并设置超时为 10 秒。 - 原始代码还尝试使用自定义
http.Transport
创建新的http.Client
,但后来在main
函数中,它使用新的http.Client
覆盖了client
变量,其中包含默认的Transport
和超时10秒的,有效丢弃自定义的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。 - 原始代码已将 URL 更改为“http://catfact.ninja/fact”,以避免 TLS 握手错误。
- 修改后的代码将
上面提供的代码中的 customDialContext
函数不包含任何专门忽略 TLS 握手错误或将 TLS 握手更改为非 TLS 连接的逻辑。它只提供了自定义拨号功能,在提供的形式中,直接调用net.Dial
函数不包含任何专门忽略 TLS 握手错误或将 TLS 握手更改为非 TLS 连接的逻辑。它只提供了自定义拨号功能,在提供的形式中,直接调用
http.Transport
结构体的TLSClientConfig
字段提供的,具体是将InsecureSkipVerify
字段设置为true
忽略TLS证书验证错误的机制实际上是由
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中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

OpenSSL,作为广泛应用于安全通信的开源库,提供了加密算法、密钥和证书管理等功能。然而,其历史版本中存在一些已知安全漏洞,其中一些危害极大。本文将重点介绍Debian系统中OpenSSL的常见漏洞及应对措施。DebianOpenSSL已知漏洞:OpenSSL曾出现过多个严重漏洞,例如:心脏出血漏洞(CVE-2014-0160):该漏洞影响OpenSSL1.0.1至1.0.1f以及1.0.2至1.0.2beta版本。攻击者可利用此漏洞未经授权读取服务器上的敏感信息,包括加密密钥等。

Go语言中用于浮点数运算的库介绍在Go语言(也称为Golang)中,进行浮点数的加减乘除运算时,如何确保精度是�...

Go爬虫Colly中的Queue线程问题探讨在使用Go语言的Colly爬虫库时,开发者常常会遇到关于线程和请求队列的问题。�...

本文讨论了GO中使用表驱动的测试,该方法使用测试用例表来测试具有多个输入和结果的功能。它突出了诸如提高的可读性,降低重复,可伸缩性,一致性和A

本文讨论了通过go.mod,涵盖规范,更新和冲突解决方案管理GO模块依赖关系。它强调了最佳实践,例如语义版本控制和定期更新。

本文讨论了GO的反思软件包,用于运行时操作代码,对序列化,通用编程等有益。它警告性能成本,例如较慢的执行和更高的内存使用,建议明智的使用和最佳
