目录
问题内容
解决方法
首页 后端开发 Golang 如何修复 proxyconnect tcp: tls: 第一条记录看起来不像 TLS 握手

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

Feb 11, 2024 am 10:18 AM

如何修复 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
  • 函数签名:

    • 修改后的代码修改了 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 握手错误。
<小时/>

上面提供的代码中的 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中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

Debian OpenSSL有哪些漏洞 Debian OpenSSL有哪些漏洞 Apr 02, 2025 am 07:30 AM

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

您如何使用PPROF工具分析GO性能? 您如何使用PPROF工具分析GO性能? Mar 21, 2025 pm 06:37 PM

本文解释了如何使用PPROF工具来分析GO性能,包括启用分析,收集数据并识别CPU和内存问题等常见的瓶颈。

您如何在GO中编写单元测试? 您如何在GO中编写单元测试? Mar 21, 2025 pm 06:34 PM

本文讨论了GO中的编写单元测试,涵盖了最佳实践,模拟技术和有效测试管理的工具。

Go语言中用于浮点数运算的库有哪些? Go语言中用于浮点数运算的库有哪些? Apr 02, 2025 pm 02:06 PM

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

Go的爬虫Colly中Queue线程的问题是什么? Go的爬虫Colly中Queue线程的问题是什么? Apr 02, 2025 pm 02:09 PM

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

您如何在GO中使用表驱动测试? 您如何在GO中使用表驱动测试? Mar 21, 2025 pm 06:35 PM

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

您如何在go.mod文件中指定依赖项? 您如何在go.mod文件中指定依赖项? Mar 27, 2025 pm 07:14 PM

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

解释GO反射软件包的目的。您什么时候使用反射?绩效有什么影响? 解释GO反射软件包的目的。您什么时候使用反射?绩效有什么影响? Mar 25, 2025 am 11:17 AM

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

See all articles