【问题标题】:Creating an http to https conversion forward proxy in golang with built in http package使用内置 http 包在 golang 中创建 http 到 https 转换转发代理
【发布时间】:2021-05-17 14:22:39
【问题描述】:

我是 Golang 的初学者,找不到直接的方法来实现它。我有一个用例,后端服务器必须使用代理与前端服务器通信,其中一些前端服务器仅支持 https。

我正在考虑使用两个端口,一个作为 HTTP 转发,另一个作为 HTTPS 转发。后端总是通过 HTTP 与代理通信。有没有更好的方法来做到这一点?

我当前的代理服务器没有进行 http 到 https 的转换。它仍然使用 HTTP,我不知道为什么?


    egressClient := &http.Client{
            Transport: &http.Transport{
                TLSClientConfig: &tls.Config{
                    InsecureSkipVerify: true,
                },
            },
        }
    
    html, err := egressClient.Transport.RoundTrip(r)
        if err != nil {
            log.Fatal(err)
        }
        website, err := ioutil.ReadAll(html.Body)
        if err != nil {
            log.Fatal(err)
        }
        html.Body.Close()
        log.Print(string(website))

其中r 是从后端获取的HTTP 请求。我必须将 r 作为 HTTPS 请求转发。

【问题讨论】:

  • 您是否尝试将r.URL.Scheme 更改为"https"
  • 我试过了,确实解决了这个问题。将方案设置为 HTTPS 后,请求现在以 HTTPS 形式发送

标签: go https proxy


【解决方案1】:

我认为这个博客解释了很多关于 golang 中的 http 和 https。你可以通过这个:https://posener.github.io/http2/

【讨论】:

    【解决方案2】:

    通过将 r.URL.Scheme = "https" 设置为 cmets 中建议的 @leaf bebop 解决了该问题。将方案设置为 HTTPS 后,请求现在以 HTTPS 形式发送。


    有用的参考资料:

    https://posener.github.io/http2/@Amrendra Yadav 建议, HTTP/2 Cleartext (H2C) ClientHow to play Go Lang with HTTP

    【讨论】:

      猜你喜欢
      • 2013-05-30
      • 2011-03-08
      • 1970-01-01
      • 2021-05-04
      • 2014-10-11
      • 2011-10-24
      • 1970-01-01
      • 1970-01-01
      • 2020-03-22
      相关资源
      最近更新 更多