首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Go on Heroku修复TLS握手错误?

如何使用Go on Heroku修复TLS握手错误?
EN

Stack Overflow用户
提问于 2018-09-23 06:27:52
回答 3查看 1.2K关注 0票数 0

我有一个简单的代理,它监听同一端口上的HTTP和HTTPS连接。

然而,我看到奇怪的TLS握手错误,似乎是由Heroku路由器引起的:

代码语言:javascript
复制
heroku[router]: sock=backend at=error code=H18 desc="Server Request Interrupted" method=GET path="/favicon.ico" host=banana.camp request_id=f56144c8-e480-476a-90b8-429b490f1ff5 fwd="24.67.185.77" dyno=web.1 connect=0ms service=1ms status=503 bytes=7 protocol=https
http: TLS handshake error from 10.81.159.108:19578: tls: first record does not look like a TLS handshake
http: TLS handshake error from 172.17.117.25:36924: EOF

有没有办法修复或忽略它们?任何线索都非常感谢。

谢谢!

代码语言:javascript
复制
func InitServer() error {
    m := autocert.Manager{
        Cache:  autocert.DirCache(*StorageDir),
        Prompt: autocert.AcceptTOS,
        HostPolicy: func(ctx context.Context, host string) error {
            return nil
        },
    }

    errchan := make(chan error)

    s := &http.Server{
        Addr:      ":" + os.Getenv("PORT"),
        TLSConfig: &tls.Config{GetCertificate: m.GetCertificate},
        Handler:   ServeHTTP(),
    }

    go (func() {
        errchan <- http.ListenAndServe(":"+os.Getenv("PORT"), m.HTTPHandler(nil))
    })()

    errchan <- s.ListenAndServeTLS("", "")

    return <-errchan
}

func ServeHTTP() http.Handler {
    return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
        if upstreams, ok := Hosts[req.Host]; ok {
            forwarder, _ := forward.New(forward.PassHostHeader(true))
            loadbalancer, _ := roundrobin.New(forwarder)

            for _, upstream := range upstreams {
                if url, err := url.Parse(upstream); err == nil {
                    loadbalancer.UpsertServer(url)
                } else {
                    log.Fatal("InitHostsList: ", err)
                }
            }

            loadbalancer.ServeHTTP(res, req)

            return
        }

        http.Error(res, "The request service couldn't be found here", http.StatusNotImplemented)
    })
}
EN

回答 3

Stack Overflow用户

发布于 2018-09-24 03:21:28

TLS终止由Heroku路由器处理,不能在应用程序级别¯_(ツ)_/‘完成

票数 1
EN

Stack Overflow用户

发布于 2018-09-23 07:13:43

你不能在同一个端口同时监听http和https,当你让http客户端尝试连接到应用程序时,就会出现这些日志错误。

票数 0
EN

Stack Overflow用户

发布于 2018-09-23 09:44:33

默认情况下,golang http服务器只支持侦听给定端口上的http或https流量(不能同时侦听两者)。要监听https流量,您需要使用http.ListenAndServeTLS

如果您在仅为一个配置的点同时定向http和https流量,您将最终看到错误。

有一些第三方解决方案(如cmux)支持在同一端口上托管安全和不安全流量。分离端口不像分离端口那么简单,但是如果您想要做的事情,它们在自述文件中有一个示例配置。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52461409

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档