Go语言 如何修复proxyconnect tcp:tls:第一条记录看起来不像TLS握手

yhxst69z  于 2023-10-14  发布在  Go
关注(0)|答案(1)|浏览(259)

How to consume a REST API in Go中,提供了fully working example code来调用公共REST API。但是,如果我尝试样本,则会发生此错误:

  1. error getting cat fact:
  2. Get "https://catfact.ninja/fact":
  3. proxyconnect tcp: tls: first record does not look like a TLS handshake

关于http状态的文档
要控制代理、TLS配置、保持活动、压缩和其他设置,请创建传输:
和运输文件:

  1. // DialContext specifies the dial function for creating unencrypted TCP connections.
  2. // If DialContext is nil (and the deprecated Dial below is also nil),
  3. // then the transport dials using package net.
  4. //
  5. // DialContext runs concurrently with calls to RoundTrip.
  6. // A RoundTrip call that initiates a dial may end up using
  7. // a connection dialed previously when the earlier connection
  8. // becomes idle before the later DialContext completes.
  9. DialContext func(ctx context.Context, network, addr string) (net.Conn, error)

因此,我假设必须配置Dialcontext以启用从客户机到代理的不安全连接without TLS。但我不知道该怎么做。阅读这些:

也没有帮助。有些有相同的错误proxyconnect tcp: tls: first record does not look like a TLS handshake,并解释原因:
这是因为代理对奇怪的HTTP请求(实际上是TLS握手的开始)回答了一个普通的HTTP错误。
但是Steffen's reply没有如何设置DialContext func(ctx context.Context, network, addr string)的示例代码,Bogdancyberdelia都建议像这样设置tls.Config{InsecureSkipVerify: true}

  1. tr := &http.Transport{
  2. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  3. }
  4. client := &http.Client{Transport: tr}

但上述方法没有效果。我仍然得到同样的错误。连接仍在调用https://*而不是http://*
这是示例代码,我尝试包含上述建议并对其进行调整:

  1. var tr = &http.Transport{ TLSClientConfig:
  2. &tls.Config{InsecureSkipVerify: true}, }
  3. // lacks DialContext config
  4. var client* http.Client = &http.Client{Transport: tr} // modified * added
  5. // var client *http.Client // code from tutorial
  6. type CatFact struct {
  7. Fact string `json:"fact"`
  8. Length int `json:"length"`
  9. }
  10. func GetCatFact() {
  11. url := "http://catfact.ninja/fact" // changed from https to http
  12. var catFact CatFact
  13. err := GetJson(url, &catFact)
  14. if err != nil {
  15. fmt.Printf("error getting cat fact: %s\n", err.Error())
  16. } else {
  17. fmt.Printf("A super interesting Cat Fact: %s\n", catFact.Fact)
  18. }
  19. }
  20. func main() {
  21. client = &http.Client{Timeout: 10 * time.Second}
  22. GetCatFact()
  23. // same error
  24. // proxyconnect tcp: tls: first record does
  25. // not look like a TLS handshake
  26. // still uses https
  27. // for GET catfact.ninja
  28. }

如何将连接配置为使用从myClient通过代理到服务器的未加密连接?设置DialContext func(ctx context.Context, network, addr string)是否有助于实现这一点?如何才能做到这一点?

piah890a

piah890a1#

