繁体   English   中英

如何在Heroku上使用Go修复TLS握手错误?

[英]How can I fix a TLS handshake error using Go on Heroku?

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

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

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

有什么办法可以解决或忽略它们吗? 任何线索都将不胜感激。

谢谢!

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)
    })
}

TLS终止是由Heroku路由器处理的,无法在应用程序级别__(_)_ /

您不能在同一端口上同时收听http和https,并且当您有http客户端尝试连接到应用程序时,这些日志错误就是这种情况。

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

如果您在仅配置一个的位置同时引导http和https流量,则最终将看到错误。

有第三方解决方案(如cmux )支持在同一端口上托管安全流量和不安全流量。 分隔端口并不容易,但是如果您要这样做,则可以在README中使用示例配置。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM