以下是我的 tls 后端:
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)
}
}
使用以下两行生成 key :
openssl genrsa -out private_key 2048
openssl req -new -x509 -key private_key -out public_key -days 365
当我启动 tls 服务器并使用浏览器 (https://example.com:8443) 访问该站点时,我得到了预期的结果, 忽略了浏览器警告:
Nobody should read this.
到目前为止一切都很酷。
现在,当我将浏览器指向 http://example.com:8443 时(注意使用的是 http,不是 https)我得到了 Firfox 的以下结果(Chrome 也这样做,但下载了网站):
问题:为什么会有问号?
最佳答案
如果将输出通过管道传输到 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 表示“我不明白你说什么”。
关于http - Golang ListenAndServeTLS 在浏览器中不使用 https 时返回数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23494082/