我只是试着:

  1. package main
  2. import (
  3. "context"
  4. "crypto/tls"
  5. "encoding/json"
  6. "fmt"
  7. "net"
  8. "net/http"
  9. "time"
  10. )
  11. type CatFact struct {
  12. Fact string `json:"fact"`
  13. Length int `json:"length"`
  14. }
  15. // Custom dialing function to handle connections
  16. func customDialContext(ctx context.Context, network, addr string) (net.Conn, error) {
  17. conn, err := net.Dial(network, addr)
  18. return conn, err
  19. }
  20. // Function to get a cat fact
  21. func GetCatFact(client *http.Client) {
  22. url := "https://catfact.ninja/fact" // Reverted back to https
  23. var catFact CatFact
  24. err := GetJson(url, &catFact, client)
  25. if err != nil {
  26. fmt.Printf("error getting cat fact: %s\n", err.Error())
  27. } else {
  28. fmt.Printf("A super interesting Cat Fact: %s\n", catFact.Fact)
  29. }
  30. }
  31. // Function to send a GET request and decode the JSON response
  32. func GetJson(url string, target interface{}, client *http.Client) error {
  33. resp, err := client.Get(url)
  34. if err != nil {
  35. return fmt.Errorf("error sending GET request: %w", err)
  36. }
  37. defer resp.Body.Close()
  38. if resp.StatusCode != http.StatusOK {
  39. return fmt.Errorf("received non-OK HTTP status: %d", resp.StatusCode)
  40. }
  41. err = json.NewDecoder(resp.Body).Decode(target)
  42. if err != nil {
  43. return fmt.Errorf("error decoding JSON response: %w", err)
  44. }
  45. return nil
  46. }
  47. func main() {
  48. // Create a custom Transport with the desired settings
  49. tr := &http.Transport{
  50. Proxy: http.ProxyFromEnvironment, // Use the proxy settings from the environment
  51. DialContext: customDialContext, // Use the custom dialing function
  52. TLSClientConfig: &tls.Config{
  53. InsecureSkipVerify: true, // Skip certificate verification (not recommended in production)
  54. },
  55. }
  56. // Create a new HTTP client using the custom Transport
  57. client := &http.Client{
  58. Transport: tr,
  59. Timeout: 10 * time.Second,
  60. }
  61. // Call the function to get a cat fact
  62. GetCatFact(client)
  63. }

它包括:

*自定义拨号功能customDialContext

该函数目前是net.Dial的一个简单 Package ,但它提供了一个地方,可以在必要时引入自定义拨号逻辑。它用作自定义拨号功能,用于创建网络连接。

*传输配置:

  • 修改后的代码使用特定设置配置自定义http.Transport,包括自定义拨号功能,环境中的代理设置以及跳过证书验证的TLS配置(用于测试)。
  • 原始代码还尝试配置自定义http.Transport,但它只包含TLS配置以跳过证书验证,并且没有设置自定义拨号功能或代理设置。
    *客户端配置:
  • 修改后的代码使用自定义http.Transport创建新的http.Client,并设置10秒的超时。
  • 原始代码还尝试使用自定义的http.Transport创建新的http.Client,但后来在main函数中,它使用新的http.Client覆盖了client变量,默认值为Transport,超时时间为10秒,实际上丢弃了自定义的Transport
    *函数签名:
  • 修改后的代码修改了GetCatFactGetJson函数以接受*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握手错误。

作为Go的新手,我希望必须向签名func customDialContext(ctx context.Context, network, addr string) (net.Conn, error)传递一些“特殊”值,以将TLS握手更改为“非tls”或忽略错误。
但我看不到任何类似设置TLS版本或类似的东西。Go语言忽略握手错误的魔力是什么?
上面提供的代码中的customDialContext函数不包含任何专门忽略TLS握手错误或将TLS握手更改为非TLS连接的逻辑。它只提供了一个自定义拨号功能,在提供的形式中,它只是直接调用net.Dial,没有任何特殊处理。
忽略TLS证书验证错误的机制实际上是由http.Transport结构体的TLSClientConfig字段提供的,具体来说就是将InsecureSkipVerify字段设置为true

  1. tr := &http.Transport{
  2. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  3. }
  4. client := &http.Client{Transport: tr}

该配置告诉Go跳过验证服务器的证书链和主机名,这是TLS握手过程的一部分。但是,它不会忽略其他类型的TLS握手错误或切换到非TLS连接。通常不建议在生产环境中使用InsecureSkipVerify: true,因为它会禁用重要的安全检查。
如果你想强制非TLS(普通HTTP)连接,你通常只需要使用http:// URL而不是https:// URL。但是,如果服务器或代理服务器将HTTP重定向到HTTPS(如http://catfact.ninja/fact的情况),则客户端将遵循重定向并切换到TLS连接。

展开查看全部

相关问题