Golang ListenAndServeTLS在浏览器中不使用https时返回数据

Kir*_*ril 10 ssl https http go

以下是我的后端:

package main

import (
    "fmt"
    "net/http"
)

const (
    PORT       = ":8443"
    PRIV_KEY   = "./private_key"
    PUBLIC_KEY = "./public_key"
)

func rootHander(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "Nobody should read this.")
}

func main() {
    http.HandleFunc("/", rootHander)
    err := http.ListenAndServeTLS(PORT, PUBLIC_KEY, PRIV_KEY, nil)
    if err != nil {
        fmt.Printf("main(): %s\n", err)
    }
}
Run Code Online (Sandbox Code Playgroud)

使用以下两行生成密钥:

openssl genrsa -out private_key 2048
openssl req -new -x509 -key private_key -out public_key -days 365
Run Code Online (Sandbox Code Playgroud)

当我启动tls服务器,并使用浏览器访问该站点(https://example.com:8443)时,忽略浏览器警告,我得到了预期的结果:

Nobody should read this.
Run Code Online (Sandbox Code Playgroud)

到目前为止一切都很酷.

现在,当我将浏览器指向http://example.com:8443(注意使用http,而不是 https)时,我得到了Firfox的以下结果(Chrome也是如此,但是下载了网站):

使用http

问题:为什么会出现问号?

Ant*_*ing 9

如果将输出传递给od,curl -k -3 http://localhost:8443 | od -A n -t x1则会得到以下15 03 01 00 02 02 0a浏览器呈现/处理的字节序列.

根据https://code.google.com/p/go/issues/detail?id=2253,这是"我不明白你说的话"的TLS.

  • 所以,它是设计的.如果我想只允许https,我应该重定向,对吗? (2认同)